Trilium Frontend API
    Preparing search index...

    Class DowncastDispatcher

    The downcast dispatcher is a central point of downcasting (conversion from the model to the view), which is a process of reacting to changes in the model and firing a set of events. The callbacks listening to these events are called converters. The converters' role is to convert the model changes to changes in view (for example, adding view nodes or changing attributes on view elements).

    During the conversion process, downcast dispatcher fires events basing on the state of the model and prepares data for these events. It is important to understand that the events are connected with the changes done on the model, for example: "a node has been inserted" or "an attribute has changed". This is in contrary to upcasting (a view-to-model conversion) where you convert the view state (view nodes) to a model tree.

    The events are prepared basing on a diff created by the module:engine/model/differ~Differ Differ, which buffers them and then passes to the downcast dispatcher as a diff between the old model state and the new model state.

    Note that because the changes are converted, there is a need to have a mapping between the model structure and the view structure. To map positions and elements during the downcast (a model-to-view conversion), use module:engine/conversion/mapper~Mapper.

    Downcast dispatcher fires the following events for model tree changes:

    • module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert insert – If a range of nodes was inserted to the model tree.
    • module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:remove remove – If a range of nodes was removed from the model tree.
    • module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute attribute – If an attribute was added, changed or removed from a model node.

    For module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert insert and module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute attribute, the downcast dispatcher generates module:engine/conversion/modelconsumable~ModelConsumable consumables. These are used to have control over which changes have already been consumed. It is useful when some converters overwrite others or convert multiple changes (for example, it converts an insertion of an element and also converts that element's attributes during the insertion).

    Additionally, downcast dispatcher fires events for module:engine/model/markercollection~Marker marker changes:

    • module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:addMarker addMarker – If a marker was added.
    • module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:removeMarker removeMarker – If a marker was removed.

    Note that changing a marker is done through removing the marker from the old range and adding it to the new range, so both of these events are fired.

    Finally, a downcast dispatcher also handles firing events for the module:engine/model/selection model selection conversion:

    • module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:selection selection – Converts the selection from the model to the view.
    • module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute attribute – Fired for every selection attribute.
    • module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:addMarker addMarker – Fired for every marker that contains a selection.

    Unlike the model tree and the markers, the events for selection are not fired for changes but for a selection state.

    When providing custom listeners for a downcast dispatcher, remember to check whether a given change has not been module:engine/conversion/modelconsumable~ModelConsumable#consume consumed yet.

    When providing custom listeners for a downcast dispatcher, keep in mind that you should not stop the event. If you stop it, then the default converter at the lowest priority will not trigger the conversion of this node's attributes and child nodes.

    When providing custom listeners for a downcast dispatcher, remember to use the provided module:engine/view/downcastwriter~ViewDowncastWriter view downcast writer to apply changes to the view document.

    You can read more about conversion in the following guide:

    • {@glink framework/deep-dive/conversion/downcast Downcast conversion}

    An example of a custom converter for the downcast dispatcher:

    // You will convert inserting a "paragraph" model element into the model.
    downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
    // Remember to check whether the change has not been consumed yet and consume it.
    if ( !conversionApi.consumable.consume( data.item, 'insert' ) ) {
    return;
    }

    // Translate the position in the model to a position in the view.
    const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );

    // Create a <p> element that will be inserted into the view at the `viewPosition`.
    const viewElement = conversionApi.writer.createContainerElement( 'p' );

    // Bind the newly created view element to the model element so positions will map accordingly in the future.
    conversionApi.mapper.bindElements( data.item, viewElement );

    // Add the newly created view element to the view.
    conversionApi.writer.insert( viewPosition, viewElement );
    } );

    Hierarchy (View Summary)

    Index

    Constructors

    • Creates a downcast dispatcher instance.

      Parameters

      • conversionApi: Pick<DowncastConversionApi, "mapper" | "schema">

        Additional properties for an interface that will be passed to events fired by the downcast dispatcher.

      Returns DowncastDispatcher

      module:engine/conversion/downcastdispatcher~DowncastConversionApi

    Properties

    _conversionApi: Pick<DowncastConversionApi, "dispatcher" | "mapper" | "schema">

    A template for an interface passed by the dispatcher to the event callbacks.

    Methods

    • Starts a conversion of a model range and the provided markers.

      Parameters

      • range: ModelRange

        The inserted range.

      • markers: Map<string, ModelRange>

        The map of markers that should be down-casted.

      • writer: ViewDowncastWriter

        The view writer that should be used to modify the view document.

      • Optionaloptions: Record<string, unknown>

        Optional options object passed to convertionApi.options.

      Returns void

      insert

      attribute

      addMarker

    • Converts changes buffered in the given module:engine/model/differ~Differ model differ and fires conversion events based on it.

      Parameters

      • differ: Differ

        The differ object with buffered changes.

      • markers: MarkerCollection

        Markers related to the model fragment to convert.

      • writer: ViewDowncastWriter

        The view writer that should be used to modify the view document.

      Returns void

      insert

      remove

      attribute

      addMarker

      removeMarker

      reduceChanges

    • Delegates selected events to another module:utils/emittermixin~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

    • Fires an event, executing all callbacks registered for it.

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

      Type Parameters

      • TEvent extends BaseEvent

        The type describing the event. See module:utils/emittermixin~BaseEvent.

      Parameters

      • eventOrInfo: GetNameOrEventInfo<TEvent>

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

      • ...args: TEvent["args"]

        Additional arguments to be passed to the callbacks.

      Returns GetEventInfo<TEvent>["return"]

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

    • 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 module:utils/eventinfo~EventInfo#stop stop the event and set the module:utils/eventinfo~EventInfo#return return value of the #fire method.

      Type Parameters

      • TEvent extends BaseEvent

        The type describing the event. See module:utils/emittermixin~BaseEvent.

      Parameters

      Returns void

      BASE_EMITTER

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

      Parameters

      • event: string

        The name of the event.

      • callback: Function

        The function to stop being called.

      Returns void

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

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

      Type Parameters

      • TEvent extends BaseEvent

        The type descibing the event. See module:utils/emittermixin~BaseEvent.

      Parameters

      Returns void

    • 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.

      Type Parameters

      • TEvent extends BaseEvent

        The type descibing the event. See module:utils/emittermixin~BaseEvent.

      Parameters

      Returns void

    • 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

      • Optionalevent: string

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

      • Optionalemitter: Emitter

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

      Returns void

    • 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

      • Optionalemitter: Emitter

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

      • Optionalevent: string

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

      • Optionalcallback: Function

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

      Returns void

      BASE_STOP