This commit is contained in:
Vanessa 2023-04-22 17:34:27 +08:00
parent 111a49294e
commit 053f88d0ce
3 changed files with 7 additions and 22 deletions

View file

@ -34,7 +34,8 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
let id = blockElement.getAttribute("data-node-id"); let id = blockElement.getAttribute("data-node-id");
range.insertNode(document.createElement("wbr")); range.insertNode(document.createElement("wbr"));
let oldHTML = blockElement.outerHTML; let oldHTML = blockElement.outerHTML;
if (!isBlock && blockElement.getAttribute("data-type") === "NodeCodeBlock") { if (!isBlock &&
(blockElement.getAttribute("data-type") === "NodeCodeBlock" || protyle.toolbar.getCurrentType(range).includes("code"))) {
range.deleteContents(); range.deleteContents();
range.insertNode(document.createTextNode(html.replace(/\r\n|\r|\u2028|\u2029/g, "\n"))); range.insertNode(document.createTextNode(html.replace(/\r\n|\r|\u2028|\u2029/g, "\n")));
range.collapse(false); range.collapse(false);

View file

@ -169,7 +169,8 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
}); });
const code = processPasteCode(textHTML, textPlain); const code = processPasteCode(textHTML, textPlain);
const range = getEditorRange(protyle.wysiwyg.element); const range = getEditorRange(protyle.wysiwyg.element);
if (nodeElement.getAttribute("data-type") === "NodeCodeBlock") { if (nodeElement.getAttribute("data-type") === "NodeCodeBlock" ||
protyle.toolbar.getCurrentType(range).includes("code")) {
// 粘贴在代码位置 // 粘贴在代码位置
insertHTML(textPlain, protyle); insertHTML(textPlain, protyle);
return; return;
@ -201,25 +202,8 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
highlightRender(protyle.wysiwyg.element); highlightRender(protyle.wysiwyg.element);
} else if (code) { } else if (code) {
if (!code.startsWith('<div data-type="NodeCodeBlock" class="code-block" data-node-id="')) { if (!code.startsWith('<div data-type="NodeCodeBlock" class="code-block" data-node-id="')) {
const wbrElement = document.createElement("wbr"); // 原有代码在行内元素中粘贴会嵌套
range.insertNode(wbrElement); insertHTML(code, protyle);
const html = nodeElement.outerHTML;
wbrElement.remove();
range.deleteContents();
const tempElement = document.createElement("span");
tempElement.setAttribute("data-type", "code");
tempElement.textContent = Constants.ZWSP + code;
range.insertNode(tempElement);
if (!hasPreviousSibling(tempElement)) {
tempElement.insertAdjacentHTML("beforebegin", Constants.ZWSP);
}
if (hasNextSibling(tempElement)) {
tempElement.insertAdjacentHTML("afterend", "<wbr>");
} else {
tempElement.insertAdjacentHTML("afterend", Constants.ZWSP + "<wbr>");
}
updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html);
focusByWbr(protyle.wysiwyg.element, range);
} else { } else {
insertHTML(code, protyle, true); insertHTML(code, protyle, true);
highlightRender(protyle.wysiwyg.element); highlightRender(protyle.wysiwyg.element);

View file

@ -33,7 +33,7 @@ export const processPasteCode = (html: string, text: string) => {
if (/\n/.test(code)) { if (/\n/.test(code)) {
return `<div data-type="NodeCodeBlock" class="code-block" data-node-id="${Lute.NewNodeID()}"><div class="protyle-action"><span class="protyle-action--first protyle-action__language" contenteditable="false">${window.siyuan.storage[Constants.LOCAL_CODELANG]}</span><span class="fn__flex-1"></span><span class="protyle-icon protyle-icon--first protyle-action__copy"><svg><use xlink:href="#iconCopy"></use></svg></span><span class="protyle-icon protyle-icon--last protyle-action__menu"><svg><use xlink:href="#iconMore"></use></svg></span></div><div contenteditable="true" spellcheck="${window.siyuan.config.editor.spellcheck}">${code.replace(/&/g, "&amp;").replace(/</g, "&lt;")}<wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`; return `<div data-type="NodeCodeBlock" class="code-block" data-node-id="${Lute.NewNodeID()}"><div class="protyle-action"><span class="protyle-action--first protyle-action__language" contenteditable="false">${window.siyuan.storage[Constants.LOCAL_CODELANG]}</span><span class="fn__flex-1"></span><span class="protyle-icon protyle-icon--first protyle-action__copy"><svg><use xlink:href="#iconCopy"></use></svg></span><span class="protyle-icon protyle-icon--last protyle-action__menu"><svg><use xlink:href="#iconMore"></use></svg></span></div><div contenteditable="true" spellcheck="${window.siyuan.config.editor.spellcheck}">${code.replace(/&/g, "&amp;").replace(/</g, "&lt;")}<wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
} else { } else {
return code; return "`" + code + "`";
} }
} }
return false; return false;