1 import {EditorView, KeyBinding, ViewUpdate} from "@codemirror/view";
2 import {CodeModule} from "../global";
3 import {MarkdownEditorEventMap} from "./dom-handlers";
4 import {MarkdownEditorShortcutMap} from "./shortcuts";
7 * Convert editor shortcuts to CodeMirror keybinding format.
9 export function shortcutsToKeyBindings(shortcuts: MarkdownEditorShortcutMap): KeyBinding[] {
10 const keyBindings = [];
12 const wrapAction = (action: () => void) => () => {
17 for (const [shortcut, action] of Object.entries(shortcuts)) {
18 keyBindings.push({key: shortcut, run: wrapAction(action), preventDefault: true});
25 * Initiate the codemirror instance for the Markdown editor.
27 export async function init(
28 input: HTMLTextAreaElement,
29 shortcuts: MarkdownEditorShortcutMap,
30 domEventHandlers: MarkdownEditorEventMap,
32 ): Promise<EditorView> {
33 const Code = await window.importVersioned('code') as CodeModule;
35 function onViewUpdate(v: ViewUpdate) {
41 const cm = Code.markdownEditor(
45 shortcutsToKeyBindings(shortcuts),
48 // Add editor view to the window for easy access/debugging.
49 // Not part of official API/Docs
51 window.mdEditorView = cm;