Browse Source

:bug: copy inline code and paste

Van 5 years ago
parent
commit
e8d4d87f47

+ 1 - 1
demo/index.js

@@ -3,7 +3,7 @@ import '../src/assets/scss/index.scss'
 window.vditor = new Vditor('vditor', {
   debugger: true,
   typewriterMode: true,
-  mode: 'ir',
+  // mode: 'ir',
   placeholder: 'placeholder',
   preview: {
     markdown: {

+ 0 - 3
src/ts/toolbar/Both.ts

@@ -6,9 +6,6 @@ import {MenuItem} from "./MenuItem";
 export class Both extends MenuItem {
     constructor(vditor: IVditor, menuItem: IMenuItem) {
         super(vditor, menuItem);
-        if (vditor.currentMode !== "sv") {
-            this.element.style.display = "none";
-        }
         this.element.children[0].innerHTML = menuItem.icon || bothSVG;
         if (vditor.options.preview.mode === "both") {
             this.element.children[0].className =

+ 0 - 3
src/ts/toolbar/Format.ts

@@ -8,9 +8,6 @@ import {MenuItem} from "./MenuItem";
 export class Format extends MenuItem {
     constructor(vditor: IVditor, menuItem: IMenuItem) {
         super(vditor, menuItem);
-        if (vditor.currentMode !== "sv") {
-            this.element.style.display = "none";
-        }
         this.element.children[0].innerHTML = menuItem.icon || formatSVG;
         this.element.children[0].addEventListener(getEventName(), (event) => {
             formatRender(vditor,  vditor.lute.FormatMd( getMarkdown(vditor)),

+ 0 - 3
src/ts/toolbar/Preview.ts

@@ -6,9 +6,6 @@ import {MenuItem} from "./MenuItem";
 export class Preview extends MenuItem {
     constructor(vditor: IVditor, menuItem: IMenuItem) {
         super(vditor, menuItem);
-        if (vditor.currentMode !== "sv") {
-            this.element.style.display = "none";
-        }
         this.element.children[0].innerHTML = menuItem.icon || previewSVG;
         if (vditor.currentPreviewMode === "preview") {
             this.element.children[0].className =

+ 1 - 1
src/ts/toolbar/Redo.ts

@@ -14,7 +14,7 @@ export class Redo extends MenuItem {
             }
             if (vditor.currentMode === "sv") {
                 vditor.undo.redo(vditor);
-            } else {
+            } else if (vditor.currentMode === "wysiwyg") {
                 vditor.wysiwygUndo.redo(vditor);
             }
             event.preventDefault();

+ 3 - 1
src/ts/toolbar/Undo.ts

@@ -14,8 +14,10 @@ export class Undo extends MenuItem {
             }
             if (vditor.currentMode === "sv") {
                 vditor.undo.undo(vditor);
-            } else {
+            } else if (vditor.currentMode === "wysiwyg") {
                 vditor.wysiwygUndo.undo(vditor);
+            }else if (vditor.currentMode === "ir") {
+                vditor.irUndo.undo(vditor);
             }
             event.preventDefault();
         });

+ 8 - 3
src/ts/util/processPasteCode.ts

@@ -1,3 +1,4 @@
+
 export const processPasteCode = (html: string, text: string, type = "sv") => {
     const tempElement = document.createElement("div");
     tempElement.innerHTML = html;
@@ -21,12 +22,16 @@ export const processPasteCode = (html: string, text: string, type = "sv") => {
 
     if (isCode) {
         const code = text || html;
-        if (type === "wysiwyg") {
-            return `${code}`;
-        }
         if (/\n/.test(code) || pres.length === 1) {
+            if (type !== "sv") {
+                return `<div class="vditor-wysiwyg__block" data-block="0" data-type="code-block"><pre><code>${
+                    code.replace(/&/g, "&amp;").replace(/</g, "&lt;")}<wbr></code></pre></div>`;
+            }
             return "```\n" + code + "\n```";
         } else {
+            if (type !== "sv") {
+                return `<code>${code.replace(/&/g, "&amp;").replace(/</g, "&lt;")}</code><wbr>`;
+            }
             return `\`${code}\``;
         }
     }

+ 5 - 4
src/ts/wysiwyg/index.ts

@@ -147,13 +147,14 @@ class WYSIWYG {
                 setSelectionByPosition(position.start + textPlain.length, position.start + textPlain.length,
                     codeElement.parentElement);
             } else if (code) {
-                const node = document.createElement("div");
-                node.innerHTML = `<div class="vditor-wysiwyg__block" data-block="0" data-type="code-block"><pre><code>${
-                    code.replace(/&/g, "&amp;").replace(/</g, "&lt;")}<wbr></code></pre></div>`;
-                range.insertNode(node.firstChild);
+                const node = document.createElement("template");
+                node.innerHTML = code;
+                range.insertNode(node.content.cloneNode(true));
                 const blockElement = hasClosestByAttribute(range.startContainer, "data-block", "0");
                 if (blockElement) {
                     blockElement.outerHTML = vditor.lute.SpinVditorDOM(blockElement.outerHTML);
+                } else {
+                    vditor.wysiwyg.element.innerHTML = vditor.lute.SpinVditorDOM(vditor.wysiwyg.element.innerHTML);
                 }
                 vditor.wysiwyg.element.querySelectorAll(".vditor-wysiwyg__block").forEach(
                     (blockRenderItem: HTMLElement) => {