mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-10 10:22:29 +08:00
fix(code): saving on change
This commit is contained in:
parent
ef312c9550
commit
3e5e9096d2
@ -1,5 +1,5 @@
|
||||
import TypeWidget from "./type_widget.js";
|
||||
import CodeMirror from "@triliumnext/codemirror";
|
||||
import CodeMirror, { type EditorConfig } from "@triliumnext/codemirror";
|
||||
|
||||
/**
|
||||
* An abstract {@link TypeWidget} which implements the CodeMirror editor, meant to be used as a parent for
|
||||
@ -26,18 +26,19 @@ export default class AbstractCodeTypeWidget extends TypeWidget {
|
||||
async #initEditor() {
|
||||
this.codeEditor = new CodeMirror({
|
||||
parent: this.$editor[0],
|
||||
...this.getExtraOpts()
|
||||
});
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Can be extended in derived classes to add extra options to the CodeMirror constructor. The options are appended
|
||||
// * at the end, so it is possible to override the default values introduced by the abstract editor as well.
|
||||
// *
|
||||
// * @returns the extra options to be passed to the CodeMirror constructor.
|
||||
// */
|
||||
// getExtraOpts(): Partial<CodeMirror> {
|
||||
// return {};
|
||||
// }
|
||||
/**
|
||||
* Can be extended in derived classes to add extra options to the CodeMirror constructor. The options are appended
|
||||
* at the end, so it is possible to override the default values introduced by the abstract editor as well.
|
||||
*
|
||||
* @returns the extra options to be passed to the CodeMirror constructor.
|
||||
*/
|
||||
getExtraOpts(): Partial<EditorConfig> {
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Called as soon as the CodeMirror library has been loaded and the editor was constructed. Can be extended in
|
||||
|
@ -7,6 +7,7 @@ import AbstractCodeTypeWidget from "./abstract_code_type_widget.js";
|
||||
import appContext from "../../components/app_context.js";
|
||||
import type { TouchBarItem } from "../../components/touch_bar.js";
|
||||
import { hasTouchBar } from "../../services/utils.js";
|
||||
import type { EditorConfig } from "@triliumnext/codemirror";
|
||||
|
||||
const TPL = /*html*/`
|
||||
<div class="note-detail-code note-detail-printable">
|
||||
@ -41,6 +42,12 @@ export default class EditableCodeTypeWidget extends AbstractCodeTypeWidget {
|
||||
super.doRender();
|
||||
}
|
||||
|
||||
getExtraOpts(): Partial<EditorConfig> {
|
||||
return {
|
||||
onContentChanged: () => this.spacedUpdate.scheduleUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
// getExtraOpts(): Partial<CodeMirrorOpts> {
|
||||
// return {
|
||||
// keyMap: options.is("vimKeymapEnabled") ? "vim" : "default",
|
||||
@ -52,10 +59,6 @@ export default class EditableCodeTypeWidget extends AbstractCodeTypeWidget {
|
||||
// };
|
||||
// }
|
||||
|
||||
onEditorInitialized() {
|
||||
// this.codeEditor.on("change", () => this.spacedUpdate.scheduleUpdate());
|
||||
}
|
||||
|
||||
async doRefresh(note: FNote) {
|
||||
const blob = await this.note?.getBlob();
|
||||
|
||||
@ -72,8 +75,7 @@ export default class EditableCodeTypeWidget extends AbstractCodeTypeWidget {
|
||||
|
||||
getData() {
|
||||
return {
|
||||
// content: this.codeEditor.getValue()
|
||||
content: ""
|
||||
content: this.codeEditor.getText()
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user