Vanessa 2025-09-06 18:31:38 +08:00
parent fc1cbf46aa
commit 4e4398ef47
3 changed files with 31 additions and 10 deletions

View file

@ -23,24 +23,30 @@ export const encodeBase64 = (text: string): string => {
} }
}; };
const getSiyuanHTML = (text: IClipboardData) => { export const getTextSiyuanFromTextHTML = (html: string) => {
const siyuanMatch = text.textHTML.match(/<!--data-siyuan='([^']+)'-->/); const siyuanMatch = html.match(/<!--data-siyuan='([^']+)'-->/);
let textSiyuan = "";
let textHtml = html;
if (siyuanMatch) { if (siyuanMatch) {
try { try {
if (typeof Buffer !== "undefined") { if (typeof Buffer !== "undefined") {
const decodedBytes = Buffer.from(siyuanMatch[1], "base64"); const decodedBytes = Buffer.from(siyuanMatch[1], "base64");
text.siyuanHTML = decodedBytes.toString("utf8"); textSiyuan = decodedBytes.toString("utf8");
} else { } else {
const decoder = new TextDecoder(); const decoder = new TextDecoder();
const bytes = Uint8Array.from(atob(siyuanMatch[1]), char => char.charCodeAt(0)); const bytes = Uint8Array.from(atob(siyuanMatch[1]), char => char.charCodeAt(0));
text.siyuanHTML = decoder.decode(bytes); textSiyuan = decoder.decode(bytes);
} }
// 移除注释节点,保持原有的 text/html 内容 // 移除注释节点,保持原有的 text/html 内容
text.textHTML = text.textHTML.replace(/<!--data-siyuan='[^']+'-->/, ""); textHtml = html.replace(/<!--data-siyuan='[^']+'-->/, "");
} catch (e) { } catch (e) {
console.log("Failed to decode siyuan data from HTML comment:", e); console.log("Failed to decode siyuan data from HTML comment:", e);
} }
} }
return {
textSiyuan,
textHtml
};
}; };
export const openByMobile = (uri: string) => { export const openByMobile = (uri: string) => {
@ -124,7 +130,9 @@ export const readClipboard = async () => {
if (item.types.includes("text/html")) { if (item.types.includes("text/html")) {
const blob = await item.getType("text/html"); const blob = await item.getType("text/html");
text.textHTML = await blob.text(); 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")) { if (item.types.includes("text/plain")) {
const blob = await item.getType("text/plain"); const blob = await item.getType("text/plain");
@ -145,11 +153,15 @@ export const readClipboard = async () => {
if (isInAndroid()) { if (isInAndroid()) {
text.textPlain = window.JSAndroid.readClipboard(); text.textPlain = window.JSAndroid.readClipboard();
text.textHTML = window.JSAndroid.readHTMLClipboard(); text.textHTML = window.JSAndroid.readHTMLClipboard();
getSiyuanHTML(text); const textObj = getTextSiyuanFromTextHTML(text.textHTML);
text.textHTML = textObj.textHtml;
text.siyuanHTML = textObj.textSiyuan;
} else if (isInHarmony()) { } else if (isInHarmony()) {
text.textPlain = window.JSHarmony.readClipboard(); text.textPlain = window.JSHarmony.readClipboard();
text.textHTML = window.JSHarmony.readHTMLClipboard(); text.textHTML = window.JSHarmony.readHTMLClipboard();
getSiyuanHTML(text); const textObj = getTextSiyuanFromTextHTML(text.textHTML);
text.textHTML = textObj.textHtml;
text.siyuanHTML = textObj.textSiyuan;
} }
return text; return text;
} }

View file

@ -463,10 +463,13 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
foldHeadingId = item.getAttribute("data-node-id"); foldHeadingId = item.getAttribute("data-node-id");
return true; return true;
} }
if (foldHeadingId && item.getAttribute("parent-heading") === foldHeadingId) { if (foldHeadingId && item.getAttribute("parent-heading")) {
foldHTML += item.outerHTML; 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) => { (insertBefore ? Array.from(tempElement.content.children) : Array.from(tempElement.content.children).reverse()).find((item) => {
if (item.getAttribute("parent-heading")) { if (item.getAttribute("parent-heading")) {
return; return;

View file

@ -1,7 +1,7 @@
import {Constants} from "../../constants"; import {Constants} from "../../constants";
import {uploadFiles, uploadLocalFiles} from "../upload"; import {uploadFiles, uploadLocalFiles} from "../upload";
import {processPasteCode, processRender} from "./processCode"; import {processPasteCode, processRender} from "./processCode";
import {getLocalFiles, readText} from "./compatibility"; import {getLocalFiles, getTextSiyuanFromTextHTML, readText} from "./compatibility";
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "./hasClosest"; import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "./hasClosest";
import {getEditorRange} from "./selection"; import {getEditorRange} from "./selection";
import {blockRender} from "../render/blockRender"; 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) { if (textPlain.endsWith(Constants.ZWSP) && !textHTML && !siyuanHTML) {
siyuanHTML = textPlain.substr(0, textPlain.length - 1); 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 // 剪切复制中首位包含空格或仅有空格 https://github.com/siyuan-note/siyuan/issues/5667
if (!siyuanHTML) { if (!siyuanHTML) {
// process word // process word