Skip to content

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

:bug: Improve plugin paste event handling when replacing clipboard data https://github.com/siyuan-note/siyuan/issues/12429

Daniel5a9761f0f97c
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts
index 0977c36fbb..93ed9a1b19 100644
--- a/app/src/protyle/util/paste.ts
+++ b/app/src/protyle/util/paste.ts
@@ -23,6 +23,7 @@ import * as dayjs from "dayjs";
import {updateListOrder} from "../wysiwyg/list";
import {refreshSbAndPersistWidth} from "../../block/util";
import {shouldPreservePastedBlockStructure} from "./pasteSource";
+import {normalizePasteResponse} from "./pasteResponse";
import {applyLuteMarkdownSyntax} from "../render/luteMarkdownSyntax";
export const beforePaste = (protyle: IProtyle, blockElement: HTMLElement) => {
@@ -521,7 +522,7 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
}
}
/// #endif
- const originalTextHTML = textHTML;
+ let originalTextHTML = textHTML;
if (vscodeEditorData) {
try {
const metadata = JSON.parse(vscodeEditorData);
@@ -565,7 +566,7 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
if (protyle && protyle.app && protyle.app.plugins) {
for (let i = 0; i < protyle.app.plugins.length; i++) {
- const response: IClipboardData & { files: FileList } = await new Promise((resolve) => {
+ const response = await new Promise<IClipboardData | undefined>((resolve) => {
const emitResult = protyle.app.plugins[i].eventBus.emit("paste", {
protyle,
resolve,
@@ -579,17 +580,14 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
}
});
- if (response?.textHTML) {
- textHTML = response.textHTML;
- }
- if (response?.textPlain) {
- textPlain = response.textPlain;
- }
- if (response?.siyuanHTML) {
- siyuanHTML = response.siyuanHTML;
- }
- if (response?.files) {
- files = response.files as FileList;
+ if (response) {
+ // 插件返回的是完整的剪贴板载荷,未返回字段需清空,避免原数据影响后续处理
+ const normalizedResponse = normalizePasteResponse(response);
+ textHTML = normalizedResponse.textHTML;
+ textPlain = normalizedResponse.textPlain;
+ siyuanHTML = normalizedResponse.siyuanHTML;
+ files = normalizedResponse.files;
+ originalTextHTML = textHTML;
}
}
}
diff --git a/app/src/protyle/util/pasteResponse.test.ts b/app/src/protyle/util/pasteResponse.test.ts
new file mode 100644
index 0000000000..17d83a4a0a
--- /dev/null
+++ b/app/src/protyle/util/pasteResponse.test.ts
@@ -0,0 +1,29 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {normalizePasteResponse} from "./pasteResponse";
+
+describe("normalizePasteResponse", () => {
+ it("clears clipboard formats omitted from a plugin response", () => {
+ assert.deepEqual(normalizePasteResponse({textPlain: "updated"}), {
+ textHTML: "",
+ textPlain: "updated",
+ siyuanHTML: "",
+ files: [],
+ });
+ });
+
+ it("preserves all clipboard formats returned by a plugin", () => {
+ const files = [] as File[];
+ assert.deepEqual(normalizePasteResponse({
+ textHTML: "<strong>updated</strong>",
+ textPlain: "updated",
+ siyuanHTML: "<div data-type=\"NodeParagraph\">updated</div>",
+ files,
+ }), {
+ textHTML: "<strong>updated</strong>",
+ textPlain: "updated",
+ siyuanHTML: "<div data-type=\"NodeParagraph\">updated</div>",
+ files,
+ });
+ });
+});
diff --git a/app/src/protyle/util/pasteResponse.ts b/app/src/protyle/util/pasteResponse.ts
new file mode 100644
index 0000000000..24a471f835
--- /dev/null
+++ b/app/src/protyle/util/pasteResponse.ts
@@ -0,0 +1,6 @@
+export const normalizePasteResponse = (response: IClipboardData) => ({
+ textHTML: response.textHTML ?? "",
+ textPlain: response.textPlain ?? "",
+ siyuanHTML: response.siyuanHTML ?? "",
+ files: response.files ?? [],
+});
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index b8df626728..609bada94b 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -380,7 +380,7 @@ interface IClipboardData {
textHTML?: string,
textPlain?: string,
siyuanHTML?: string,
- files?: File[],
+ files?: FileList | DataTransferItemList | File[],
localFiles?: ILocalFiles[],
}