Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(client): bind window shortcuts only once the tab manager exists
Elian Doranfb82a4006e70
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.tsindex 19acf8542c..af96f80865 100644--- a/apps/client/src/components/app_context.ts+++ b/apps/client/src/components/app_context.ts@@ -673,6 +673,8 @@ export class AppContext extends Component {if (utils.isElectron()) {this.child(zoomComponent);}++ void keyboardActionsService.setupWindowShortcuts();}renderWidgets() {diff --git a/apps/client/src/services/keyboard_actions.spec.ts b/apps/client/src/services/keyboard_actions.spec.tsindex 76beca412f..1f7d7f016f 100644--- a/apps/client/src/services/keyboard_actions.spec.ts+++ b/apps/client/src/services/keyboard_actions.spec.ts@@ -109,7 +109,7 @@ describe("keyboard_actions", () => {expect(treeActions.map((a) => a.actionName)).toEqual(["treeA"]);});- it("binds window-scoped shortcuts globally at module load", async () => {+ it("setupWindowShortcuts binds window-scoped shortcuts globally, and only when asked", async () => {let spy: ReturnType<typeof vi.spyOn> | undefined;const { kb } = await loadModule([@@ -123,9 +123,13 @@ describe("keyboard_actions", () => {});- // wait for the module-load `getActionsForScope("window").then(...)` to settle+ // Importing the module must not bind anything: the handlers read `appContext.tabManager`,+ // which only exists once `AppContext.initComponents()` has run and called setupWindowShortcuts.await kb.default.getActions();await Promise.resolve();+ expect(spy).not.toHaveBeenCalled();++ await kb.default.setupWindowShortcuts();expect(spy).toHaveBeenCalledTimes(2);const boundShortcuts = spy!.mock.calls.map((c) => c[0]);diff --git a/apps/client/src/services/keyboard_actions.ts b/apps/client/src/services/keyboard_actions.tsindex f55de3791a..2025930372 100644--- a/apps/client/src/services/keyboard_actions.ts+++ b/apps/client/src/services/keyboard_actions.ts@@ -54,14 +54,22 @@ async function setupActionsForElement(scope: string, $el: JQuery<HTMLElement>, creturn bindings;}-getActionsForScope("window").then((actions) => {+/**+ * Binds every window-scoped action (jump to note, reload, tab switching, …) as a global shortcut+ * routed through `appContext.triggerCommand`. `AppContext.initComponents()` calls this once+ * `appContext.tabManager` exists; binding at module load instead left a window during startup in+ * which a matching keypress (F5 held across a reload, for instance) threw on the missing tab manager.+ */+async function setupWindowShortcuts() {+ const actions = await getActionsForScope("window");+for (const action of actions) {/* v8 ignore next -- effectiveShortcuts is always normalized to an array by the loader (line 13) before this resolves, so the ?? [] fallback is unreachable */for (const shortcut of action.effectiveShortcuts ?? []) {shortcutService.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName, { ntxId: appContext.tabManager.activeNtxId }));}}-});+}async function getAction(actionName: string, silent = false) {await keyboardActionsLoaded;@@ -129,6 +137,7 @@ function updateDisplayedShortcuts($container: JQuery<HTMLElement>) {export default {updateDisplayedShortcuts,+ setupWindowShortcuts,setupActionsForElement,getAction,getActions,