Skip to content

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

:bug: Allow flashcards to reopen after dialog cleanup failures

Daniel963ba71031b6
diff --git a/app/src/card/openCard.ts b/app/src/card/openCard.ts
index 4d3db70c3b..dd85a240a1 100644
--- a/app/src/card/openCard.ts
+++ b/app/src/card/openCard.ts
@@ -837,14 +837,16 @@ export const openCardByData = async (app: App, cardsData: ICardData, cardType: T
width: isMobile() ? "100vw" : "80vw",
height: isMobile() ? "100dvh" : "70vh",
destroyCallback() {
- if (editor) {
- editor.destroy();
- if (window.siyuan.mobile) {
+ try {
+ editor?.destroy();
+ } finally {
+ // 仅释放当前复习窗口的编辑器,保留销毁期间打开的其他编辑器。
+ if (editor && window.siyuan.mobile?.popEditor === editor) {
window.siyuan.mobile.popEditor = null;
}
- }
- if (lastRange) {
- focusByRange(lastRange);
+ if (lastRange) {
+ focusByRange(lastRange);
+ }
}
},
resizeCallback(type: string) {
diff --git a/app/src/dialog/index.ts b/app/src/dialog/index.ts
index 41c128d039..4de47aab77 100644
--- a/app/src/dialog/index.ts
+++ b/app/src/dialog/index.ts
@@ -148,31 +148,33 @@ left:${left || "auto"};top:${top || "auto"}">
const activeElement = document.activeElement;
const restoreFocus = activeElement === document.body || this.element.contains(activeElement);
this.element.remove();
- if (this.destroyCallback) {
- this.destroyCallback(options);
- }
- window.siyuan.dialogs.find((item, index) => {
- if (item.id === this.id) {
- window.siyuan.dialogs.splice(index, 1);
- 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);
+ try {
+ this.destroyCallback?.(options);
+ } finally {
+ // 关闭回调失败时也注销对话框,避免残留状态阻止再次打开。
+ window.siyuan.dialogs.find((item, index) => {
+ if (item.id === this.id) {
+ window.siyuan.dialogs.splice(index, 1);
+ 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");
}
- // 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
index 6fad3092e2..d71f140058 100644
--- a/app/tests/dialogFocus.test.js
+++ b/app/tests/dialogFocus.test.js
@@ -107,6 +107,48 @@ const runCases = async (source) => {
assert.equal(window.getSelection().anchorOffset, 3);
assert.equal(window.siyuan.dialogs.length, 0);
+ const errors = [];
+ document.body.append(trigger);
+ const onError = event => {
+ errors.push(event.error);
+ event.preventDefault();
+ };
+ window.addEventListener("error", onError);
+ const drag = document.createElement("div");
+ drag.id = "drag";
+ document.body.append(drag);
+ try {
+ for (const replacement of [false, true]) {
+ trigger.focus();
+ drag.classList.add("fn__hidden");
+ const error = new Error("Dialog cleanup failed");
+ let next;
+ let calls = 0;
+ dialog = open({destroyCallback: () => {
+ calls++;
+ if (replacement) {
+ next = open();
+ }
+ throw error;
+ }});
+ dialog.destroy();
+ dialog.destroy();
+ await tick();
+ assert.equal(errors.pop(), error);
+ assert.equal(calls, 1);
+ assert.equal(dialog.element.isConnected, false);
+ assert.equal(window.siyuan.dialogs.includes(dialog), false);
+ assert.equal(drag.classList.contains("fn__hidden"), false);
+ assert.equal(document.activeElement, replacement ? next.element.querySelector("input") : trigger);
+ assert.equal(window.siyuan.dialogs.length, replacement ? 1 : 0);
+ next?.destroy();
+ await tick();
+ }
+ } finally {
+ window.removeEventListener("error", onError);
+ drag.remove();
+ }
+
dialog = new Dialog({content: '<button id="first">First</button><input disabled><button hidden>Hidden</button><button id="last">Last</button>'});
const container = dialog.element.querySelector(".b3-dialog__container");
assert.equal(document.activeElement, container);
@@ -139,6 +181,102 @@ const runCases = async (source) => {
await tick();
};
+const runFlashcardCases = async (dialogSource, cardSource, mobile) => {
+ const assert = require("node:assert/strict");
+ const tick = () => new Promise(resolve => setTimeout(resolve, 20));
+ let id = 0;
+ const constants = {TIMEOUT_OPENDIALOG: 0, TIMEOUT_DBLCLICK: 0, DIALOG_OPENCARD: "card", LOCAL_DIALOGPOSITION: "positions"};
+ window.siyuan = {
+ dialogs: [], zIndex: 1, config: {}, storage: {positions: {}},
+ mobile: mobile ? {popEditor: null} : undefined,
+ menus: {menu: {element: document.createElement("div"), remove() {}}},
+ };
+ const modules = {
+ "../util/genID": {genUUID: () => String(++id)},
+ "../util/zIndex": {isAbove: () => false},
+ "./moveResize": {moveResize() {}},
+ "../util/functions": {isMobile: () => mobile},
+ "../constants": {Constants: constants},
+ "../protyle/util/selection": {focusByRange: range => {
+ window.getSelection().removeAllRanges();
+ window.getSelection().addRange(range);
+ }},
+ "./util": {updateCardHV() {}},
+ };
+ const load = source => {
+ const exports = {};
+ new Function("require", "exports", source)(name => modules[name] || {}, exports);
+ return exports;
+ };
+ modules["../dialog"] = load(dialogSource);
+ const cards = load(cardSource);
+ const app = {plugins: []};
+ const data = {cards: []};
+ const errors = [];
+ const error = new Error("Flashcard editor cleanup failed");
+ let destroyError = error;
+ let destroyCount = 0;
+ let replacement;
+ cards.genCardHTML = () => '<div class="block__icons"><button class="block__icon">Review</button></div>';
+ cards.bindCardEvent = async ({element}) => {
+ element.setAttribute("data-key", constants.DIALOG_OPENCARD);
+ const editor = {resize() {}, destroy() {
+ destroyCount++;
+ if (replacement) {
+ window.siyuan.mobile.popEditor = replacement;
+ }
+ if (destroyError) {
+ throw destroyError;
+ }
+ }};
+ if (mobile) {
+ window.siyuan.mobile.popEditor = editor;
+ }
+ return editor;
+ };
+ const onError = event => {
+ errors.push(event.error);
+ event.preventDefault();
+ };
+ window.addEventListener("error", onError);
+ try {
+ await cards.openCardByData(app, data, "all");
+ const first = window.siyuan.dialogs[0];
+ first.destroy();
+ first.destroy();
+ await tick();
+ assert.equal(errors.pop(), error);
+ assert.equal(destroyCount, 1);
+ assert.equal(first.element.isConnected, false);
+ assert.equal(window.siyuan.dialogs.length, 0);
+ if (mobile) {
+ assert.equal(window.siyuan.mobile.popEditor, null);
+ }
+
+ destroyError = undefined;
+ await cards.openCardByData(app, data, "all");
+ const second = window.siyuan.dialogs[0];
+ assert.notEqual(second, first);
+ assert.equal(second.element.isConnected, true);
+ await cards.openCardByData(app, data, "all", undefined, undefined, true);
+ assert.equal(window.siyuan.dialogs[0], second);
+ assert.equal(destroyCount, 1);
+ if (mobile) {
+ replacement = {};
+ }
+ await cards.openCardByData(app, data, "all");
+ await tick();
+ assert.equal(destroyCount, 2);
+ assert.equal(window.siyuan.dialogs.length, 0);
+ if (mobile) {
+ assert.equal(window.siyuan.mobile.popEditor, replacement);
+ }
+ assert.equal(errors.length, 0);
+ } finally {
+ window.removeEventListener("error", onError);
+ }
+};
+
if (process.versions.electron && process.type === "browser") {
const {app, BrowserWindow, ipcMain} = require("electron");
app.setPath("userData", process.argv[2]);
@@ -161,6 +299,15 @@ if (process.versions.electron && process.type === "browser") {
compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
}).outputText;
await win.webContents.executeJavaScript(`(${runCases.toString()})(${JSON.stringify(source)})`);
+ const {parse} = require("ifdef-loader/preprocessor");
+ const cardSource = fs.readFileSync(path.join(__dirname, "../src/card/openCard.ts"), "utf8");
+ for (const mobile of [false, true]) {
+ const compiled = ts.transpileModule(parse(cardSource, {MOBILE: mobile, BROWSER: true}, false, true), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+ }).outputText;
+ await win.webContents.executeJavaScript(
+ `(${runFlashcardCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(compiled)}, ${mobile})`);
+ }
} catch (error) {
console.error(error);
code = 1;
@@ -173,7 +320,7 @@ if (process.versions.electron && process.type === "browser") {
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 () => {
+ test("dialogs restore focus and flashcards reopen after cleanup failures on desktop and mobile", async () => {
const profile = fs.mkdtempSync(path.join(os.tmpdir(), "siyuan-dialog-focus-"));
const env = {...process.env};
delete env.ELECTRON_RUN_AS_NODE;