Skip to content

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

:bug: Restore mind map focus after exiting node editing with Escape https://github.com/siyuan-note/siyuan/issues/18831

Vanessa46b5aa3c9629
diff --git a/app/src/protyle/render/listMindmap/editor.test.ts b/app/src/protyle/render/listMindmap/editor.test.ts
index e7ead47a24..fc560a5c21 100644
--- a/app/src/protyle/render/listMindmap/editor.test.ts
+++ b/app/src/protyle/render/listMindmap/editor.test.ts
@@ -249,6 +249,22 @@ const browserCases = async (sourceCode: string, css: string) => {
check.equal(current.state.destroyed, 1);
await current.remove();
+ // Esc 成功保存后恢复脑图焦点;保存失败时仍留在节点编辑器。
+ for (const accept of [false, true]) {
+ current = create();
+ current.container.className = "list-mindmap";
+ current.container.tabIndex = -1;
+ current.host.tabIndex = -1;
+ current.host.focus();
+ current.state.accept = accept;
+ await current.type("Escape content");
+ current.host.dispatchEvent(new KeyboardEvent("keydown", {key: "Escape", bubbles: true}));
+ await until(() => accept ? current.state.finished === 1 : current.state.saves.length === 1);
+ await settle();
+ check.equal(document.activeElement, accept ? current.container : current.host);
+ await current.remove();
+ }
+
// 使用完整样式和外层文档结构,覆盖嵌套编辑器的最小高度及行高继承。
const style = document.createElement("style");
style.textContent = css;
diff --git a/app/src/protyle/render/listMindmap/editor.ts b/app/src/protyle/render/listMindmap/editor.ts
index f55d229415..71cd068261 100644
--- a/app/src/protyle/render/listMindmap/editor.ts
+++ b/app/src/protyle/render/listMindmap/editor.ts
@@ -251,7 +251,14 @@ export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {
fragment.protyle.toolbar.subElement.classList.contains("fn__none")) {
event.preventDefault();
event.stopImmediatePropagation();
- finish();
+ const mindmap = host.closest<HTMLElement>(".list-mindmap");
+ void finish().then(finished => {
+ // 退出节点编辑后将键盘焦点交回脑图,保留已选节点的快捷键操作。
+ if (finished && mindmap?.isConnected &&
+ (document.activeElement === document.body || mindmap.contains(document.activeElement))) {
+ mindmap.focus({preventScroll: true});
+ }
+ });
}
}, {capture: true, signal});
host.addEventListener("compositionstart", () => {