fix(code): history of undo/redo preserved across notes

This commit is contained in:
Elian Doran 2025-05-11 12:23:09 +03:00
parent a4054dfa42
commit 9c8126016e
No known key found for this signature in database
2 changed files with 22 additions and 15 deletions

View File

@ -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() {

View File

@ -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 = [];