https://github.com/siyuan-note/siyuan/issues/2911 选择 span 中的一部分需进行包裹

This commit is contained in:
Vanessa 2022-09-16 17:29:04 +08:00
parent 06a2e5fbd1
commit dcee38bcfa

View File

@ -309,11 +309,22 @@ export class Toolbar {
// aaa**bbb** 选中 aaa 加粗 // aaa**bbb** 选中 aaa 加粗
nextElement = nextSibling as HTMLElement; nextElement = nextSibling as HTMLElement;
} }
const wbrElement = document.createElement("wbr"); this.range.insertNode(document.createElement("wbr"));
this.range.insertNode(wbrElement);
const html = nodeElement.outerHTML; const html = nodeElement.outerHTML;
const contents = this.range.extractContents(); const contents = this.range.extractContents();
this.mergeNode(contents.childNodes); this.mergeNode(contents.childNodes);
// 选择 span 中的一部分需进行包裹
if (previousElement && nextElement && previousElement.isSameNode(nextElement) && contents.firstChild.nodeType === 3) {
const attributes = previousElement.attributes;
contents.childNodes.forEach(item => {
const spanElement = document.createElement("span");
for (let i = 0; i < attributes.length; i++) {
spanElement.setAttribute(attributes[i].name, attributes[i].value);
}
spanElement.innerHTML = item.textContent;
item.replaceWith(spanElement);
})
}
const actionBtn = action === "toolbar" ? this.element.querySelector(`[data-type="${type}"]`) : undefined; const actionBtn = action === "toolbar" ? this.element.querySelector(`[data-type="${type}"]`) : undefined;
const newNodes: Node[] = []; const newNodes: Node[] = [];
if (actionBtn?.classList.contains("protyle-toolbar__item--current") || ( if (actionBtn?.classList.contains("protyle-toolbar__item--current") || (
@ -506,7 +517,10 @@ export class Toolbar {
} }
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html); updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html);
wbrElement.remove(); const wbrElement = nodeElement.querySelector("wbr")
if (wbrElement) {
wbrElement.remove();
}
} }
public showFileAnnotationRef(protyle: IProtyle, refElement: HTMLElement) { public showFileAnnotationRef(protyle: IProtyle, refElement: HTMLElement) {