Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: fix https://github.com/siyuan-note/siyuan/issues/8066

Vanessa053f88d0ce7e
diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts
index 69b703226c..adc1f1c801 100644
--- a/app/src/protyle/util/insertHTML.ts
+++ b/app/src/protyle/util/insertHTML.ts
@@ -34,7 +34,8 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
let id = blockElement.getAttribute("data-node-id");
range.insertNode(document.createElement("wbr"));
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.insertNode(document.createTextNode(html.replace(/\r\n|\r|\u2028|\u2029/g, "\n")));
range.collapse(false);
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts
index 1837564062..84bda1c33e 100644
--- a/app/src/protyle/util/paste.ts
+++ b/app/src/protyle/util/paste.ts
@@ -169,7 +169,8 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
});
const code = processPasteCode(textHTML, textPlain);
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);
return;
@@ -201,25 +202,8 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
highlightRender(protyle.wysiwyg.element);
} else if (code) {
if (!code.startsWith('<div data-type="NodeCodeBlock" class="code-block" data-node-id="')) {
- const wbrElement = document.createElement("wbr");
- range.insertNode(wbrElement);
- 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);
+ // 原有代码在行内元素中粘贴会嵌套
+ insertHTML(code, protyle);
} else {
insertHTML(code, protyle, true);
highlightRender(protyle.wysiwyg.element);
diff --git a/app/src/protyle/util/processCode.ts b/app/src/protyle/util/processCode.ts
index b78bc7442e..3c26225358 100644
--- a/app/src/protyle/util/processCode.ts
+++ b/app/src/protyle/util/processCode.ts
@@ -33,7 +33,7 @@ export const processPasteCode = (html: string, text: string) => {
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>`;
} else {
- return code;
+ return "`" + code + "`";
}
}
return false;