tinymce-vue格式刷

配置参考 https://blog.csdn.net/qq_41490275/article/details/108094650

 <el-button
          v-bind:class="{ active: formatpainterFlag }"
          plain
          size="small"
          class="searchBtnClear formatpainterBtn"
          @click="formatpainterEditor"
        >
          格式刷
        </el-button>
<tinymce-editor
          v-model="editorMsg"
          @onClick="onEditorClick"
          ref="editorRef"
        ></tinymce-editor>
 onEditorClick(e, editor) {
      /*  console.log("Element clicked");
      console.log(e);
      console.log(editor); */
      console.log(
        tinyMCE.activeEditor.selection.getContent({ format: "html" })
      );
      console.log(tinyMCE.activeEditor.selection.getRng().startContainer);
      let currentContainer = tinyMCE.activeEditor.selection.getRng()
        .startContainer;
      let dom = tinyMCE.activeEditor.dom;
      while (true) {
        currentContainer = currentContainer.parentNode;
        if ("P" === currentContainer.nodeName) {
          this.currentBlockStyle = dom.getAttrib(currentContainer, "style");
          break;
        }
        if ("SPAN" === currentContainer.nodeName) {
          this.currentNodeStyle = dom.getAttrib(currentContainer, "style");
        }
        if ("EM" === currentContainer.nodeName) {
          this.currentEm = true;
        }
        if ("STRONG" === currentContainer.nodeName) {
          this.currentStrong = true;
        }
      }
      console.log(
        this.currentBlockStyle,
        this.currentNodeStyle,
        this.currentEm,
        this.currentStrong
      );
      if (this.formatpainterFlag) {
        var range = tinyMCE.activeEditor.selection.getRng();
        let that = this;
        var setStyle = function (text) {
          //粗体
          if (that.currentStrong) {
            text = "<strong>" + text + "</stong>";
          }
          //斜体
          if (that.currentEm) {
            text = "<em>" + text + "</em>";
          }
          if (that.currentNodeStyle) {
            text =
              "<span style='" + that.currentNodeStyle + "'>" + text + "</span>";
          }
          return text;
        };
        this.formatpainterFlag = false;
        //应用样式到选中的range中
        var blocks = tinyMCE.activeEditor.selection.getSelectedBlocks();
        for (var i = 0; i < blocks.length; i++) {
          //先应用段落的样式
          tinyMCE.activeEditor.dom.setAttrib(
            blocks[i],
            "style",
            this.currentBlockStyle
          );
          //如果是第一个
          if (0 === i) {
            //如果同一个container
            if (range.startContainer === range.endContainer) {
              var text = tinyMCE.activeEditor.selection.getContent({
                format: "text",
              });
              tinyMCE.activeEditor.selection.setContent(setStyle(text));
            } else {
              //否则构造一个select让其选中当前block的最后一个
              var currentRange = range.cloneRange();
              var lastNode = range.startContainer;
              while (true) {
                var nextNode = tinyMCE.activeEditor.dom.getNext(
                  lastNode,
                  function (node) {
                    return true;
                  }
                );
                console.log(nextNode);
                if (tinyMCE.activeEditor.dom.isChildOf(nextNode, blocks[i])) {
                  lastNode = nextNode;
                } else {
                  break;
                }
              }
              currentRange.setEnd(lastNode, lastNode.length);
              console.log(currentRange);
              tinyMCE.activeEditor.selection.setRng(currentRange);
              var text = tinyMCE.activeEditor.selection.getContent({
                format: "text",
              });
              tinyMCE.activeEditor.selection.setContent(setStyle(text));
              //把范围设置回去
              tinyMCE.activeEditor.selection.setRng(range);
            }
          } else if (blocks.length - 1 === i && blocks.length > 1) {
            //否则构造一个select让其选中当前block的最开始那个
            var currentRange = range.cloneRange();
            var startNode = range.endContainer;
            while (true) {
              var prevNode = tinyMCE.activeEditor.dom.getPrev(
                startNode,
                function (node) {
                  return true;
                }
              );
              if (tinyMCE.activeEditor.dom.isChildOf(prevNode, blocks[i])) {
                startNode = prevNode;
              } else {
                break;
              }
            }
            currentRange.setStart(startNode, 0);
            tinyMCE.activeEditor.selection.setRng(currentRange);
            var text = tinyMCE.activeEditor.selection.getContent({
              format: "text",
            });
            tinyMCE.activeEditor.selection.setContent(setStyle(text));
            //把范围设置回去
            tinyMCE.activeEditor.selection.setRng(range);
          } else {
            //如果是夹在中间的则所有文字应用样式
            var text = strip_tags(blocks[i].innerHTML);
            blocks[i].innerHTML = setStyle(text);
            //如果已经有span标签了
            //如果是最后一个,且不是只选中了一个
          }
        }
      }
      // e.currentTarget.innerHTML
    },
formatpainterEditor() {
      this.formatpainterFlag = true;
    },
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 夜莺2517阅读 128,155评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 7,450评论 1 6
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,834评论 28 54
  • 兔子虽然是枚小硕 但学校的硕士四人寝不够 就被分到了博士楼里 两人一间 在学校的最西边 靠山 兔子的室友身体不好 ...
    待业的兔子阅读 2,766评论 2 9

友情链接更多精彩内容