diff --git a/apps/client/src/widgets/type_widgets/abstract_code_type_widget.ts b/apps/client/src/widgets/type_widgets/abstract_code_type_widget.ts index 30684bdc5..e76901577 100644 --- a/apps/client/src/widgets/type_widgets/abstract_code_type_widget.ts +++ b/apps/client/src/widgets/type_widgets/abstract_code_type_widget.ts @@ -1,3 +1,4 @@ +import type FNote from "../../entities/fnote.js"; import options from "../../services/options.js"; import TypeWidget from "./type_widget.js"; import CodeMirror, { type EditorConfig } from "@triliumnext/codemirror"; @@ -55,22 +56,12 @@ export default class AbstractCodeTypeWidget extends TypeWidget { /** * Must be called by the derived classes in `#doRefresh(note)` in order to react to changes. * - * @param {*} note the note that was changed. - * @param {*} content the new content of the note. + * @param the note that was changed. + * @param new content of the note. */ - _update(note: { mime: string }, content: string) { + _update(note: FNote, content: string) { this.codeEditor.setText(content); - // this.codeEditor.clearHistory(); - - // let info = CodeMirror.findModeByMIME(note.mime); - // if (!info) { - // // Switch back to plain text if CodeMirror does not have a mode for whatever MIME type we're editing. - // // To avoid inheriting a mode from a previously open code note. - // info = CodeMirror.findModeByMIME("text/plain"); - // } - - // this.codeEditor.setOption("mode", info.mime); - // CodeMirror.autoLoadMode(this.codeEditor, info.mode); + this.codeEditor.clearHistory(); } show() { diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts index 98548bab2..e3febc6bd 100644 --- a/packages/codemirror/src/index.ts +++ b/packages/codemirror/src/index.ts @@ -17,18 +17,21 @@ export default class CodeMirror extends EditorView { private config: EditorConfig; private languageCompartment: Compartment; + private historyCompartment: Compartment; constructor(config: EditorConfig) { const languageCompartment = new Compartment(); + const historyCompartment = new Compartment(); + let extensions = [ languageCompartment.of([]), + historyCompartment.of(history()), syntaxHighlighting(defaultHighlightStyle, { fallback: true }), highlightActiveLine(), highlightSelectionMatches(), bracketMatching(), lineNumbers(), indentUnit.of(" ".repeat(4)), - history(), keymap.of([ ...defaultKeymap, ...historyKeymap, @@ -58,6 +61,7 @@ export default class CodeMirror extends EditorView { }); this.config = config; this.languageCompartment = languageCompartment; + this.historyCompartment = historyCompartment; } #onDocumentUpdated(v: ViewUpdate) { @@ -80,6 +84,18 @@ export default class CodeMirror extends EditorView { }) } + /** + * Clears the history of undo/redo. Generally useful when changing to a new document. + */ + clearHistory() { + this.dispatch({ + effects: [ this.historyCompartment.reconfigure([]) ] + }); + this.dispatch({ + effects: [ this.historyCompartment.reconfigure(history())] + }); + } + async setMimeType(mime: string) { const newExtension = [];