1
0

Renamed "keyboardShortcuts" to "shortcuts"

This commit is contained in:
Zef Hemel 2023-12-27 08:14:57 +01:00
parent 4759a20b23
commit a205178ff0
4 changed files with 36 additions and 28 deletions

View File

@ -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) {

View File

@ -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<string>();
// 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,

View File

@ -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<string, Partial<Manifest>>;
keyboardShortcuts?: KeyboardShortcut[];
shortcuts?: Shortcut[];
// Format: compatible with docker ignore
spaceIgnore?: string;
};

View File

@ -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: |