2022-11-27 00:43:28 +08:00
|
|
|
/**
|
2023-04-11 18:48:58 +08:00
|
|
|
* Provide shortcuts for the editor instance.
|
2022-11-27 00:43:28 +08:00
|
|
|
* @param {MarkdownEditor} editor
|
|
|
|
* @returns {Object<String, Function>}
|
|
|
|
*/
|
2023-04-11 18:48:58 +08:00
|
|
|
function provide(editor) {
|
2022-11-27 00:43:28 +08:00
|
|
|
const shortcuts = {};
|
|
|
|
|
|
|
|
// Insert Image shortcut
|
2023-04-19 22:20:04 +08:00
|
|
|
shortcuts['Shift-Mod-i'] = () => editor.actions.insertImage();
|
2022-11-27 00:43:28 +08:00
|
|
|
|
|
|
|
// Save draft
|
2023-04-19 22:20:04 +08:00
|
|
|
shortcuts['Mod-s'] = () => window.$events.emit('editor-save-draft');
|
2022-11-27 00:43:28 +08:00
|
|
|
|
|
|
|
// Save page
|
2023-04-19 22:20:04 +08:00
|
|
|
shortcuts['Mod-Enter'] = () => window.$events.emit('editor-save-page');
|
2022-11-27 00:43:28 +08:00
|
|
|
|
|
|
|
// Show link selector
|
2023-04-19 22:20:04 +08:00
|
|
|
shortcuts['Shift-Mod-k'] = () => editor.actions.showLinkSelector();
|
2022-11-27 00:43:28 +08:00
|
|
|
|
|
|
|
// Insert Link
|
2023-04-19 22:20:04 +08:00
|
|
|
shortcuts['Mod-k'] = () => editor.actions.insertLink();
|
2022-11-27 00:43:28 +08:00
|
|
|
|
|
|
|
// FormatShortcuts
|
2023-04-19 22:20:04 +08:00
|
|
|
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();
|
2022-11-27 00:43:28 +08:00
|
|
|
|
|
|
|
return shortcuts;
|
2023-04-11 18:48:58 +08: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-19 05:20:02 +08:00
|
|
|
const shortcuts = provide(editor);
|
2023-04-11 18:48:58 +08:00
|
|
|
const keyBindings = [];
|
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
const wrapAction = action => () => {
|
|
|
|
action();
|
|
|
|
return true;
|
2023-04-18 21:21:22 +08:00
|
|
|
};
|
|
|
|
|
2023-04-11 18:48:58 +08:00
|
|
|
for (const [shortcut, action] of Object.entries(shortcuts)) {
|
2023-04-18 21:21:22 +08:00
|
|
|
keyBindings.push({key: shortcut, run: wrapAction(action), preventDefault: true});
|
2023-04-11 18:48:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return keyBindings;
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|