Skip to content

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

:bug: Restore dialog focus without overriding active targets https://github.com/siyuan-note/siyuan/issues/19506

Daniel8d880fe1ea65
diff --git a/app/src/dialog/index.ts b/app/src/dialog/index.ts
index 2348bd9cb2..d699db9926 100644
--- a/app/src/dialog/index.ts
+++ b/app/src/dialog/index.ts
@@ -14,6 +14,9 @@ export class Dialog {
public editors: { [key: string]: Protyle };
public data: any;
private resizeCallback: (type: string) => void;
+ private previousFocus: HTMLElement;
+ private previousRange: Range;
+ private destroying = false;
constructor(options: {
positionId?: string,
@@ -29,6 +32,15 @@ export class Dialog {
resizeCallback?: (type: string) => void,
containerClassName?: string
}) {
+ const activeElement = document.activeElement;
+ if (activeElement instanceof HTMLElement && activeElement !== document.body) {
+ this.previousFocus = activeElement;
+ const selection = window.getSelection();
+ if (activeElement.isContentEditable && selection?.rangeCount &&
+ activeElement.contains(selection.getRangeAt(0).commonAncestorContainer)) {
+ this.previousRange = selection.getRangeAt(0).cloneRange();
+ }
+ }
this.resizeCallback = options.resizeCallback;
this.disableClose = options.disableClose;
this.id = genUUID();
@@ -94,6 +106,10 @@ left:${left || "auto"};top:${top || "auto"}">
}
public destroy(options?: IObject) {
+ if (this.destroying) {
+ return;
+ }
+ this.destroying = true;
this.element.classList.remove("b3-dialog--open");
setTimeout(() => {
// av 修改列头emoji后点击关闭emoji图标
@@ -101,6 +117,8 @@ left:${left || "auto"};top:${top || "auto"}">
// https://github.com/siyuan-note/siyuan/issues/6783
window.siyuan.menus.menu.remove();
}
+ const activeElement = document.activeElement;
+ const restoreFocus = activeElement === document.body || this.element.contains(activeElement);
this.element.remove();
if (this.destroyCallback) {
this.destroyCallback(options);
@@ -111,6 +129,20 @@ left:${left || "auto"};top:${top || "auto"}">
return true;
}
});
+ // 调用方和上层对话框已接管焦点时,不覆盖其焦点;失效或隐藏的触发元素不再恢复。
+ const target = this.previousFocus;
+ const topDialog = window.siyuan.dialogs[window.siyuan.dialogs.length - 1];
+ if (restoreFocus && document.activeElement === document.body && target?.isConnected &&
+ target.getClientRects().length && getComputedStyle(target).visibility === "visible" &&
+ !target.closest("[inert]") && (!topDialog || topDialog.element.contains(target))) {
+ target.focus({preventScroll: true});
+ if (document.activeElement === target && this.previousRange?.startContainer.isConnected &&
+ this.previousRange.endContainer.isConnected && target.contains(this.previousRange.commonAncestorContainer)) {
+ const selection = window.getSelection();
+ selection.removeAllRanges();
+ selection.addRange(this.previousRange);
+ }
+ }
// https://github.com/siyuan-note/siyuan/issues/10475
document.getElementById("drag")?.classList.remove("fn__hidden");
}, Constants.TIMEOUT_DBLCLICK);
diff --git a/app/tests/dialogFocus.test.js b/app/tests/dialogFocus.test.js
new file mode 100644
index 0000000000..caa73f78ae
--- /dev/null
+++ b/app/tests/dialogFocus.test.js
@@ -0,0 +1,149 @@
+const assert = require("node:assert/strict");
+const fs = require("node:fs");
+const os = require("node:os");
+const path = require("node:path");
+
+const runCases = async (source) => {
+ const assert = require("node:assert/strict");
+ const tick = () => new Promise(resolve => setTimeout(resolve, 20));
+ let id = 0;
+ window.siyuan = {dialogs: [], zIndex: 1, menus: {menu: {element: document.createElement("div"), remove() {}}}};
+ const modules = {
+ "../util/genID": {genUUID: () => String(++id)},
+ "../util/zIndex": {isAbove: () => false},
+ "./moveResize": {moveResize() {}},
+ "../util/functions": {isMobile: () => false},
+ "../protyle/util/compatibility": {isNotCtrl: () => true},
+ "../constants": {Constants: {TIMEOUT_OPENDIALOG: 0, TIMEOUT_DBLCLICK: 0}},
+ };
+ const exports = {};
+ new Function("require", "exports", source)(name => {
+ assert.ok(name in modules, name);
+ return modules[name];
+ }, exports);
+ const {Dialog} = exports;
+ const trigger = document.createElement("button");
+ trigger.textContent = "Open";
+ document.body.append(trigger);
+ const open = (options = {}) => {
+ const dialog = new Dialog({content: "<input>", ...options});
+ dialog.element.querySelector("input").focus();
+ return dialog;
+ };
+ trigger.focus();
+ let dialog = open();
+ dialog.destroy();
+ await tick();
+ assert.equal(document.activeElement, trigger);
+
+ const parent = open();
+ const parentInput = document.activeElement;
+ const child = open();
+ child.destroy();
+ await tick();
+ assert.equal(document.activeElement, parentInput);
+ parent.destroy();
+ await tick();
+ assert.equal(document.activeElement, trigger);
+
+ const other = document.createElement("button");
+ document.body.append(other);
+ dialog = open({destroyCallback: () => other.focus()});
+ dialog.destroy();
+ await tick();
+ assert.equal(document.activeElement, other);
+
+ trigger.focus();
+ dialog = open();
+ dialog.destroy();
+ other.focus();
+ await tick();
+ assert.equal(document.activeElement, other);
+
+ trigger.focus();
+ const lower = open();
+ const upper = open();
+ const upperInput = document.activeElement;
+ lower.destroy();
+ await tick();
+ assert.equal(document.activeElement, upperInput);
+ upper.destroy();
+ await tick();
+ assert.equal(document.activeElement, document.body);
+
+ for (const unavailable of ["hidden", "disabled", "removed"]) {
+ trigger.hidden = false;
+ trigger.disabled = false;
+ document.body.append(trigger);
+ trigger.focus();
+ dialog = open();
+ if (unavailable === "removed") {
+ trigger.remove();
+ } else {
+ trigger[unavailable] = true;
+ }
+ dialog.destroy();
+ await tick();
+ assert.equal(document.activeElement, document.body);
+ }
+
+ const editor = document.createElement("div");
+ editor.contentEditable = "true";
+ editor.textContent = "Editor content";
+ document.body.append(editor);
+ editor.focus();
+ const range = document.createRange();
+ range.setStart(editor.firstChild, 3);
+ range.collapse(true);
+ window.getSelection().removeAllRanges();
+ window.getSelection().addRange(range);
+ let callbacks = 0;
+ dialog = open({destroyCallback: () => callbacks++});
+ dialog.destroy();
+ dialog.destroy();
+ await tick();
+ assert.equal(callbacks, 1);
+ assert.equal(document.activeElement, editor);
+ assert.equal(window.getSelection().anchorOffset, 3);
+ assert.equal(window.siyuan.dialogs.length, 0);
+};
+
+if (process.versions.electron && process.type === "browser") {
+ const {app, BrowserWindow} = require("electron");
+ app.setPath("userData", process.argv[2]);
+ app.whenReady().then(async () => {
+ const win = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false}});
+ let code = 0;
+ try {
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ win.webContents.debugger.attach("1.3");
+ await win.webContents.debugger.sendCommand("Emulation.setFocusEmulationEnabled", {enabled: true});
+ const ts = require("typescript");
+ const source = ts.transpileModule(fs.readFileSync(path.join(__dirname, "../src/dialog/index.ts"), "utf8"), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+ }).outputText;
+ await win.webContents.executeJavaScript(`(${runCases.toString()})(${JSON.stringify(source)})`);
+ } catch (error) {
+ console.error(error);
+ code = 1;
+ } finally {
+ win.destroy();
+ app.exit(code);
+ }
+ });
+} else {
+ const {test} = require("node:test");
+ const {execFile} = require("node:child_process");
+ const {promisify} = require("node:util");
+ test("dialog focus restoration respects callbacks, nested dialogs and unavailable targets", async () => {
+ const profile = fs.mkdtempSync(path.join(os.tmpdir(), "siyuan-dialog-focus-"));
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ await promisify(execFile)(require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 30000});
+ } finally {
+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));
+ fs.rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});
+ }
+ });
+}