Sign up (with export icon)

EditorWatchdog

Api-class icon class

A watchdog for CKEditor 5 editors.

See the Watchdog feature guide to learn the rationale behind it and how to use it.

Type parameters

Properties

  • Chevron-right icon

    crashes : Array<object>
    readonlyinherited

    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,
  • Chevron-right icon

    editor : null | TEditor
    readonly

    The current editor instance.

  • Chevron-right icon

    state : WatchdogState
    inherited

    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 to initializing or crashedPermanently 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 calling watchdog.destroy().
  • _item : null | TEditor
    internalreadonly

  • Chevron-right icon

  • Chevron-right icon

    _destructor : ( editor: Editor ) => Promise<unknown>
    protected

  • Chevron-right icon

    _config : EditorConfig | undefined
    Lock icon private

    The editor configuration.

  • Chevron-right icon

    _data : EditorData | undefined
    Lock icon private

    The latest saved editor data represented as a root name -> root data object.

  • Chevron-right icon

    _editables : Record<string, HTMLElement>
    Lock icon private

    The latest record of the editor editable elements. Used to restart the editor.

  • Chevron-right icon

    _editor : null | TEditor
    Lock icon private

    The current editor instance.

  • Chevron-right icon

    _elementOrData : string | HTMLElement | Record<string, string> | Record<string, HTMLElement> | undefined
    Lock icon private

    The editor source element or data.

  • _excludedProps : Set<unknown> | undefined
    Lock icon private

  • Chevron-right icon

    _initUsingData : boolean
    Lock icon private

    Specifies whether the editor was initialized using document data (true) or HTML elements (false).

  • Chevron-right icon

    _lastDocumentVersion : number | undefined
    Lock icon private

    The last document version.

  • Chevron-right icon

    _lifecyclePromise : null | Promise<unknown>
    Lock icon private

    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.

  • Chevron-right icon

    _throttledSave : DebouncedFunc<() => void>
    Lock icon private

    Throttled save method. The save() method is called the specified saveInterval after throttledSave() is called, unless a new action happens in the meantime.

Methods

  • Chevron-right icon

    constructor( Editor = { Editor.create }, watchdogConfig )

    Type parameters

    TEditor : extends [object Object] = Editor

    Parameters

    Editor : null | object

    The editor class.

    Properties
    Editor.create : Promise<TEditor>
    watchdogConfig : WatchdogConfig

    The watchdog plugin configuration.

    Defaults to {}

  • Chevron-right icon

    create( elementOrData, config, [ context ] ) → Promise<unknown>

    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>
  • Chevron-right icon

    destroy() → Promise<unknown>

    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 to destroyed.

    Returns

    Promise<unknown>
  • Chevron-right icon

    off( eventName, callback ) → void
    inherited

    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
  • Chevron-right icon

    on( eventName, callback ) → void
    inherited

    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
  • Chevron-right icon

    setCreator( creator ) → void

    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 ) );
    
    Copy code

    Parameters

    creator : EditorWatchdogCreatorFunction<TEditor>

    Returns

    void
  • Chevron-right icon

    setDestructor( destructor ) → void

    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.
    		} );
    } );
    
    Copy code

    Parameters

    destructor : ( editor: Editor ) => Promise<unknown>

    Returns

    void
  • Chevron-right icon

    _isErrorComingFromThisItem( error ) → boolean
    internal

    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
  • Chevron-right icon

    _setExcludedProperties( props ) → void
    internal

    Parameters

    props : Set<unknown>

    Returns

    void
  • Chevron-right icon

    _fire( eventName, args ) → void
    protectedinherited

    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
  • Chevron-right icon

    _restart() → Promise<unknown>
    protected

    Restarts the editor instance. This method is called whenever an editor error occurs. It fires the restart event and changes the state to initializing.

    Returns

    Promise<unknown>

    Fires

  • Chevron-right icon

    _startErrorHandling() → void
    protectedinherited

    Starts error handling by attaching global error handlers.

    Returns

    void
  • Chevron-right icon

    _stopErrorHandling() → void
    protectedinherited

    Stops error handling by detaching global error handlers.

    Returns

    void
  • Chevron-right icon

    _cloneEditorConfiguration( config ) → EditorConfig
    Lock icon private

    Clones the editor configuration.

    Parameters

    config : EditorConfig

    Returns

    EditorConfig
  • Chevron-right icon

    _destroy() → Promise<unknown>
    Lock icon private

    Returns

    Promise<unknown>
  • Chevron-right icon

    _getData() → EditorData
    Lock icon private

    Gets all data that is required to reinitialize editor instance.

    Returns

    EditorData
  • Chevron-right icon

    _getEditables() → Record<string, HTMLElement>
    Lock icon private

    For each attached model root, returns its HTML editable element (if available).

    Returns

    Record<string, HTMLElement>
  • Chevron-right icon

    _save() → void
    Lock icon private

    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

  • Chevron-right icon

    error( eventInfo, <anonymous> )
    inherited

    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.' );
    } );
    
    Copy code

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    <anonymous> : WatchdogErrorEventData
  • Chevron-right icon

    restart( eventInfo )

    Fired after the watchdog restarts the error in case of a crash.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

  • Chevron-right icon

    stateChange( eventInfo )
    inherited

    Fired when the watchdog state changed.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.