From 5c7f3ab0bd14c64ec1a879dc8e812de7b8425c2c Mon Sep 17 00:00:00 2001 From: Vanessa Date: Wed, 1 Feb 2023 16:11:49 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/7220 --- app/src/protyle/util/paste.ts | 15 ++++++++++++--- app/src/protyle/util/processCode.ts | 13 ++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts index 349f68d55..8799df5f8 100644 --- a/app/src/protyle/util/paste.ts +++ b/app/src/protyle/util/paste.ts @@ -16,6 +16,7 @@ import {isDynamicRef, isFileAnnotation} from "../../util/functions"; import {insertHTML} from "./insertHTML"; import {scrollCenter} from "../../util/highlightById"; import {hideElements} from "../ui/hideElements"; +import {hasNextSibling, hasPreviousSibling} from "../wysiwyg/getBlock"; const filterClipboardHint = (protyle: IProtyle, textPlain: string) => { let needRender = true; @@ -176,10 +177,18 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven const html = nodeElement.outerHTML; wbrElement.remove(); range.deleteContents(); - const tempElement = document.createElement("code"); - tempElement.textContent = code; - range.insertNode(document.createElement("wbr")); + 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", ""); + } else { + tempElement.insertAdjacentHTML("afterend", Constants.ZWSP + ""); + } updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html); focusByWbr(protyle.wysiwyg.element, range); } else { diff --git a/app/src/protyle/util/processCode.ts b/app/src/protyle/util/processCode.ts index 63616dea2..b78bc7442 100644 --- a/app/src/protyle/util/processCode.ts +++ b/app/src/protyle/util/processCode.ts @@ -16,18 +16,13 @@ export const processPasteCode = (html: string, text: string) => { (tempElement.lastElementChild as HTMLElement).style.fontFamily.indexOf("monospace") > -1) { // VS Code isCode = true; - } - const pres = tempElement.querySelectorAll("pre"); - if (tempElement.childElementCount === 1 && pres.length === 1 - && pres[0].className !== "protyle-sv") { + } else if (tempElement.childElementCount === 1 && tempElement.querySelectorAll("pre").length === 1) { // IDE isCode = true; - } - if (html.indexOf('\n

') === 0) { + } else if (html.indexOf('\n

') === 0) { // Xcode isCode = true; - } - if (tempElement.childElementCount === 1 && tempElement.firstElementChild.tagName === "TABLE" && + } else if (tempElement.childElementCount === 1 && tempElement.firstElementChild.tagName === "TABLE" && tempElement.querySelector(".line-number") && tempElement.querySelector(".line-content")) { // 网页源码 isCode = true; @@ -35,7 +30,7 @@ export const processPasteCode = (html: string, text: string) => { if (isCode) { const code = text || html; - if (/\n/.test(code) || pres.length === 1) { + if (/\n/.test(code)) { return `

${window.siyuan.storage[Constants.LOCAL_CODELANG]}
${code.replace(/&/g, "&").replace(/
${Constants.ZWSP}
`; } else { return code;