]> BookStack Code Mirror - bookstack/blobdiff - resources/js/markdown/shortcuts.js
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / resources / js / markdown / shortcuts.js
index 17ffe2fb3a6fc69d94654fdcc63cc631d0af21f6..543e6dcdde67dd26ab8beb8f2987a71932151fbc 100644 (file)
@@ -1,48 +1,62 @@
 /**
- * Provide shortcuts for the given codemirror instance.
+ * Provide shortcuts for the editor instance.
  * @param {MarkdownEditor} editor
- * @param {String} metaKey
  * @returns {Object<String, Function>}
  */
-export function provide(editor, metaKey) {
+function provide(editor) {
     const shortcuts = {};
 
     // Insert Image shortcut
-    shortcuts[`${metaKey}-Alt-I`] = function(cm) {
-        const selectedText = cm.getSelection();
-        const newText = `![${selectedText}](http://)`;
-        const cursorPos = cm.getCursor('from');
-        cm.replaceSelection(newText);
-        cm.setCursor(cursorPos.line, cursorPos.ch + newText.length -1);
-    };
+    shortcuts['Shift-Mod-i'] = () => editor.actions.insertImage();
 
     // Save draft
-    shortcuts[`${metaKey}-S`] = cm => window.$events.emit('editor-save-draft');
+    shortcuts['Mod-s'] = () => window.$events.emit('editor-save-draft');
 
     // Save page
-    shortcuts[`${metaKey}-Enter`] = cm => window.$events.emit('editor-save-page');
+    shortcuts['Mod-Enter'] = () => window.$events.emit('editor-save-page');
 
     // Show link selector
-    shortcuts[`Shift-${metaKey}-K`] = cm => editor.actions.showLinkSelector();
+    shortcuts['Shift-Mod-k'] = () => editor.actions.showLinkSelector();
 
     // Insert Link
-    shortcuts[`${metaKey}-K`] = cm => editor.actions.insertLink();
+    shortcuts['Mod-k'] = () => editor.actions.insertLink();
 
     // FormatShortcuts
-    shortcuts[`${metaKey}-1`] = cm => editor.actions.replaceLineStart('##');
-    shortcuts[`${metaKey}-2`] = cm => editor.actions.replaceLineStart('###');
-    shortcuts[`${metaKey}-3`] = cm => editor.actions.replaceLineStart('####');
-    shortcuts[`${metaKey}-4`] = cm => editor.actions.replaceLineStart('#####');
-    shortcuts[`${metaKey}-5`] = cm => editor.actions.replaceLineStart('');
-    shortcuts[`${metaKey}-D`] = cm => editor.actions.replaceLineStart('');
-    shortcuts[`${metaKey}-6`] = cm => editor.actions.replaceLineStart('>');
-    shortcuts[`${metaKey}-Q`] = cm => editor.actions.replaceLineStart('>');
-    shortcuts[`${metaKey}-7`] = cm => editor.actions.wrapSelection('\n```\n', '\n```');
-    shortcuts[`${metaKey}-8`] = cm => editor.actions.wrapSelection('`', '`');
-    shortcuts[`Shift-${metaKey}-E`] = cm => editor.actions.wrapSelection('`', '`');
-    shortcuts[`${metaKey}-9`] = cm => editor.actions.cycleCalloutTypeAtSelection();
-    shortcuts[`${metaKey}-P`] = cm => editor.actions.replaceLineStart('-')
-    shortcuts[`${metaKey}-O`] = cm => editor.actions.replaceLineStartForOrderedList()
+    shortcuts['Mod-1'] = () => editor.actions.replaceLineStart('##');
+    shortcuts['Mod-2'] = () => editor.actions.replaceLineStart('###');
+    shortcuts['Mod-3'] = () => editor.actions.replaceLineStart('####');
+    shortcuts['Mod-4'] = () => editor.actions.replaceLineStart('#####');
+    shortcuts['Mod-5'] = () => editor.actions.replaceLineStart('');
+    shortcuts['Mod-d'] = () => editor.actions.replaceLineStart('');
+    shortcuts['Mod-6'] = () => editor.actions.replaceLineStart('>');
+    shortcuts['Mod-q'] = () => editor.actions.replaceLineStart('>');
+    shortcuts['Mod-7'] = () => editor.actions.wrapSelection('\n```\n', '\n```');
+    shortcuts['Mod-8'] = () => editor.actions.wrapSelection('`', '`');
+    shortcuts['Shift-Mod-e'] = () => editor.actions.wrapSelection('`', '`');
+    shortcuts['Mod-9'] = () => editor.actions.cycleCalloutTypeAtSelection();
+    shortcuts['Mod-p'] = () => editor.actions.replaceLineStart('-');
+    shortcuts['Mod-o'] = () => editor.actions.replaceLineStartForOrderedList();
 
     return shortcuts;
-}
\ No newline at end of file
+}
+
+/**
+ * Get the editor shortcuts in CodeMirror keybinding format.
+ * @param {MarkdownEditor} editor
+ * @return {{key: String, run: function, preventDefault: boolean}[]}
+ */
+export function provideKeyBindings(editor) {
+    const shortcuts = provide(editor);
+    const keyBindings = [];
+
+    const wrapAction = action => () => {
+        action();
+        return true;
+    };
+
+    for (const [shortcut, action] of Object.entries(shortcuts)) {
+        keyBindings.push({key: shortcut, run: wrapAction(action), preventDefault: true});
+    }
+
+    return keyBindings;
+}