diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts index bf274bb30..75bd37c02 100644 --- a/app/src/protyle/util/compatibility.ts +++ b/app/src/protyle/util/compatibility.ts @@ -23,24 +23,30 @@ export const encodeBase64 = (text: string): string => { } }; -const getSiyuanHTML = (text: IClipboardData) => { - const siyuanMatch = text.textHTML.match(//); +export const getTextSiyuanFromTextHTML = (html: string) => { + const siyuanMatch = html.match(//); + let textSiyuan = ""; + let textHtml = html; if (siyuanMatch) { try { if (typeof Buffer !== "undefined") { const decodedBytes = Buffer.from(siyuanMatch[1], "base64"); - text.siyuanHTML = decodedBytes.toString("utf8"); + textSiyuan = decodedBytes.toString("utf8"); } else { const decoder = new TextDecoder(); const bytes = Uint8Array.from(atob(siyuanMatch[1]), char => char.charCodeAt(0)); - text.siyuanHTML = decoder.decode(bytes); + textSiyuan = decoder.decode(bytes); } // 移除注释节点,保持原有的 text/html 内容 - text.textHTML = text.textHTML.replace(//, ""); + textHtml = html.replace(//, ""); } catch (e) { console.log("Failed to decode siyuan data from HTML comment:", e); } } + return { + textSiyuan, + textHtml + }; }; export const openByMobile = (uri: string) => { @@ -124,7 +130,9 @@ export const readClipboard = async () => { if (item.types.includes("text/html")) { const blob = await item.getType("text/html"); text.textHTML = await blob.text(); - getSiyuanHTML(text); + const textObj = getTextSiyuanFromTextHTML(text.textHTML); + text.textHTML = textObj.textHtml; + text.siyuanHTML = textObj.textSiyuan; } if (item.types.includes("text/plain")) { const blob = await item.getType("text/plain"); @@ -145,11 +153,15 @@ export const readClipboard = async () => { if (isInAndroid()) { text.textPlain = window.JSAndroid.readClipboard(); text.textHTML = window.JSAndroid.readHTMLClipboard(); - getSiyuanHTML(text); + const textObj = getTextSiyuanFromTextHTML(text.textHTML); + text.textHTML = textObj.textHtml; + text.siyuanHTML = textObj.textSiyuan; } else if (isInHarmony()) { text.textPlain = window.JSHarmony.readClipboard(); text.textHTML = window.JSHarmony.readHTMLClipboard(); - getSiyuanHTML(text); + const textObj = getTextSiyuanFromTextHTML(text.textHTML); + text.textHTML = textObj.textHtml; + text.siyuanHTML = textObj.textSiyuan; } return text; } diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts index 3fe2db1ac..f004ac63b 100644 --- a/app/src/protyle/util/insertHTML.ts +++ b/app/src/protyle/util/insertHTML.ts @@ -463,10 +463,13 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false, foldHeadingId = item.getAttribute("data-node-id"); return true; } - if (foldHeadingId && item.getAttribute("parent-heading") === foldHeadingId) { + if (foldHeadingId && item.getAttribute("parent-heading")) { foldHTML += item.outerHTML; } }); + if (foldHeadingId && foldHTML) { + fetchPost("/api/block/appendHeadingChildren", {id: foldHeadingId, dom: foldHTML}); + } (insertBefore ? Array.from(tempElement.content.children) : Array.from(tempElement.content.children).reverse()).find((item) => { if (item.getAttribute("parent-heading")) { return; diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts index 30a942b1e..2552907aa 100644 --- a/app/src/protyle/util/paste.ts +++ b/app/src/protyle/util/paste.ts @@ -1,7 +1,7 @@ import {Constants} from "../../constants"; import {uploadFiles, uploadLocalFiles} from "../upload"; import {processPasteCode, processRender} from "./processCode"; -import {getLocalFiles, readText} from "./compatibility"; +import {getLocalFiles, getTextSiyuanFromTextHTML, readText} from "./compatibility"; import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "./hasClosest"; import {getEditorRange} from "./selection"; import {blockRender} from "../render/blockRender"; @@ -288,6 +288,12 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven if (textPlain.endsWith(Constants.ZWSP) && !textHTML && !siyuanHTML) { siyuanHTML = textPlain.substr(0, textPlain.length - 1); } + // 复制/剪切折叠标题需获取 siyuanHTML + if (textHTML && textPlain && !siyuanHTML) { + const textObj = getTextSiyuanFromTextHTML(textHTML); + siyuanHTML = textObj.textSiyuan; + textHTML = textObj.textHtml; + } // 剪切复制中首位包含空格或仅有空格 https://github.com/siyuan-note/siyuan/issues/5667 if (!siyuanHTML) { // process word