Marker (engine/model)
@ckeditor/ckeditor5-engine/src/model/markercollection
Marker
is a continuous parts of model (like a range), is named and represent some kind of information about marked
part of model document. In contrary to nodes, which are building blocks of
model document tree, markers are not stored directly in document tree but in
model markers' collection. Still, they are document data, by giving
additional meaning to the part of a model document between marker start and marker end.
In this sense, markers are similar to adding and converting attributes on nodes. The difference is that attribute is connected with a given node (e.g. a character is bold no matter if it gets moved or content around it changes). Markers on the other hand are continuous ranges and are characterized by their start and end position. This means that any character in the marker is marked by the marker. For example, if a character is moved outside of marker it stops being "special" and the marker is shrunk. Similarly, when a character is moved into the marker from other place in document model, it starts being "special" and the marker is enlarged.
Another upside of markers is that finding marked part of document is fast and easy. Using attributes to mark some nodes and then trying to find that part of document would require traversing whole document tree. Marker gives instant access to the range which it is marking at the moment.
Markers are built from a name and a range.
Range of the marker is updated automatically when document changes, using live range mechanism.
Name is used to group and identify markers. Names have to be unique, but markers can be grouped by
using common prefixes, separated with :
, for example: user:john
or search:3
. That's useful in term of creating
namespaces for custom elements (e.g. comments, highlights). You can use this prefixes in
event-update
listeners to listen on changes in a group of markers.
For instance: model.markers.on( 'update:user', callback );
will be called whenever any user:*
markers changes.
There are two types of markers.
-
Markers managed directly, without using operations. They are added directly by
Writer
to theMarkerCollection
without any additional mechanism. They can be used as bookmarks or visual markers. They are great for showing results of the find, or select link when the focus is in the input. -
Markers managed using operations. These markers are also stored in
MarkerCollection
but changes in these markers is managed the same way all other changes in the model structure - using operations. Therefore, they are handled in the undo stack and synchronized between clients if the collaboration plugin is enabled. This type of markers is useful for solutions like spell checking or comments.
Both type of them should be added / updated by addMarker
and removed by removeMarker
methods.
model.change( ( writer ) => {
const marker = writer.addMarker( name, { range, usingOperation: true } );
// ...
writer.removeMarker( marker );
} );
See Writer
to find more examples.
Since markers need to track change in the document, for efficiency reasons, it is best to create and keep as little markers as possible and remove them as soon as they are not needed anymore.
Markers can be downcasted and upcasted.
Markers downcast happens on event-addMarker
and
event-removeMarker
events.
Use downcast converters or attach a custom converter to mentioned events.
For data pipeline, marker should be downcasted to an element.
Then, it can be upcasted back to a marker. Again, use upcast converters or
attach a custom converter to event-element
.
Marker
instances are created and destroyed only by MarkerCollection.
Filtering
Properties
-
affectsData
module:engine/model/markercollection~Marker#affectsData
A value indicating if the marker changes the data.
-
managedUsingOperations
module:engine/model/markercollection~Marker#managedUsingOperations
A value indicating if the marker is managed using operations. See marker class description to learn more about marker types. See
addMarker
. -
name : String
module:engine/model/markercollection~Marker#name
readonly
Marker's name.
-
_liveRange : LiveRange
module:engine/model/markercollection~Marker#_liveRange
protected
Range marked by the marker.
-
_affectsData : Boolean
module:engine/model/markercollection~Marker#_affectsData
private
Specifies whether the marker affects the data produced by the data pipeline (is persisted in the editor's data).
-
_managedUsingOperations : Boolean
module:engine/model/markercollection~Marker#_managedUsingOperations
private
Flag indicates if the marker is managed using operations or not.
Methods
-
constructor( name, liveRange, managedUsingOperations, affectsData )
module:engine/model/markercollection~Marker#constructor
Creates a marker instance.
Parameters
name : String
Marker name.
liveRange : LiveRange
Range marked by the marker.
managedUsingOperations : Boolean
Specifies whether the marker is managed using operations.
affectsData : Boolean
Specifies whether the marker affects the data produced by the data pipeline (is persisted in the editor's data).
-
getData() → MarkerData
module:engine/model/markercollection~Marker#getData
Returns the marker data (properties defining the marker).
Returns
-
Returns current marker end position.
Returns
-
Returns a range that represents the current state of the marker.
Keep in mind that returned value is a Range, not a LiveRange. This means that it is up-to-date and relevant only until next model document change. Do not store values returned by this method. Instead, store
name
and getMarker
instance from MarkerCollection every time there is a need to read marker properties. This will guarantee that the marker has not been removed and that it's data is up-to-date.Returns
-
Returns current marker start position.
Returns
-
is( type ) → Boolean
module:engine/model/markercollection~Marker#is
Checks whether this object is of the given.
marker.is( 'marker' ); // -> true marker.is( 'model:marker' ); // -> true marker.is( 'view:element' ); // -> false marker.is( 'documentSelection' ); // -> false
Check the entire list of model objects which implement the
is()
method.Parameters
type : String
Returns
Boolean
-
_attachLiveRange( liveRange ) → LiveRange
module:engine/model/markercollection~Marker#_attachLiveRange
protected
Binds new live range to the marker and detach the old one if is attached.
-
_detachLiveRange()
module:engine/model/markercollection~Marker#_detachLiveRange
protected
Unbinds and destroys currently attached live range.
Events
-
change:content( eventInfo, oldRange, data )
module:engine/model/markercollection~Marker#event:change:content
Fired whenever change on
Document
is done inside marker range. This is a delegated LiveRange change:content event.When marker is removed from MarkerCollection, all event listeners listening to it should be removed. It is best to do it on MarkerCollection update event.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
oldRange : Range
data : Object
Related:
-
change:range( eventInfo, oldRange, data )
module:engine/model/markercollection~Marker#event:change:range
Fired whenever marker range is changed due to changes on
Document
. This is a delegated LiveRange change:range event.When marker is removed from MarkerCollection, all event listeners listening to it should be removed. It is best to do it on MarkerCollection update event.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
oldRange : Range
data : Object
Related:
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.