Report an issue
Class

Document (engine/model)

@ckeditor/ckeditor5-engine/src/model/document

class

Document tree model describes all editable data in the editor. It may contain multiple root elements. For example, if the editor has multiple editable areas, each area will be represented by a separate root.

Filtering

Properties

  • differ : Differ

    readonly

    The model differ object. Its role is to buffer changes done on the model document and then calculate a diff of those changes.

  • graveyard : RootElement

    readonly

    The graveyard tree root. A document always has a graveyard root that stores removed nodes.

  • history : History

    readonly

    The document's history.

    Note: Be aware that deltas applied to the document might get removed or changed.

  • model : Model

    readonly

    The model that the document is a part of.

  • roots : Collection

    readonly

    A list of roots that are owned and managed by this document. Use createRoot and getRoot to manipulate it.

  • The selection done on this document.

  • version : Number

    readonly

    The document version. It starts from 0 and every operation increases the version number. It is used to ensure that operations are applied on a proper document version. If the base version does not match the document version, a model-document-applyOperation-wrong-version error is thrown.

  • _postFixers : Set

    private

    Post-fixer callbacks registered to the model document.

Methods

  • Creates an empty document instance with no roots (other than the graveyard root).

  • createRoot( [ elementName ], [ rootName ] ) → RootElement

    Creates a new root.

    Parameters

    [ elementName ] : String

    The element name. Defaults to '$root' which also has some basic schema defined ($blocks are allowed inside the $root). Make sure to define a proper schema if you use a different name.

    Defaults to '$root'

    [ rootName ] : String

    A unique root name.

    Defaults to 'main'

    Returns

    RootElement

    The created root.

  • delegate( events ) → EmitterMixinDelegateChain

    mixed

    Delegates selected events to another Emitter. For instance:

    emitterA.delegate( 'eventX' ).to( emitterB );
    emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );

    then eventX is delegated (fired by) emitterB and emitterC along with data:

    emitterA.fire( 'eventX', data );

    and eventY is delegated (fired by) emitterC along with data:

    emitterA.fire( 'eventY', data );

    Parameters

    events : String

    Event names that will be delegated to another emitter.

    Returns

    EmitterMixinDelegateChain
  • Removes all event listeners set by the document instance.

  • fire( eventOrInfo, [ args ] ) → *

    mixed

    Fires an event, executing all callbacks registered for it.

    The first parameter passed to callbacks is an EventInfo object, followed by the optional args provided in the fire() method call.

    Parameters

    eventOrInfo : String | EventInfo

    The name of the event or EventInfo object if event is delegated.

    [ args ] : *

    Additional arguments to be passed to the callbacks.

    Returns

    *

    By default the method returns undefined. However, the return value can be changed by listeners through modification of the evt.return's property (the event info is the first param of every callback).

  • getRoot( [ name ] ) → RootElement | null

    Returns a root by its name.

    Parameters

    [ name ] : String

    A unique root name.

    Defaults to 'main'

    Returns

    RootElement | null

    The root registered under a given name or null when there is no root with the given name.

  • getRootNames() → Array.<String>

    Returns an array with names of all roots (without the graveyard) added to the document.

    Returns

    Array.<String>

    Roots names.

  • listenTo( emitter, event, callback, [ options ] = { [options.priority] } )

    mixed

    Registers a callback function to be executed when an event is fired in a specific (emitter) object.

    Events can be grouped in namespaces using :. When namespaced event is fired, it additionally fires all callbacks for that namespace.

    // myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ).
    myEmitter.on( 'myGroup', genericCallback );
    myEmitter.on( 'myGroup:myEvent', specificCallback );
    
    // genericCallback is fired.
    myEmitter.fire( 'myGroup' );
    // both genericCallback and specificCallback are fired.
    myEmitter.fire( 'myGroup:myEvent' );
    // genericCallback is fired even though there are no callbacks for "foo".
    myEmitter.fire( 'myGroup:foo' );

    An event callback can stop the event and set the return value of the fire method.

    Parameters

    emitter : Emitter

    The object that fires the event.

    event : String

    The name of the event.

    callback : function

    The function to be called on event.

    [ options ] : Object

    Additional options.

    Properties
    [ options.priority ] : PriorityString | Number

    The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.

    Defaults to 'normal'

    Defaults to {}

  • off( event, callback )

    mixed

    Stops executing the callback on the given event. Shorthand for this.stopListening( this, event, callback ).

    Parameters

    event : String

    The name of the event.

    callback : function

    The function to stop being called.

  • on( event, callback, [ options ] = { [options.priority] } )

    mixed

    Registers a callback function to be executed when an event is fired.

    Shorthand for this.listenTo( this, event, callback, options ) (it makes the emitter listen on itself).

    Parameters

    event : String

    The name of the event.

    callback : function

    The function to be called on event.

    [ options ] : Object

    Additional options.

    Properties
    [ options.priority ] : PriorityString | Number

    The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.

    Defaults to 'normal'

    Defaults to {}

  • once( event, callback, [ options ] = { [options.priority] } )

    mixed

    Registers a callback function to be executed on the next time the event is fired only. This is similar to calling on followed by off in the callback.

    Parameters

    event : String

    The name of the event.

    callback : function

    The function to be called on event.

    [ options ] : Object

    Additional options.

    Properties
    [ options.priority ] : PriorityString | Number

    The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.

    Defaults to 'normal'

    Defaults to {}

  • registerPostFixer( postFixer )

    Used to register a post-fixer callback. A post-fixer mechanism guarantees that the features that listen to the model's change event will operate on a correct model state.

    An execution of a feature may lead to an incorrect document tree state. The callbacks are used to fix the document tree after it has changed. Post-fixers are fired just after all changes from the outermost change block were applied but before the change event is fired. If a post-fixer callback made a change, it should return true. When this happens, all post-fixers are fired again to check if something else should not be fixed in the new document tree state.

    As a parameter, a post-fixer callback receives a writer instance connected with the executed changes block. Thanks to that, all changes done by the callback will be added to the same batch (and undo step) as the original changes. This makes post-fixer changes transparent for the user.

    An example of a post-fixer is a callback that checks if all the data were removed from the editor. If so, the callback should add an empty paragraph so that the editor is never empty:

    document.registerPostFixer( writer => {
        const changes = document.differ.getChanges();
    
        // Check if the changes lead to an empty root in the editor.
        for ( const entry of changes ) {
            if ( entry.type == 'remove' && entry.position.root.isEmpty ) {
                writer.insertElement( 'paragraph', entry.position.root, 0 );
    
                // It is fine to return early, even if multiple roots would need to be fixed.
                // All post-fixers will be fired again, so if there are more empty roots, those will be fixed, too.
                return true;
            }
        }
    } );

    Parameters

    postFixer : function
  • stopDelegating( [ event ], [ emitter ] )

    mixed

    Stops delegating events. It can be used at different levels:

    • To stop delegating all events.
    • To stop delegating a specific event to all emitters.
    • To stop delegating a specific event to a specific emitter.

    Parameters

    [ event ] : String

    The name of the event to stop delegating. If omitted, stops it all delegations.

    [ emitter ] : Emitter

    (requires event) The object to stop delegating a particular event to. If omitted, stops delegation of event to all emitters.

  • stopListening( [ emitter ], [ event ], [ callback ] )

    mixed

    Stops listening for events. It can be used at different levels:

    • To stop listening to a specific callback.
    • To stop listening to a specific event.
    • To stop listening to all events fired by a specific object.
    • To stop listening to all events fired by all objects.

    Parameters

    [ emitter ] : Emitter

    The object to stop listening to. If omitted, stops it for all objects.

    [ event ] : String

    (Requires the emitter) The name of the event to stop listening to. If omitted, stops it for all events from emitter.

    [ callback ] : function

    (Requires the event) The function to be removed from the call list for the given event.

  • toJSON() → Object

    A custom toJSON() method to solve child-parent circular dependencies.

    Returns

    Object

    A clone of this object with the document property changed to a string.

  • _getDefaultRange() → Range

    protected

    Returns the default range for this selection. The default range is a collapsed range that starts and ends at the beginning of this selection's document default root.

    Returns

    Range
  • _getDefaultRoot() → RootElement

    protected

    Returns the default root for this document which is either the first root that was added to the document using createRoot or the graveyard root if no other roots were created.

    Returns

    RootElement

    The default root for this document.

  • _callPostFixers()

    private

    Performs post-fixer loops. Executes post-fixer callbacks as long as none of them has done any changes to the model.

  • _validateSelectionRange( range ) → Boolean

    private

    Checks whether a given range is a valid range for the document's selection.

    Parameters

    range : Range

    A range to check.

    Returns

    Boolean

    true if range is valid, false otherwise.

Events

  • change( eventInfo, batch )

    Fired after each enqueueChange() block or the outermost change() block was executed and the document was changed during that block's execution.

    The changes which this event will cover include:

    • document structure changes,
    • selection changes,
    • marker changes.

    If you want to be notified about all these changes, then simply listen to this event like this:

    model.document.on( 'change', () => {
        console.log( 'The Document has changed!' );
    } );

    If, however, you only want to be notified about structure changes, then check whether the differ contains any changes:

    model.document.on( 'change', () => {
        if ( model.document.differ.getChanges().length > 0 ) {
            console.log( 'The Document has changed!' );
        }
    } );

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    batch : Batch

    The batch that was used in the executed changes block.