Trilium Frontend API
    Preparing search index...

    Class ModelRange

    Represents a range in the model tree.

    A range is defined by its module:engine/model/range~ModelRange#start and module:engine/model/range~ModelRange#end positions.

    You can create range instances via its constructor or the createRange*() factory methods of module:engine/model/model~Model and module:engine/model/writer~ModelWriter.

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    End position.

    Start position.

    Accessors

    • get isCollapsed(): boolean

      Describes whether the range is collapsed, that is if #start and #end positions are equal.

      Returns boolean

    • get isFlat(): boolean

      Describes whether this range is flat, that is if #start position and #end position are in the same module:engine/model/position~ModelPosition#parent.

      Returns boolean

    Methods

    • Internal

      Returns a copy of this range that is transformed by deletion of howMany nodes from deletePosition.

      If the deleted range is intersecting with the transformed range, the transformed range will be shrank.

      If the deleted range contains transformed range, null will be returned.

      Parameters

      • deletePosition: ModelPosition

        Position from which nodes are removed.

      • howMany: number

        How many nodes are removed.

      Returns ModelRange

      Result of the transformation.

    • Internal

      Returns an array containing one or two ~ModelRange ranges that are a result of transforming this ~ModelRange range by inserting howMany nodes at insertPosition. Two ~ModelRange ranges are returned if the insertion was inside this ~ModelRange range and spread is set to true.

      Examples:

      let range = model.createRange(
      model.createPositionFromPath( root, [ 2, 7 ] ),
      model.createPositionFromPath( root, [ 4, 0, 1 ] )
      );
      let transformed = range._getTransformedByInsertion( model.createPositionFromPath( root, [ 1 ] ), 2 );
      // transformed array has one range from [ 4, 7 ] to [ 6, 0, 1 ]

      transformed = range._getTransformedByInsertion( model.createPositionFromPath( root, [ 4, 0, 0 ] ), 4 );
      // transformed array has one range from [ 2, 7 ] to [ 4, 0, 5 ]

      transformed = range._getTransformedByInsertion( model.createPositionFromPath( root, [ 3, 2 ] ), 4 );
      // transformed array has one range, which is equal to original range

      transformed = range._getTransformedByInsertion( model.createPositionFromPath( root, [ 3, 2 ] ), 4, true );
      // transformed array has two ranges: from [ 2, 7 ] to [ 3, 2 ] and from [ 3, 6 ] to [ 4, 0, 1 ]

      Parameters

      • insertPosition: ModelPosition

        Position where nodes are inserted.

      • howMany: number

        How many nodes are inserted.

      • Optionalspread: boolean

        Flag indicating whether this range should be spread if insertion was inside the range. Defaults to false.

      Returns ModelRange[]

      Result of the transformation.

    • Internal

      Returns a result of transforming a copy of this range by insert operation.

      One or more ranges may be returned as a result of this transformation.

      Parameters

      Returns ModelRange[]

    • Internal

      Returns a result of transforming a copy of this range by merge operation.

      Always one range is returned. The transformation is done in a way to not break the range.

      Parameters

      Returns ModelRange

    • Internal

      Returns an array containing ~ModelRange ranges that are a result of transforming this ~ModelRange range by moving howMany nodes from sourcePosition to targetPosition.

      Parameters

      • sourcePosition: ModelPosition

        Position from which nodes are moved.

      • targetPosition: ModelPosition

        Position to where nodes are moved.

      • howMany: number

        How many nodes are moved.

      • Optionalspread: boolean

        Whether the range should be spread if the move points inside the range.

      Returns ModelRange[]

      Result of the transformation.

    • Internal

      Returns a result of transforming a copy of this range by move operation.

      One or more ranges may be returned as a result of this transformation.

      Parameters

      Returns ModelRange[]

    • Internal

      Returns a result of transforming a copy of this range by split operation.

      Always one range is returned. The transformation is done in a way to not break the range.

      Parameters

      Returns ModelRange

    • Iterable interface.

      Iterates over all module:engine/model/item~ModelItem items that are in this range and returns them together with additional information like length or module:engine/model/position~ModelPosition positions, grouped as module:engine/model/treewalker~ModelTreeWalkerValue. It iterates over all module:engine/model/textproxy~ModelTextProxy text contents that are inside the range and all the module:engine/model/element~ModelElements that are entered into when iterating over this range.

      This iterator uses module:engine/model/treewalker~ModelTreeWalker with boundaries set to this range and ignoreElementEnd option set to true.

      Returns IterableIterator<ModelTreeWalkerValue>

    • Returns a new range that is equal to current range.

      Returns this

    • Checks whether given module:engine/model/item~ModelItem is inside this range.

      Parameters

      Returns boolean

    • Checks whether this range contains given module:engine/model/position~ModelPosition position.

      Parameters

      Returns boolean

      true if given module:engine/model/position~ModelPosition position is contained in this range,false otherwise.

    • Checks whether this range contains given ~ModelRange range.

      Parameters

      • otherRange: ModelRange

        Range to check.

      • Optionalloose: boolean

        Whether the check is loose or strict. If the check is strict (false), compared range cannot start or end at the same position as this range boundaries. If the check is loose (true), compared range can start, end or even be equal to this range. Note that collapsed ranges are always compared in strict mode.

      Returns boolean

      true if given ~ModelRange range boundaries are contained by this range, false otherwise.

    • Returns an module:engine/model/element~ModelElement or module:engine/model/documentfragment~ModelDocumentFragment which is a common ancestor of the range's both ends (in which the entire range is contained).

      Returns ModelElement | ModelDocumentFragment

    • Returns an module:engine/model/element~ModelElement Element contained by the range. The element will be returned when it is the only node within the range and fully–contained at the same time.

      Returns ModelElement

    • Computes which part(s) of this ~ModelRange range is not a part of given ~ModelRange range. Returned array contains zero, one or two ~ModelRange ranges.

      Examples:

      let range = model.createRange(
      model.createPositionFromPath( root, [ 2, 7 ] ),
      model.createPositionFromPath( root, [ 4, 0, 1 ] )
      );
      let otherRange = model.createRange( model.createPositionFromPath( root, [ 1 ] ), model.createPositionFromPath( root, [ 5 ] ) );
      let transformed = range.getDifference( otherRange );
      // transformed array has no ranges because `otherRange` contains `range`

      otherRange = model.createRange( model.createPositionFromPath( root, [ 1 ] ), model.createPositionFromPath( root, [ 3 ] ) );
      transformed = range.getDifference( otherRange );
      // transformed array has one range: from [ 3 ] to [ 4, 0, 1 ]

      otherRange = model.createRange( model.createPositionFromPath( root, [ 3 ] ), model.createPositionFromPath( root, [ 4 ] ) );
      transformed = range.getDifference( otherRange );
      // transformed array has two ranges: from [ 2, 7 ] to [ 3 ] and from [ 4 ] to [ 4, 0, 1 ]

      Parameters

      • otherRange: ModelRange

        Range to differentiate against.

      Returns ModelRange[]

      The difference between ranges.

    • Returns an intersection of this ~ModelRange range and given ~ModelRange range. Intersection is a common part of both of those ranges. If ranges has no common part, returns null.

      Examples:

      let range = model.createRange(
      model.createPositionFromPath( root, [ 2, 7 ] ),
      model.createPositionFromPath( root, [ 4, 0, 1 ] )
      );
      let otherRange = model.createRange( model.createPositionFromPath( root, [ 1 ] ), model.createPositionFromPath( root, [ 2 ] ) );
      let transformed = range.getIntersection( otherRange ); // null - ranges have no common part

      otherRange = model.createRange( model.createPositionFromPath( root, [ 3 ] ), model.createPositionFromPath( root, [ 5 ] ) );
      transformed = range.getIntersection( otherRange ); // range from [ 3 ] to [ 4, 0, 1 ]

      Parameters

      • otherRange: ModelRange

        Range to check for intersection.

      Returns ModelRange

      A common part of given ranges or null if ranges have no common part.

    • Returns an iterator that iterates over all module:engine/model/item~ModelItem items that are in this range and returns them.

      This method uses module:engine/model/treewalker~ModelTreeWalker with boundaries set to this range and ignoreElementEnd option set to true. However it returns only module:engine/model/item~ModelItem model items, not module:engine/model/treewalker~ModelTreeWalkerValue.

      You may specify additional options for the tree walker. See module:engine/model/treewalker~ModelTreeWalker for a full list of available options.

      Parameters

      • Optionaloptions: ModelTreeWalkerOptions

        Object with configuration options. See module:engine/model/treewalker~ModelTreeWalker.

      Returns IterableIterator<ModelItem>

    • Returns a range created by joining this ~ModelRange range with the given ~ModelRange range. If ranges have no common part, returns null.

      Examples:

      let range = model.createRange(
      model.createPositionFromPath( root, [ 2, 7 ] ),
      model.createPositionFromPath( root, [ 4, 0, 1 ] )
      );
      let otherRange = model.createRange(
      model.createPositionFromPath( root, [ 1 ] ),
      model.createPositionFromPath( root, [ 2 ] )
      );
      let transformed = range.getJoined( otherRange ); // null - ranges have no common part

      otherRange = model.createRange(
      model.createPositionFromPath( root, [ 3 ] ),
      model.createPositionFromPath( root, [ 5 ] )
      );
      transformed = range.getJoined( otherRange ); // range from [ 2, 7 ] to [ 5 ]

      Parameters

      • otherRange: ModelRange

        Range to be joined.

      • Optionalloose: boolean

        Whether the intersection check is loose or strict. If the check is strict (false), ranges are tested for intersection or whether start/end positions are equal. If the check is loose (true), compared range is also checked if it's module:engine/model/position~ModelPosition#isTouching touching current range.

      Returns ModelRange

      A sum of given ranges or null if ranges have no common part.

    • Computes and returns the smallest set of #isFlat flat ranges, that covers this range in whole.

      See an example of a model structure ([ and ] are range boundaries):

      root                                                            root
      |- element DIV DIV P2 P3 DIV
      | |- element H H P1 f o o b a r H P4
      | | |- "fir[st" fir[st lorem se]cond ipsum
      | |- element P1
      | | |- "lorem" ||
      |- element P2 ||
      | |- "foo" VV
      |- element P3
      | |- "bar" root
      |- element DIV DIV [P2 P3] DIV
      | |- element H H [P1] f o o b a r H P4
      | | |- "se]cond" fir[st] lorem [se]cond ipsum
      | |- element P4
      | | |- "ipsum"

      As it can be seen, letters contained in the range are: stloremfoobarse, spread across different parents. We are looking for minimal set of flat ranges that contains the same nodes.

      Minimal flat ranges for above range ( [ 0, 0, 3 ], [ 3, 0, 2 ] ) will be:

      ( [ 0, 0, 3 ], [ 0, 0, 5 ] ) = "st"
      ( [ 0, 1 ], [ 0, 2 ] ) = element P1 ("lorem")
      ( [ 1 ], [ 3 ] ) = element P2, element P3 ("foobar")
      ( [ 3, 0, 0 ], [ 3, 0, 2 ] ) = "se"

      Note: if an module:engine/model/element~ModelElement element is not wholly contained in this range, it won't be returned in any of the returned flat ranges. See in the example how H elements at the beginning and at the end of the range were omitted. Only their parts that were wholly in the range were returned.

      Note: this method is not returning flat ranges that contain no nodes.

      Returns ModelRange[]

      Array of flat ranges covering this range.

    • Returns an iterator that iterates over all module:engine/model/position~ModelPosition positions that are boundaries or contained in this range.

      This method uses module:engine/model/treewalker~ModelTreeWalker with boundaries set to this range. However it returns only module:engine/model/position~ModelPosition positions, not module:engine/model/treewalker~ModelTreeWalkerValue.

      You may specify additional options for the tree walker. See module:engine/model/treewalker~ModelTreeWalker for a full list of available options.

      Parameters

      • Optionaloptions: ModelTreeWalkerOptions

        Object with configuration options. See module:engine/model/treewalker~ModelTreeWalker.

      Returns IterableIterator<ModelPosition>

    • Returns a range that is a result of transforming this range by given operation.

      Note: transformation may break one range into multiple ranges (for example, when a part of the range is moved to a different part of document tree). For this reason, an array is returned by this method and it may contain one or more Range instances.

      Parameters

      • operation: Operation

        Operation to transform range by.

      Returns ModelRange[]

      Range which is the result of transformation.

    • Returns a range that is a result of transforming this range by multiple operations.

      Parameters

      Returns ModelRange[]

      Range which is the result of transformation.

      ~ModelRange#getTransformedByOperation

    • Creates a module:engine/model/treewalker~ModelTreeWalker TreeWalker instance with this range as a boundary.

      For example, to iterate over all items in the entire document root:

      // Create a range spanning over the entire root content:
      const range = editor.model.createRangeIn( editor.model.document.getRoot() );

      // Iterate over all items in this range:
      for ( const value of range.getWalker() ) {
      console.log( value.item );
      }

      Parameters

      • Optionaloptions: ModelTreeWalkerOptions

        Object with configuration options. See module:engine/model/treewalker~ModelTreeWalker.

      Returns ModelTreeWalker

    • 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

    • Two ranges are equal if their #start and #end positions are equal.

      Parameters

      Returns boolean

      true if ranges are equal, false otherwise.

    • Checks and returns whether this range intersects with given range.

      Parameters

      Returns boolean

      true if ranges intersect, false otherwise.

    • Converts Range to plain object and returns it.

      Returns unknown

      Range converted to plain object.

    • Internal

      Creates a new range, spreading from specified module:engine/model/position~ModelPosition position to a position moved by given shift. If shift is a negative value, shifted position is treated as the beginning of the range.

      Parameters

      • position: ModelPosition

        Beginning of the range.

      • shift: number

        How long the range should be.

      Returns ModelRange

    • Internal

      Combines all ranges from the passed array into a one range. At least one range has to be passed. Passed ranges must not have common parts.

      The first range from the array is a reference range. If other ranges start or end on the exactly same position where the reference range, they get combined into one range.

      [  ][]  [    ][ ][             ][ ][]  [  ]  // Passed ranges, shown sorted
      [ ] // The result of the function if the first range was a reference range.
      [ ] // The result of the function if the third-to-seventh range was a reference range.
      [ ] // The result of the function if the last range was a reference range.

      Parameters

      Returns ModelRange

      Combined range.

    • Internal

      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.

      Parameters

      Returns ModelRange

    • Internal

      Creates a range that starts before given module:engine/model/item~ModelItem model item and ends after it.

      Parameters

      Returns ModelRange

    • Creates a Range instance from given plain object (i.e. parsed JSON string).

      Parameters

      • json: any

        Plain object to be converted to Range.

      • doc: ModelDocument

        Document object that will be range owner.

      Returns ModelRange

      Range instance created using given plain object.