From d1f95b7df5420f0bcdb7f101cbb9e8cdc6ba97d7 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Sun, 31 Aug 2025 01:07:13 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/14269 --- app/src/protyle/util/compatibility.ts | 31 ++++++++++++++++- app/src/protyle/util/paste.ts | 48 ++++++--------------------- app/src/types/index.d.ts | 1 + 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts index d2041419a..bf274bb30 100644 --- a/app/src/protyle/util/compatibility.ts +++ b/app/src/protyle/util/compatibility.ts @@ -1,6 +1,9 @@ import {focusByRange} from "./selection"; -import {fetchPost} from "../../util/fetch"; +import {fetchPost, fetchSyncPost} from "../../util/fetch"; import {Constants} from "../../constants"; +/// #if !BROWSER +import {clipboard} from "electron"; +/// #endif export const encodeBase64 = (text: string): string => { if (typeof Buffer !== "undefined") { @@ -92,6 +95,27 @@ export const readText = () => { return navigator.clipboard.readText(); }; +/// #if !BROWSER +export const getLocalFiles = async () => { + // 不再支持 PC 浏览器 https://github.com/siyuan-note/siyuan/issues/7206 + let localFiles: string[] = []; + if ("darwin" === window.siyuan.config.system.os) { + const xmlString = clipboard.read("NSFilenamesPboardType"); + const domParser = new DOMParser(); + const xmlDom = domParser.parseFromString(xmlString, "application/xml"); + Array.from(xmlDom.getElementsByTagName("string")).forEach(item => { + localFiles.push(item.childNodes[0].nodeValue); + }); + } else { + const xmlString = await fetchSyncPost("/api/clipboard/readFilePaths", {}); + if (xmlString.data.length > 0) { + localFiles = xmlString.data; + } + } + return localFiles; +}; +/// #endif + export const readClipboard = async () => { const text: IClipboardData = {textPlain: "", textHTML: "", siyuanHTML: ""}; try { @@ -111,6 +135,11 @@ export const readClipboard = async () => { text.files = [new File([blob], "image.png", {type: "image/png", lastModified: Date.now()})]; } } + /// #if !BROWSER + if (!text.textHTML && !text.files) { + text.localFiles = await getLocalFiles(); + } + /// #endif return text; } catch (e) { if (isInAndroid()) { diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts index 26fc192b5..0a48075e5 100644 --- a/app/src/protyle/util/paste.ts +++ b/app/src/protyle/util/paste.ts @@ -1,15 +1,12 @@ import {Constants} from "../../constants"; import {uploadFiles, uploadLocalFiles} from "../upload"; import {processPasteCode, processRender} from "./processCode"; -import {readText} from "./compatibility"; -/// #if !BROWSER -import {clipboard} from "electron"; -/// #endif +import {getLocalFiles, readText} from "./compatibility"; import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "./hasClosest"; import {getEditorRange} from "./selection"; import {blockRender} from "../render/blockRender"; import {highlightRender} from "../render/highlightRender"; -import {fetchPost, fetchSyncPost} from "../../util/fetch"; +import {fetchPost} from "../../util/fetch"; import {isDynamicRef, isFileAnnotation} from "../../util/functions"; import {insertHTML} from "./insertHTML"; import {scrollCenter} from "../../util/highlightById"; @@ -145,19 +142,7 @@ export const pasteEscaped = async (protyle: IProtyle, nodeElement: Element) => { export const pasteAsPlainText = async (protyle: IProtyle) => { let localFiles: string[] = []; /// #if !BROWSER - if ("darwin" === window.siyuan.config.system.os) { - const xmlString = clipboard.read("NSFilenamesPboardType"); - const domParser = new DOMParser(); - const xmlDom = domParser.parseFromString(xmlString, "application/xml"); - Array.from(xmlDom.getElementsByTagName("string")).forEach(item => { - localFiles.push(item.childNodes[0].nodeValue); - }); - } else { - const xmlString = await fetchSyncPost("/api/clipboard/readFilePaths", {}); - if (xmlString.data.length > 0) { - localFiles = xmlString.data; - } - } + localFiles = await getLocalFiles(); if (localFiles.length > 0) { uploadLocalFiles(localFiles, protyle, false); return; @@ -269,6 +254,10 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven files = event.dataTransfer.items; } } else { + if (event.localFiles.length > 0) { + readLocalFile(protyle, event.localFiles); + return; + } textHTML = event.textHTML; textPlain = event.textPlain; siyuanHTML = event.siyuanHTML; @@ -279,26 +268,11 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven textPlain = textPlain.replace(/\r\n|\r|\u2028|\u2029/g, "\n"); /// #if !BROWSER - // 不再支持 PC 浏览器 https://github.com/siyuan-note/siyuan/issues/7206 if (!siyuanHTML && !textHTML && !textPlain && ("clipboardData" in event)) { - if ("darwin" === window.siyuan.config.system.os) { - const xmlString = clipboard.read("NSFilenamesPboardType"); - const domParser = new DOMParser(); - const xmlDom = domParser.parseFromString(xmlString, "application/xml"); - const localFiles: string[] = []; - Array.from(xmlDom.getElementsByTagName("string")).forEach(item => { - localFiles.push(item.childNodes[0].nodeValue); - }); - if (localFiles.length > 0) { - readLocalFile(protyle, localFiles); - return; - } - } else { - const xmlString = await fetchSyncPost("/api/clipboard/readFilePaths", {}); - if (xmlString.data.length > 0) { - readLocalFile(protyle, xmlString.data); - return; - } + const localFiles: string[] = await getLocalFiles(); + if (localFiles.length > 0) { + readLocalFile(protyle, localFiles); + return; } } /// #endif diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index d4eb13657..1020058d5 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -274,6 +274,7 @@ interface IClipboardData { textPlain?: string, siyuanHTML?: string, files?: File[], + localFiles?: string[] } interface IRefDefs {