mirror of
https://github.com/zadam/trilium.git
synced 2025-10-21 07:38:53 +02:00
refactor(command_palette): use declarative command approach
This commit is contained in:
parent
fa9b142cb7
commit
964bc74b83
@ -1,6 +1,6 @@
|
|||||||
import appContext, { type CommandNames } from "../components/app_context.js";
|
import appContext, { type CommandNames } from "../components/app_context.js";
|
||||||
import type NoteTreeWidget from "../widgets/note_tree.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 {
|
export interface CommandDefinition {
|
||||||
id: string;
|
id: string;
|
||||||
@ -12,6 +12,8 @@ export interface CommandDefinition {
|
|||||||
handler?: () => void | Promise<void>;
|
handler?: () => void | Promise<void>;
|
||||||
aliases?: string[];
|
aliases?: string[];
|
||||||
source?: "manual" | "keyboard-action";
|
source?: "manual" | "keyboard-action";
|
||||||
|
/** Reference to the original keyboard action for scope checking. */
|
||||||
|
keyboardAction?: Action;
|
||||||
}
|
}
|
||||||
|
|
||||||
class CommandRegistry {
|
class CommandRegistry {
|
||||||
@ -155,7 +157,7 @@ class CommandRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerKeyboardActions(actions: any[]) {
|
private registerKeyboardActions(actions: Action[]) {
|
||||||
for (const action of actions) {
|
for (const action of actions) {
|
||||||
// Skip actions that we've already manually registered
|
// Skip actions that we've already manually registered
|
||||||
if (this.commands.has(action.actionName)) {
|
if (this.commands.has(action.actionName)) {
|
||||||
@ -167,9 +169,6 @@ class CommandRegistry {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note-tree scoped actions need special handling
|
|
||||||
const needsFocusEmulation = action.scope === "note-tree";
|
|
||||||
|
|
||||||
// Get the primary shortcut (first one in the list)
|
// Get the primary shortcut (first one in the list)
|
||||||
const primaryShortcut = action.effectiveShortcuts?.[0];
|
const primaryShortcut = action.effectiveShortcuts?.[0];
|
||||||
|
|
||||||
@ -182,7 +181,7 @@ class CommandRegistry {
|
|||||||
shortcut: primaryShortcut ? this.formatShortcut(primaryShortcut) : undefined,
|
shortcut: primaryShortcut ? this.formatShortcut(primaryShortcut) : undefined,
|
||||||
commandName: action.actionName as CommandNames,
|
commandName: action.actionName as CommandNames,
|
||||||
source: "keyboard-action",
|
source: "keyboard-action",
|
||||||
handler: needsFocusEmulation ? () => this.executeWithNoteTreeFocus(action.actionName) : undefined
|
keyboardAction: action
|
||||||
};
|
};
|
||||||
|
|
||||||
this.register(commandDef);
|
this.register(commandDef);
|
||||||
@ -325,13 +324,29 @@ class CommandRegistry {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute custom handler if provided
|
||||||
if (command.handler) {
|
if (command.handler) {
|
||||||
await command.handler();
|
await command.handler();
|
||||||
} else if (command.commandName) {
|
return;
|
||||||
appContext.triggerCommand(command.commandName);
|
|
||||||
} else {
|
|
||||||
console.error(`Command ${commandId} has no handler or commandName`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error(`Command ${commandId} has no handler or commandName`);
|
||||||
}
|
}
|
||||||
|
|
||||||
private executeWithNoteTreeFocus(actionName: CommandNames) {
|
private executeWithNoteTreeFocus(actionName: CommandNames) {
|
||||||
|
@ -10,6 +10,9 @@ export interface Action {
|
|||||||
actionName: CommandNames;
|
actionName: CommandNames;
|
||||||
effectiveShortcuts: string[];
|
effectiveShortcuts: string[];
|
||||||
scope: string;
|
scope: string;
|
||||||
|
friendlyName: string;
|
||||||
|
description?: string;
|
||||||
|
iconClass?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const keyboardActionsLoaded = server.get<Action[]>("keyboard-actions").then((actions) => {
|
const keyboardActionsLoaded = server.get<Action[]>("keyboard-actions").then((actions) => {
|
||||||
|
@ -179,6 +179,7 @@ export default class JumpToNoteDialog extends BasicWidget {
|
|||||||
|
|
||||||
// If we restored a command mode value, manually trigger command display
|
// If we restored a command mode value, manually trigger command display
|
||||||
if (this.isCommandMode) {
|
if (this.isCommandMode) {
|
||||||
|
console.log("DEBUG: Restoring command mode, clearing and showing commands");
|
||||||
// Clear the value first, then set it to ">" to trigger a proper change
|
// Clear the value first, then set it to ">" to trigger a proper change
|
||||||
this.$autoComplete.autocomplete("val", "");
|
this.$autoComplete.autocomplete("val", "");
|
||||||
noteAutocompleteService.showAllCommands(this.$autoComplete);
|
noteAutocompleteService.showAllCommands(this.$autoComplete);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user