Van 6 years ago
parent
commit
ac19d95b61
2 changed files with 25 additions and 2 deletions
  1. 23 2
      src/ts/wysiwyg/index.ts
  2. 2 0
      src/ts/wysiwyg/setRangeByWbr.ts

+ 23 - 2
src/ts/wysiwyg/index.ts

@@ -78,9 +78,7 @@ class WYSIWYG {
             }
 
             if (textHTML.trim() !== "") {
-                console.log(`HTML2VditorDOM-argument[${textHTML}]`);
                 document.execCommand("insertHTML", false, vditor.lute.HTML2VditorDOM(textHTML));
-                console.log(`HTML2VditorDOM-result[${vditor.lute.HTML2VditorDOM(textHTML)}]`);
             } else if (event.clipboardData.files.length > 0 && vditor.options.upload.url) {
                 uploadFiles(vditor, event.clipboardData.files);
             } else if (textPlain.trim() !== "" && event.clipboardData.files.length === 0) {
@@ -99,6 +97,29 @@ class WYSIWYG {
             afterRenderEvent(vditor);
         });
 
+        // 中文处理
+        this.element.addEventListener("compositionend", () => {
+            const range = getSelection().getRangeAt(0).cloneRange();
+            // 保存光标
+            this.element.querySelectorAll("wbr").forEach((wbr) => {
+                wbr.remove();
+            });
+            const wbrNode = document.createElement("wbr");
+            range.insertNode(wbrNode);
+
+            // markdown 纠正
+            // 合并多个 em, strong,s。以防止多个相同元素在一起时不满足 commonmark 规范,出现标记符
+            const vditorHTML = this.element.innerHTML.replace(/<\/strong><strong data-marker="\W{2}">/g, "")
+                .replace(/<\/em><em data-marker="\W{1}">/g, "")
+                .replace(/<\/s><s data-marker="~{1,2}">/g, "");
+            this.element.innerHTML = vditor.lute.SpinVditorDOM(vditorHTML);
+            this.element.insertAdjacentElement("beforeend", this.popover);
+
+            // 设置光标
+            setRangeByWbr(this.element, range);
+            afterRenderEvent(vditor);
+        });
+
         this.element.addEventListener("input", (event: IHTMLInputEvent) => {
             const range = getSelection().getRangeAt(0).cloneRange();
 

+ 2 - 0
src/ts/wysiwyg/setRangeByWbr.ts

@@ -24,6 +24,8 @@ export const setRangeByWbr = (element: HTMLElement, range: Range) => {
                     document.execCommand("italic", false, "");
                 } else if (wbrElement.previousElementSibling.tagName === "STRONG") {
                     document.execCommand("bold", false, "");
+                } else if (wbrElement.previousElementSibling.tagName === "S") {
+                    document.execCommand("strikeThrough", false, "");
                 }
                 return;
             } else {