]> BookStack Code Mirror - bookstack/blob - resources/js/markdown/shortcuts.js
Opensearch: Fixed XML declaration when php short tags enabled
[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'] = () => editor.actions.insertImage();
11
12     // Save draft
13     shortcuts['Mod-s'] = () => window.$events.emit('editor-save-draft');
14
15     // Save page
16     shortcuts['Mod-Enter'] = () => window.$events.emit('editor-save-page');
17
18     // Show link selector
19     shortcuts['Shift-Mod-k'] = () => editor.actions.showLinkSelector();
20
21     // Insert Link
22     shortcuts['Mod-k'] = () => editor.actions.insertLink();
23
24     // FormatShortcuts
25     shortcuts['Mod-1'] = () => editor.actions.replaceLineStart('##');
26     shortcuts['Mod-2'] = () => editor.actions.replaceLineStart('###');
27     shortcuts['Mod-3'] = () => editor.actions.replaceLineStart('####');
28     shortcuts['Mod-4'] = () => editor.actions.replaceLineStart('#####');
29     shortcuts['Mod-5'] = () => editor.actions.replaceLineStart('');
30     shortcuts['Mod-d'] = () => editor.actions.replaceLineStart('');
31     shortcuts['Mod-6'] = () => editor.actions.replaceLineStart('>');
32     shortcuts['Mod-q'] = () => editor.actions.replaceLineStart('>');
33     shortcuts['Mod-7'] = () => editor.actions.wrapSelection('\n```\n', '\n```');
34     shortcuts['Mod-8'] = () => editor.actions.wrapSelection('`', '`');
35     shortcuts['Shift-Mod-e'] = () => editor.actions.wrapSelection('`', '`');
36     shortcuts['Mod-9'] = () => editor.actions.cycleCalloutTypeAtSelection();
37     shortcuts['Mod-p'] = () => editor.actions.replaceLineStart('-');
38     shortcuts['Mod-o'] = () => 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         action();
54         return true;
55     };
56
57     for (const [shortcut, action] of Object.entries(shortcuts)) {
58         keyBindings.push({key: shortcut, run: wrapAction(action), preventDefault: true});
59     }
60
61     return keyBindings;
62 }