Trilium Frontend API
    Preparing search index...

    Class ModelSelection

    Selection is a set of module:engine/model/range~ModelRange ranges. It has a direction specified by its module:engine/model/selection~ModelSelection#anchor anchor and module:engine/model/selection~ModelSelection#focus focus (it can be module:engine/model/selection~ModelSelection#isBackward forward or backward). Additionally, selection may have its own attributes (think – whether text typed in in this selection should have those attributes – e.g. whether you type a bolded text).

    Hierarchy (View Summary)

    Index

    Constructors

    • Internal

      Creates a new selection instance based on the given module:engine/model/selection~ModelSelectable selectable or creates an empty selection if no arguments were passed.

      // Creates empty selection without ranges.
      const selection = writer.createSelection();

      // Creates selection at the given range.
      const range = writer.createRange( start, end );
      const selection = writer.createSelection( range );

      // Creates selection at the given ranges
      const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
      const selection = writer.createSelection( ranges );

      // Creates selection from the other selection.
      // Note: It doesn't copy selection attributes.
      const otherSelection = writer.createSelection();
      const selection = writer.createSelection( otherSelection );

      // Creates selection from the given document selection.
      // Note: It doesn't copy selection attributes.
      const documentSelection = model.document.selection;
      const selection = writer.createSelection( documentSelection );

      // Creates selection at the given position.
      const position = writer.createPositionFromPath( root, path );
      const selection = writer.createSelection( position );

      // Creates selection at the given offset in the given element.
      const paragraph = writer.createElement( 'paragraph' );
      const selection = writer.createSelection( paragraph, offset );

      // Creates a range inside an {@link module:engine/model/element~ModelElement element} which starts before the
      // first child of that element and ends after the last child of that element.
      const selection = writer.createSelection( paragraph, 'in' );

      // Creates a range on an {@link module:engine/model/item~ModelItem item} which starts before the item and ends
      // just after the item.
      const selection = writer.createSelection( paragraph, 'on' );

      Selection's constructor allow passing additional options ('backward') as the last argument.

      // Creates backward selection.
      const selection = writer.createSelection( range, { backward: true } );

      Parameters

      Returns ModelSelection

    Properties

    _attrs: Map<string, unknown>

    List of attributes set on current selection.

    _ranges: ModelRange[]

    Accessors

    • get anchor(): ModelPosition

      Selection anchor. Anchor is the position from which the selection was started. If a user is making a selection by dragging the mouse, the anchor is where the user pressed the mouse button (the beginning of the selection).

      Anchor and #focus define the direction of the selection, which is important when expanding/shrinking selection. The focus moves, while the anchor should remain in the same place.

      Anchor is always set to the module:engine/model/range~ModelRange#start start or module:engine/model/range~ModelRange#end end position of the last of selection's ranges. Whether it is the start or end depends on the specified options.backward. See the #setTo setTo() method.

      May be set to null if there are no ranges in the selection.

      Returns ModelPosition

      #focus

    • get focus(): ModelPosition

      Selection focus. Focus is the position where the selection ends. If a user is making a selection by dragging the mouse, the focus is where the mouse cursor is.

      May be set to null if there are no ranges in the selection.

      Returns ModelPosition

      #anchor

    • get isBackward(): boolean

      Specifies whether the selection's #focus precedes the selection's #anchor.

      Returns boolean

    • get isCollapsed(): boolean

      Whether the selection is collapsed. Selection is collapsed when there is exactly one range in it and it is collapsed.

      Returns boolean

    • get rangeCount(): number

      Returns the number of ranges in the selection.

      Returns number

    Methods

    • Checks if given range intersects with ranges that are already in the selection. Throws an error if it does.

      Parameters

      Returns void

    • Removes most recently added range from the selection.

      Returns void

    • Adds given range to internal #_ranges ranges array. Throws an error if given range is intersecting with any range that is already stored in this selection.

      Parameters

      Returns void

    • Deletes ranges from internal range array. Uses #_popRange _popRange to ensure proper ranges removal.

      Returns void

    • Replaces all the ranges by the given ones. Uses #_popRange _popRange and #_pushRange _pushRange to ensure proper ranges removal and addition.

      Parameters

      Returns void

    • Replaces all ranges that were added to the selection with given array of ranges. Last range of the array is treated like the last added range and is used to set module:engine/model/selection~ModelSelection#anchor and module:engine/model/selection~ModelSelection#focus. Accepts a flag describing in which direction the selection is made.

      Parameters

      • newRanges: Iterable<ModelRange>

        Ranges to set.

      • OptionalisLastBackward: boolean

        Flag describing if last added range was selected forward - from start to end (false) or backward - from end to start (true).

      Returns void

      change:range

    • Checks whether the selection contains the entire content of the given element. This means that selection must start at a position module:engine/model/position~ModelPosition#isTouching touching the element's start and ends at position touching the element's end.

      By default, this method will check whether the entire content of the selection's current root is selected. Useful to check if e.g. the user has just pressed Ctrl + A.

      Parameters

      Returns boolean

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

    • Gets an attribute value for given key or undefined if that attribute is not set on the selection.

      Parameters

      • key: string

        Key of attribute to look for.

      Returns unknown

      Attribute value or undefined.

    • Returns iterable that iterates over this selection's attribute keys.

      Returns IterableIterator<string>

    • Returns iterable that iterates over this selection's attributes.

      Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value. This format is accepted by native Map object and also can be passed in Node constructor.

      Returns IterableIterator<[string, unknown]>

    • Returns the first position in the selection. First position is the position that module:engine/model/position~ModelPosition#isBefore is before any other position in the selection.

      Returns null if there are no ranges in selection.

      Returns ModelPosition

    • Returns a copy of the first range in the selection. First range is the one which module:engine/model/range~ModelRange#start start position module:engine/model/position~ModelPosition#isBefore is before start position of all other ranges (not to confuse with the first range added to the selection).

      Returns null if there are no ranges in selection.

      Returns ModelRange

    • Returns the last position in the selection. Last position is the position that module:engine/model/position~ModelPosition#isAfter is after any other position in the selection.

      Returns null if there are no ranges in selection.

      Returns ModelPosition

    • Returns a copy of the last range in the selection. Last range is the one which module:engine/model/range~ModelRange#end end position module:engine/model/position~ModelPosition#isAfter is after end position of all other ranges (not to confuse with the range most recently added to the selection).

      Returns null if there are no ranges in selection.

      Returns ModelRange

    • Gets elements of type module:engine/model/schema~ModelSchema#isBlock "block" touched by the selection.

      This method's result can be used for example to apply block styling to all blocks covered by this selection.

      Note: getSelectedBlocks() returns blocks that are nested in other non-block elements but will not return blocks nested in other blocks.

      In this case the function will return exactly all 3 paragraphs (note: <blockQuote> is not a block itself):

      [a
      
      b
      c]d

      In this case the paragraph will also be returned, despite the collapsed selection:

      []a
      

      In such a scenario, however, only blocks A, B & E will be returned as blocks C & D are nested in block B:

      [
      
      	
      	
      
      ]
      

      If the selection is inside a block all the inner blocks (A & B) are returned:

      
      	[a
      	b]
      
      

      Special case: Selection ignores first and/or last blocks if nothing (from user perspective) is selected in them.

      // Selection ends and the beginning of the last block.
      [a
      b
      ]c // This block will not be returned
      
      // Selection begins at the end of the first block.
      a[ // This block will not be returned
      b
      c]
      
      // Selection begings at the end of the first block and ends at the beginning of the last block.
      a[ // This block will not be returned
      b
      ]c // This block will not be returned
      

      Returns IterableIterator<ModelElement>

    • Returns the selected element. module:engine/model/element~ModelElement Element is considered as selected if there is only one range in the selection, and that range contains exactly one element. Returns null if there is no selected element.

      Returns ModelElement

    • Checks if the selection has an attribute for given key.

      Parameters

      • key: string

        Key of attribute to check.

      Returns boolean

      true if attribute with given key is set on selection, false otherwise.

    • Checks whether the object is of type module:engine/model/node~ModelNode or its subclass.

      This method is useful when processing model objects that are of unknown type. For example, a function may return a module:engine/model/documentfragment~ModelDocumentFragment or a module:engine/model/node~ModelNode that can be either a text node or an element. This method can be used to check what kind of object is returned.

      someObject.is( 'element' ); // -> true if this is an element
      someObject.is( 'node' ); // -> true if this is a node (a text node or an element)
      someObject.is( 'documentFragment' ); // -> true if this is a document fragment

      Since this method is also available on a range of view objects, you can prefix the type of the object with model: or view: to check, for example, if this is the model's or view's element:

      modelElement.is( 'model:element' ); // -> true
      modelElement.is( 'view:element' ); // -> false

      By using this method it is also possible to check a name of an element:

      imageElement.is( 'element', 'imageBlock' ); // -> true
      imageElement.is( 'element', 'imageBlock' ); // -> same as above
      imageElement.is( 'model:element', 'imageBlock' ); // -> same as above, but more precise

      Parameters

      • type: "node" | "model:node"

      Returns this is ModelElement | ModelNode | ModelText | ModelRootElement

      NODE

    • Checks whether the object is of type module:engine/model/element~ModelElement or its subclass.

      element.is( 'element' ); // -> true
      element.is( 'node' ); // -> true
      element.is( 'model:element' ); // -> true
      element.is( 'model:node' ); // -> true

      element.is( 'view:element' ); // -> false
      element.is( 'documentSelection' ); // -> false

      Assuming that the object being checked is an element, you can also check its module:engine/model/element~ModelElement#name name:

      element.is( 'element', 'imageBlock' ); // -> true if this is an <imageBlock> element
      text.is( 'element', 'imageBlock' ); -> false

      Parameters

      • type: "element" | "model:element"

      Returns this is ModelElement | ModelRootElement

      ELEMENT

    • Checks whether the object is of type module:engine/model/rootelement~ModelRootElement.

      rootElement.is( 'rootElement' ); // -> true
      rootElement.is( 'element' ); // -> true
      rootElement.is( 'node' ); // -> true
      rootElement.is( 'model:rootElement' ); // -> true
      rootElement.is( 'model:element' ); // -> true
      rootElement.is( 'model:node' ); // -> true

      rootElement.is( 'view:element' ); // -> false
      rootElement.is( 'documentFragment' ); // -> false

      Assuming that the object being checked is an element, you can also check its module:engine/model/element~ModelElement#name name:

      rootElement.is( 'rootElement', '$root' ); // -> same as above
      

      Parameters

      • type: "rootElement" | "model:rootElement"

      Returns this is ModelRootElement

      ROOT_ELEMENT

    • Checks whether the object is of type module:engine/model/text~ModelText.

      text.is( '$text' ); // -> true
      text.is( 'node' ); // -> true
      text.is( 'model:$text' ); // -> true
      text.is( 'model:node' ); // -> true

      text.is( 'view:$text' ); // -> false
      text.is( 'documentSelection' ); // -> false

      Note: Until version 20.0.0 this method wasn't accepting '$text' type. The legacy 'text' type is still accepted for backward compatibility.

      Parameters

      • type: "$text" | "model:$text"

      Returns this is ModelText

      TEXT

    • Checks whether the object is of type module:engine/model/position~ModelPosition or its subclass.

      position.is( 'position' ); // -> true
      position.is( 'model:position' ); // -> true

      position.is( 'view:position' ); // -> false
      position.is( 'documentSelection' ); // -> false

      Parameters

      • type: "position" | "model:position"

      Returns this is ModelPosition | ModelLivePosition

      POSITION

    • Checks whether the object is of type module:engine/model/liveposition~ModelLivePosition.

      livePosition.is( 'position' ); // -> true
      livePosition.is( 'model:position' ); // -> true
      livePosition.is( 'liveposition' ); // -> true
      livePosition.is( 'model:livePosition' ); // -> true

      livePosition.is( 'view:position' ); // -> false
      livePosition.is( 'documentSelection' ); // -> false

      Parameters

      • type: "livePosition" | "model:livePosition"

      Returns this is ModelLivePosition

      LIVE_POSITION

    • Checks whether the object is of type module:engine/model/range~ModelRange or its subclass.

      range.is( 'range' ); // -> true
      range.is( 'model:range' ); // -> true

      range.is( 'view:range' ); // -> false
      range.is( 'documentSelection' ); // -> false

      Parameters

      • type: "range" | "model:range"

      Returns this is ModelRange | ModelLiveRange

      RANGE

    • Checks whether the object is of type module:engine/model/liverange~ModelLiveRange.

      liveRange.is( 'range' ); // -> true
      liveRange.is( 'model:range' ); // -> true
      liveRange.is( 'liveRange' ); // -> true
      liveRange.is( 'model:liveRange' ); // -> true

      liveRange.is( 'view:range' ); // -> false
      liveRange.is( 'documentSelection' ); // -> false

      Parameters

      • type: "liveRange" | "model:liveRange"

      Returns this is ModelLiveRange

      LIVE_RANGE

    • Checks whether the object is of type module:engine/model/documentfragment~ModelDocumentFragment.

      docFrag.is( 'documentFragment' ); // -> true
      docFrag.is( 'model:documentFragment' ); // -> true

      docFrag.is( 'view:documentFragment' ); // -> false
      docFrag.is( 'element' ); // -> false
      docFrag.is( 'node' ); // -> false

      Parameters

      • type: "documentFragment" | "model:documentFragment"

      Returns this is ModelDocumentFragment

      DOCUMENT_FRAGMENT

    • Checks whether the object is of type module:engine/model/selection~ModelSelection or module:engine/model/documentselection~ModelDocumentSelection.

      selection.is( 'selection' ); // -> true
      selection.is( 'model:selection' ); // -> true

      selection.is( 'view:selection' ); // -> false
      selection.is( 'range' ); // -> false

      Parameters

      • type: "selection" | "model:selection"

      Returns this is ModelSelection | ModelDocumentSelection

      SELECTION

    • Checks whether the object is of type module:engine/model/documentselection~ModelDocumentSelection.

      selection.is( 'selection' ); // -> true
      selection.is( 'documentSelection' ); // -> true
      selection.is( 'model:selection' ); // -> true
      selection.is( 'model:documentSelection' ); // -> true

      selection.is( 'view:selection' ); // -> false
      selection.is( 'element' ); // -> false
      selection.is( 'node' ); // -> false

      Parameters

      • type: "documentSelection" | "model:documentSelection"

      Returns this is ModelDocumentSelection

      DOCUMENT_SELECTION

    • Checks whether the object is of type module:engine/model/markercollection~Marker.

      marker.is( 'marker' ); // -> true
      marker.is( 'model:marker' ); // -> true

      marker.is( 'view:element' ); // -> false
      marker.is( 'documentSelection' ); // -> false

      Parameters

      • type: "marker" | "model:marker"

      Returns this is Marker

      MARKER

    • Checks whether the object is of type module:engine/model/textproxy~ModelTextProxy.

      textProxy.is( '$textProxy' ); // -> true
      textProxy.is( 'model:$textProxy' ); // -> true

      textProxy.is( 'view:$textProxy' ); // -> false
      textProxy.is( 'range' ); // -> false

      Note: Until version 20.0.0 this method wasn't accepting '$textProxy' type. The legacy 'textProxt' type is still accepted for backward compatibility.

      Parameters

      • type: "$textProxy" | "model:$textProxy"

      Returns this is ModelTextProxy

      TEXT_PROXY

    • Checks whether the object is of type module:engine/model/element~ModelElement or its subclass and has the specified name.

      element.is( 'element', 'imageBlock' ); // -> true if this is an <imageBlock> element
      text.is( 'element', 'imageBlock' ); -> false

      Type Parameters

      • N extends string

      Parameters

      • type: "element" | "model:element"
      • name: N

      Returns this is (ModelElement | ModelRootElement) & { name: N }

      ELEMENT_NAME

    • Checks whether the object is of type module:engine/model/rootelement~ModelRootElement and has the specified name.

      rootElement.is( 'rootElement', '$root' );
      

      Type Parameters

      • N extends string

      Parameters

      • type: "rootElement" | "model:rootElement"
      • name: N

      Returns this is ModelRootElement & { name: N }

      ROOT_ELEMENT_NAME

    • Checks whether this selection is equal to the given selection. Selections are equal if they have the same directions, the same number of ranges and all ranges from one selection equal to ranges from the another selection.

      Parameters

      Returns boolean

      true if selections are equal, false otherwise.

    • 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

    • Removes an attribute with given key from the selection.

      If given attribute was set on the selection, fires the #event:change:range event with removed attribute key.

      Parameters

      • key: string

        Key of attribute to remove.

      Returns void

      change:attribute

    • Sets attribute on the selection. If attribute with the same key already is set, it's value is overwritten.

      If the attribute value has changed, fires the #event:change:range event with the attribute key.

      Parameters

      • key: string

        Key of attribute to set.

      • value: unknown

        Attribute value.

      Returns void

      change:attribute

    • Moves module:engine/model/selection~ModelSelection#focus to the specified location.

      The location can be specified in the same form as module:engine/model/writer~ModelWriter#createPositionAt writer.createPositionAt() parameters.

      Parameters

      Returns void

      change:range

    • Sets this selection's ranges and direction to the specified location based on the given module:engine/model/selection~ModelSelectable selectable.

      // Removes all selection's ranges.
      selection.setTo( null );

      // Sets selection to the given range.
      const range = writer.createRange( start, end );
      selection.setTo( range );

      // Sets selection to given ranges.
      const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
      selection.setTo( ranges );

      // Sets selection to other selection.
      // Note: It doesn't copy selection attributes.
      const otherSelection = writer.createSelection();
      selection.setTo( otherSelection );

      // Sets selection to the given document selection.
      // Note: It doesn't copy selection attributes.
      const documentSelection = new ModelDocumentSelection( doc );
      selection.setTo( documentSelection );

      // Sets collapsed selection at the given position.
      const position = writer.createPositionFromPath( root, path );
      selection.setTo( position );

      // Sets collapsed selection at the position of the given node and an offset.
      selection.setTo( paragraph, offset );

      Creates a range inside an module:engine/model/element~ModelElement element which starts before the first child of that element and ends after the last child of that element.

      selection.setTo( paragraph, 'in' );
      

      Creates a range on an module:engine/model/item~ModelItem item which starts before the item and ends just after the item.

      selection.setTo( paragraph, 'on' );
      

      Selection#setTo()' method allow passing additional options (backward) as the last argument.

      // Sets backward selection.
      const selection = writer.createSelection( range, { backward: true } );

      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

    • Converts Selection to plain object and returns it.

      Returns unknown

      Selection converted to plain object.