chore(react/type_widget): react to content language changes

This commit is contained in:
Elian Doran 2025-09-22 12:49:03 +03:00
parent 8a185262fb
commit 2eab8b92d5
No known key found for this signature in database
3 changed files with 8 additions and 45 deletions

View File

@ -1,20 +1,19 @@
import { HTMLProps, RefObject, useEffect, useRef, useState } from "preact/compat"; import { HTMLProps, RefObject, useEffect, useRef, useState } from "preact/compat";
import { PopupEditor, ClassicEditor, EditorWatchdog, type WatchdogConfig, CKTextEditor } from "@triliumnext/ckeditor5"; import { PopupEditor, ClassicEditor, EditorWatchdog, type WatchdogConfig, CKTextEditor } from "@triliumnext/ckeditor5";
import { buildConfig, BuildEditorOptions } from "./config"; import { buildConfig, BuildEditorOptions } from "./config";
import { Editor } from "tabulator-tables";
interface CKEditorWithWatchdogProps extends Pick<HTMLProps<HTMLDivElement>, "className" | "tabIndex"> { interface CKEditorWithWatchdogProps extends Pick<HTMLProps<HTMLDivElement>, "className" | "tabIndex"> {
content?: string; content: string | undefined;
contentLanguage: string | null | undefined;
isClassicEditor?: boolean; isClassicEditor?: boolean;
watchdogRef: RefObject<EditorWatchdog>; watchdogRef: RefObject<EditorWatchdog>;
watchdogConfig?: WatchdogConfig; watchdogConfig?: WatchdogConfig;
buildEditorOpts: Omit<BuildEditorOptions, "isClassicEditor">;
onNotificationWarning?: (evt: any, data: any) => void; onNotificationWarning?: (evt: any, data: any) => void;
onWatchdogStateChange?: (watchdog: EditorWatchdog<any>) => void; onWatchdogStateChange?: (watchdog: EditorWatchdog<any>) => void;
onChange: () => void; onChange: () => void;
} }
export default function CKEditorWithWatchdog({ content, className, tabIndex, isClassicEditor, watchdogRef: externalWatchdogRef, watchdogConfig, buildEditorOpts, onNotificationWarning, onWatchdogStateChange, onChange }: CKEditorWithWatchdogProps) { export default function CKEditorWithWatchdog({ content, contentLanguage, className, tabIndex, isClassicEditor, watchdogRef: externalWatchdogRef, watchdogConfig, buildEditorOpts, onNotificationWarning, onWatchdogStateChange, onChange }: CKEditorWithWatchdogProps) {
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const watchdogRef = useRef<EditorWatchdog>(null); const watchdogRef = useRef<EditorWatchdog>(null);
const [ editor, setEditor ] = useState<CKTextEditor>(); const [ editor, setEditor ] = useState<CKTextEditor>();
@ -27,8 +26,9 @@ export default function CKEditorWithWatchdog({ content, className, tabIndex, isC
externalWatchdogRef.current = watchdog; externalWatchdogRef.current = watchdog;
watchdog.setCreator(async () => { watchdog.setCreator(async () => {
const editor = await buildEditor(container, !!isClassicEditor, { const editor = await buildEditor(container, !!isClassicEditor, {
...buildEditorOpts, forceGplLicense: false,
isClassicEditor: !!isClassicEditor isClassicEditor: !!isClassicEditor,
contentLanguage: contentLanguage ?? null
}); });
setEditor(editor); setEditor(editor);
@ -43,7 +43,7 @@ export default function CKEditorWithWatchdog({ content, className, tabIndex, isC
watchdog.create(container); watchdog.create(container);
return () => watchdog.destroy(); return () => watchdog.destroy();
}, []); }, [ contentLanguage ]);
// React to content changes. // React to content changes.
useEffect(() => editor?.setData(content ?? ""), [ editor, content ]); useEffect(() => editor?.setData(content ?? ""), [ editor, content ]);

View File

@ -47,6 +47,7 @@ export default function EditableText({ note }: TypeWidgetProps) {
{note && <CKEditorWithWatchdog {note && <CKEditorWithWatchdog
className="note-detail-editable-text-editor use-tn-links" tabIndex={300} className="note-detail-editable-text-editor use-tn-links" tabIndex={300}
content={content} content={content}
contentLanguage={language}
isClassicEditor={isClassicEditor} isClassicEditor={isClassicEditor}
watchdogRef={watchdogRef} watchdogRef={watchdogRef}
watchdogConfig={{ watchdogConfig={{
@ -57,10 +58,6 @@ export default function EditableText({ note }: TypeWidgetProps) {
// A minimum number of milliseconds between saving the editor data internally (defaults to 5000). Note that for large documents, this might impact the editor performance. // A minimum number of milliseconds between saving the editor data internally (defaults to 5000). Note that for large documents, this might impact the editor performance.
saveInterval: 5000 saveInterval: 5000
}} }}
buildEditorOpts={{
contentLanguage: language ?? null,
forceGplLicense: false,
}}
onNotificationWarning={onNotificationWarning} onNotificationWarning={onNotificationWarning}
onWatchdogStateChange={onWatchdogStateChange} onWatchdogStateChange={onWatchdogStateChange}
onChange={() => spacedUpdate.scheduleUpdate()} onChange={() => spacedUpdate.scheduleUpdate()}

View File

@ -92,18 +92,6 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
await this.createEditor(); await this.createEditor();
} }
async doRefresh(note: FNote) {
const blob = await note.getBlob();
await this.spacedUpdate.allowUpdateWithoutChange(async () => {
const data = blob?.content || "";
const newContentLanguage = this.note?.getLabelValue("language");
if (this.contentLanguage !== newContentLanguage) {
await this.reinitializeWithData(data);
}
});
}
focus() { focus() {
const editor = this.watchdog.editor; const editor = this.watchdog.editor;
if (editor) { if (editor) {
@ -130,14 +118,6 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
return this.watchdog?.editor; return this.watchdog?.editor;
} }
cleanup() {
if (this.watchdog?.editor) {
this.spacedUpdate.allowUpdateWithoutChange(() => {
this.watchdog.editor?.setData("");
});
}
}
insertDateTimeToTextCommand() { insertDateTimeToTextCommand() {
const date = new Date(); const date = new Date();
const customDateTimeFormat = options.get("customDateTimeFormat"); const customDateTimeFormat = options.get("customDateTimeFormat");
@ -323,16 +303,6 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
this.refreshIncludedNote(this.$editor, noteId); this.refreshIncludedNote(this.$editor, noteId);
} }
async reinitializeWithData(data: string) {
if (!this.watchdog) {
return;
}
this.watchdog.destroy();
await this.createEditor();
this.watchdog.editor?.setData(data);
}
async reinitialize() { async reinitialize() {
const data = this.watchdog.editor?.getData(); const data = this.watchdog.editor?.getData();
await this.reinitializeWithData(data ?? ""); await this.reinitializeWithData(data ?? "");
@ -342,10 +312,6 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
await this.reinitialize(); await this.reinitialize();
} }
async onLanguageChanged() {
await this.reinitialize();
}
async entitiesReloadedEvent(e: EventData<"entitiesReloaded">) { async entitiesReloadedEvent(e: EventData<"entitiesReloaded">) {
await super.entitiesReloadedEvent(e); await super.entitiesReloadedEvent(e);