import { EditorView, PluginValue, ViewPlugin, ViewUpdate, } from "@codemirror/view"; import { Compartment, EditorState } from "@codemirror/state"; export const extensions = new Compartment(); export const getCodeEditorState = () => { return EditorState.create({ extensions: extensions.of([]), }); }; export const setCodeEditorElement = ( el: HTMLElement, state: ReturnType, ) => { const view = new EditorView({ state, parent: el, }); return view; }; export const docUpdaterPlugin = (updateFn: (state: string) => void) => ViewPlugin.fromClass( class DocUpdater implements PluginValue { constructor(val: EditorView) { updateFn(val.state.doc.toString()); } update(update: ViewUpdate) { if (update.docChanged) updateFn(update.state.doc.toString()); } }, );