Skip to content

Commit

Permalink
💄 #11285
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed May 9, 2024
1 parent 317359c commit 402a7a2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
49 changes: 45 additions & 4 deletions app/src/protyle/toolbar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/protyle/wysiwyg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {};
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 402a7a2

Please sign in to comment.