Skip to content

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

:sparkles: Support inserting and pasting images in mind map nodes https://github.com/siyuan-note/siyuan/issues/20015

Danieldef7010f4417
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-6yi0dv5/20200924101225-k254i8g.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-6yi0dv5/20200924101225-k254i8g.sy
index 4abcff0283..762baa3ffb 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-6yi0dv5/20200924101225-k254i8g.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-6yi0dv5/20200924101225-k254i8g.sy
@@ -6,7 +6,7 @@
"id": "20200924101225-k254i8g",
"title": "Content Block Type",
"type": "doc",
- "updated": "20260928225427"
+ "updated": "20260930234403"
},
"Children": [
{
@@ -2556,6 +2556,29 @@
}
]
},
+ {
+ "ID": "20260930234403-jmgkoqd",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260930234403-jmgkoqd",
+ "updated": "20260930234403"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "While editing a node, type "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "/"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " and choose “Insert image or file” to upload a local image. You can also paste clipboard images. Images are stored as document attachments. The virtual center title used for multiple top-level nodes remains plain text."
+ }
+ ]
+ },
{
"ID": "20260926151827-kkcg79g",
"Type": "NodeParagraph",
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
index 436a5fddd9..b32ccd1da7 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
@@ -7,7 +7,7 @@
"id": "20200924100950-9op5xi1",
"title": "Shortcuts",
"type": "doc",
- "updated": "20260930171029"
+ "updated": "20260930234403"
},
"Children": [
{
@@ -13262,6 +13262,29 @@
}
]
},
+ {
+ "ID": "20260930234403-5eh3gn8",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260930234403-5eh3gn8",
+ "updated": "20260930234403"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "Paste an image with "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Ctrl+V / ⌘V"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " while editing a node, or after selecting a node to append it to the end of its visible content. Wait for an active upload to finish before leaving node editing."
+ }
+ ]
+ },
{
"ID": "20260926152753-8trdt10",
"Type": "NodeParagraph",
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-fqgskfj/20200905090211-2vixtlf.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-fqgskfj/20200905090211-2vixtlf.sy
index 2333c38b5f..eb51acc450 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-fqgskfj/20200905090211-2vixtlf.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-fqgskfj/20200905090211-2vixtlf.sy
@@ -6,7 +6,7 @@
"id": "20200905090211-2vixtlf",
"title": "内容块类型",
"type": "doc",
- "updated": "20260928225427"
+ "updated": "20260930234403"
},
"Children": [
{
@@ -2635,6 +2635,29 @@
}
]
},
+ {
+ "ID": "20260930234403-5g40dth",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260930234403-5g40dth",
+ "updated": "20260930234403"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "编辑节点时,输入 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "/"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 并选择“插入图片或文件”,即可上传本地图片,也可以直接粘贴剪贴板图片。图片作为文档附件保存。多个顶层节点形成的虚拟中心标题仍为纯文本。"
+ }
+ ]
+ },
{
"ID": "20260926151827-8wl1iml",
"Type": "NodeParagraph",
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
index 2de495ccde..d68d540043 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
@@ -7,7 +7,7 @@
"id": "20200813004551-gm0pbn1",
"title": "快捷键",
"type": "doc",
- "updated": "20260930171029"
+ "updated": "20260930234403"
},
"Children": [
{
@@ -13340,6 +13340,29 @@
}
]
},
+ {
+ "ID": "20260930234403-6bw63i6",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260930234403-6bw63i6",
+ "updated": "20260930234403"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "编辑节点时可按 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Ctrl+V / ⌘V"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 粘贴图片;仅选中节点时粘贴,图片追加到当前可见正文末尾。上传进行中需等待完成后再退出节点编辑。"
+ }
+ ]
+ },
{
"ID": "20260926152753-tamny2o",
"Type": "NodeParagraph",
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226115825-mhcslw2/20211226120147-ib6yy3i.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226115825-mhcslw2/20211226120147-ib6yy3i.sy
index ea14beb2d1..1e9af60d12 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226115825-mhcslw2/20211226120147-ib6yy3i.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226115825-mhcslw2/20211226120147-ib6yy3i.sy
@@ -6,7 +6,7 @@
"id": "20211226120147-ib6yy3i",
"title": "內容區塊類型",
"type": "doc",
- "updated": "20260928225427"
+ "updated": "20260930234403"
},
"Children": [
{
@@ -2571,6 +2571,29 @@
}
]
},
+ {
+ "ID": "20260930234403-vfgciw1",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260930234403-vfgciw1",
+ "updated": "20260930234403"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "編輯節點時,輸入 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "/"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 並選擇「插入圖片或檔案」,即可上傳本機圖片,也可以直接貼上剪貼簿圖片。圖片會儲存為文件附件。多個頂層節點形成的虛擬中心標題仍為純文字。"
+ }
+ ]
+ },
{
"ID": "20260926151827-6pycwgr",
"Type": "NodeParagraph",
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
index 9cd9424d5d..99e71b1310 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
@@ -7,7 +7,7 @@
"id": "20211226122549-jktxego",
"title": "快捷鍵",
"type": "doc",
- "updated": "20260930171029"
+ "updated": "20260930234403"
},
"Children": [
{
@@ -13258,6 +13258,29 @@
}
]
},
+ {
+ "ID": "20260930234403-rg4uej3",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260930234403-rg4uej3",
+ "updated": "20260930234403"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "編輯節點時可按 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Ctrl+V / ⌘V"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 貼上圖片;僅選取節點時貼上,圖片會附加到目前可見正文的末尾。上傳進行中需等待完成後再結束節點編輯。"
+ }
+ ]
+ },
{
"ID": "20260926152753-0k1m0f6",
"Type": "NodeParagraph",
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
index 11b3ae98f1..96e5585595 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
@@ -9,7 +9,7 @@
"id": "20240530101000-xsbxokr",
"title": "ショートカット",
"type": "doc",
- "updated": "20260930171029"
+ "updated": "20260930234403"
},
"Children": [
{
@@ -13212,6 +13212,29 @@
}
]
},
+ {
+ "ID": "20260930234403-cyh3qk6",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260930234403-cyh3qk6",
+ "updated": "20260930234403"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "ノードの編集中に "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Ctrl+V / ⌘V"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " で画像を貼り付けられます。ノードを選択した状態で貼り付けると、現在表示されている本文の末尾に画像が追加されます。アップロード中は完了を待ってからノードの編集を終了してください。"
+ }
+ ]
+ },
{
"ID": "20260926152753-5vwcnw1",
"Type": "NodeParagraph",
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-gvtksyj/20240530101000-35bbvcx.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-gvtksyj/20240530101000-35bbvcx.sy
index e8b97bc99b..9d34d2f979 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-gvtksyj/20240530101000-35bbvcx.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-gvtksyj/20240530101000-35bbvcx.sy
@@ -7,7 +7,7 @@
"id": "20240530101000-35bbvcx",
"title": "コンテンツブロックの種類",
"type": "doc",
- "updated": "20260928225427"
+ "updated": "20260930234403"
},
"Children": [
{
@@ -2722,6 +2722,29 @@
}
]
},
+ {
+ "ID": "20260930234403-3zow3ge",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260930234403-3zow3ge",
+ "updated": "20260930234403"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "ノードの編集中に "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "/"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " を入力して「画像やファイルを挿入」を選択すると、ローカル画像をアップロードできます。クリップボードの画像を貼り付けることもできます。画像は文書の添付ファイルとして保存されます。複数の最上位ノードに対して表示される仮想中心のタイトルはプレーンテキストのままです。"
+ }
+ ]
+ },
{
"ID": "20260926151827-vknoaqd",
"Type": "NodeParagraph",
diff --git a/app/src/protyle/render/listMindmap/editor.test.ts b/app/src/protyle/render/listMindmap/editor.test.ts
index 0675022798..deccc600ef 100644
--- a/app/src/protyle/render/listMindmap/editor.test.ts
+++ b/app/src/protyle/render/listMindmap/editor.test.ts
@@ -72,15 +72,17 @@ const browserCases = async (sourceCode: string, css: string, cleanupSource: stri
flush: async (): Promise<void> => undefined,
saveBarrier: undefined as Promise<void> | undefined,
finishNested: async (): Promise<boolean> => true,
+ uploads: [] as File[][],
};
const protyle = {
block: {rootID: ""},
+ upload: {isUploading: false},
undo: {clear: noop},
wysiwyg: {lastHTMLs: {} as Record<string, string>, flushPendingInput: async () => {
state.flushed++;
await state.flush();
}},
- toolbar: {element: toolbarElement, subElement},
+ toolbar: {element: toolbarElement, subElement, range: undefined as Range | undefined},
};
const dependencies = {
genEmptyElement: () => {
@@ -130,6 +132,14 @@ const browserCases = async (sourceCode: string, css: string, cleanupSource: stri
};
},
bindLiteCodeActions: noop,
+ uploadFiles: (target: typeof protyle, files: File[], _element: unknown, _success: unknown,
+ _complete: unknown, options: {source: string}) => {
+ check.equal(target, protyle);
+ check.equal(options.source, "paste");
+ check.equal(target.block.rootID, "document");
+ check.equal(target.toolbar.range, undefined);
+ state.uploads.push(files);
+ },
setMobileToolbarUndo: noop,
getDefaultToolbar: (): unknown[] => [],
hideElements: noop,
@@ -215,6 +225,37 @@ const browserCases = async (sourceCode: string, css: string, cleanupSource: stri
// 直接键入只替换首个段落,保留其余内容块。
const paragraph = (id: string, html: string) =>
`<div data-type="NodeParagraph" data-node-id="${id}" class="p"><div contenteditable="true">${html}</div></div>`;
+ // 粘贴到选中节点时保留原正文,在当前可见正文末尾启动上传;上传结束前保留编辑器。
+ const imageEditor = create(paragraph("body", "Keep text"));
+ const images = [new File(["image"], "image.png", {type: "image/png"})];
+ imageEditor.editor.pasteImages(images);
+ check.equal(imageEditor.state.focusedText, "Keep text");
+ check.deepEqual(imageEditor.state.uploads, [images]);
+ check.equal(imageEditor.wysiwyg.textContent, "Keep text");
+ imageEditor.protyle.upload.isUploading = true;
+ check.equal(await imageEditor.editor.finish(), false);
+ check.equal(imageEditor.state.destroyed, 0);
+ const image = document.createElement("img");
+ image.src = "assets/image.png";
+ imageEditor.wysiwyg.querySelector('[contenteditable="true"]').append(image);
+ imageEditor.protyle.upload.isUploading = false;
+ check.equal(await imageEditor.editor.finish(), true);
+ check.equal(imageEditor.element.querySelector("img").getAttribute("src"), "assets/image.png");
+ imageEditor.editor.pasteImages(images);
+ check.equal(imageEditor.state.uploads.length, 1, "closed editors do not accept another upload");
+ await imageEditor.remove();
+ // 关闭等待内层编辑结束时启动上传,也不能提前销毁外层编辑器。
+ const uploadingEditor = create();
+ const nestedGate = gate();
+ uploadingEditor.state.finishNested = () => nestedGate.promise.then(() => true);
+ const finishBeforeUpload = uploadingEditor.editor.finish();
+ uploadingEditor.protyle.upload.isUploading = true;
+ nestedGate.release();
+ check.equal(await finishBeforeUpload, false);
+ check.equal(uploadingEditor.state.destroyed, 0);
+ uploadingEditor.protyle.upload.isUploading = false;
+ check.equal(await uploadingEditor.editor.finish(), true);
+ await uploadingEditor.remove();
for (const active of ["first", "second"]) {
const tabs = create(`<div class="tabs" data-type="NodeTabs" data-node-id="tabs" tabs-active-id="${active}">
${["first", "second"].map(id => `<div class="tab-item" data-type="NodeTabItem" data-node-id="${id}">
diff --git a/app/src/protyle/render/listMindmap/editor.ts b/app/src/protyle/render/listMindmap/editor.ts
index be6e9de437..f247fe9cc1 100644
--- a/app/src/protyle/render/listMindmap/editor.ts
+++ b/app/src/protyle/render/listMindmap/editor.ts
@@ -11,6 +11,7 @@ import {bindLiteCodeActions} from "../../lite/codeActions";
import {setMobileToolbarUndo} from "../../lite/mobileToolbar";
import {getDefaultToolbar} from "../../toolbar/defaults";
import {hideElements} from "../../ui/hideElements";
+import {uploadFiles} from "../../upload";
import {matchHotKey} from "../../util/hotKey";
import {processRender} from "../../util/processCode";
import {initEditorTabs} from "../../wysiwyg/tabs";
@@ -150,7 +151,7 @@ export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {
initialBlockHTML,
protyleOptions: {notebookId: owner.notebookId, toolbar, hint},
runtimeCapabilities: {
- upload: false, websocket: false, pluginExtensions: false,
+ upload: true, websocket: false, pluginExtensions: false,
listItemFragment: true,
getTransactionOwner: operations => {
const avIDs = new Set(Array.from(host.querySelectorAll<HTMLElement>(
@@ -303,6 +304,10 @@ export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {
if (finished) {
return true;
}
+ if (fragment.protyle.upload?.isUploading) {
+ showMessage(window.siyuan.languages.uploading);
+ return false;
+ }
if (composing) {
finishAfterComposition = true;
return false;
@@ -319,6 +324,10 @@ export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {
return false;
}
} while (comparableContent(fragment.getBlockHTML()) !== lastHTML);
+ if (fragment.protyle.upload?.isUploading) {
+ showMessage(window.siyuan.languages.uploading);
+ return false;
+ }
cleanup();
return true;
};
@@ -468,5 +477,12 @@ export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {
selection.addRange(range);
}
}
- return {finish, destroy};
+ return {finish, destroy, pasteImages: (files: File[]) => {
+ if (finished || closing || !options.canEdit()) {
+ return;
+ }
+ fragment.focus(true);
+ fragment.protyle.toolbar.range = undefined;
+ uploadFiles(fragment.protyle, files, undefined, undefined, undefined, {source: "paste"});
+ }};
};
diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.ts
index 4f07ddbef9..3dde211aed 100644
--- a/app/src/protyle/render/listMindmap/index.ts
+++ b/app/src/protyle/render/listMindmap/index.ts
@@ -123,7 +123,7 @@ class ListMindmapController {
private readonly view: ListMindmapView;
private model: ListMindmapModel;
private snapshot = "";
- private activeEditor?: {finish: () => Promise<boolean>, destroy: () => void};
+ private activeEditor?: ReturnType<typeof openListMindmapEditor>;
private editRequest = 0;
private disposed = false;
private taskChanges: Promise<void> = Promise.resolve();
@@ -285,6 +285,7 @@ class ListMindmapController {
}
},
onEdit: (id, contentHost, replaceFirstParagraph) => this.edit(id, contentHost, replaceFirstParagraph),
+ onPasteImages: (id, files) => void this.edit(id, this.view.getContentHost(id), undefined, files),
isAddSiblingShortcut: event => {
const key = window.siyuan.config.keymap.editor.list?.mindmapAddSibling;
return !!key && matchHotKey(key, event);
@@ -545,7 +546,7 @@ class ListMindmapController {
}
}
- private async edit(id: string, host: HTMLElement, replaceFirstParagraph?: string) {
+ private async edit(id: string, host: HTMLElement, replaceFirstParagraph?: string, images?: File[]) {
const request = ++this.editRequest;
if (!canEdit(this.owner, this.list) || (this.activeEditor && !await this.activeEditor.finish()) ||
request !== this.editRequest || this.disposed || !host?.isConnected) {
@@ -574,6 +575,8 @@ class ListMindmapController {
if (!this.activeEditor) {
this.view.setEditing(undefined);
this.view.update(this.model);
+ } else if (images) {
+ this.activeEditor.pasteImages(images);
}
}
diff --git a/app/src/protyle/render/listMindmap/paste.test.ts b/app/src/protyle/render/listMindmap/paste.test.ts
new file mode 100644
index 0000000000..234fd05487
--- /dev/null
+++ b/app/src/protyle/render/listMindmap/paste.test.ts
@@ -0,0 +1,86 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import test from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
+
+const source = createSourceFile("view.ts", readFileSync("src/protyle/render/listMindmap/view.ts", "utf8"),
+ ScriptTarget.ES2021, true);
+const view = source.statements.find(item => isClassDeclaration(item) && item.name?.text === "ListMindmapView");
+assert.ok(view && isClassDeclaration(view));
+const handler = view.members.find(item => item.name?.getText(source) === "pasteImages");
+assert.ok(handler);
+const compiled = transpileModule(`class View { ${handler.getText(source)} }
+globalThis.View = View;`, {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+
+const create = () => {
+ const context: any = {};
+ runInNewContext(compiled, context);
+ const calls: {id: string, files: unknown[]}[] = [];
+ const images = [{name: "image.png", type: "image/png"}, {name: "photo.jpg", type: "image/jpeg"}];
+ let finish: () => void;
+ const target = Object.assign(new context.View(), {
+ readOnly: false, selectedId: "node",
+ model: {nodes: new Map([["node", {virtual: false}]])},
+ options: {onPasteImages: (id: string, files: unknown[]) => calls.push({id, files})},
+ finishThen: (action: () => void) => { finish = action; },
+ });
+ const event = {
+ defaultPrevented: false,
+ target: {isContentEditable: false, closest: (): object | null => null},
+ clipboardData: {files: [...images, {name: "file.pdf", type: "application/pdf"}]},
+ preventDefault() { this.defaultPrevented = true; },
+ };
+ return {target, event, calls, images, finish: () => finish?.()};
+};
+
+test("selected mind map nodes accept clipboard images after finishing the previous editor", () => {
+ const current = create();
+ current.target.pasteImages(current.event);
+ assert.equal(current.event.defaultPrevented, true);
+ assert.equal(current.calls.length, 0);
+ current.finish();
+ assert.equal(current.calls[0].id, "node");
+ assert.deepEqual(Array.from(current.calls[0].files), current.images);
+});
+
+test("mind map image paste respects edit state, selection and read-only boundaries", () => {
+ for (const mode of ["readonly", "editing", "relation", "summary", "virtual", "missing", "unselected",
+ "input", "editable", "handled", "text", "export"]) {
+ const current = create();
+ switch (mode) {
+ case "readonly": current.target.readOnly = true; break;
+ case "editing": current.target.editingId = "node"; break;
+ case "relation": current.target.relationFrom = "node"; break;
+ case "summary": current.target.summaryFrom = "node"; break;
+ case "virtual": current.target.model.nodes.get("node").virtual = true; break;
+ case "missing": current.target.model.nodes.clear(); break;
+ case "unselected": current.target.selectedId = undefined; break;
+ case "input": current.event.target.closest = () => ({}); break;
+ case "editable": current.event.target.isContentEditable = true; break;
+ case "handled": current.event.defaultPrevented = true; break;
+ case "text": current.event.clipboardData.files = []; break;
+ case "export": current.target.options.onPasteImages = undefined; break;
+ }
+ current.target.pasteImages(current.event);
+ current.finish();
+ assert.equal(current.calls.length, 0, mode);
+ assert.equal(current.event.defaultPrevented, mode === "handled", mode);
+ }
+});
+
+test("delayed image paste never follows a changed selection or deleted node", () => {
+ for (const change of ["selection", "delete", "readonly"]) {
+ const current = create();
+ current.target.pasteImages(current.event);
+ if (change === "selection") {
+ current.target.selectedId = "other";
+ } else if (change === "delete") {
+ current.target.model.nodes.clear();
+ } else {
+ current.target.readOnly = true;
+ }
+ current.finish();
+ assert.equal(current.calls.length, 0, change);
+ }
+});
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 1db8404bbe..50ccf9d18a 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -39,6 +39,7 @@ export interface ListMindmapViewOptions {
onFullscreen?: (enter: boolean, button: HTMLButtonElement) => void;
onInteractionStart?: (event: PointerEvent) => () => void;
onEdit?: (id: string, contentHost: HTMLElement, replaceFirstParagraph?: string) => void;
+ onPasteImages?: (id: string, files: File[]) => void;
isAddSiblingShortcut?: (event: KeyboardEvent) => boolean;
isAddChildShortcut?: (event: KeyboardEvent) => boolean;
onRootTitleChange?: (title: string) => void;
@@ -246,6 +247,7 @@ export class ListMindmapView {
this.listen(this.viewport, "wheel", this.wheel, {passive: false});
this.listen(this.viewport, "dragstart", (event: Event) => event.preventDefault());
this.listen(options.host, "keydown", this.keyDown);
+ this.listen(options.host, "paste", this.pasteImages);
// 脑图内部先处理交互,再阻止外层文档将节点操作识别为列表块编辑。
["pointerdown", "pointerup", "pointermove", "pointercancel", "mousedown", "mouseup", "mousemove", "click",
"dblclick", "contextmenu", "dragstart", "dragover", "drop", "beforeinput", "input", "compositionstart",
@@ -2517,6 +2519,27 @@ export class ListMindmapView {
this.draw();
}
+ private pasteImages = (event: ClipboardEvent) => {
+ if (event.defaultPrevented || this.readOnly || this.editingId || this.relationFrom || this.summaryFrom ||
+ !this.options.onPasteImages || !this.selectedId ||
+ (event.target as HTMLElement).isContentEditable ||
+ (event.target as HTMLElement).closest("input, textarea, select")) {
+ return;
+ }
+ const id = this.selectedId;
+ const node = this.model.nodes.get(id);
+ const files = Array.from(event.clipboardData?.files || []).filter(file => file.type.startsWith("image/"));
+ if (!node || node.virtual || files.length === 0) {
+ return;
+ }
+ event.preventDefault();
+ this.finishThen(() => {
+ if (!this.readOnly && this.selectedId === id && this.model.nodes.has(id)) {
+ this.options.onPasteImages(id, files);
+ }
+ });
+ };
+
private keyDown = (event: KeyboardEvent) => {
if (this.locked) {
if (event.key === "Escape" && this.fullscreenMarker) {