This commit is contained in:
Vanessa 2023-02-01 16:11:49 +08:00
parent c0240f20d6
commit 5c7f3ab0bd
2 changed files with 16 additions and 12 deletions

View File

@ -16,6 +16,7 @@ import {isDynamicRef, isFileAnnotation} from "../../util/functions";
import {insertHTML} from "./insertHTML"; import {insertHTML} from "./insertHTML";
import {scrollCenter} from "../../util/highlightById"; import {scrollCenter} from "../../util/highlightById";
import {hideElements} from "../ui/hideElements"; import {hideElements} from "../ui/hideElements";
import {hasNextSibling, hasPreviousSibling} from "../wysiwyg/getBlock";
const filterClipboardHint = (protyle: IProtyle, textPlain: string) => { const filterClipboardHint = (protyle: IProtyle, textPlain: string) => {
let needRender = true; let needRender = true;
@ -176,10 +177,18 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
const html = nodeElement.outerHTML; const html = nodeElement.outerHTML;
wbrElement.remove(); wbrElement.remove();
range.deleteContents(); range.deleteContents();
const tempElement = document.createElement("code"); const tempElement = document.createElement("span");
tempElement.textContent = code; tempElement.setAttribute("data-type", "code")
range.insertNode(document.createElement("wbr")); tempElement.textContent = Constants.ZWSP + code;
range.insertNode(tempElement); 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); updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, html);
focusByWbr(protyle.wysiwyg.element, range); focusByWbr(protyle.wysiwyg.element, range);
} else { } else {

View File

@ -16,18 +16,13 @@ export const processPasteCode = (html: string, text: string) => {
(tempElement.lastElementChild as HTMLElement).style.fontFamily.indexOf("monospace") > -1) { (tempElement.lastElementChild as HTMLElement).style.fontFamily.indexOf("monospace") > -1) {
// VS Code // VS Code
isCode = true; isCode = true;
} } else if (tempElement.childElementCount === 1 && tempElement.querySelectorAll("pre").length === 1) {
const pres = tempElement.querySelectorAll("pre");
if (tempElement.childElementCount === 1 && pres.length === 1
&& pres[0].className !== "protyle-sv") {
// IDE // IDE
isCode = true; isCode = true;
} } else if (html.indexOf('\n<p class="p1">') === 0) {
if (html.indexOf('\n<p class="p1">') === 0) {
// Xcode // Xcode
isCode = true; isCode = true;
} } else if (tempElement.childElementCount === 1 && tempElement.firstElementChild.tagName === "TABLE" &&
if (tempElement.childElementCount === 1 && tempElement.firstElementChild.tagName === "TABLE" &&
tempElement.querySelector(".line-number") && tempElement.querySelector(".line-content")) { tempElement.querySelector(".line-number") && tempElement.querySelector(".line-content")) {
// 网页源码 // 网页源码
isCode = true; isCode = true;
@ -35,7 +30,7 @@ export const processPasteCode = (html: string, text: string) => {
if (isCode) { if (isCode) {
const code = text || html; const code = text || html;
if (/\n/.test(code) || pres.length === 1) { 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;