diff --git a/web/components/command_palette.tsx b/web/components/command_palette.tsx index a492f11..ef3f811 100644 --- a/web/components/command_palette.tsx +++ b/web/components/command_palette.tsx @@ -26,13 +26,15 @@ export function CommandPalette({ const isMac = isMacLike(); for (const [name, def] of commands.entries()) { let shortcut: { key?: string; mac?: string } = def.command; - // Let's see if there's a keyboard shortcut override - if (settings.keyboardShortcuts) { - const commandKeyboardOverride = settings.keyboardShortcuts.find(( + // Let's see if there's a shortcut override + if (settings.shortcuts) { + const commandKeyboardOverride = settings.shortcuts.find(( shortcut, ) => { const commandMatch = commandLinkRegex.exec(shortcut.command); - return commandMatch && commandMatch[1] === name || + // If this is a command link, we want to match the command name but also make sure no arguments were set + return commandMatch && commandMatch[1] === name && !commandMatch[5] || + // or if it's not a command link, let's match exactly shortcut.command === name; }); if (commandKeyboardOverride) { diff --git a/web/editor_state.ts b/web/editor_state.ts index b1f2b4b..ca17ae4 100644 --- a/web/editor_state.ts +++ b/web/editor_state.ts @@ -54,22 +54,27 @@ export function createEditorState( ): EditorState { const commandKeyBindings: KeyBinding[] = []; + // Track which keyboard shortcuts for which commands we've overridden, so we can skip them later + const overriddenCommands = new Set(); // Keyboard shortcuts from SETTINGS take precedense - if (client.settings?.keyboardShortcuts) { - for (const shortcut of client.settings.keyboardShortcuts) { - // console.info("Configuring keyboard shortcut", shortcut); + if (client.settings?.shortcuts) { + for (const shortcut of client.settings.shortcuts) { + // Figure out if we're using the command link syntax here, if so: parse it out + const commandMatch = commandLinkRegex.exec(shortcut.command); + let cleanCommandName = shortcut.command; + let args: any[] = []; + if (commandMatch) { + cleanCommandName = commandMatch[1]; + args = commandMatch[5] ? JSON.parse(`[${commandMatch[5]}]`) : []; + } + if (args.length === 0) { + // If there was no "specialization" of this command (that is, we effectively created a keybinding for an existing command but with arguments), let's add it to the overridden command set: + overriddenCommands.add(cleanCommandName); + } commandKeyBindings.push({ key: shortcut.key, mac: shortcut.mac, run: (): boolean => { - const commandMatch = commandLinkRegex.exec(shortcut.command); - - let cleanCommandName = shortcut.command; - let args: any[] = []; - if (commandMatch) { - cleanCommandName = commandMatch[1]; - args = commandMatch[5] ? JSON.parse(`[${commandMatch[5]}]`) : []; - } client.runCommandByName(cleanCommandName, args).catch((e: any) => { console.error(e); client.flashNotification( @@ -89,6 +94,10 @@ export function createEditorState( // Then add bindings for plug commands for (const def of client.system.commandHook.editorCommands.values()) { if (def.command.key) { + // If we've already overridden this command, skip it + if (overriddenCommands.has(def.command.key)) { + continue; + } commandKeyBindings.push({ key: def.command.key, mac: def.command.mac, diff --git a/web/types.ts b/web/types.ts index c002364..908a301 100644 --- a/web/types.ts +++ b/web/types.ts @@ -19,17 +19,17 @@ export type Notification = { export type PanelMode = number; -export type KeyboardShortcut = { - command: string; +export type Shortcut = { key?: string; mac?: string; + command: string; }; export type BuiltinSettings = { indexPage: string; customStyles?: string | string[]; plugOverrides?: Record>; - keyboardShortcuts?: KeyboardShortcut[]; + shortcuts?: Shortcut[]; // Format: compatible with docker ignore spaceIgnore?: string; }; diff --git a/website/SETTINGS.md b/website/SETTINGS.md index 153adf8..da6a187 100644 --- a/website/SETTINGS.md +++ b/website/SETTINGS.md @@ -15,16 +15,13 @@ weeklyNotePrefix: "🗓️ " weeklyNoteTemplate: "[[template/page/Weekly Note]]" weeklyNoteMonday: false -# Keyboard shortcut overrides take presedence over built-in shortcuts -keyboardShortcuts: - # Using the command-link syntax - - command: "{[Stats: Show]}" - # Mac-specific keyboard - mac: "Cmd-s" - # Key binding for Windows/Linux (and Mac if not defined) - key: "Ctrl-s" - - command: "Navigate: Center Cursor" - key: "Alt-x" +# (Keyboard) shortcut overrides take presedence over built-in shortcuts +shortcuts: + - mac: "Cmd-s" # Mac-specific keyboard shortcut + key: "Ctrl-s" # Windows/Linux specific keyboard shortcut + command: "{[Stats: Show]}" # Using the command link syntax here + - key: "Alt-x" + command: "Navigate: Center Cursor" # But a command name is also supported # Defines files to ignore in a format compatible with .gitignore spaceIgnore: |