Differ (engine/model)
@ckeditor/ckeditor5-engine/src/model/differ
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.
Filtering
Properties
-
isEmpty : Boolean
module:engine/model/differ~Differ#isEmpty
readonly
Informs whether there are any changes buffered in
Differ
. -
_cachedChanges : Array.<Object> | null
module:engine/model/differ~Differ#_cachedChanges
private
For efficiency purposes,
Differ
stores the change set returned by the differ aftergetChanges
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.
-
_cachedChangesWithGraveyard : Array.<Object> | null
module:engine/model/differ~Differ#_cachedChangesWithGraveyard
private
For efficiency purposes,
Differ
stores the change set returned by the differ after thegetChanges
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. -
_changeCount : Number
module:engine/model/differ~Differ#_changeCount
private
Stores the number of changes that were processed. Used to order the changes chronologically. It is important when changes are sorted.
-
_changedMarkers : Map.<String, Object>
module:engine/model/differ~Differ#_changedMarkers
private
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
.
-
_changesInElement : Map
module:engine/model/differ~Differ#_changesInElement
private
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.
-
_elementSnapshots : Map
module:engine/model/differ~Differ#_elementSnapshots
private
A map that stores "element's children snapshots". A snapshot is representing children of a given element before the first change was applied on that element. Snapshot items are objects with two properties:
name
, containing the element name (or'$text'
for a text node) andattributes
which is a map of the node's attributes. -
_markerCollection : MarkerCollection
module:engine/model/differ~Differ#_markerCollection
private
Reference to the model's marker collection.
-
_refreshedItems : Set.<Item>
module:engine/model/differ~Differ#_refreshedItems
private
Set of model items that were marked to get refreshed in
_refreshItem
.
Methods
-
constructor( markerCollection )
module:engine/model/differ~Differ#constructor
Creates a
Differ
instance.Parameters
markerCollection : MarkerCollection
Model's marker collection.
-
bufferMarkerChange( markerName, oldMarkerData, newMarkerData )
module:engine/model/differ~Differ#bufferMarkerChange
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.
-
bufferOperation( operation )
module:engine/model/differ~Differ#bufferOperation
Buffers the given operation. An operation has to be buffered before it is executed.
Operation type is checked and it is checked which nodes it will affect. These nodes are then stored in
Differ
in the state before the operation is executed.Parameters
operation : Operation
An operation to buffer.
-
getChangedMarkers() → Array.<Object>
module:engine/model/differ~Differ#getChangedMarkers
Returns all markers which changed.
Returns
Array.<Object>
-
getChanges( options = { [options.includeChangesInGraveyard] } ) → Array.<DiffItem>
module:engine/model/differ~Differ#getChanges
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
false
Returns
Array.<DiffItem>
Diff between the old and the new model tree state.
-
getMarkersToAdd() → Array.<Object>
module:engine/model/differ~Differ#getMarkersToAdd
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
andrange
properties.
-
getMarkersToRemove() → Array.<Object>
module:engine/model/differ~Differ#getMarkersToRemove
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
andrange
properties.
-
getRefreshedItems() → Set.<Item>
module:engine/model/differ~Differ#getRefreshedItems
Returns a set of model items that were marked to get refreshed.
Returns
Set.<Item>
-
hasDataChanges() → Boolean
module:engine/model/differ~Differ#hasDataChanges
Checks whether some of the buffered changes affect the editor data.
Types of changes which affect the editor data:
- model structure changes,
- attribute changes,
- changes of markers which were defined as
affectsData
, - changes of markers'
affectsData
property.
Returns
Boolean
-
reset()
module:engine/model/differ~Differ#reset
Resets
Differ
. Removes all buffered changes. -
_refreshItem( item )
module:engine/model/differ~Differ#_refreshItem
protected
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 : Item
Item to refresh.
-
_getAttributesDiff( range, oldAttributes, newAttributes ) → Array.<Object>
module:engine/model/differ~Differ#_getAttributesDiff
private
Returns an array of objects where each one is a single attribute change description.
Parameters
range : Range
The range where the change happened.
oldAttributes : Map
A map, map iterator or compatible object that contains attributes before the change.
newAttributes : Map
A map, map iterator or compatible object that contains attributes after the change.
Returns
Array.<Object>
An array containing one or more diff items.
-
_getChangesForElement( element ) → Array.<Object>
module:engine/model/differ~Differ#_getChangesForElement
private
Gets an array of changes that have already been saved for a given element.
-
_getInsertDiff( parent, offset, elementSnapshot ) → Object
module:engine/model/differ~Differ#_getInsertDiff
private
Returns an object with a single insert change description.
Parameters
parent : Element
The element in which the change happened.
offset : Number
The offset at which change happened.
elementSnapshot : Object
The snapshot of the removed element a character.
Returns
Object
The diff item.
-
_getRemoveDiff( parent, offset, elementSnapshot ) → Object
module:engine/model/differ~Differ#_getRemoveDiff
private
Returns an object with a single remove change description.
Parameters
parent : Element
The element in which change happened.
offset : Number
The offset at which change happened.
elementSnapshot : Object
The snapshot of the removed element a character.
Returns
Object
The diff item.
-
_handleChange( inc, changes )
module:engine/model/differ~Differ#_handleChange
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 : Object
Incoming (new) change.
changes : Array.<Object>
An array containing all the changes done on that element.
-
_isInInsertedElement( element ) → Boolean
module:engine/model/differ~Differ#_isInInsertedElement
private
Checks whether given element or any of its parents is an element that is buffered as an inserted element.
-
_makeSnapshot( element )
module:engine/model/differ~Differ#_makeSnapshot
private
Saves a children snapshot for a given element.
Parameters
element : Element
-
_markAttribute( item )
module:engine/model/differ~Differ#_markAttribute
private
Saves and handles an attribute change.
Parameters
item : Item
-
_markChange( parent, changeItem )
module:engine/model/differ~Differ#_markChange
private
Saves and handles a model change.
Parameters
parent : Element
changeItem : Object
-
_markInsert( parent, offset, howMany )
module:engine/model/differ~Differ#_markInsert
private
Saves and handles an insert change.
Parameters
parent : Element
offset : Number
howMany : Number
-
_markRemove( parent, offset, howMany )
module:engine/model/differ~Differ#_markRemove
private
Saves and handles a remove change.
Parameters
parent : Element
offset : Number
howMany : Number
-
_removeAllNestedChanges( parent, offset, howMany )
module:engine/model/differ~Differ#_removeAllNestedChanges
private
Removes deeply all buffered changes that are registered in elements from range specified by
parent
,offset
andhowMany
.Parameters
parent : Element
offset : Number
howMany : Number
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.