import {Constants} from "../../constants"; import {uploadFiles, uploadLocalFiles} from "../upload"; import {processPasteCode, processRender} from "./processCode"; import {writeText} from "./compatibility"; import {hasClosestBlock} from "./hasClosest"; import {focusByWbr, getEditorRange} from "./selection"; import {blockRender} from "../markdown/blockRender"; import * as dayjs from "dayjs"; import {highlightRender} from "../markdown/highlightRender"; import {transaction, updateTransaction} from "../wysiwyg/transaction"; import {fetchPost, fetchSyncPost} from "../../util/fetch"; import {isDynamicRef, isFileAnnotation} from "../../util/functions"; import {insertHTML} from "./insertHTML"; import {scrollCenter} from "../../util/highlightById"; import {getContenteditableElement} from "../wysiwyg/getBlock"; const filterClipboardHint = (protyle: IProtyle, textPlain: string) => { let needRender = true; protyle.options.hint.extend.find(item => { if (item.key === textPlain) { needRender = false; return true; } }); if (needRender) { protyle.hint.render(protyle); } }; export const pasteText = (protyle: IProtyle, textPlain: string, nodeElement: Element) => { const range = getEditorRange(protyle.wysiwyg.element); const id = nodeElement.getAttribute("data-node-id"); if (nodeElement.getAttribute("data-type") === "NodeCodeBlock") { // 粘贴在代码位置 range.insertNode(document.createElement("wbr")); const html = nodeElement.outerHTML; range.deleteContents(); range.insertNode(document.createTextNode(textPlain.replace(/\r\n|\r|\u2028|\u2029/g, "\n"))); range.collapse(false); range.insertNode(document.createElement("wbr")); nodeElement.outerHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML); nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`) as HTMLElement; highlightRender(nodeElement); nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); updateTransaction(protyle, id, nodeElement.outerHTML, html); return; } if (range.toString() !== "") { if (isDynamicRef(textPlain)) { textPlain = textPlain.replace(/'.+'\)\)$/, ` "${range.toString()}"))`); } else if (isFileAnnotation(textPlain)) { textPlain = textPlain.replace(/".+">>$/, `"${range.toString()}">>`); } else if (protyle.lute.IsValidLinkDest(textPlain)) { textPlain = `[${range.toString()}](${textPlain})`; } } insertHTML(protyle.lute.Md2BlockDOM(textPlain), protyle); blockRender(protyle, protyle.wysiwyg.element); processRender(protyle.wysiwyg.element); highlightRender(protyle.wysiwyg.element); filterClipboardHint(protyle, textPlain); scrollCenter(protyle); }; export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEvent) & { target: HTMLElement }) => { event.stopPropagation(); event.preventDefault(); let textHTML; let textPlain; let files; if ("clipboardData" in event) { textHTML = event.clipboardData.getData("text/html"); textPlain = event.clipboardData.getData("text/plain"); files = event.clipboardData.files; } else { textHTML = event.dataTransfer.getData("text/html"); textPlain = event.dataTransfer.getData("text/plain"); if (event.dataTransfer.types[0] === "Files") { files = event.dataTransfer.items; } } /// #if !MOBILE if (!textHTML && !textPlain && ("clipboardData" in event) && "darwin" !== window.siyuan.config.system.os) { const xmlString = await fetchSyncPost("/api/clipboard/readFilePaths", {}); if (xmlString.data.length > 0) { uploadLocalFiles(xmlString.data, protyle); writeText(""); return; } } /// #endif // 浏览器地址栏拷贝处理 if (textHTML.replace(/&/g, "&").replace(/<(|\/)(html|body|meta)[^>]*?>/ig, "").trim() === `${textPlain}` || textHTML.replace(/&/g, "&").replace(/<(|\/)(html|body|meta)[^>]*?>/ig, "").trim() === `${textPlain}`) { textHTML = ""; } if (!textHTML.endsWith(Constants.ZWSP) && !textHTML.startsWith(Constants.ZWSP)) { // process word const doc = new DOMParser().parseFromString(textHTML, "text/html"); if (doc.body && doc.body.innerHTML) { textHTML = doc.body.innerHTML; } } textHTML = Lute.Sanitize(textHTML); const nodeElement = hasClosestBlock(event.target); if (!nodeElement) { if (files && files.length > 0) { uploadFiles(protyle, files); } return; } protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select, .protyle-wysiwyg--hl").forEach(item => { item.classList.remove("protyle-wysiwyg--select", "protyle-wysiwyg--hl"); }); const code = processPasteCode(textHTML, textPlain); const range = getEditorRange(protyle.wysiwyg.element); const id = nodeElement.getAttribute("data-node-id"); // process code if (nodeElement.getAttribute("data-type") === "NodeCodeBlock") { // 粘贴在代码位置 range.insertNode(document.createElement("wbr")); const html = nodeElement.outerHTML; range.deleteContents(); range.insertNode(document.createTextNode(textPlain.replace(/\r\n|\r|\u2028|\u2029/g, "\n"))); range.collapse(false); range.insertNode(document.createElement("wbr")); getContenteditableElement(nodeElement).removeAttribute("data-render"); highlightRender(nodeElement); nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); updateTransaction(protyle, id, nodeElement.outerHTML, html); setTimeout(() => { scrollCenter(protyle, nodeElement as Element); }, Constants.TIMEOUT_BLOCKLOAD); return; } else if (code) { if (!code.startsWith('