This commit is contained in:
Vanessa 2022-09-26 15:32:49 +08:00
parent a3c03eca07
commit 1c69c2bbdc
2 changed files with 23 additions and 59 deletions

View file

@ -28,66 +28,9 @@ export class InlineMemo extends ToolbarItem {
return;
}
fixTableRange(range);
if (!["DIV", "TD", "TH", "TR"].includes(range.startContainer.parentElement.tagName) && range.startOffset === 0 && !hasPreviousSibling(range.startContainer)) {
range.setStartBefore(range.startContainer.parentElement);
}
if (!["DIV", "TD", "TH", "TR"].includes(range.endContainer.parentElement.tagName) && range.endOffset === range.endContainer.textContent.length && !hasNextSibling(range.endContainer)) {
range.setEndAfter(range.endContainer.parentElement);
}
const oldHTML = nodeElement.outerHTML;
const newNodes: Element[] = [];
const contents = range.extractContents();
contents.childNodes.forEach((item: HTMLElement) => {
if (item.nodeType === 3) {
if (item.textContent) {
const inlineElement = document.createElement("span");
inlineElement.setAttribute("data-type", "inline-memo");
inlineElement.textContent = item.textContent;
newNodes.push(inlineElement);
}
} else {
let types = (item.getAttribute("data-type") || "").split(" ");
types.push("inline-memo");
types = [...new Set(types)];
if (item.tagName !== "BR" && item.tagName !== "WBR" && !types.includes("inline-math")) {
item.setAttribute("data-type", types.join(" "));
newNodes.push(item);
} else if (item.tagName !== "WBR") {
newNodes.push(item);
}
}
protyle.toolbar.setInlineMark(protyle, "inline-memo", "range", {
type: "inline-memo",
});
for (let i = 0; i < newNodes.length; i++) {
const currentNewNode = newNodes[i] as HTMLElement;
const nextNewNode = newNodes[i + 1] as HTMLElement;
if (currentNewNode.nodeType !== 3 && nextNewNode && nextNewNode.nodeType !== 3 &&
isArrayEqual(nextNewNode.getAttribute("data-type").split(" "), currentNewNode.getAttribute("data-type").split(" ")) &&
currentNewNode.style.fontSize === nextNewNode.style.fontSize &&
currentNewNode.style.color === nextNewNode.style.color &&
currentNewNode.style.webkitTextFillColor === nextNewNode.style.webkitTextFillColor &&
currentNewNode.style.webkitTextStroke === nextNewNode.style.webkitTextStroke &&
currentNewNode.style.textShadow === nextNewNode.style.textShadow &&
currentNewNode.style.backgroundColor === nextNewNode.style.backgroundColor) {
// 合并相同的 node
nextNewNode.innerHTML = currentNewNode.innerHTML + nextNewNode.innerHTML;
newNodes.splice(i, 1);
i--;
} else {
range.insertNode(newNodes[i]);
range.collapse(false);
// 数学公式不允许备注
if (currentNewNode.nodeType !== 3 && (currentNewNode.getAttribute("data-type") || "").indexOf("inline-math") > -1) {
newNodes.splice(i, 1);
i--;
}
}
}
if (newNodes[0]) {
range.setStart(newNodes[0].firstChild, 0);
protyle.toolbar.showRender(protyle, newNodes[0], newNodes, oldHTML);
}
});
}
}

View file

@ -434,6 +434,23 @@ export class Toolbar {
return true;
}
});
} else if (type === "inline-memo" && types.includes("inline-math")) {
// 数学公式和备注不能同时存在
types.find((item, index) => {
if (item === "inline-math") {
types.splice(index, 1);
return true;
}
});
item.textContent = item.getAttribute("data-content");
} else if (type === "inline-math" && types.includes("inline-memo")) {
// 数学公式和备注不能同时存在
types.find((item, index) => {
if (item === "inline-memo") {
types.splice(index, 1);
return true;
}
});
}
types = [...new Set(types)];
if (index === 0 && previousElement && previousElement.nodeType !== 3 &&
@ -553,6 +570,10 @@ export class Toolbar {
return;
}
}
if (type === "inline-memo") {
protyle.toolbar.showRender(protyle, newNodes[0] as HTMLElement, newNodes as Element[], html);
return;
}
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html);
const wbrElement = nodeElement.querySelector("wbr");