mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-11 11:02:27 +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 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
|
* 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() {
|
async #initEditor() {
|
||||||
this.codeEditor = new CodeMirror({
|
this.codeEditor = new CodeMirror({
|
||||||
parent: this.$editor[0],
|
parent: this.$editor[0],
|
||||||
|
...this.getExtraOpts()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Can be extended in derived classes to add extra options to the CodeMirror constructor. The options are appended
|
* 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.
|
* 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.
|
* @returns the extra options to be passed to the CodeMirror constructor.
|
||||||
// */
|
*/
|
||||||
// getExtraOpts(): Partial<CodeMirror> {
|
getExtraOpts(): Partial<EditorConfig> {
|
||||||
// return {};
|
return {};
|
||||||
// }
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called as soon as the CodeMirror library has been loaded and the editor was constructed. Can be extended in
|
* 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 appContext from "../../components/app_context.js";
|
||||||
import type { TouchBarItem } from "../../components/touch_bar.js";
|
import type { TouchBarItem } from "../../components/touch_bar.js";
|
||||||
import { hasTouchBar } from "../../services/utils.js";
|
import { hasTouchBar } from "../../services/utils.js";
|
||||||
|
import type { EditorConfig } from "@triliumnext/codemirror";
|
||||||
|
|
||||||
const TPL = /*html*/`
|
const TPL = /*html*/`
|
||||||
<div class="note-detail-code note-detail-printable">
|
<div class="note-detail-code note-detail-printable">
|
||||||
@ -41,6 +42,12 @@ export default class EditableCodeTypeWidget extends AbstractCodeTypeWidget {
|
|||||||
super.doRender();
|
super.doRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getExtraOpts(): Partial<EditorConfig> {
|
||||||
|
return {
|
||||||
|
onContentChanged: () => this.spacedUpdate.scheduleUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// getExtraOpts(): Partial<CodeMirrorOpts> {
|
// getExtraOpts(): Partial<CodeMirrorOpts> {
|
||||||
// return {
|
// return {
|
||||||
// keyMap: options.is("vimKeymapEnabled") ? "vim" : "default",
|
// 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) {
|
async doRefresh(note: FNote) {
|
||||||
const blob = await this.note?.getBlob();
|
const blob = await this.note?.getBlob();
|
||||||
|
|
||||||
@ -72,8 +75,7 @@ export default class EditableCodeTypeWidget extends AbstractCodeTypeWidget {
|
|||||||
|
|
||||||
getData() {
|
getData() {
|
||||||
return {
|
return {
|
||||||
// content: this.codeEditor.getValue()
|
content: this.codeEditor.getText()
|
||||||
content: ""
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,15 +1,45 @@
|
|||||||
import { defaultKeymap } from "@codemirror/commands";
|
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 {
|
export default class CodeMirror extends EditorView {
|
||||||
constructor(config: EditorViewConfig) {
|
|
||||||
super({
|
private config: EditorConfig;
|
||||||
...config,
|
|
||||||
extensions: [
|
constructor(config: EditorConfig) {
|
||||||
|
let extensions = [
|
||||||
keymap.of(defaultKeymap),
|
keymap.of(defaultKeymap),
|
||||||
lineNumbers()
|
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
|
||||||
});
|
});
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
#onDocumentUpdated(v: ViewUpdate) {
|
||||||
|
if (v.docChanged) {
|
||||||
|
this.config.onContentChanged?.();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getText() {
|
||||||
|
return this.state.doc.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
setText(content: string) {
|
setText(content: string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user