1
0
This commit is contained in:
Zef Hemel 2023-12-22 13:22:25 +01:00
parent 333eb8c151
commit df83c62dec
6 changed files with 65 additions and 16 deletions

View File

@ -930,7 +930,7 @@ export class Client {
}
}
async runCommandByName(name: string, args?: string[]) {
async runCommandByName(name: string, args?: any[]) {
const cmd = this.ui.viewState.commands.get(name);
if (cmd) {
if (args) {

View File

@ -1,4 +1,6 @@
import buildMarkdown from "../common/markdown_parser/parser.ts";
import buildMarkdown, {
commandLinkRegex,
} from "../common/markdown_parser/parser.ts";
import { readonlyMode } from "./cm_plugins/readonly.ts";
import customMarkdownStyle from "./style.ts";
import {
@ -51,6 +53,40 @@ export function createEditorState(
readOnly: boolean,
): EditorState {
const commandKeyBindings: KeyBinding[] = [];
// Keyboard shortcuts from SETTINGS take precedense
if (client.settings?.keyboardShortcuts) {
for (const shortcut of client.settings.keyboardShortcuts) {
console.info("Configuring keyboard shortcut", shortcut);
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(
`Error running command: ${e.message}`,
"error",
);
}).then(() => {
// Always be focusing the editor after running a command
client.focus();
});
return true;
},
});
}
}
// Then add bindings for plug commands
for (const def of client.system.commandHook.editorCommands.values()) {
if (def.command.key) {
commandKeyBindings.push({
@ -81,6 +117,7 @@ export function createEditorState(
});
}
}
let touchCount = 0;
const markdownLanguage = buildMarkdown(client.system.mdExtensions);

View File

@ -14,7 +14,7 @@ export type CommandDef = {
export type AppCommand = {
command: CommandDef;
run: (args?: string[]) => Promise<void>;
run: (args?: any[]) => Promise<void>;
};
export type CommandHookT = {
@ -44,7 +44,7 @@ export class CommandHook extends EventEmitter<CommandHookEvents>
this.editorCommands.set(cmd.name, {
command: cmd,
run: (args?: string[]) => {
return plug.invoke(name, [cmd, ...args??[]]);
return plug.invoke(name, [cmd, ...args ?? []]);
},
});
}

View File

@ -19,10 +19,17 @@ export type Notification = {
export type PanelMode = number;
export type KeyboardShortcut = {
command: string;
key?: string;
mac?: string;
};
export type BuiltinSettings = {
indexPage: string;
customStyles?: string | string[];
plugOverrides?: Record<string, Partial<Manifest>>;
keyboardShortcuts?: KeyboardShortcut[];
// Format: compatible with docker ignore
spaceIgnore?: string;
};

View File

@ -3,6 +3,11 @@ release.
---
## Next
* Keyboard shortcuts can now be configured in [[SETTINGS]]
---
## 0.5.10
* **Breaking change**: Local attachment URLs (`[page](url)` syntax and `![alt](url)` image syntax) are now interpreted to relative to the page's folder, unless their URL starts with a `/` then they're relative to the space root (as per [this issue](https://github.com/silverbulletmd/silverbullet/issues/363))
* **Breaking change:** Revamped [[Templates]], specifically changed the format of [[Page Templates]]. The “Template: Instantiate Page” has been renamed to {[Page: From Template]}.

View File

@ -7,29 +7,29 @@ indexPage: "[[SilverBullet]]"
# Load custom CSS styles from the following page, can also be an array
customStyles: "[[STYLES]]"
# Template settings
quickNotePrefix: "📥 "
dailyNotePrefix: "📅 "
dailyNoteTemplate: "[[template/page/Daily Note]]"
weeklyNotePrefix: "🗓️ "
weeklyNoteTemplate: "[[template/page/Weekly Note]]"
weeklyNoteMonday: false
# Markdown
previewOnRHS: true
# 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"
# Defines files to ignore in a format compatible with .gitignore
spaceIgnore: |
dist
largefolder
*.mp4
# Plug overrides allow you to override any property in a plug manifest at runtime
# The primary use case of this is to override or define keyboard shortcuts. You can use the . notation, to quickly "dive deep" into the structure
plugOverrides:
editor:
# Matching this YAML structure:
# https://github.com/silverbulletmd/silverbullet/blob/main/plugs/editor/editor.plug.yaml
# and overriding the "key" for centering the cursor
functions.centerCursor.command.key: Ctrl-Alt-p
```