EditorWatchdog
A watchdog for CKEditor 5 editors.
See the Watchdog feature guide to learn the rationale behind it and how to use it.
Type parameters
TEditor : extends Editor
Properties
crashes : Array<object>readonlyinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#crashesAn array of crashes saved as an object with the following properties:
message:String,stack:String,date:Number,filename:String | undefined,lineno:Number | undefined,colno:Number | undefined,
editor : null | TEditorreadonlymodule:watchdog/editorwatchdog~EditorWatchdog#editorThe current editor instance.
state : WatchdogStateinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#stateSpecifies the state of the item watched by the watchdog. The state can be one of the following values:
initializing– Before the first initialization, and after crashes, before the item is ready.ready– A state when the user can interact with the item.crashed– A state when an error occurs. It quickly changes toinitializingorcrashedPermanentlydepending on how many and how frequent errors have been caught recently.crashedPermanently– A state when the watchdog stops reacting to errors and keeps the item it is watching crashed,destroyed– A state when the item is manually destroyed by the user after callingwatchdog.destroy().
_item : null | TEditorinternalreadonlymodule:watchdog/editorwatchdog~EditorWatchdog#_item_creator : EditorWatchdogCreatorFunction<TEditor>protectedmodule:watchdog/editorwatchdog~EditorWatchdog#_creator_destructor : ( editor: Editor ) => Promise<unknown>protectedmodule:watchdog/editorwatchdog~EditorWatchdog#_destructor_config : EditorConfig | undefinedprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_configThe editor configuration.
_data : EditorData | undefinedprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_dataThe latest saved editor data represented as a root name -> root data object.
_editables : Record<string, HTMLElement>privatemodule:watchdog/editorwatchdog~EditorWatchdog#_editablesThe latest record of the editor editable elements. Used to restart the editor.
_editor : null | TEditorprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_editorThe current editor instance.
_elementOrData : string | HTMLElement | Record<string, string> | Record<string, HTMLElement> | undefinedprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_elementOrDataThe editor source element or data.
_excludedProps : Set<unknown> | undefinedprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_excludedProps_initUsingData : booleanprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_initUsingDataSpecifies whether the editor was initialized using document data (
true) or HTML elements (false)._lastDocumentVersion : number | undefinedprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_lastDocumentVersionThe last document version.
_lifecyclePromise : null | Promise<unknown>privatemodule:watchdog/editorwatchdog~EditorWatchdog#_lifecyclePromiseA promise associated with the life cycle of the editor (creation or destruction processes).
It is used to prevent the initialization of the editor if the previous instance has not been destroyed yet, and conversely, to prevent the destruction of the editor if it has not been initialized.
_throttledSave : DebouncedFunc<() => void>privatemodule:watchdog/editorwatchdog~EditorWatchdog#_throttledSaveThrottled save method. The
save()method is called the specifiedsaveIntervalafterthrottledSave()is called, unless a new action happens in the meantime.
Methods
constructor( Editor = { Editor.create }, watchdogConfig )module:watchdog/editorwatchdog~EditorWatchdog#constructorType parameters
TEditor : extends Editor
Parameters
Editor : null | objectThe editor class.
PropertiesEditor.create : Promise<TEditor>
watchdogConfig : WatchdogConfigThe watchdog plugin configuration.
Defaults to
{}
create( elementOrData, config, [ context ] ) → Promise<unknown>module:watchdog/editorwatchdog~EditorWatchdog#createCreates the editor instance and keeps it running, using the defined creator and destructor.
Parameters
elementOrData : string | HTMLElement | Record<string, string> | Record<string, HTMLElement>The editor source element or the editor data.
Defaults to
...config : EditorConfigThe editor configuration.
Defaults to
...[ context ] : ContextA context for the editor.
Returns
Promise<unknown>
destroy() → Promise<unknown>module:watchdog/editorwatchdog~EditorWatchdog#destroyDestroys the watchdog and the current editor instance. It fires the callback registered in
setDestructor()and uses it to destroy the editor instance. It also sets the state todestroyed.Returns
Promise<unknown>
off( eventName, callback ) → voidinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#offStops listening to the specified event name by removing the callback from event listeners.
Note that this method differs from the CKEditor 5's default
EventEmitterMixinimplementation.Parameters
eventName : keyof WatchdogEventMapThe event name.
callback : unknownA callback which will be removed from event listeners.
Returns
void
on( eventName, callback ) → voidinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#onStarts listening to a specific event name by registering a callback that will be executed whenever an event with a given name fires.
Note that this method differs from the CKEditor 5's default
EventEmitterMixinimplementation.Type parameters
K : extends keyof WatchdogEventMap
Parameters
eventName : KThe event name.
callback : WatchdogEventCallback<K>A callback which will be added to event listeners.
Returns
void
setCreator( creator ) → voidmodule:watchdog/editorwatchdog~EditorWatchdog#setCreatorSets the function that is responsible for the editor creation. It expects a function that should return a promise.
watchdog.setCreator( ( element, config ) => ClassicEditor.create( element, config ) );Copy codeParameters
creator : EditorWatchdogCreatorFunction<TEditor>
Returns
void
setDestructor( destructor ) → voidmodule:watchdog/editorwatchdog~EditorWatchdog#setDestructorSets the function that is responsible for the editor destruction. Overrides the default destruction function, which destroys only the editor instance. It expects a function that should return a promise or
undefined.watchdog.setDestructor( editor => { // Do something before the editor is destroyed. return editor .destroy() .then( () => { // Do something after the editor is destroyed. } ); } );Copy codeParameters
destructor : ( editor: Editor ) => Promise<unknown>
Returns
void
_isErrorComingFromThisItem( error ) → booleaninternalmodule:watchdog/editorwatchdog~EditorWatchdog#_isErrorComingFromThisItemTraverses the error context and the current editor to find out whether these structures are connected to each other via properties.
Parameters
error : CKEditorError
Returns
boolean
_setExcludedProperties( props ) → voidinternalmodule:watchdog/editorwatchdog~EditorWatchdog#_setExcludedProperties_fire( eventName, args ) → voidprotectedinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#_fireFires an event with a given event name and arguments.
Note that this method differs from the CKEditor 5's default
EventEmitterMixinimplementation.Type parameters
K : extends keyof WatchdogEventMap
Parameters
eventName : Kargs : WatchdogEventArgs<K>
Returns
void
_restart() → Promise<unknown>protectedmodule:watchdog/editorwatchdog~EditorWatchdog#_restartRestarts the editor instance. This method is called whenever an editor error occurs. It fires the
restartevent and changes the state toinitializing.Returns
Promise<unknown>
Fires
_startErrorHandling() → voidprotectedinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#_startErrorHandling_stopErrorHandling() → voidprotectedinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#_stopErrorHandling_cloneEditorConfiguration( config ) → EditorConfigprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_cloneEditorConfiguration_destroy() → Promise<unknown>privatemodule:watchdog/editorwatchdog~EditorWatchdog#_destroy_getData() → EditorDataprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_getDataGets all data that is required to reinitialize editor instance.
Returns
_getEditables() → Record<string, HTMLElement>privatemodule:watchdog/editorwatchdog~EditorWatchdog#_getEditablesFor each attached model root, returns its HTML editable element (if available).
Returns
Record<string, HTMLElement>
_save() → voidprivatemodule:watchdog/editorwatchdog~EditorWatchdog#_saveSaves the editor data, so it can be restored after the crash even if the data cannot be fetched at the moment of the crash.
Returns
void
Events
error( eventInfo, <anonymous> )inheritedmodule:watchdog/editorwatchdog~EditorWatchdog#event:errorFired when a new
CKEditorErrorerror connected to the watchdog instance occurs and the watchdog will react to it.watchdog.on( 'error', ( evt, { error, causesRestart } ) => { console.log( 'An error occurred.' ); } );Copy codeParameters
eventInfo : EventInfoAn object containing information about the fired event.
<anonymous> : WatchdogErrorEventData
restart( eventInfo )module:watchdog/editorwatchdog~EditorWatchdog#event:restartFired after the watchdog restarts the error in case of a crash.
Parameters
eventInfo : EventInfoAn object containing information about the fired event.
stateChange( eventInfo )inheritedmodule:watchdog/editorwatchdog~EditorWatchdog#event:stateChangeFired when the watchdog state changed.
Parameters
eventInfo : EventInfoAn object containing information about the fired event.