diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts index 4f6890d49..f78dcb36f 100644 --- a/app/src/protyle/toolbar/index.ts +++ b/app/src/protyle/toolbar/index.ts @@ -629,10 +629,10 @@ export class Toolbar { i--; } else { this.range.insertNode(currentNewNode); - // https://github.com/siyuan-note/siyuan/issues/6155 - if (currentNewNode.nodeType !== 3 && ["code", "tag", "kbd"].includes(type)) { - const previousSibling = hasPreviousSibling(currentNewNode); - if (!previousSibling || previousSibling.textContent.endsWith("\n")) { + if (currentNewNode.nodeType === 1 && ["code", "tag", "kbd"].includes(type)) { + // 添加为 span https://github.com/siyuan-note/siyuan/issues/6155 + const currentPreviousSibling = hasPreviousSibling(currentNewNode); + if (!currentPreviousSibling || currentPreviousSibling.textContent.endsWith("\n")) { currentNewNode.before(document.createTextNode(Constants.ZWSP)); } if (!currentNewNode.textContent.startsWith(Constants.ZWSP)) { @@ -647,6 +647,47 @@ export class Toolbar { ) { currentNewNode.after(document.createTextNode(Constants.ZWSP)); } + } else if (currentNewNode.nodeType === 3 && ["code", "tag", "kbd", "clear"].includes(type)) { + const currentPreviousSibling = hasPreviousSibling(currentNewNode) as HTMLElement; + let previousIsCTK = false; + if (currentPreviousSibling) { + if (currentPreviousSibling.nodeType === 1) { + const currentPreviousSiblingTypes = currentPreviousSibling.dataset.type.split(" ") + if (currentPreviousSiblingTypes.includes("code") || currentPreviousSiblingTypes.includes("tag") || currentPreviousSiblingTypes.includes("kbd")) { + previousIsCTK = true; + } + } else if (currentPreviousSibling.textContent.endsWith(Constants.ZWSP)) { + currentPreviousSibling.textContent = currentPreviousSibling.textContent.substring(0, currentPreviousSibling.textContent.length - 1); + } + } + const currentNextSibling = hasNextSibling(currentNewNode) as HTMLElement; + let nextIsCTK = false; + if (currentNextSibling) { + if (currentNextSibling.nodeType === 1) { + const currentNextSiblingTypes = currentNextSibling.dataset.type.split(" ") + if (currentNextSiblingTypes.includes("code") || currentNextSiblingTypes.includes("tag") || currentNextSiblingTypes.includes("kbd")) { + nextIsCTK = true; + } + } else if (currentNextSibling.textContent.startsWith(Constants.ZWSP)) { + currentNextSibling.textContent = currentNextSibling.textContent.substring(1); + } + } + if (currentNewNode) { + if (previousIsCTK) { + if (!currentNewNode.textContent.startsWith(Constants.ZWSP)) { + currentNewNode.textContent = Constants.ZWSP + currentNewNode.textContent; + } + } else if (currentNewNode.textContent.startsWith(Constants.ZWSP)) { + currentNewNode.textContent = currentNewNode.textContent.substring(1); + } + if (nextIsCTK) { + if (!currentNextSibling.textContent.startsWith(Constants.ZWSP)) { + currentNextSibling.textContent = Constants.ZWSP + currentNextSibling.textContent; + } + } else if (currentNewNode.textContent.endsWith(Constants.ZWSP)) { + currentNewNode.textContent = currentNewNode.textContent.substring(0, currentNewNode.textContent.length - 1); + } + } } this.range.collapse(false); } diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts index 5926b9b2d..3570f7e5d 100644 --- a/app/src/protyle/wysiwyg/index.ts +++ b/app/src/protyle/wysiwyg/index.ts @@ -91,6 +91,7 @@ import { } from "../render/av/cell"; import {openEmojiPanel, unicode2Emoji} from "../../emoji"; import {openLink} from "../../editor/openLink"; +import {mathRender} from "../render/mathRender"; export class WYSIWYG { public lastHTMLs: { [key: string]: string } = {}; @@ -1443,6 +1444,7 @@ export class WYSIWYG { tempElement.append(range.extractContents()); nodeElement.outerHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML); nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`) as HTMLElement; + mathRender(nodeElement); focusByWbr(nodeElement, range); } else { const inlineMathElement = hasClosestByAttribute(range.commonAncestorContainer, "data-type", "inline-math");