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
2 changed files with 22 additions and 15 deletions

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