Skip to content

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

:sparkles: Keep explicitly created mind map nodes selected https://github.com/siyuan-note/siyuan/issues/21016

Danielf51bf815cc71
diff --git a/app/src/protyle/render/listMindmap/focus.test.ts b/app/src/protyle/render/listMindmap/focus.test.ts
index 8efe68c10f..b870897271 100644
--- a/app/src/protyle/render/listMindmap/focus.test.ts
+++ b/app/src/protyle/render/listMindmap/focus.test.ts
@@ -61,9 +61,10 @@ test("canvas history follows embedded source documents, local stacks, busy state
assert.equal(listeners.size, 0);
});
-test("new mind map nodes become selected before editing", async () => {
+test("explicit mind map additions stay selected while clipboard additions still open the editor", async () => {
const steps: string[] = [];
const item = {dataset: {nodeId: "new"}, classList: {replace: () => {}}};
+ const clipboard = {textPlain: "Pasted content"};
const context: any = {
canEdit: () => true,
genListItemElement: () => item,
@@ -78,10 +79,28 @@ test("new mind map nodes become selected before editing", async () => {
view: {focusNode: (id: string) => steps.push(`focus:${id}`),
getContentHost: () => ({}),
},
- edit: async (id: string) => { steps.push(`edit:${id}`); },
+ edit: async (id: string, _host: unknown, replace: unknown, data: unknown) => {
+ assert.equal(replace, undefined);
+ assert.equal(data, clipboard);
+ steps.push(`edit:${id}`);
+ },
});
+ for (const type of ["NodeList", "NodeMindmap"]) {
+ target.list.dataset.type = type;
+ for (const kind of ["child", "sibling"]) {
+ steps.length = 0;
+ await target.add("root", kind);
+ assert.deepEqual(steps, ["focus:new"], `${type} ${kind} keeps node selection without opening the editor`);
+ steps.length = 0;
+ await target.add("root", kind, clipboard);
+ assert.deepEqual(steps, ["focus:new", "edit:new"], `${type} ${kind} imports clipboard content through the editor`);
+ }
+ }
+ target.change = async () => false;
+ steps.length = 0;
await target.add("root", "child");
- assert.deepEqual(steps, ["focus:new", "edit:new"]);
+ await target.add("root", "sibling", clipboard);
+ assert.deepEqual(steps, [], "failed additions neither move focus nor open the editor");
});
test("canvas undo and redo preserve the viewport throughout replay and release it on errors", async () => {
diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.ts
index 8d02925fc6..2b379a249e 100644
--- a/app/src/protyle/render/listMindmap/index.ts
+++ b/app/src/protyle/render/listMindmap/index.ts
@@ -881,9 +881,11 @@ class ListMindmapController {
return;
}
this.view.focusNode(item.dataset.nodeId);
- const content = this.view.getContentHost(item.dataset.nodeId);
- if (content) {
- await this.edit(item.dataset.nodeId, content, undefined, clipboard);
+ if (clipboard) {
+ const content = this.view.getContentHost(item.dataset.nodeId);
+ if (content) {
+ await this.edit(item.dataset.nodeId, content, undefined, clipboard);
+ }
}
}
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index ffe7df21c4..b1282376cd 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -388,6 +388,7 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
Constants: {CB_GET_HISTORY: "history", ATTRIBUTE_EDITING: "data-editing",
CUSTOM_SY_LIST_MINDMAP: "custom-sy-list-mindmap", CUSTOM_SY_LIST_MINDMAP_DATA: "custom-sy-list-mindmap-data"},
isProtyleListItemFragment: () => false, isHiddenTabContent: () => false,
+ matchHotKey: (binding: unknown) => { check.equal(binding, undefined); return false; },
isMobile: () => mobile, waitForPendingTransactions: async () => {},
getUndoStateSnapshot: () => ({canUndo: true, canRedo: true, pending: false}),
hasUndoStateMirror: () => true,
@@ -403,7 +404,10 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
showMessage: () => check.fail("Embedded edit failed"), globalClickHideMenu: () => {},
hideAllElements: () => {}, hideElements: () => {}, suspendBlockPopover: () => () => {},
getInlineStylesCache: () => ({}), getVisibleOrderedStyleKeys: (): string[] => [],
- openListMindmapEditor: (options: any) => { editorOptions = options; return {finish: async () => true, destroy() {}}; },
+ openListMindmapEditor: (options: any) => {
+ editorOptions = options;
+ return {finish: async () => { options.onFinish(); return true; }, destroy() {}};
+ },
genListItemElement: () => {
const holder = document.createElement("div");
holder.innerHTML = lute.Md2BlockDOM("- Added\n");
@@ -442,15 +446,44 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
check.equal(operations[0].undoOperations[0].context.undoFocusIndex, "1");
check.match(operations[0].doOperations[0].data as string, /Folded child/);
await target.view.options.onAdd(itemID, "child");
- check.equal(editorOptions.rootID, "source-document");
+ const addedID = target.view.getSelectedId();
+ check.notEqual(addedID, itemID);
+ check.equal(target.activeEditor, undefined, "new nodes remain selected without mounting an editor");
+ check.equal(editorOptions, undefined);
+ check.equal(document.activeElement, target.host, "new nodes retain the canvas keyboard focus");
check.match(copies[1].textContent, /Added/);
+ // Direct typing opens the new node's editor and preserves the embedded source identity.
+ target.host.dispatchEvent(new KeyboardEvent("keydown", {key: "x", bubbles: true, cancelable: true}));
+ await new Promise(resolve => setTimeout(resolve, 0));
+ check.ok(editorOptions, "typing opens the newly selected node editor");
+ check.equal(editorOptions.node.id, addedID);
+ check.equal(editorOptions.replaceFirstParagraph, "x");
+ check.equal(editorOptions.rootID, "source-document");
await target.finish();
- target.activeEditor = undefined;
+ check.equal(target.activeEditor, undefined, "finishing text input releases the node editor");
await target.undo(false);
check.doesNotMatch(copies[0].textContent, /Added/);
await target.undo(true);
check.match(copies[0].textContent, /Added/);
check.deepEqual(historyRoots, ["source-document", "source-document"]);
+ // An explicitly created sibling can be deleted immediately, without Esc.
+ editorOptions = undefined;
+ await target.view.options.onAdd(itemID, "sibling");
+ const siblingID = target.view.getSelectedId();
+ check.equal(target.activeEditor, undefined);
+ target.host.dispatchEvent(new KeyboardEvent("keydown", {key: "Delete", bubbles: true, cancelable: true}));
+ await new Promise(resolve => setTimeout(resolve, 0));
+ check.equal(target.model.nodes.has(siblingID), false);
+ check.equal(editorOptions, undefined, "deleting a newly selected node never opens an editor");
+ check.equal(target.model.nodes.has(addedID), true, "deleting the sibling preserves the previously added child");
+ // Double-click remains available on explicitly created children.
+ await target.view.options.onAdd(itemID, "child");
+ const childID = target.view.getSelectedId();
+ target.view.getContentHost(childID).dispatchEvent(new MouseEvent("dblclick", {bubbles: true, cancelable: true}));
+ await new Promise(resolve => setTimeout(resolve, 0));
+ check.ok(editorOptions, "double-click opens the newly selected child editor");
+ check.equal(editorOptions.node.id, childID);
+ check.equal(editorOptions.replaceFirstParagraph, undefined);
controllers.forEach(controller => controller.destroy());
root.remove();
}