Przeglądaj źródła

:art: fix https://github.com/siyuan-note/siyuan/issues/6321

Vanessa 2 lat temu
rodzic
commit
5fdfa40551
2 zmienionych plików z 21 dodań i 27 usunięć
  1. 17 0
      app/src/protyle/util/insertHTML.ts
  2. 4 27
      app/src/protyle/util/paste.ts

+ 17 - 0
app/src/protyle/util/insertHTML.ts

@@ -6,6 +6,8 @@ import {getContenteditableElement} from "../wysiwyg/getBlock";
 import {focusBlock, getEditorRange, focusByWbr, fixTableRange} from "./selection";
 import {mathRender} from "../markdown/mathRender";
 import {Constants} from "../../constants";
+import {highlightRender} from "../markdown/highlightRender";
+import {scrollCenter} from "../../util/highlightById";
 
 export const insertHTML = (html: string, protyle: IProtyle, isBlock = false) => {
     if (html === "") {
@@ -31,6 +33,21 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false) =>
     let id = blockElement.getAttribute("data-node-id");
     range.insertNode(document.createElement("wbr"));
     let oldHTML = blockElement.outerHTML;
+    if (!isBlock && blockElement.getAttribute("data-type") === "NodeCodeBlock") {
+        range.deleteContents();
+        range.insertNode(document.createTextNode(html.replace(/\r\n|\r|\u2028|\u2029/g, "\n")));
+        range.collapse(false);
+        range.insertNode(document.createElement("wbr"));
+        getContenteditableElement(blockElement).removeAttribute("data-render");
+        highlightRender(blockElement);
+        blockElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
+        updateTransaction(protyle, id, blockElement.outerHTML, oldHTML);
+        setTimeout(() => {
+            scrollCenter(protyle, blockElement);
+        }, Constants.TIMEOUT_BLOCKLOAD);
+        return;
+    }
+
     const undoOperation: IOperation[] = [];
     const doOperation: IOperation[] = [];
     if (range.toString() !== "") {

+ 4 - 27
app/src/protyle/util/paste.ts

@@ -31,7 +31,7 @@ const filterClipboardHint = (protyle: IProtyle, textPlain: string) => {
     }
 };
 
-export const pasteAsPlainText = async (protyle:IProtyle) => {
+export const pasteAsPlainText = async (protyle: IProtyle) => {
     /// #if !BROWSER && !MOBILE
     let localFiles: string[] = [];
     if ("darwin" === window.siyuan.config.system.os) {
@@ -54,27 +54,16 @@ export const pasteAsPlainText = async (protyle:IProtyle) => {
         protyle.lute.SetHTMLTag2TextMark(true); // 临时设置 Lute 解析参数,行级元素键盘和下划线无法粘贴为纯文本 https://github.com/siyuan-note/siyuan/issues/6220
         const dom = protyle.lute.InlineMd2BlockDOM(clipboard.readText());
         protyle.lute.SetHTMLTag2TextMark(false);
-        insertHTML(protyle.lute.BlockDOM2Content(dom), protyle, false);
+        insertHTML(protyle.lute.BlockDOM2Content(dom), protyle);
     }
     /// #endif
 };
 
 export const pasteText = (protyle: IProtyle, textPlain: string, nodeElement: Element) => {
     const range = getEditorRange(protyle.wysiwyg.element);
-    const id = nodeElement.getAttribute("data-node-id");
     if (nodeElement.getAttribute("data-type") === "NodeCodeBlock") {
         // 粘贴在代码位置
-        range.insertNode(document.createElement("wbr"));
-        const html = nodeElement.outerHTML;
-        range.deleteContents();
-        range.insertNode(document.createTextNode(textPlain.replace(/\r\n|\r|\u2028|\u2029/g, "\n")));
-        range.collapse(false);
-        range.insertNode(document.createElement("wbr"));
-        nodeElement.outerHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);
-        nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`) as HTMLElement;
-        highlightRender(nodeElement);
-        nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
-        updateTransaction(protyle, id, nodeElement.outerHTML, html);
+        insertHTML(textPlain, protyle);
         return;
     }
     if (range.toString() !== "") {
@@ -184,19 +173,7 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
     // process code
     if (nodeElement.getAttribute("data-type") === "NodeCodeBlock") {
         // 粘贴在代码位置
-        range.insertNode(document.createElement("wbr"));
-        const html = nodeElement.outerHTML;
-        range.deleteContents();
-        range.insertNode(document.createTextNode(textPlain.replace(/\r\n|\r|\u2028|\u2029/g, "\n")));
-        range.collapse(false);
-        range.insertNode(document.createElement("wbr"));
-        getContenteditableElement(nodeElement).removeAttribute("data-render");
-        highlightRender(nodeElement);
-        nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
-        updateTransaction(protyle, id, nodeElement.outerHTML, html);
-        setTimeout(() => {
-            scrollCenter(protyle, nodeElement as Element);
-        }, Constants.TIMEOUT_BLOCKLOAD);
+        insertHTML(textPlain, protyle);
         return;
     } else if (code) {
         if (!code.startsWith('<div data-type="NodeCodeBlock" class="code-block" data-node-id="')) {