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#crashes
An 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 | TEditor
readonlymodule:watchdog/editorwatchdog~EditorWatchdog#editor
The current editor instance.
-
state : WatchdogState
inheritedmodule:watchdog/editorwatchdog~EditorWatchdog#state
Specifies 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 toinitializing
orcrashedPermanently
depending 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 | TEditor
internalreadonlymodule:watchdog/editorwatchdog~EditorWatchdog#_item
-
_creator : EditorWatchdogCreatorFunction<TEditor>
protectedmodule:watchdog/editorwatchdog~EditorWatchdog#_creator
-
_destructor : ( editor: Editor ) => Promise<unknown>
protectedmodule:watchdog/editorwatchdog~EditorWatchdog#_destructor
-
_config : EditorConfig | undefined
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_config
The editor configuration.
-
_data : EditorData | undefined
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_data
The latest saved editor data represented as a root name -> root data object.
-
_editables : Record<string, HTMLElement>
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_editables
The latest record of the editor editable elements. Used to restart the editor.
-
_editor : null | TEditor
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_editor
The current editor instance.
-
_elementOrData : string | HTMLElement | Record<string, string> | Record<string, HTMLElement> | undefined
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_elementOrData
The editor source element or data.
-
_excludedProps : Set<unknown> | undefined
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_excludedProps
-
_initUsingData : boolean
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_initUsingData
Specifies whether the editor was initialized using document data (
true
) or HTML elements (false
). -
_lastDocumentVersion : number | undefined
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_lastDocumentVersion
The last document version.
-
_lifecyclePromise : null | Promise<unknown>
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_lifecyclePromise
A 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#_throttledSave
Throttled save method. The
save()
method is called the specifiedsaveInterval
afterthrottledSave()
is called, unless a new action happens in the meantime.
Methods
-
constructor( Editor = { Editor.create }, watchdogConfig )
module:watchdog/editorwatchdog~EditorWatchdog#constructor
Type parameters
TEditor : extends [object Object] = Editor
Parameters
Editor : null | object
The editor class.
PropertiesEditor.create : Promise<TEditor>
watchdogConfig : WatchdogConfig
The watchdog plugin configuration.
Defaults to
{}
-
create( elementOrData, config, [ context ] ) → Promise<unknown>
module:watchdog/editorwatchdog~EditorWatchdog#create
Creates 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 : EditorConfig
The editor configuration.
Defaults to
...
[ context ] : Context
A context for the editor.
Returns
Promise<unknown>
-
destroy() → Promise<unknown>
module:watchdog/editorwatchdog~EditorWatchdog#destroy
Destroys 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 ) → void
inheritedmodule:watchdog/editorwatchdog~EditorWatchdog#off
Stops listening to the specified event name by removing the callback from event listeners.
Note that this method differs from the CKEditor 5's default
EventEmitterMixin
implementation.Parameters
eventName : keyof WatchdogEventMap
The event name.
callback : unknown
A callback which will be removed from event listeners.
Returns
void
-
on( eventName, callback ) → void
inheritedmodule:watchdog/editorwatchdog~EditorWatchdog#on
Starts 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
EventEmitterMixin
implementation.Type parameters
K : extends keyof WatchdogEventMap
Parameters
eventName : K
The event name.
callback : WatchdogEventCallback<K>
A callback which will be added to event listeners.
Returns
void
-
setCreator( creator ) → void
module:watchdog/editorwatchdog~EditorWatchdog#setCreator
Sets 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 ) );
Parameters
creator : EditorWatchdogCreatorFunction<TEditor>
Returns
void
-
setDestructor( destructor ) → void
module:watchdog/editorwatchdog~EditorWatchdog#setDestructor
Sets 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. } ); } );
Parameters
destructor : ( editor: Editor ) => Promise<unknown>
Returns
void
-
_isErrorComingFromThisItem( error ) → boolean
internalmodule:watchdog/editorwatchdog~EditorWatchdog#_isErrorComingFromThisItem
Traverses 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 ) → void
internalmodule:watchdog/editorwatchdog~EditorWatchdog#_setExcludedProperties
-
_fire( eventName, args ) → void
protectedinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#_fire
Fires an event with a given event name and arguments.
Note that this method differs from the CKEditor 5's default
EventEmitterMixin
implementation.Type parameters
K : extends keyof WatchdogEventMap
Parameters
eventName : K
args : WatchdogEventArgs<K>
Returns
void
-
_restart() → Promise<unknown>
protectedmodule:watchdog/editorwatchdog~EditorWatchdog#_restart
Restarts the editor instance. This method is called whenever an editor error occurs. It fires the
restart
event and changes the state toinitializing
.Returns
Promise<unknown>
Fires
-
_startErrorHandling() → void
protectedinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#_startErrorHandling
Starts error handling by attaching global error handlers.
Returns
void
-
_stopErrorHandling() → void
protectedinheritedmodule:watchdog/editorwatchdog~EditorWatchdog#_stopErrorHandling
-
_cloneEditorConfiguration( config ) → EditorConfig
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_cloneEditorConfiguration
-
_destroy() → Promise<unknown>
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_destroy
-
_getData() → EditorData
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_getData
Gets all data that is required to reinitialize editor instance.
Returns
-
_getEditables() → Record<string, HTMLElement>
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_getEditables
For each attached model root, returns its HTML editable element (if available).
Returns
Record<string, HTMLElement>
-
_save() → void
privatemodule:watchdog/editorwatchdog~EditorWatchdog#_save
Saves 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:error
Fired when a new
CKEditorError
error connected to the watchdog instance occurs and the watchdog will react to it.watchdog.on( 'error', ( evt, { error, causesRestart } ) => { console.log( 'An error occurred.' ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
<anonymous> : WatchdogErrorEventData
-
restart( eventInfo )
module:watchdog/editorwatchdog~EditorWatchdog#event:restart
Fired after the watchdog restarts the error in case of a crash.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
-
stateChange( eventInfo )
inheritedmodule:watchdog/editorwatchdog~EditorWatchdog#event:stateChange
Fired when the watchdog state changed.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.