Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: https://github.com/siyuan-note/siyuan/issues/14269

Vanessad1f95b7df542
diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts
index d2041419a1..bf274bb30f 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 26fc192b54..0a48075e59 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 d4eb13657a..1020058d52 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 {