Skip to content

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

:bug: Bind mind map text undo to the edited node https://github.com/siyuan-note/siyuan/issues/20499

Daniele53cbf8676d0
diff --git a/app/src/protyle/render/listMindmap/focus.test.ts b/app/src/protyle/render/listMindmap/focus.test.ts
index 15a7d8612c..c36b3830e9 100644
--- a/app/src/protyle/render/listMindmap/focus.test.ts
+++ b/app/src/protyle/render/listMindmap/focus.test.ts
@@ -80,6 +80,31 @@ test("undo from a node editor restores that node or its previous sibling", async
assert.equal(selected.pop(), "existing", "undoing node insertion focuses the previous sibling");
});
+test("text history records the edited node even when selection changes before its save", async () => {
+ const nodes = new Map([["root", {id: "root", children: [{id: "child"}]}],
+ ["child", {id: "child", parentId: "root", children: []}]]);
+ const list = {isConnected: true, dataset: {nodeId: "map"}, outerHTML: "before"};
+ const operations: {undo: Record<string, string>, redo: Record<string, string>}[] = [];
+ const context: any = {
+ canEdit: () => true, readListMindmap: () => ({nodes, root: nodes.get("root")}),
+ cleanListMindmapHTML: (html: string) => html, getListMindmapSiblingIDs: () => new Map(),
+ normalizeListMindmapSummaryMetadata: () => {},
+ updateTransaction: (_owner: unknown, _list: unknown, _before: string, undo: Record<string, string>,
+ additional: {context: Record<string, string>}) => operations.push({undo, redo: additional.context}),
+ console, showMessage: assert.fail, window: {siyuan: {languages: {listMindmapInvalid: "save failed"}}},
+ };
+ runInNewContext(compiled, context);
+ const target = Object.assign(new context.Controller(), {owner: {}, list, refresh() {},
+ view: {getSelectedId: () => "root"}});
+ const undo = {undoFocusId: "paragraph", undoFocusStart: "2", undoFocusEnd: "2"};
+ const redo = {...undo, undoFocusStart: "5", undoFocusEnd: "5"};
+ assert.equal(await target.change(() => { list.outerHTML = "after"; }, true, "child", {undo, redo}), true);
+ assert.equal(operations[0].undo.undoFocusId, "child", "undo must reopen the edited child instead of the selected root");
+ assert.equal(operations[0].redo.undoFocusId, "child");
+ assert.deepEqual(JSON.parse(operations[0].undo.undoFocusMindmapSelection), undo);
+ assert.deepEqual(JSON.parse(operations[0].redo.undoFocusMindmapSelection), redo);
+});
+
test("mind map transaction focus survives insertion, deletion and repeated undo/redo", async () => {
type FocusContext = {undoFocusId: string, undoFocusStart: string, undoFocusEnd: string};
type State = {id: string, children: string[]}[];
diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.ts
index 94aa188a05..0d5868ee8b 100644
--- a/app/src/protyle/render/listMindmap/index.ts
+++ b/app/src/protyle/render/listMindmap/index.ts
@@ -586,7 +586,8 @@ class ListMindmapController {
const before = cleanListMindmapHTML(this.list.outerHTML);
try {
const model = readListMindmap(this.list);
- const selectedID = this.view.getSelectedId() || model.root.id;
+ // 文本保存可能晚于画布选择变化,撤销定位始终使用本次编辑的节点。
+ const selectedID = editing && model.nodes.has(focusID) ? focusID : this.view.getSelectedId() || model.root.id;
const selected = model.nodes.get(selectedID);
const siblings = selected?.parentId ? model.nodes.get(selected.parentId)?.children || [] : [];
const index = siblings.findIndex(node => node.id === selectedID);