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