1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-06 12:02:45 +03:00

MD Editor: Added plaintext/cm switching

Also aligned the construction of the inputs where possible.
This commit is contained in:
Dan Brown
2025-07-22 10:34:29 +01:00
parent 6b4b500a33
commit d55db06c01
6 changed files with 82 additions and 43 deletions

View File

@@ -1,25 +1,48 @@
import {provideKeyBindings} from './shortcuts';
import {EditorView, ViewUpdate} from "@codemirror/view";
import {MarkdownEditor} from "./index.mjs";
import {EditorView, KeyBinding, ViewUpdate} from "@codemirror/view";
import {CodeModule} from "../global";
import {MarkdownEditorEventMap} from "./dom-handlers";
import {MarkdownEditorShortcutMap} from "./shortcuts";
/**
* Convert editor shortcuts to CodeMirror keybinding format.
*/
export function shortcutsToKeyBindings(shortcuts: MarkdownEditorShortcutMap): KeyBinding[] {
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;
}
/**
* Initiate the codemirror instance for the Markdown editor.
*/
export function init(editor: MarkdownEditor, Code: CodeModule, domEventHandlers: MarkdownEditorEventMap): EditorView {
export async function init(
input: HTMLTextAreaElement,
shortcuts: MarkdownEditorShortcutMap,
domEventHandlers: MarkdownEditorEventMap,
onChange: () => void
): Promise<EditorView> {
const Code = await window.importVersioned('code') as CodeModule;
function onViewUpdate(v: ViewUpdate) {
if (v.docChanged) {
editor.actions.updateAndRender();
onChange();
}
}
const cm = Code.markdownEditor(
editor.config.inputEl,
input,
onViewUpdate,
domEventHandlers,
provideKeyBindings(editor),
shortcutsToKeyBindings(shortcuts),
);
// Add editor view to the window for easy access/debugging.