2022-11-26 17:43:28 +01:00
|
|
|
/**
|
2023-04-11 12:48:58 +02:00
|
|
|
* Provide shortcuts for the editor instance.
|
2022-11-26 17:43:28 +01:00
|
|
|
* @param {MarkdownEditor} editor
|
|
|
|
* @returns {Object<String, Function>}
|
|
|
|
*/
|
2023-04-11 12:48:58 +02:00
|
|
|
function provide(editor) {
|
2022-11-26 17:43:28 +01:00
|
|
|
const shortcuts = {};
|
|
|
|
|
|
|
|
// Insert Image shortcut
|
2023-04-18 15:21:22 +02:00
|
|
|
shortcuts['Shift-Mod-i'] = cm => editor.actions.insertImage();
|
2022-11-26 17:43:28 +01:00
|
|
|
|
|
|
|
// Save draft
|
2023-04-11 12:48:58 +02:00
|
|
|
shortcuts['Mod-s'] = cm => window.$events.emit('editor-save-draft');
|
2022-11-26 17:43:28 +01:00
|
|
|
|
|
|
|
// Save page
|
2023-04-11 12:48:58 +02:00
|
|
|
shortcuts['Mod-Enter'] = cm => window.$events.emit('editor-save-page');
|
2022-11-26 17:43:28 +01:00
|
|
|
|
|
|
|
// Show link selector
|
2023-04-11 12:48:58 +02:00
|
|
|
shortcuts['Shift-Mod-k'] = cm => editor.actions.showLinkSelector();
|
2022-11-26 17:43:28 +01:00
|
|
|
|
|
|
|
// Insert Link
|
2023-04-11 12:48:58 +02:00
|
|
|
shortcuts['Mod-k'] = cm => editor.actions.insertLink();
|
2022-11-26 17:43:28 +01:00
|
|
|
|
|
|
|
// FormatShortcuts
|
2023-04-11 12:48:58 +02:00
|
|
|
shortcuts['Mod-1'] = cm => editor.actions.replaceLineStart('##');
|
|
|
|
shortcuts['Mod-2'] = cm => editor.actions.replaceLineStart('###');
|
|
|
|
shortcuts['Mod-3'] = cm => editor.actions.replaceLineStart('####');
|
|
|
|
shortcuts['Mod-4'] = cm => editor.actions.replaceLineStart('#####');
|
|
|
|
shortcuts['Mod-5'] = cm => editor.actions.replaceLineStart('');
|
|
|
|
shortcuts['Mod-d'] = cm => editor.actions.replaceLineStart('');
|
|
|
|
shortcuts['Mod-6'] = cm => editor.actions.replaceLineStart('>');
|
|
|
|
shortcuts['Mod-q'] = cm => editor.actions.replaceLineStart('>');
|
|
|
|
shortcuts['Mod-7'] = cm => editor.actions.wrapSelection('\n```\n', '\n```');
|
|
|
|
shortcuts['Mod-8'] = cm => editor.actions.wrapSelection('`', '`');
|
|
|
|
shortcuts['Shift-Mod-e'] = cm => editor.actions.wrapSelection('`', '`');
|
|
|
|
shortcuts['Mod-9'] = cm => editor.actions.cycleCalloutTypeAtSelection();
|
2023-04-18 23:20:02 +02:00
|
|
|
shortcuts['Mod-p'] = cm => editor.actions.replaceLineStart('-');
|
|
|
|
shortcuts['Mod-o'] = cm => editor.actions.replaceLineStartForOrderedList();
|
2022-11-26 17:43:28 +01:00
|
|
|
|
|
|
|
return shortcuts;
|
2023-04-11 12:48:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the editor shortcuts in CodeMirror keybinding format.
|
|
|
|
* @param {MarkdownEditor} editor
|
|
|
|
* @return {{key: String, run: function, preventDefault: boolean}[]}
|
|
|
|
*/
|
|
|
|
export function provideKeyBindings(editor) {
|
2023-04-18 23:20:02 +02:00
|
|
|
const shortcuts = provide(editor);
|
2023-04-11 12:48:58 +02:00
|
|
|
const keyBindings = [];
|
|
|
|
|
2023-04-18 23:20:02 +02:00
|
|
|
const wrapAction = action => () => {
|
|
|
|
action();
|
|
|
|
return true;
|
2023-04-18 15:21:22 +02:00
|
|
|
};
|
|
|
|
|
2023-04-11 12:48:58 +02:00
|
|
|
for (const [shortcut, action] of Object.entries(shortcuts)) {
|
2023-04-18 15:21:22 +02:00
|
|
|
keyBindings.push({key: shortcut, run: wrapAction(action), preventDefault: true});
|
2023-04-11 12:48:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return keyBindings;
|
2023-04-18 23:20:02 +02:00
|
|
|
}
|