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
refactor(command_palette): use declarative command approach
Elian Doran964bc74b83d5
diff --git a/apps/client/src/services/command_registry.ts b/apps/client/src/services/command_registry.tsindex 5bb4bbde65..2fc9beb839 100644--- a/apps/client/src/services/command_registry.ts+++ b/apps/client/src/services/command_registry.ts@@ -1,6 +1,6 @@import appContext, { type CommandNames } from "../components/app_context.js";import type NoteTreeWidget from "../widgets/note_tree.js";-import keyboardActions from "./keyboard_actions.js";+import keyboardActions, { Action } from "./keyboard_actions.js";export interface CommandDefinition {id: string;@@ -12,6 +12,8 @@ export interface CommandDefinition {handler?: () => void | Promise<void>;aliases?: string[];source?: "manual" | "keyboard-action";+ /** Reference to the original keyboard action for scope checking. */+ keyboardAction?: Action;}class CommandRegistry {@@ -155,7 +157,7 @@ class CommandRegistry {}}- private registerKeyboardActions(actions: any[]) {+ private registerKeyboardActions(actions: Action[]) {for (const action of actions) {// Skip actions that we've already manually registeredif (this.commands.has(action.actionName)) {@@ -167,9 +169,6 @@ class CommandRegistry {continue;}- // Note-tree scoped actions need special handling- const needsFocusEmulation = action.scope === "note-tree";-// Get the primary shortcut (first one in the list)const primaryShortcut = action.effectiveShortcuts?.[0];@@ -182,7 +181,7 @@ class CommandRegistry {shortcut: primaryShortcut ? this.formatShortcut(primaryShortcut) : undefined,commandName: action.actionName as CommandNames,source: "keyboard-action",- handler: needsFocusEmulation ? () => this.executeWithNoteTreeFocus(action.actionName) : undefined+ keyboardAction: action};this.register(commandDef);@@ -325,13 +324,29 @@ class CommandRegistry {return;}+ // Execute custom handler if providedif (command.handler) {await command.handler();- } else if (command.commandName) {+ return;+ }++ // Handle keyboard action with scope-aware execution+ if (command.keyboardAction && command.commandName) {+ if (command.keyboardAction.scope === "note-tree") {+ this.executeWithNoteTreeFocus(command.commandName);+ } else {+ appContext.triggerCommand(command.commandName);+ }+ return;+ }++ // Fallback for commands without keyboard action reference+ if (command.commandName) {appContext.triggerCommand(command.commandName);- } else {- console.error(`Command ${commandId} has no handler or commandName`);+ return;}++ console.error(`Command ${commandId} has no handler or commandName`);}private executeWithNoteTreeFocus(actionName: CommandNames) {diff --git a/apps/client/src/services/keyboard_actions.ts b/apps/client/src/services/keyboard_actions.tsindex 3cb0ffd33b..35cae43fa9 100644--- a/apps/client/src/services/keyboard_actions.ts+++ b/apps/client/src/services/keyboard_actions.ts@@ -10,6 +10,9 @@ export interface Action {actionName: CommandNames;effectiveShortcuts: string[];scope: string;+ friendlyName: string;+ description?: string;+ iconClass?: string;}const keyboardActionsLoaded = server.get<Action[]>("keyboard-actions").then((actions) => {diff --git a/apps/client/src/widgets/dialogs/jump_to_note.ts b/apps/client/src/widgets/dialogs/jump_to_note.tsindex abadde0d3f..5139f55ab4 100644--- a/apps/client/src/widgets/dialogs/jump_to_note.ts+++ b/apps/client/src/widgets/dialogs/jump_to_note.ts@@ -179,6 +179,7 @@ export default class JumpToNoteDialog extends BasicWidget {// If we restored a command mode value, manually trigger command displayif (this.isCommandMode) {+ console.log("DEBUG: Restoring command mode, clearing and showing commands");// Clear the value first, then set it to ">" to trigger a proper changethis.$autoComplete.autocomplete("val", "");noteAutocompleteService.showAllCommands(this.$autoComplete);