mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 10:55:55 +01:00
fix(code): saving on change
This commit is contained in:
@@ -1,15 +1,45 @@
|
||||
import { defaultKeymap } from "@codemirror/commands";
|
||||
import { EditorView, keymap, lineNumbers, type EditorViewConfig } from "@codemirror/view";
|
||||
import { EditorView, keymap, lineNumbers, ViewUpdate, type EditorViewConfig } from "@codemirror/view";
|
||||
|
||||
type ContentChangedListener = () => void;
|
||||
|
||||
export interface EditorConfig extends EditorViewConfig {
|
||||
onContentChanged?: ContentChangedListener;
|
||||
}
|
||||
|
||||
export default class CodeMirror extends EditorView {
|
||||
constructor(config: EditorViewConfig) {
|
||||
|
||||
private config: EditorConfig;
|
||||
|
||||
constructor(config: EditorConfig) {
|
||||
let extensions = [
|
||||
keymap.of(defaultKeymap),
|
||||
lineNumbers()
|
||||
];
|
||||
|
||||
if (Array.isArray(config.extensions)) {
|
||||
extensions = [...extensions, ...config.extensions];
|
||||
}
|
||||
|
||||
if (config.onContentChanged) {
|
||||
extensions.push(EditorView.updateListener.of((v) => this.#onDocumentUpdated(v)));
|
||||
}
|
||||
|
||||
super({
|
||||
...config,
|
||||
extensions: [
|
||||
keymap.of(defaultKeymap),
|
||||
lineNumbers()
|
||||
]
|
||||
extensions
|
||||
});
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
#onDocumentUpdated(v: ViewUpdate) {
|
||||
if (v.docChanged) {
|
||||
this.config.onContentChanged?.();
|
||||
}
|
||||
}
|
||||
|
||||
getText() {
|
||||
return this.state.doc.toString();
|
||||
}
|
||||
|
||||
setText(content: string) {
|
||||
|
||||
Reference in New Issue
Block a user