Trilium Frontend API
    Preparing search index...

    Class DataController

    Controller for the data pipeline. The data pipeline controls how data is retrieved from the document and set inside it. Hence, the controller features two methods which allow to ~DataController#get get and ~DataController#set set data of the ~DataController#model model using the given:

    • module:engine/dataprocessor/dataprocessor~DataProcessor data processor,
    • downcast converters,
    • upcast converters.

    An instance of the data controller is always available in the module:core/editor/editor~Editor#data editor.data property:

    editor.data.get( { rootName: 'customRoot' } ); // -> '<p>Hello!</p>'
    

    Hierarchy (View Summary)

    Index

    Constructors

    • Creates a data controller instance.

      Parameters

      Returns DataController

    Properties

    downcastDispatcher: DowncastDispatcher

    Downcast dispatcher used by the #get get method. Downcast converters should be attached to it.

    htmlProcessor: HtmlDataProcessor

    Data processor used specifically for HTML conversion.

    mapper: Mapper

    Mapper used for the conversion. It has no permanent bindings, because these are created while getting data and are cleared directly after the data are converted. However, the mapper is defined as a class property, because it needs to be passed to the DowncastDispatcher as a conversion API.

    model: Model

    Data model.

    processor: DataProcessor

    Data processor used during the conversion. Same instance as #htmlProcessor by default. Can be replaced at run time to handle different format, e.g. XML or Markdown.

    stylesProcessor: StylesProcessor

    Styles processor used during the conversion.

    upcastDispatcher: UpcastDispatcher

    Upcast dispatcher used by the #set set method. Upcast converters should be attached to it.

    viewDocument: ViewDocument

    The view document used by the data controller.

    Methods

    • Adds the style processor normalization rules.

      You can implement your own rules as well as use one of the available processor rules:

      • background: module:engine/view/styles/background~addBackgroundStylesRules
      • border: module:engine/view/styles/border~addBorderStylesRules
      • margin: module:engine/view/styles/margin~addMarginStylesRules
      • padding: module:engine/view/styles/padding~addPaddingStylesRules

      Parameters

      Returns void

    • 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

    • Removes all event listeners set by the DataController.

      Returns void

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

    • Returns the model's data converted by downcast dispatchers attached to #downcastDispatcher and formatted by the #processor data processor.

      A warning is logged when you try to retrieve data for a detached root, as most probably this is a mistake. A detached root should be treated like it is removed, and you should not save its data. Note, that the detached root data is always an empty string.

      Parameters

      • Optionaloptions: { rootName?: string; trim?: "empty" | "none"; [key: string]: unknown }

        Additional configuration for the retrieved data. DataController provides two optional properties: rootName and trim. Other properties of this object are specified by various editor features.

        • [key: string]: unknown
        • OptionalrootName?: string

          Root name. Default 'main'.

        • Optionaltrim?: "empty" | "none"

          Whether returned data should be trimmed. This option is set to empty by default, which means whenever editor content is considered empty, an empty string will be returned. To turn off trimming completely use 'none'. In such cases the exact content will be returned (for example a <p>&nbsp;</p> for an empty editor).

      Returns string

      Output data.

      get

    • Sets the initial input data parsed by the #processor data processor and converted by the #upcastDispatcher view-to-model converters. Initial data can be only set to a document whose module:engine/model/document~ModelDocument#version is equal 0.

      Note This method is module:utils/observablemixin~Observable#decorate decorated which is used by e.g. collaborative editing plugin that syncs remote data on init.

      When data is passed as a string, it is initialized on the default main root:

      dataController.init( '<p>Foo</p>' ); // Initializes data on the `main` root only, as no other is specified.
      

      To initialize data on a different root or multiple roots at once, an object containing rootName - data pairs should be passed:

      dataController.init( { main: '<p>Foo</p>', title: '<h1>Bar</h1>' } ); // Initializes data on both the `main` and `title` roots.
      

      Parameters

      • data: string | Record<string, string>

        Input data as a string or an object containing the rootName - data pairs to initialize data on multiple roots at once.

      Returns Promise<void>

      Promise that is resolved after the data is set on the editor.

      init

    • 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

    • Returns the data parsed by the #processor data processor and then converted by upcast converters attached to the #upcastDispatcher.

      Parameters

      • data: string

        Data to parse.

      • Optionalcontext: ModelSchemaContextDefinition

        Base context in which the view will be converted to the model. See: module:engine/conversion/upcastdispatcher~UpcastDispatcher#convert.

      Returns ModelDocumentFragment

      Parsed data.

      #set

    • Registers a module:engine/view/matcher~MatcherPattern on an #htmlProcessor htmlProcessor and a #processor processor for view elements whose content should be treated as raw data and not processed during the conversion from DOM to view elements.

      The raw data can be later accessed by the module:engine/view/element~ViewElement#getCustomProperty view element custom property "$rawContent".

      Parameters

      • pattern: MatcherPattern

        Pattern matching all view elements whose content should be treated as a raw data.

      Returns void

    • Sets the input data parsed by the #processor data processor and converted by the #upcastDispatcher view-to-model converters. This method can be used any time to replace existing editor data with the new one without clearing the module:engine/model/document~ModelDocument#history document history.

      This method also creates a batch with all the changes applied. If all you need is to parse data, use the #parse method.

      When data is passed as a string it is set on the default main root:

      dataController.set( '<p>Foo</p>' ); // Sets data on the `main` root, as no other is specified.
      

      To set data on a different root or multiple roots at once, an object containing rootName - data pairs should be passed:

      dataController.set( { main: '<p>Foo</p>', title: '<h1>Bar</h1>' } ); // Sets data on the `main` and `title` roots as specified.
      

      To set the data with a preserved undo stack and add the change to the undo stack, set { isUndoable: true } as a batchType option.

      dataController.set( '<p>Foo</p>', { batchType: { isUndoable: true } } );
      

      Parameters

      • data: string | Record<string, string>

        Input data as a string or an object containing the rootName - data pairs to set data on multiple roots at once.

      • Optionaloptions: { batchType?: BatchType; [key: string]: unknown }

        Options for setting data.

        • [key: string]: unknown
        • OptionalbatchType?: BatchType

          The batch type that will be used to create a batch for the changes applied by this method. By default, the batch will be set as module:engine/model/batch~Batch#isUndoable not undoable and the undo stack will be cleared after the new data is applied (all undo steps will be removed). If the batch type isUndoable flag is be set to true, the undo stack will be preserved instead and not cleared when new data is applied.

      Returns void

      set

    • 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

    • Returns the content of the given module:engine/model/element~ModelElement model's element or module:engine/model/documentfragment~ModelDocumentFragment model document fragment converted by the downcast converters attached to the #downcastDispatcher and formatted by the #processor data processor.

      Parameters

      • modelElementOrFragment: ModelElement | ModelDocumentFragment

        The element whose content will be stringified.

      • Optionaloptions: Record<string, unknown>

        Additional configuration passed to the conversion process.

      Returns string

      Output data.

    • Returns the result of the given module:engine/view/element~ViewElement view element or module:engine/view/documentfragment~ViewDocumentFragment view document fragment converted by the #upcastDispatcher view-to-model converters, wrapped by module:engine/model/documentfragment~ModelDocumentFragment.

      When marker elements were converted during the conversion process, it will be set as a document fragment's module:engine/model/documentfragment~ModelDocumentFragment#markers static markers map.

      Parameters

      • viewElementOrFragment: ViewDocumentFragment | ViewElement

        The element or document fragment whose content will be converted.

      • Optionalcontext: ModelSchemaContextDefinition

        Base context in which the view will be converted to the model. See: module:engine/conversion/upcastdispatcher~UpcastDispatcher#convert.

      Returns ModelDocumentFragment

      Output document fragment.

      toModel

    • Returns the content of the given module:engine/model/element~ModelElement model element or module:engine/model/documentfragment~ModelDocumentFragment model document fragment converted by the downcast converters attached to #downcastDispatcher into a module:engine/view/documentfragment~ViewDocumentFragment view document fragment.

      Parameters

      • modelElementOrFragment: ModelElement | ModelDocumentFragment

        Element or document fragment whose content will be converted.

      • Optionaloptions: Record<string, unknown>

        Additional configuration that will be available through the module:engine/conversion/downcastdispatcher~DowncastConversionApi#options during the conversion process.

      Returns ViewDocumentFragment

      Output view ModelDocumentFragment.

      toView