An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Restore focus after mind map undo and node deletion https://github.com/siyuan-note/siyuan/issues/19835
Daniel45d484e02310
diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.tsindex c9cbb4fbac..4836a8b907 100644--- a/app/src/protyle/render/listMindmap/index.ts+++ b/app/src/protyle/render/listMindmap/index.ts@@ -38,7 +38,8 @@ import {focusListMindmap} from "./create";import {getListMindmapFoldStates} from "./fold";import {isMobile} from "../../../util/functions";-const roots = new WeakMap<IProtyle, {refresh: () => void, mountNew: (list: HTMLElement) => void, destroy: () => void}>();+const roots = new WeakMap<IProtyle, {refresh: () => void, mountNew: (list: HTMLElement) => void,+ restoreFocus: (listID: string, selectedID: string | undefined, oldHost: HTMLElement) => void, destroy: () => void}>();export const mountNewListMindmap = (owner: IProtyle, list: HTMLElement) => roots.get(owner)?.mountNew(list);@@ -472,14 +473,23 @@ class ListMindmapController {}private async undo(redo: boolean) {+ const selectedID = this.view.getSelectedId();+ const hadFocus = this.host.contains(document.activeElement);if (!canEdit(this.owner, this.list) || (this.activeEditor && !await this.activeEditor.finish())) {return;}if (redo) {- this.owner.undo.redo(this.owner);+ await this.owner.undo.redo(this.owner);} else {- this.owner.undo.undo(this.owner);+ await this.owner.undo.undo(this.owner);}+ if (hadFocus) {+ roots.get(this.owner)?.restoreFocus(this.list.dataset.nodeId, selectedID, this.host);+ }+ }++ public focusNode(id?: string) {+ this.view.focusNode(id && this.model.nodes.has(id) ? id : this.model.root.id);}public refresh() {@@ -544,6 +554,7 @@ export const initListMindmaps = (owner: IProtyle) => {const instances = new Map<HTMLElement, ListMindmapController>();const loading = new WeakSet<HTMLElement>();const newlyCreated = new WeakSet<HTMLElement>();+ let pendingFocus: {listID: string, selectedID?: string, oldHost: HTMLElement};let frame = 0;let disposed = false;const refresh = () => {@@ -575,7 +586,15 @@ export const initListMindmaps = (owner: IProtyle) => {return;}try {- instances.set(list, new ListMindmapController(owner, list));+ const instance = new ListMindmapController(owner, list);+ instances.set(list, instance);+ if (pendingFocus?.listID === list.dataset.nodeId) {+ if (document.activeElement === document.body || pendingFocus.oldHost.contains(document.activeElement) ||+ list.contains(document.activeElement)) {+ instance.focusNode(pendingFocus.selectedID);+ }+ pendingFocus = undefined;+ }} catch (error) {console.error(error);list.querySelector(":scope > .mindmap-view")?.remove();@@ -627,6 +646,19 @@ export const initListMindmaps = (owner: IProtyle) => {newlyCreated.add(list);refresh();}+ }, restoreFocus: (listID, selectedID, oldHost) => {+ const list = getListMindmapElements(root).find(item => item.dataset.nodeId === listID);+ if (!list || document.activeElement !== document.body && !oldHost.contains(document.activeElement) &&+ !list.contains(document.activeElement)) {+ return;+ }+ pendingFocus = {listID, selectedID, oldHost};+ refresh();+ const instance = instances.get(list);+ if (instance) {+ instance.focusNode(selectedID);+ pendingFocus = undefined;+ }}, destroy: () => {disposed = true;unregister();diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.tsindex 0b4df69431..e89de89329 100644--- a/app/src/protyle/render/listMindmap/model.test.ts+++ b/app/src/protyle/render/listMindmap/model.test.ts@@ -790,7 +790,9 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,finishEdit: () => finishAllowed,onMove: (...args: unknown[]) => moves.push(args),onAdd: (...args: unknown[]) => additions.push(args),- onDelete: (id: string) => deletions.push(id),+ onDelete: (id: string) => {+ deletions.push(id);+ },onFold: (id: string) => folds.push(id),onUndo: () => undo++,onRedo: () => redo++,@@ -2190,6 +2192,8 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,check.equal(deleteList.querySelector(`[data-node-id="${deleteFirst}"]`), null);check.equal(nodeElement(deleteFirst), null);check.ok(deleteList.querySelector(`[data-node-id="${deleteSecond}"]`));+ check.equal(nodeElement(deleteSecond).getAttribute("aria-selected"), "true");+ check.equal(document.activeElement, host);await clickMouse(nodeElement(deleteSecond));const beforeProtectedDelete = deleteList.outerHTML;deleting.setEditing(deleteSecond);@@ -2207,6 +2211,8 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,check.equal(deleteList.querySelector(`[data-node-id="${deleteSecond}"]`), null);check.equal(nodeElement(deleteSecond), null);check.ok(nodeElement(deleteModel.root.id));+ check.equal(nodeElement(deleteModel.root.id).getAttribute("aria-selected"), "true");+ check.equal(document.activeElement, host);await clickMouse(nodeElement(deleteModel.root.id));await nativeKey("Backspace");check.ok(nodeElement(deleteModel.root.id), "the last root is preserved for the macOS Delete key");diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.tsindex 881cc4bb22..9b734f87ce 100644--- a/app/src/protyle/render/listMindmap/view.ts+++ b/app/src/protyle/render/listMindmap/view.ts@@ -41,7 +41,7 @@ export interface ListMindmapViewOptions {finishEdit?: () => boolean | void | Promise<boolean | void>;onMove?: (id: string, targetId: string, placement: "before" | "child" | "after") => void;onAdd?: (id: string, kind: "child" | "sibling") => void;- onDelete?: (id: string) => void;+ onDelete?: (id: string) => boolean | void | Promise<boolean | void>;onFold?: (id: string) => void;onFoldLevel?: (level: ListMindmapFoldTarget) => Promise<boolean>;onExpandLevelMenu?: (anchor: HTMLElement, select: (level: ListMindmapFoldTarget) => void) => void;@@ -636,6 +636,18 @@ export class ListMindmapView {return this.nodeElements.get(id)?.querySelector<HTMLElement>(".mindmap-view__content");}+ public getSelectedId() {+ return this.selectedId;+ }++ public focusNode(id: string) {+ if (this.destroyed || !this.model.nodes.has(id)) {+ return;+ }+ this.selectNode(id);+ this.options.host.focus({preventScroll: true});+ }+public setEditing(id?: string) {if (this.editingId) {this.nodeElements.get(this.editingId)?.classList.remove("mindmap-view__node--editing");@@ -1375,7 +1387,17 @@ export class ListMindmapView {} else {const node = this.model.nodes.get(this.selectedId);if (node && !node.virtual && node !== this.model.root) {- this.options.onDelete?.(node.id);+ const siblings = this.model.nodes.get(node.parentId)?.children || [];+ const index = siblings.findIndex(item => item.id === node.id);+ const nextId = siblings[index + 1]?.id || siblings[index - 1]?.id || node.parentId;+ if (this.options.onDelete) {+ void Promise.resolve(this.options.onDelete(node.id)).then(deleted => {+ if (deleted !== false && !this.model.nodes.has(node.id) && nextId &&+ (!this.selectedId || this.selectedId === node.id)) {+ this.focusNode(nextId);+ }+ }).catch(error => console.error(error));+ }}}}diff --git a/app/src/protyle/undo/index.ts b/app/src/protyle/undo/index.tsindex af68d0bfc9..2b22740992 100644--- a/app/src/protyle/undo/index.ts+++ b/app/src/protyle/undo/index.ts@@ -10,9 +10,9 @@ import {scrollCenter} from "../../util/highlightById";// 撤销/重做统一契约:kernel 模式由 Undo 实现(转发 kernel),lite 模式由 LocalUndo 实现(前端操作日志)。export interface IUndo {- undo(protyle: IProtyle): void;+ undo(protyle: IProtyle): void | Promise<void>;- redo(protyle: IProtyle): void;+ redo(protyle: IProtyle): void | Promise<void>;add(doOperations: IOperation[], undoOperations: IOperation[], protyle: IProtyle): void;@@ -48,7 +48,7 @@ export class Undo implements IUndo {this.lastHistoryRootID = rootID;protyle.wysiwyg.flushPendingInput();// 转发到全局 Manager,由 kernel 弹栈 + 广播,发起窗口本地乐观应用- requestUndo(protyle, rootID);+ return requestUndo(protyle, rootID);}public redo(protyle: IProtyle) {@@ -58,7 +58,7 @@ export class Undo implements IUndo {const rootID = getUndoRootID(protyle, undefined, this.lastHistoryRootID);this.lastHistoryRootID = rootID;protyle.wysiwyg.flushPendingInput();- requestRedo(protyle, rootID);+ return requestRedo(protyle, rootID);}// renderLocal 仅在发起窗口本地应用操作(isUndo=true),不 POST 到 kernel