Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(react/type_widget): port watchdog state change
Elian Doran1e323de01b3e
diff --git a/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsxindex bd1cd21588..61fcd5d15e 100644--- a/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx+++ b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx@@ -7,9 +7,10 @@ interface CKEditorWithWatchdogProps extends Pick<HTMLProps<HTMLDivElement>, "clawatchdogConfig?: WatchdogConfig;buildEditorOpts: Omit<BuildEditorOptions, "isClassicEditor">;onNotificationWarning?: (evt: any, data: any) => void;+ onWatchdogStateChange?: (watchdog: EditorWatchdog<any>) => void;}-export default function CKEditorWithWatchdog({ className, tabIndex, isClassicEditor, watchdogConfig, buildEditorOpts, onNotificationWarning }: CKEditorWithWatchdogProps) {+export default function CKEditorWithWatchdog({ className, tabIndex, isClassicEditor, watchdogConfig, buildEditorOpts, onNotificationWarning, onWatchdogStateChange }: CKEditorWithWatchdogProps) {const containerRef = useRef<HTMLDivElement>(null);useEffect(() => {@@ -29,6 +30,10 @@ export default function CKEditorWithWatchdog({ className, tabIndex, isClassicEdireturn editor;});+ if (onWatchdogStateChange) {+ watchdog.on("stateChange", () => onWatchdogStateChange(watchdog));+ }+watchdog.create(container);return () => watchdog.destroy();diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex 185d05e174..e74c265bd9 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -1,9 +1,11 @@+import dialog from "../../../services/dialog";import toast from "../../../services/toast";import { isMobile } from "../../../services/utils";import { useNoteLabel, useTriliumOption } from "../../react/hooks";import { TypeWidgetProps } from "../type_widget";import CKEditorWithWatchdog from "./CKEditorWithWatchdog";import "./EditableText.css";+import type { EditorWatchdog } from "@triliumnext/ckeditor5";/*** The editor can operate into two distinct modes:@@ -34,11 +36,28 @@ export default function EditableText({ note }: TypeWidgetProps) {forceGplLicense: false,}}onNotificationWarning={onNotificationWarning}+ onWatchdogStateChange={onWatchdogStateChange}/></div>)}+function onWatchdogStateChange(watchdog: EditorWatchdog) {+ const currentState = watchdog.state;+ logInfo(`CKEditor state changed to ${currentState}`);++ if (!["crashed", "crashedPermanently"].includes(currentState)) {+ return;+ }++ logError(`CKEditor crash logs: ${JSON.stringify(watchdog.crashes, null, 4)}`);++ if (currentState === "crashedPermanently") {+ dialog.info(`Editing component keeps crashing. Please try restarting Trilium. If problem persists, consider creating a bug report.`);+ watchdog.editor?.enableReadOnlyMode("crashed-editor");+ }+}+function onNotificationWarning(data, evt) {const title = data.title;const message = data.message.message;diff --git a/apps/client/src/widgets/type_widgets_old/editable_text.ts b/apps/client/src/widgets/type_widgets_old/editable_text.tsindex 5c33a36461..6c910a35a5 100644--- a/apps/client/src/widgets/type_widgets_old/editable_text.ts+++ b/apps/client/src/widgets/type_widgets_old/editable_text.ts@@ -37,23 +37,6 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {}async initEditor() {- this.watchdog.on("stateChange", () => {- const currentState = this.watchdog.state;- logInfo(`CKEditor state changed to ${currentState}`);-- if (!["crashed", "crashedPermanently"].includes(currentState)) {- return;- }-- logError(`CKEditor crash logs: ${JSON.stringify(this.watchdog.crashes, null, 4)}`);-- if (currentState === "crashedPermanently") {- dialogService.info(`Editing component keeps crashing. Please try restarting Trilium. If problem persists, consider creating a bug report.`);-- this.watchdog.editor?.enableReadOnlyMode("crashed-editor");- }- });-this.watchdog.setCreator(async (_, editorConfig) => {logInfo("Creating new CKEditor");diff --git a/packages/ckeditor5/src/index.ts b/packages/ckeditor5/src/index.tsindex b20fa0db5d..1f7be24e4b 100644--- a/packages/ckeditor5/src/index.ts+++ b/packages/ckeditor5/src/index.ts@@ -5,7 +5,7 @@ import { BalloonEditor, DecoupledEditor, FindAndReplaceEditing, FindCommand } frimport "./translation_overrides.js";export { EditorWatchdog } from "ckeditor5";export { PREMIUM_PLUGINS } from "./plugins.js";-export type { EditorConfig, MentionFeed, MentionFeedObjectItem, ModelNode, ModelPosition, ModelElement, WatchdogConfig } from "ckeditor5";+export type { EditorConfig, MentionFeed, MentionFeedObjectItem, ModelNode, ModelPosition, ModelElement, WatchdogConfig, WatchdogState } from "ckeditor5";export type { TemplateDefinition } from "ckeditor5-premium-features";export { default as buildExtraCommands } from "./extra_slash_commands.js";