]> BookStack Code Mirror - bookstack/blob - resources/js/markdown/shortcuts.js
1249f7d609839142e8b0b8b9230961cb72ef1b04
[bookstack] / resources / js / markdown / shortcuts.js
1 /**
2  * Provide shortcuts for the given codemirror instance.
3  * @param {MarkdownEditor} editor
4  * @param {String} metaKey
5  * @returns {Object<String, Function>}
6  */
7 export function provide(editor, metaKey) {
8     const shortcuts = {};
9
10     // Insert Image shortcut
11     shortcuts[`${metaKey}-Alt-I`] = function(cm) {
12         const selectedText = cm.getSelection();
13         const newText = `![${selectedText}](http://)`;
14         const cursorPos = cm.getCursor('from');
15         cm.replaceSelection(newText);
16         cm.setCursor(cursorPos.line, cursorPos.ch + newText.length -1);
17     };
18
19     // Save draft
20     shortcuts[`${metaKey}-S`] = cm => window.$events.emit('editor-save-draft');
21
22     // Save page
23     shortcuts[`${metaKey}-Enter`] = cm => window.$events.emit('editor-save-page');
24
25     // Show link selector
26     shortcuts[`Shift-${metaKey}-K`] = cm => editor.actions.showLinkSelector();
27
28     // Insert Link
29     shortcuts[`${metaKey}-K`] = cm => editor.actions.insertLink();
30
31     // FormatShortcuts
32     shortcuts[`${metaKey}-1`] = cm => editor.actions.replaceLineStart('##');
33     shortcuts[`${metaKey}-2`] = cm => editor.actions.replaceLineStart('###');
34     shortcuts[`${metaKey}-3`] = cm => editor.actions.replaceLineStart('####');
35     shortcuts[`${metaKey}-4`] = cm => editor.actions.replaceLineStart('#####');
36     shortcuts[`${metaKey}-5`] = cm => editor.actions.replaceLineStart('');
37     shortcuts[`${metaKey}-D`] = cm => editor.actions.replaceLineStart('');
38     shortcuts[`${metaKey}-6`] = cm => editor.actions.replaceLineStart('>');
39     shortcuts[`${metaKey}-Q`] = cm => editor.actions.replaceLineStart('>');
40     shortcuts[`${metaKey}-7`] = cm => editor.actions.wrapSelection('\n```\n', '\n```');
41     shortcuts[`${metaKey}-8`] = cm => editor.actions.wrapSelection('`', '`');
42     shortcuts[`Shift-${metaKey}-E`] = cm => editor.actions.wrapSelection('`', '`');
43     shortcuts[`${metaKey}-9`] = cm => editor.actions.wrapSelection('<p class="callout info">', '</p>');
44     shortcuts[`${metaKey}-P`] = cm => editor.actions.replaceLineStart('-')
45     shortcuts[`${metaKey}-O`] = cm => editor.actions.replaceLineStartForOrderedList()
46
47     return shortcuts;
48 }