1 import {MarkdownEditor} from "./index.mjs";
2 import {KeyBinding} from "@codemirror/view";
4 export type MarkdownEditorShortcutMap = Record<string, () => void>;
7 * Provide shortcuts for the editor instance.
9 export function provideShortcutMap(editor: MarkdownEditor): MarkdownEditorShortcutMap {
10 const shortcuts: MarkdownEditorShortcutMap = {};
12 // Insert Image shortcut
13 shortcuts['Shift-Mod-i'] = () => editor.actions.insertImage();
16 shortcuts['Mod-s'] = () => window.$events.emit('editor-save-draft');
19 shortcuts['Mod-Enter'] = () => window.$events.emit('editor-save-page');
22 shortcuts['Shift-Mod-k'] = () => editor.actions.showLinkSelector();
25 shortcuts['Mod-k'] = () => editor.actions.insertLink();
28 shortcuts['Mod-1'] = () => editor.actions.replaceLineStart('##');
29 shortcuts['Mod-2'] = () => editor.actions.replaceLineStart('###');
30 shortcuts['Mod-3'] = () => editor.actions.replaceLineStart('####');
31 shortcuts['Mod-4'] = () => editor.actions.replaceLineStart('#####');
32 shortcuts['Mod-5'] = () => editor.actions.replaceLineStart('');
33 shortcuts['Mod-d'] = () => editor.actions.replaceLineStart('');
34 shortcuts['Mod-6'] = () => editor.actions.replaceLineStart('>');
35 shortcuts['Mod-q'] = () => editor.actions.replaceLineStart('>');
36 shortcuts['Mod-7'] = () => editor.actions.wrapSelection('\n```\n', '\n```');
37 shortcuts['Mod-8'] = () => editor.actions.wrapSelection('`', '`');
38 shortcuts['Shift-Mod-e'] = () => editor.actions.wrapSelection('`', '`');
39 shortcuts['Mod-9'] = () => editor.actions.cycleCalloutTypeAtSelection();
40 shortcuts['Mod-p'] = () => editor.actions.replaceLineStart('-');
41 shortcuts['Mod-o'] = () => editor.actions.replaceLineStartForOrderedList();
47 * Get the editor shortcuts in CodeMirror keybinding format.
49 export function provideKeyBindings(editor: MarkdownEditor): KeyBinding[] {
50 const shortcuts = provideShortcutMap(editor);
51 const keyBindings = [];
53 const wrapAction = (action: ()=>void) => () => {
58 for (const [shortcut, action] of Object.entries(shortcuts)) {
59 keyBindings.push({key: shortcut, run: wrapAction(action), preventDefault: true});