Differ

Api-class icon class

Calculates the difference between two model states.

Receives operations that are to be applied on the model document. Marks parts of the model document tree which are changed and saves the state of these elements before the change. Then, it compares saved elements with the changed elements, after all changes are applied on the model document. Calculates the diff between saved elements and new ones and returns a change set.

Properties

  • Chevron-right icon

    isEmpty : boolean
    readonly

    Informs whether there are any changes buffered in Differ.

  • Chevron-right icon

    _cachedChanges : null | Array<DifferItem>
    Lock icon private

    For efficiency purposes, Differ stores the change set returned by the differ after getChanges call. Cache is reset each time a new operation is buffered. If the cache has not been reset, getChanges will return the cached value instead of calculating it again.

    This property stores those changes that did not take place in graveyard root.

  • Chevron-right icon

    _cachedChangesWithGraveyard : null | Array<DifferItem>
    Lock icon private

    For efficiency purposes, Differ stores the change set returned by the differ after the getChanges call. The cache is reset each time a new operation is buffered. If the cache has not been reset, getChanges will return the cached value instead of calculating it again.

    This property stores all changes evaluated by Differ, including those that took place in the graveyard.

  • Chevron-right icon

    _changeCount : number
    Lock icon private

    Stores the number of changes that were processed. Used to order the changes chronologically. It is important when changes are sorted.

  • Chevron-right icon

    _changedMarkers : Map<string, object>
    Lock icon privatereadonly

    A map that stores all changed markers.

    The keys of the map are marker names.

    The values of the map are objects with the following properties:

    • oldMarkerData,
    • newMarkerData.
  • Chevron-right icon

    _changedRoots : Map<string, DifferItemRoot>
    Lock icon privatereadonly

    A map that stores all roots that have been changed.

    The keys are the names of the roots while value represents the changes.

  • Chevron-right icon

    _changesInElement : Map<ModelElement | ModelDocumentFragment, Array<ChangeItem>>
    Lock icon privatereadonly

    A map that stores changes that happened in a given element.

    The keys of the map are references to the model elements. The values of the map are arrays with changes that were done on this element.

  • Chevron-right icon

    For each element or document fragment inside which there was a change, it stores a snapshot of the child nodes list (an array of children snapshots that represent the state in the element / fragment before any change has happened).

    This complements _elementsSnapshots.

    See also DifferSnapshot.

  • Chevron-right icon

    _elementState : Map<ModelElement, 'rename' | 'move' | 'refresh'>
    Lock icon privatereadonly

    Keeps the state for a given element, describing how the element was changed so far. It is used to evaluate the action property of diff items returned by getChanges.

    Possible values, in the order from the lowest priority to the highest priority:

    • 'refresh' - element was refreshed,
    • 'rename' - element was renamed,
    • 'move' - element was moved (or, usually, removed, that is moved to the graveyard).

    Element that was refreshed, may change its state to 'rename' if it was later renamed, or to 'move' if it was removed. But the element cannot change its state from 'move' to 'rename', or from 'rename' to 'refresh'.

    Only already existing elements are registered in _elementState. If a new element was inserted as a result of a buffered operation, it is not be registered in _elementState.

  • Chevron-right icon

    _elementsSnapshots : Map<ModelNode, DifferSnapshot>
    Lock icon privatereadonly

    Stores a snapshot for these model nodes that might have changed.

    This complements _elementChildrenSnapshots.

    See also DifferSnapshot.

  • Chevron-right icon

    _markerCollection : MarkerCollection
    Lock icon privatereadonly

    Reference to the model's marker collection.

  • Chevron-right icon

    _refreshedItems : Set<ModelItem>
    Lock icon private

    Set of model items that were marked to get refreshed in _refreshItem.

Static properties

