From e050c7a4cf043bcdcc2b8ce2d3a7875749f42ac0 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 14 Jul 2024 19:57:06 +0200 Subject: [PATCH] Support creating a slashCommand as a command shortcut --- lib/web.ts | 7 ++++++- web/client.ts | 3 +++ web/hooks/slash_command.ts | 21 +++++++++++++++++++++ website/SETTINGS.md | 15 +++++++++++---- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/web.ts b/lib/web.ts index 13a79800..0d0ba40d 100644 --- a/lib/web.ts +++ b/lib/web.ts @@ -16,10 +16,15 @@ export type Notification = { export type PanelMode = number; export type Shortcut = { + // Command we're creating the shortcut for + command: string; + // (Re)bind to keyboard shortcut key?: string; mac?: string; + // Bind to slash command + slashCommand?: string; + // Tweak priority in command palette priority?: number; - command: string; }; export type ActionButton = { diff --git a/web/client.ts b/web/client.ts index f410105b..6d7b0afc 100644 --- a/web/client.ts +++ b/web/client.ts @@ -249,6 +249,9 @@ export class Client { type: "settings-loaded", settings: this.settings, }); + this.clientSystem.slashCommandHook.buildAllCommands( + this.clientSystem.system, + ); } private async initSync() { diff --git a/web/hooks/slash_command.ts b/web/hooks/slash_command.ts index b282ce53..92575640 100644 --- a/web/hooks/slash_command.ts +++ b/web/hooks/slash_command.ts @@ -13,6 +13,7 @@ import { } from "../../plug-api/types.ts"; import { safeRun } from "$lib/async.ts"; import { SlashCommandDef, SlashCommandHookT } from "$lib/manifest.ts"; +import { parseCommand } from "$common/command.ts"; export type AppSlashCommand = { slashCommand: SlashCommandDef; @@ -49,6 +50,26 @@ export class SlashCommandHook implements Hook { }); } } + if (this.editor.settings?.shortcuts) { + // Add slash commands for shortcuts that configure them + for (const shortcut of this.editor.settings.shortcuts) { + if (shortcut.slashCommand) { + const parsedCommand = parseCommand(shortcut.command); + this.slashCommands.set(shortcut.slashCommand, { + slashCommand: { + name: shortcut.slashCommand, + description: parsedCommand.alias || parsedCommand.name, + }, + run: () => { + return this.editor.runCommandByName( + parsedCommand.name, + parsedCommand.args, + ); + }, + }); + } + } + } } // Completer for CodeMirror diff --git a/website/SETTINGS.md b/website/SETTINGS.md index 53231ab9..9b51e92b 100644 --- a/website/SETTINGS.md +++ b/website/SETTINGS.md @@ -1,5 +1,5 @@ #meta - +/ind This page contains settings for configuring SilverBullet and its Plugs. Changing any of these will go into effect immediately in most cases except `indexPage` which requires a page reload. ```yaml @@ -41,12 +41,19 @@ actionButtons: # description: "Go to the previous page" # mobile: true # Only show on mobile devices, set to false to show only on desktop -# Override keyboard shortcuts and command priority +# Create keyboard or slash command shortcuts for commands shortcuts: -- command: "Navigate: Center Cursor" # But a command name is also supported +# Map a key to a command +- command: "{[Navigate: Center Cursor]}" key: "Alt-x" +# Map a slash command (e.g. `/indent`) to a command +- command: "{[Outline: Move Right]}" + slashCommand: "indent" +- command: "{[Outline: Move Left]}" + slashCommand: "outdent" +# Bump a command's priority in the command palette - command: "{[Upload: File]}" - priority: 1 # Make sure this appears at the top of the list in the command palette + priority: 1 # Page decorations pageDecorations: