1
0
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:
Dan Brown
2025-07-20 15:05:19 +01:00
parent 7bbf591a7f
commit 61adc735c8
9 changed files with 188 additions and 138 deletions

View 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;
}