mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-07 23:03:00 +03:00
MD Editor: Finished conversion to Typescript
This commit is contained in:
61
resources/js/markdown/shortcuts.ts
Normal file
61
resources/js/markdown/shortcuts.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import {MarkdownEditor} from "./index.mjs";
|
||||
import {KeyBinding} from "@codemirror/view";
|
||||
|
||||
/**
|
||||
* Provide shortcuts for the editor instance.
|
||||
*/
|
||||
function provide(editor: MarkdownEditor): Record<string, () => void> {
|
||||
const shortcuts: Record<string, () => void> = {};
|
||||
|
||||
// Insert Image shortcut
|
||||
shortcuts['Shift-Mod-i'] = () => editor.actions.insertImage();
|
||||
|
||||
// Save draft
|
||||
shortcuts['Mod-s'] = () => window.$events.emit('editor-save-draft');
|
||||
|
||||
// Save page
|
||||
shortcuts['Mod-Enter'] = () => window.$events.emit('editor-save-page');
|
||||
|
||||
// Show link selector
|
||||
shortcuts['Shift-Mod-k'] = () => editor.actions.showLinkSelector();
|
||||
|
||||
// Insert Link
|
||||
shortcuts['Mod-k'] = () => editor.actions.insertLink();
|
||||
|
||||
// FormatShortcuts
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the editor shortcuts in CodeMirror keybinding format.
|
||||
*/
|
||||
export function provideKeyBindings(editor: MarkdownEditor): KeyBinding[] {
|
||||
const shortcuts = provide(editor);
|
||||
const keyBindings = [];
|
||||
|
||||
const wrapAction = (action: ()=>void) => () => {
|
||||
action();
|
||||
return true;
|
||||
};
|
||||
|
||||
for (const [shortcut, action] of Object.entries(shortcuts)) {
|
||||
keyBindings.push({key: shortcut, run: wrapAction(action), preventDefault: true});
|
||||
}
|
||||
|
||||
return keyBindings;
|
||||
}
|
Reference in New Issue
Block a user