]> BookStack Code Mirror - bookstack/blob - resources/js/markdown/shortcuts.js
CM6: Further fixes/improvements after testing
[bookstack] / resources / js / markdown / shortcuts.js
1 /**
2  * Provide shortcuts for the editor instance.
3  * @param {MarkdownEditor} editor
4  * @returns {Object<String, Function>}
5  */
6 function provide(editor) {
7     const shortcuts = {};
8
9     // Insert Image shortcut
10     shortcuts['Shift-Mod-i'] = cm => editor.actions.insertImage();
11
12     // Save draft
13     shortcuts['Mod-s'] = cm => window.$events.emit('editor-save-draft');
14
15     // Save page
16     shortcuts['Mod-Enter'] = cm => window.$events.emit('editor-save-page');
17
18     // Show link selector
19     shortcuts['Shift-Mod-k'] = cm => editor.actions.showLinkSelector();
20
21     // Insert Link
22     shortcuts['Mod-k'] = cm => editor.actions.insertLink();
23
24     // FormatShortcuts
25     shortcuts['Mod-1'] = cm => editor.actions.replaceLineStart('##');
26     shortcuts['Mod-2'] = cm => editor.actions.replaceLineStart('###');
27     shortcuts['Mod-3'] = cm => editor.actions.replaceLineStart('####');
28     shortcuts['Mod-4'] = cm => editor.actions.replaceLineStart('#####');
29     shortcuts['Mod-5'] = cm => editor.actions.replaceLineStart('');
30     shortcuts['Mod-d'] = cm => editor.actions.replaceLineStart('');
31     shortcuts['Mod-6'] = cm => editor.actions.replaceLineStart('>');
32     shortcuts['Mod-q'] = cm => editor.actions.replaceLineStart('>');
33     shortcuts['Mod-7'] = cm => editor.actions.wrapSelection('\n```\n', '\n```');
34     shortcuts['Mod-8'] = cm => editor.actions.wrapSelection('`', '`');
35     shortcuts['Shift-Mod-e'] = cm => editor.actions.wrapSelection('`', '`');
36     shortcuts['Mod-9'] = cm => editor.actions.cycleCalloutTypeAtSelection();
37     shortcuts['Mod-p'] = cm => editor.actions.replaceLineStart('-')
38     shortcuts['Mod-o'] = cm => editor.actions.replaceLineStartForOrderedList()
39
40     return shortcuts;
41 }
42
43 /**
44  * Get the editor shortcuts in CodeMirror keybinding format.
45  * @param {MarkdownEditor} editor
46  * @return {{key: String, run: function, preventDefault: boolean}[]}
47  */
48 export function provideKeyBindings(editor) {
49     const shortcuts= provide(editor);
50     const keyBindings = [];
51
52     const wrapAction = (action) => {
53         return () => {
54             action();
55             return true;
56         };
57     };
58
59     for (const [shortcut, action] of Object.entries(shortcuts)) {
60         keyBindings.push({key: shortcut, run: wrapAction(action), preventDefault: true});
61     }
62
63     return keyBindings;
64 }