From cc099ff315ef74c23bf3b15ca60c5361706ee4c3 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Thu, 21 Dec 2023 23:46:08 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/9951 --- app/src/protyle/toolbar/index.ts | 19 ++++++----- app/src/protyle/wysiwyg/enter.ts | 44 +++++++++++++++++++------- app/src/protyle/wysiwyg/transaction.ts | 7 +++- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts index da5161a1e..4564ac22a 100644 --- a/app/src/protyle/toolbar/index.ts +++ b/app/src/protyle/toolbar/index.ts @@ -759,7 +759,7 @@ export class Toolbar { window.siyuan.menus.menu.remove(); const id = nodeElement.getAttribute("data-node-id"); const types = (renderElement.getAttribute("data-type") || "").split(" "); - const html = oldHTML || protyle.lute.SpinBlockDOM(nodeElement.outerHTML); + const html = oldHTML || nodeElement.outerHTML; let title = "HTML"; let placeholder = ""; const isInlineMemo = types.includes("inline-memo"); @@ -1009,16 +1009,16 @@ export class Toolbar { } let inlineLastNode: Element; if (types.includes("NodeHTMLBlock")) { - let html = textElement.value; - if (html) { + let htmlText = textElement.value; + if (htmlText) { // 需移除首尾的空白字符与连续的换行 (空行) https://github.com/siyuan-note/siyuan/issues/7921 - html = html.trim().replace(/\n+/g, "\n"); + htmlText = htmlText.trim().replace(/\n+/g, "\n"); // 需一对 div 标签包裹,否则行内元素会解析错误 https://github.com/siyuan-note/siyuan/issues/6764 - if (!(html.startsWith("
") && html.endsWith("
"))) { - html = `
\n${html}\n
`; + if (!(htmlText.startsWith("
") && htmlText.endsWith("
"))) { + htmlText = `
\n${htmlText}\n
`; } } - renderElement.querySelector("protyle-html").setAttribute("data-content", Lute.EscapeHTMLStr(html)); + renderElement.querySelector("protyle-html").setAttribute("data-content", Lute.EscapeHTMLStr(htmlText)); } else if (isInlineMemo) { let inlineMemoElements; if (updateElements) { @@ -1100,16 +1100,15 @@ export class Toolbar { } nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); - const newHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML); // HTML 块中包含多个
 时只能保存第一个 https://github.com/siyuan-note/siyuan/issues/5732
             if (types.includes("NodeHTMLBlock")) {
                 const tempElement = document.createElement("template");
-                tempElement.innerHTML = newHTML;
+                tempElement.innerHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);
                 if (tempElement.content.childElementCount > 1) {
                     showMessage(window.siyuan.languages.htmlBlockTip);
                 }
             }
-            updateTransaction(protyle, id, newHTML, html);
+            updateTransaction(protyle, id, nodeElement.outerHTML, html);
         };
         this.subElement.style.zIndex = (++window.siyuan.zIndex).toString();
         this.subElement.classList.remove("fn__none");
diff --git a/app/src/protyle/wysiwyg/enter.ts b/app/src/protyle/wysiwyg/enter.ts
index 76576162a..addf90f8d 100644
--- a/app/src/protyle/wysiwyg/enter.ts
+++ b/app/src/protyle/wysiwyg/enter.ts
@@ -382,19 +382,39 @@ export const enter = (blockElement: HTMLElement, range: Range, protyle: IProtyle
     // 图片后的零宽空格前回车 https://github.com/siyuan-note/siyuan/issues/5690
     const enterElement = document.createElement("div");
     enterElement.innerHTML = protyle.lute.SpinBlockDOM(editableElement.parentElement.outerHTML);
-    editableElement.innerHTML = enterElement.querySelector('[contenteditable="true"]').innerHTML;
-    mathRender(editableElement);
+    const doOperation: IOperation[] = [];
+    const undoOperation: IOperation[] = [];
+    // 回车之前的块为 1\n\n2 时会产生多个块
+    Array.from(enterElement.children).forEach((item: HTMLElement) => {
+        if (item.dataset.nodeId === id) {
+            editableElement.innerHTML = item.querySelector('[contenteditable="true"]').innerHTML;
+            doOperation.push({
+                action: "update",
+                data: blockElement.outerHTML,
+                id,
+            })
+            undoOperation.push({
+                action: "update",
+                data: html,
+                id,
+            })
+            mathRender(editableElement);
+        } else {
+            doOperation.push({
+                action: "insert",
+                data: item.outerHTML,
+                id: item.dataset.nodeId,
+                nextID: id,
+            })
+            blockElement.insertAdjacentElement("beforebegin", item);
+            undoOperation.push({
+                action: "delete",
+                id: item.dataset.nodeId,
+            })
+            mathRender(item);
+        }
+    })
 
-    const doOperation: IOperation[] = [{
-        action: "update",
-        data: blockElement.outerHTML,
-        id: id,
-    }];
-    const undoOperation: IOperation[] = [{
-        action: "update",
-        data: html,
-        id: id,
-    }];
     let previousElement = blockElement;
     Array.from(newElement.children).forEach((item: HTMLElement) => {
         const newId = item.getAttribute("data-node-id");
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 50559ef2f..ed92196af 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -336,7 +336,12 @@ const deleteBlock = (updateElements: Element[], id: string, protyle: IProtyle, i
 
 const updateBlock = (updateElements: Element[], protyle: IProtyle, operation: IOperation, isUndo: boolean) => {
     updateElements.forEach(item => {
-        item.outerHTML = operation.data;
+        // 图标撤销后无法渲染
+        if (item.getAttribute("data-subtype") === "echarts") {
+            item.outerHTML = protyle.lute.SpinBlockDOM(operation.data);
+        } else {
+            item.outerHTML = operation.data;
+        }
     });
     Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`)).find(item => {
         if (item.getAttribute("data-type") === "NodeBlockQueryEmbed" // 引用转换为块嵌入,undo、redo 后也需要更新 updateElement