Methods

  • Chevron-right icon

    constructor( markerCollection )

    Creates a Differ instance.

    Parameters

    markerCollection : MarkerCollection

    Model's marker collection.

  • Chevron-right icon

    bufferMarkerChange( markerName, oldMarkerData, newMarkerData ) → void

    Buffers a marker change.

    Parameters

    markerName : string

    The name of the marker that changed.

    oldMarkerData : MarkerData

    Marker data before the change.

    newMarkerData : MarkerData

    Marker data after the change.

    Returns

    void
  • Chevron-right icon

    bufferOperation( operationToBuffer ) → void

    Buffers the given operation. An operation has to be buffered before it is executed.

    Parameters

    operationToBuffer : Operation

    An operation to buffer.

    Returns

    void
  • Chevron-right icon

    getChangedMarkers() → Array<object>

    Returns all markers which changed.

    Returns

    Array<object>
  • Chevron-right icon

    Returns all roots that have changed (either were attached, or detached, or their attributes changed).

    Returns

    Array<DifferItemRoot>

    Diff between the old and the new roots state.

  • Chevron-right icon

    getChanges( options = { [options.includeChangesInGraveyard] } ) → Array<DifferItem>

    Calculates the diff between the old model tree state (the state before the first buffered operations since the last reset call) and the new model tree state (actual one). It should be called after all buffered operations are executed.

    The diff set is returned as an array of diff items, each describing a change done on the model. The items are sorted by the position on which the change happened. If a position is before another one, it will be on an earlier index in the diff set.

    Note: Elements inside inserted element will not have a separate diff item, only the top most element change will be reported.

    Because calculating the diff is a costly operation, the result is cached. If no new operation was buffered since the previous getChanges call, the next call will return the cached value.

    Parameters

    options : object

    Additional options.

    Properties
    [ options.includeChangesInGraveyard ] : boolean

    If set to true, also changes that happened in the graveyard root will be returned. By default, changes in the graveyard root are not returned.

    Defaults to {}

    Returns

    Array<DifferItem>

    Diff between the old and the new model tree state.

  • Chevron-right icon

    getMarkersToAdd() → Array<object>

    Returns all markers which should be added as a result of buffered changes.

    Returns

    Array<object>

    Markers to add. Each array item is an object containing the name and range properties.

  • Chevron-right icon

    getMarkersToRemove() → Array<object>

    Returns all markers that should be removed as a result of buffered changes.

    Returns

    Array<object>

    Markers to remove. Each array item is an object containing the name and range properties.

  • Chevron-right icon

    Returns a set of model items that were marked to get refreshed.

    Returns

    Set<ModelItem>
  • Chevron-right icon

    hasDataChanges() → boolean

    Checks whether some of the buffered changes affect the editor data.

    Types of changes which affect the editor data:

    • model structure changes,
    • attribute changes,
    • a root is added or detached,
    • changes of markers which were defined as affectsData,
    • changes of markers' affectsData property.

    Returns

    boolean
  • Chevron-right icon

    reset() → void

    Resets Differ. Removes all buffered changes.

    Returns

    void
  • Chevron-right icon

    _bufferRootLoad( root ) → void
    internal

    Buffers all the data related to given root like it was all just added to the editor.

    Following changes are buffered:

    • root is attached,
    • all root content is inserted,
    • all root attributes are added,
    • all markers inside the root are added.

    Parameters

    root : ModelRootElement

    Returns

    void
  • Chevron-right icon

    _refreshItem( item ) → void
    internal

    Marks the given item in differ to be "refreshed". It means that the item will be marked as removed and inserted in the differ changes set, so it will be effectively re-converted when the differ changes are handled by a dispatcher.

    Parameters

    item : ModelItem

    Item to refresh.

    Returns

    void
  • Chevron-right icon

    _bufferRootAttributeChange( rootName, key, oldValue, newValue ) → void
    Lock icon private

    Buffers a root attribute change.

    Parameters

    rootName : string
    key : string
    oldValue : unknown
    newValue : unknown

    Returns

    void
  • Chevron-right icon

    _bufferRootStateChange( rootName, isAttached ) → void
    Lock icon private

    Buffers the root state change after the root was attached or detached

    Parameters

    rootName : string
    isAttached : boolean

    Returns

    void
  • Chevron-right icon

    _getAttributesDiff( range, oldAttributes, newAttributes ) → Array<object>
    Lock icon private

    Returns an array of objects where each one is a single attribute change description.

    Parameters

    range : ModelRange

    The range where the change happened.

    oldAttributes : Map<string, unknown>

    A map, map iterator or compatible object that contains attributes before the change.

    newAttributes : Map<string, unknown>

    A map, map iterator or compatible object that contains attributes after the change.

    Returns

    Array<object>

    An array containing one or more diff items.

  • Chevron-right icon

    _getChangesForElement( element ) → Array<ChangeItem>
    Lock icon private

    Gets an array of changes that have already been saved for a given element.

    Parameters

    element : ModelElement | ModelDocumentFragment

    Returns

    Array<ChangeItem>
  • Chevron-right icon

    _getDiffActionForNode( node, diffItemType ) → DifferItemAction
    Lock icon private

    Returns a value for action property for diff items returned by getChanges. This method aims to return 'rename' or 'refresh' when it should, and diffItemType ("default action") in all other cases.

    It bases on a few factors:

    • for text nodes, the method always returns diffItemType,
    • for newly inserted element, the method returns diffItemType,
    • if element state was not recorded, the method returns diffItemType,
    • if state was recorded, and it was 'move' (default action), the method returns diffItemType,
    • finally, if state was 'refresh' or 'rename', the method returns the state value.

    Parameters

    node : ModelNode
    diffItemType : 'insert' | 'remove'

    Returns

    DifferItemAction
  • Chevron-right icon

    _getInsertDiff( parent, offset, action, elementSnapshot, [ elementSnapshotBefore ] ) → object
    Lock icon private

    Returns an object with a single insert change description.

    Parameters

    parent : ModelElement | ModelDocumentFragment

    The element in which the change happened.

    offset : number

    The offset at which change happened.

    action : DifferItemAction

    Further specifies what kind of action led to generating this change.

    elementSnapshot : DifferSnapshot

    Snapshot of the inserted node after changes.

    [ elementSnapshotBefore ] : DifferSnapshot

    Snapshot of the inserted node before changes.

    Returns

    object

    The diff item.

  • Chevron-right icon

    _getRemoveDiff( parent, offset, action, elementSnapshot ) → object
    Lock icon private

    Returns an object with a single remove change description.

    Parameters

    parent : ModelElement | ModelDocumentFragment

    The element in which change happened.

    offset : number

    The offset at which change happened.

    action : DifferItemAction

    Further specifies what kind of action led to generating this change.

    elementSnapshot : DifferSnapshot

    The snapshot of the removed node before changes.

    Returns

    object

    The diff item.

  • Chevron-right icon

    _handleChange( inc, changes ) → void
    Lock icon private

    For a given newly saved change, compares it with a change already done on the element and modifies the incoming change and/or the old change.

    Parameters

    inc : ChangeItem

    Incoming (new) change.

    changes : Array<ChangeItem>

    An array containing all the changes done on that element.

    Returns

    void
  • Chevron-right icon

    _isInInsertedElement( element ) → boolean
    Lock icon private

    Checks whether given element or any of its parents is an element that is buffered as an inserted element.

    Parameters

    element : ModelElement | ModelDocumentFragment

    Returns

    boolean
  • Chevron-right icon

    _makeSnapshots( element ) → void
    Lock icon private

    Creates and saves a snapshot for all children of the given element.

    Parameters

    element : ModelElement | ModelDocumentFragment

    Returns

    void
  • Chevron-right icon

    _markAttribute( item ) → void
    Lock icon private

    Saves and handles an attribute change.

    Parameters

    item : ModelItem

    Returns

    void
  • Chevron-right icon

    _markChange( parent, changeItem ) → void
    Lock icon private

    Saves and handles a model change.

    Parameters

    parent : ModelElement | ModelDocumentFragment
    changeItem : ChangeItem

    Returns

    void
  • Chevron-right icon

    _markInsert( parent, offset, howMany ) → void
    Lock icon private

    Saves and handles an insert change.

    Parameters

    parent : ModelElement | ModelDocumentFragment
    offset : number
    howMany : number

    Returns

    void
  • Chevron-right icon

    _markRemove( parent, offset, howMany ) → void
    Lock icon private

    Saves and handles a remove change.

    Parameters

    parent : ModelElement | ModelDocumentFragment
    offset : number
    howMany : number

    Returns

    void
  • Chevron-right icon

    _removeAllNestedChanges( parent, offset, howMany ) → void
    Lock icon private

    Removes deeply all buffered changes that are registered in elements from range specified by parent, offset and howMany.

    Parameters

    parent : ModelElement | ModelDocumentFragment
    offset : number
    howMany : number

    Returns

    void
  • Chevron-right icon

    _setElementState( node, state ) → void
    Lock icon private

    Tries to set given state for given item.

    This method does simple validation (it sets the state only for model elements, not for text proxy nodes). It also follows state setting rules, that is, 'refresh' cannot overwrite 'rename', and 'rename' cannot overwrite 'move'.

    Parameters

    node : ModelItem
    state : 'rename' | 'move' | 'refresh'

    Returns

    void