Browse Source

:art: 为空数学公式关闭,光标位置错误

Vanessa 2 năm trước cách đây
mục cha
commit
c35a9fde6b
2 tập tin đã thay đổi với 26 bổ sung14 xóa
  1. 9 5
      app/src/protyle/toolbar/InlineMath.ts
  2. 17 9
      app/src/protyle/toolbar/index.ts

+ 9 - 5
app/src/protyle/toolbar/InlineMath.ts

@@ -4,7 +4,7 @@ import {updateTransaction} from "../wysiwyg/transaction";
 import {hasClosestBlock, hasClosestByAttribute} from "../util/hasClosest";
 import {hasNextSibling, hasPreviousSibling} from "../wysiwyg/getBlock";
 import {mathRender} from "../markdown/mathRender";
-import {fixTableRange} from "../util/selection";
+import {fixTableRange, focusByRange} from "../util/selection";
 
 export class InlineMath extends ToolbarItem {
     public element: HTMLElement;
@@ -50,11 +50,15 @@ export class InlineMath extends ToolbarItem {
             range.insertNode(newElement);
             mathRender(newElement);
             if (rangeString.trim() === "") {
-                protyle.toolbar.showRender(protyle, newElement);
+                protyle.toolbar.showRender(protyle, newElement, undefined, html);
+            } else {
+                range.setStartAfter(newElement);
+                range.collapse(true);
+                focusByRange(range)
+                nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
+                updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html);
+                wbrElement.remove();
             }
-            nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
-            updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html);
-            wbrElement.remove();
         });
     }
 }

+ 17 - 9
app/src/protyle/toolbar/index.ts

@@ -876,7 +876,7 @@ export class Toolbar {
             if (!renderElement.parentElement) {
                 return;
             }
-            let inlineMemoLastNode: Element;
+            let inlineLastNode: Element;
             if (types.includes("NodeHTMLBlock")) {
                 renderElement.querySelector("protyle-html").setAttribute("data-content", Lute.EscapeHTMLStr(textElement.value));
             } else if (isInlineMemo) {
@@ -903,12 +903,22 @@ export class Toolbar {
                             item.removeAttribute("data-inline-memo-content");
                         }
                         if (index === inlineMemoElements.length - 1) {
-                            inlineMemoLastNode = item;
+                            inlineLastNode = item;
                         }
                     } else {
                         item.setAttribute("data-inline-memo-content", Lute.EscapeHTMLStr(textElement.value));
                     }
                 });
+            } else if (types.includes("inline-math")) {
+                // 行内数学公式不允许换行 https://github.com/siyuan-note/siyuan/issues/2187
+                if (textElement.value) {
+                    renderElement.setAttribute("data-content", Lute.EscapeHTMLStr(textElement.value.replace(/\n/g, "")));
+                    renderElement.removeAttribute("data-render");
+                    processRender(renderElement);
+                } else {
+                    inlineLastNode = renderElement;
+                    renderElement.outerHTML = "<wbr>"
+                }
             } else {
                 renderElement.setAttribute("data-content", Lute.EscapeHTMLStr(textElement.value));
                 renderElement.removeAttribute("data-render");
@@ -919,10 +929,11 @@ export class Toolbar {
                 }
             }
 
+            // 光标定位
             if (renderElement.tagName === "SPAN") {
-                if (inlineMemoLastNode) {
-                    if (inlineMemoLastNode.parentElement) {
-                        this.range.setStartAfter(inlineMemoLastNode);
+                if (inlineLastNode) {
+                    if (inlineLastNode.parentElement) {
+                        this.range.setStartAfter(inlineLastNode);
                         this.range.collapse(true);
                         focusByRange(this.range);
                     } else {
@@ -936,10 +947,7 @@ export class Toolbar {
             } else {
                 focusSideBlock(renderElement);
             }
-            if (types.includes("inline-math")) {
-                // 行内数学公式不允许换行 https://github.com/siyuan-note/siyuan/issues/2187
-                renderElement.setAttribute("data-content", renderElement.getAttribute("data-content").replace(/\n/g, ""));
-            }
+
             nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
             const newHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);
             // HTML 块中包含多个 <pre> 时只能保存第一个 https://github.com/siyuan-note/siyuan/issues/5732