Skip to content

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

:bug: Prevent blank detached windows during dialog initialization https://github.com/siyuan-note/siyuan/issues/20165

Daniela021d2b5bf59
diff --git a/app/src/mobile/util/MobileEditorDialog.test.ts b/app/src/mobile/util/MobileEditorDialog.test.ts
index cf2d6e312c..72baf32134 100644
--- a/app/src/mobile/util/MobileEditorDialog.test.ts
+++ b/app/src/mobile/util/MobileEditorDialog.test.ts
@@ -33,11 +33,37 @@ const setup = () => {
assert.ok(modules[name], `Unexpected module: ${name}`);
return modules[name];
}, console: {error: (error: unknown) => errors.push(error)}});
- const dialog = () => new api.MobileEditorDialog({content: "editor"}) as
- InstanceType<typeof api.MobileEditorDialog> & {destroyed: number};
+ const dialog = () => new (api.getMobileEditorDialog())({content: "editor"}) as
+ import("./MobileEditorDialog").MobileEditorDialog & {destroyed: number};
return {...api, dialog, messages, errors};
};
+test("editor sheet imports wait for the dialog base class to finish initializing", () => {
+ const api = {} as typeof import("./MobileEditorDialog");
+ const dialogModule: {Dialog?: unknown} = {};
+ let dialogReads = 0;
+ runInNewContext(compiled, {exports: api, require: (name: string) => {
+ if (name === "../../dialog") {
+ dialogReads++;
+ return dialogModule;
+ }
+ return {};
+ }});
+ assert.equal(dialogReads, 0);
+ assert.equal(api.closeMobileEditorSheets(), undefined);
+ class Dialog {
+ constructor(public options: unknown) {}
+ }
+ dialogModule.Dialog = Dialog;
+ const DialogClass = api.getMobileEditorDialog();
+ const options = {content: "editor"};
+ const dialog = new DialogClass(options);
+ assert.ok(dialog instanceof Dialog);
+ assert.equal(dialog.options, options);
+ assert.equal(api.getMobileEditorDialog(), DialogClass);
+ assert.equal(dialogReads, 1);
+});
+
test("registered editor sheets coalesce concurrent close requests and wait for every save", async () => {
const fixture = setup();
assert.equal(fixture.closeMobileEditorSheets(), undefined);
diff --git a/app/src/mobile/util/MobileEditorDialog.ts b/app/src/mobile/util/MobileEditorDialog.ts
index 7a9ae387b5..1e3c56d4af 100644
--- a/app/src/mobile/util/MobileEditorDialog.ts
+++ b/app/src/mobile/util/MobileEditorDialog.ts
@@ -1,8 +1,10 @@
-import {Dialog} from "../../dialog";
import {showMessage} from "../../dialog/message";
import {escapeHtml} from "../../util/escape";
+export type MobileEditorDialog = InstanceType<ReturnType<typeof createMobileEditorDialog>>;
+
const sheets = new Set<MobileEditorDialog>();
+let dialogClass: ReturnType<typeof createMobileEditorDialog>;
export const closeMobileEditorSheets = () => {
if (sheets.size) {
@@ -10,43 +12,49 @@ export const closeMobileEditorSheets = () => {
}
};
-export class MobileEditorDialog extends Dialog {
- public beforeClose: () => Promise<void>;
- private closing?: Promise<void>;
- private discarded = false;
+// 创建抽屉时再加载对话框基类,避免菜单和编辑器模块在启动期间形成循环依赖。
+export const getMobileEditorDialog = () => dialogClass ??= createMobileEditorDialog();
- public register() {
- sheets.add(this);
- }
+const createMobileEditorDialog = () => {
+ const {Dialog}: typeof import("../../dialog") = require("../../dialog");
+ return class MobileEditorDialog extends Dialog {
+ public beforeClose: () => Promise<void>;
+ private closing?: Promise<void>;
+ private discarded = false;
- public close() {
- if (this.discarded) {
- return Promise.resolve();
+ public register() {
+ sheets.add(this);
}
- if (!this.closing) {
- this.closing = Promise.resolve().then(() => this.beforeClose?.()).then(() => {
- sheets.delete(this);
- super.destroy();
- }).catch(error => {
- if (this.discarded) {
- return;
- }
- this.closing = undefined;
- showMessage(escapeHtml(String(error)));
- throw error;
- });
+
+ public close(): Promise<void> {
+ if (this.discarded) {
+ return Promise.resolve();
+ }
+ if (!this.closing) {
+ this.closing = Promise.resolve().then(() => this.beforeClose?.()).then(() => {
+ sheets.delete(this);
+ super.destroy();
+ }).catch(error => {
+ if (this.discarded) {
+ return;
+ }
+ this.closing = undefined;
+ showMessage(escapeHtml(String(error)));
+ throw error;
+ });
+ }
+ return this.closing;
}
- return this.closing;
- }
- public destroy() {
- void this.close().catch(error => console.error(error));
- }
+ public destroy() {
+ void this.close().catch(error => console.error(error));
+ }
- // 笔记本锁定或内容删除时立即注销面板,不等待已经失去访问权限的保存请求。
- public discard() {
- this.discarded = true;
- sheets.delete(this);
- super.destroy();
- }
-}
+ // 笔记本锁定或内容删除时立即注销面板,不等待已经失去访问权限的保存请求。
+ public discard() {
+ this.discarded = true;
+ sheets.delete(this);
+ super.destroy();
+ }
+ };
+};
diff --git a/app/src/mobile/util/openBacklinks.ts b/app/src/mobile/util/openBacklinks.ts
index 3f625970c6..87a8a3695d 100644
--- a/app/src/mobile/util/openBacklinks.ts
+++ b/app/src/mobile/util/openBacklinks.ts
@@ -1,4 +1,4 @@
-import {MobileEditorDialog} from "./MobileEditorDialog";
+import {getMobileEditorDialog, type MobileEditorDialog} from "./MobileEditorDialog";
import {BacklinkContent} from "../../layout/dock/BacklinkContent";
import {activeBlur} from "./keyboardToolbar";
import {registerMobileBacklinkPanel} from "./backlinkPanels";
@@ -21,6 +21,7 @@ export const openMobileBacklinks = async (protyle: IProtyle, blockId: string) =>
if (version !== openVersion || !protyle.element.isConnected) {
return;
}
+ const MobileEditorDialog = getMobileEditorDialog();
const dialog = new MobileEditorDialog({
content: '<div class="mobile-backlinks-content fn__flex-column"></div>',
width: "100vw",
diff --git a/app/src/mobile/util/openReference.test.ts b/app/src/mobile/util/openReference.test.ts
index 3968b04cdc..54ac645064 100644
--- a/app/src/mobile/util/openReference.test.ts
+++ b/app/src/mobile/util/openReference.test.ts
@@ -202,7 +202,7 @@ const setup = (readonly = false) => {
return {...api, ...dialogAPI, ...saveAPI, ...editorSessionAPI, ...cellEditorAPI,
source, requests, messages, editors, registered, flushes, refreshed, titleEvents, cellMasks, viewport,
window, document, answer, open,
- dialogs: dialogs as Array<DialogMock & InstanceType<typeof dialogAPI.MobileEditorDialog>>,
+ dialogs: dialogs as Array<DialogMock & import("./MobileEditorDialog").MobileEditorDialog>,
setFlush: (callback: typeof flush) => { flush = callback; },
finishDestroy: () => { destroyCallbacks.splice(0).forEach(callback => callback()); },
disposedBindings: () => disposedBindings};
diff --git a/app/src/mobile/util/openReference.ts b/app/src/mobile/util/openReference.ts
index 4f0d7b95d8..026cdddae4 100644
--- a/app/src/mobile/util/openReference.ts
+++ b/app/src/mobile/util/openReference.ts
@@ -4,7 +4,7 @@ import {fetchSyncPost} from "../../util/fetch";
import {showMessage} from "../../dialog/message";
import {escapeHtml} from "../../util/escape";
import {bindBottomSheetDialog} from "./bindBottomSheetDialog";
-import {MobileEditorDialog} from "./MobileEditorDialog";
+import {getMobileEditorDialog, type MobileEditorDialog} from "./MobileEditorDialog";
import {activeBlur} from "./keyboardToolbar";
import {flushMobileSecondaryEditor, registerMobileSecondaryEditor} from "./secondaryEditors";
import {getVisibleViewportBounds} from "./visibleViewport";
@@ -117,6 +117,7 @@ export const openMobileReference = async (protyle: IProtyle, blockId: string) =>
source.contentElement.scrollLeft = scrollLeft;
}
};
+ const MobileEditorDialog = getMobileEditorDialog();
const dialog = new MobileEditorDialog({
title: window.siyuan.languages.viewRefContent,
content: '<div class="mobile-reference-editor fn__flex-1"></div>',