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
+1 -1
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) {
+38 -1
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);
+2 -2
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 ?? []]);
},
});
}
+7
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;
};