Skip to content

Build your personal knowledge base with Trilium Notes

fix(menus): cancel a loading menu when a shortcut runs

Elian Doran5c3355a89661
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index e95937e2c4..becf274df9 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -132,6 +132,22 @@ describe("contextMenu", () => {
expect(onHide).toHaveBeenCalledTimes(1);
});
+ it("cancels a menu still loading `Menu` for `closeOpenPopups()`", async () => {
+ buildPage();
+ const contextMenu = await buildContextMenu();
+ const { closeOpenPopups } = await import("../widgets/react/Popup");
+ const onShow = vi.fn();
+
+ const shown = contextMenu.show({ x: 10, y: 20, items, selectMenuItemHandler: () => {}, onShow });
+ closeOpenPopups();
+ await shown;
+
+ expect(onShow).not.toHaveBeenCalled();
+ expect(menuElement()).toBeNull();
+ expect(contextMenu.isShown).toBe(false);
+ expect(document.body.innerHTML).toBe(`<div id="app"></div>`);
+ });
+
it("shows only the last of two menus asked for while `Menu` still loads, and hides it", async () => {
buildPage();
const contextMenu = await buildContextMenu();
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.ts
index 16cf2245f2..378fa113cd 100644
--- a/apps/client/src/menus/context_menu.ts
+++ b/apps/client/src/menus/context_menu.ts
@@ -203,7 +203,9 @@ class ContextMenu {
const request = ++this.lastShowRequest;
// Loaded here rather than imported: the core services that import this module would
// otherwise reach the widget tree through the rows `Menu` draws, in a cycle.
- const { default: Menu } = await import("../widgets/react/Menu");
+ // `closeOpenPopups()` during the import cancels this menu before it opens.
+ const cancelPending = registerOpenPopup(() => this.lastShowRequest++);
+ const { default: Menu } = await import("../widgets/react/Menu").finally(cancelPending);
// A later `show()` made during the import replaces this one.
if (request !== this.lastShowRequest) return;