Marker
Marker is a continuous part of the model (like a range), is named and represents some kind of information about the
marked part of the model document. In contrary to nodes, which are building blocks of
the model document tree, markers are not stored directly in the document tree but in the
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
ModelWriterto theMarkerCollectionwithout 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
MarkerCollectionbut 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 ModelWriter 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.
Properties
-
affectsData : booleanreadonlymodule:engine/model/markercollection~Marker#affectsDataA value indicating if the marker changes the data.
-
managedUsingOperations : booleanreadonlymodule:engine/model/markercollection~Marker#managedUsingOperationsA value indicating if the marker is managed using operations. See marker class description to learn more about marker types. See
addMarker. -
name : stringreadonlymodule:engine/model/markercollection~Marker#nameMarker's name.
-
_affectsData : booleaninternalmodule:engine/model/markercollection~Marker#_affectsDataSpecifies whether the marker affects the data produced by the data pipeline (is persisted in the editor's data).
-
_managedUsingOperations : booleaninternalmodule:engine/model/markercollection~Marker#_managedUsingOperationsFlag indicates if the marker is managed using operations or not.
-
_liveRange : null | ModelLiveRangeprivatemodule:engine/model/markercollection~Marker#_liveRangeRange marked by the marker.
Methods
-
constructor( name, liveRange, managedUsingOperations, affectsData )module:engine/model/markercollection~Marker#constructorCreates a marker instance.
Parameters
name : stringMarker name.
liveRange : ModelLiveRangeRange marked by the marker.
managedUsingOperations : booleanSpecifies whether the marker is managed using operations.
affectsData : booleanSpecifies whether the marker affects the data produced by the data pipeline (is persisted in the editor's data).
-
delegate( events ) → EmitterMixinDelegateChaininheritedmodule:engine/model/markercollection~Marker#delegateDelegates selected events to another
Emitter. For instance:emitterA.delegate( 'eventX' ).to( emitterB ); emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );Copy codethen
eventXis delegated (fired by)emitterBandemitterCalong withdata:emitterA.fire( 'eventX', data );Copy codeand
eventYis delegated (fired by)emitterCalong withdata:emitterA.fire( 'eventY', data );Copy codeParameters
events : Array<string>Event names that will be delegated to another emitter.
Returns
-
fire( eventOrInfo, args ) → GetEventInfo<TEvent>[ 'return' ]inheritedmodule:engine/model/markercollection~Marker#fireFires an event, executing all callbacks registered for it.
The first parameter passed to callbacks is an
EventInfoobject, followed by the optionalargsprovided in thefire()method call.Type parameters
Parameters
eventOrInfo : GetNameOrEventInfo<TEvent>The name of the event or
EventInfoobject 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 theevt.return's property (the event info is the first param of every callback).
-
getData() → MarkerDatamodule:engine/model/markercollection~Marker#getDataReturns the marker data (properties defining the marker).
Returns
-
getEnd() → ModelPositionmodule:engine/model/markercollection~Marker#getEnd -
getRange() → ModelRangemodule:engine/model/markercollection~Marker#getRangeReturns a range that represents the current state of the marker.
Keep in mind that returned value is a Range, not a ModelLiveRange. 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
nameand getMarkerinstance 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
-
module:engine/model/markercollection~Marker#getStart -
is( type ) → this is ModelElement | ModelRootElementinheritedmodule:engine/model/markercollection~Marker#is:ELEMENTChecks whether the object is of type
ModelElementor 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' ); // -> falseCopy codeAssuming that the object being checked is an element, you can also check its name:
element.is( 'element', 'imageBlock' ); // -> true if this is an <imageBlock> element text.is( 'element', 'imageBlock' ); -> falseCopy codeParameters
type : 'element' | 'model:element'
Returns
this is ModelElement | ModelRootElement
-
is( type ) → this is ModelRootElementinheritedmodule:engine/model/markercollection~Marker#is:ROOT_ELEMENTChecks whether the object is of type
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' ); // -> falseCopy codeAssuming that the object being checked is an element, you can also check its name:
rootElement.is( 'rootElement', '$root' ); // -> same as aboveCopy codeParameters
type : 'rootElement' | 'model:rootElement'
Returns
this is ModelRootElement
-
module:engine/model/markercollection~Marker#is:TEXTChecks whether the object is of type
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' ); // -> falseCopy codeNote: 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
-
is( type ) → this is ModelLiveRangeinheritedmodule:engine/model/markercollection~Marker#is:LIVE_RANGEChecks whether the object is of type
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' ); // -> falseCopy codeParameters
type : 'liveRange' | 'model:liveRange'
Returns
this is ModelLiveRange
-
is( type ) → this is ModelDocumentFragmentinheritedmodule:engine/model/markercollection~Marker#is:DOCUMENT_FRAGMENTChecks whether the object is of type
ModelDocumentFragment.docFrag.is( 'documentFragment' ); // -> true docFrag.is( 'model:documentFragment' ); // -> true docFrag.is( 'view:documentFragment' ); // -> false docFrag.is( 'element' ); // -> false docFrag.is( 'node' ); // -> falseCopy codeParameters
type : 'documentFragment' | 'model:documentFragment'
Returns
this is ModelDocumentFragment
-
is( type ) → this is ModelRange | ModelLiveRangeinheritedmodule:engine/model/markercollection~Marker#is:RANGEChecks whether the object is of type
ModelRangeor its subclass.range.is( 'range' ); // -> true range.is( 'model:range' ); // -> true range.is( 'view:range' ); // -> false range.is( 'documentSelection' ); // -> falseCopy codeParameters
type : 'range' | 'model:range'
Returns
this is ModelRange | ModelLiveRange
-
is( type ) → this is ModelLivePositioninheritedmodule:engine/model/markercollection~Marker#is:LIVE_POSITIONChecks whether the object is of type
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' ); // -> falseCopy codeParameters
type : 'livePosition' | 'model:livePosition'
Returns
this is ModelLivePosition
-
is( type ) → this is ModelPosition | ModelLivePositioninheritedmodule:engine/model/markercollection~Marker#is:POSITIONChecks whether the object is of type
ModelPositionor its subclass.position.is( 'position' ); // -> true position.is( 'model:position' ); // -> true position.is( 'view:position' ); // -> false position.is( 'documentSelection' ); // -> falseCopy codeParameters
type : 'position' | 'model:position'
Returns
this is ModelPosition | ModelLivePosition
-
is( type ) → this is ModelSelection | ModelDocumentSelectioninheritedmodule:engine/model/markercollection~Marker#is:SELECTIONChecks whether the object is of type
ModelSelectionorModelDocumentSelection.selection.is( 'selection' ); // -> true selection.is( 'model:selection' ); // -> true selection.is( 'view:selection' ); // -> false selection.is( 'range' ); // -> falseCopy codeParameters
type : 'selection' | 'model:selection'
Returns
this is ModelSelection | ModelDocumentSelection
-
module:engine/model/markercollection~Marker#is:MARKER -
is( type, name ) → booleaninheritedmodule:engine/model/markercollection~Marker#is:ELEMENT_NAMEChecks whether the object is of type
ModelElementor its subclass and has the specifiedname.element.is( 'element', 'imageBlock' ); // -> true if this is an <imageBlock> element text.is( 'element', 'imageBlock' ); -> falseCopy codeType parameters
N : extends string
Parameters
type : 'element' | 'model:element'name : N
Returns
boolean
-
is( type, name ) → booleaninheritedmodule:engine/model/markercollection~Marker#is:ROOT_ELEMENT_NAMEChecks whether the object is of type
ModelRootElementand has the specifiedname.rootElement.is( 'rootElement', '$root' );Copy codeType parameters
N : extends string
Parameters
type : 'rootElement' | 'model:rootElement'name : N
Returns
boolean
-
is( type ) → this is ModelTextProxyinheritedmodule:engine/model/markercollection~Marker#is:TEXT_PROXYChecks whether the object is of type
ModelTextProxy.textProxy.is( '$textProxy' ); // -> true textProxy.is( 'model:$textProxy' ); // -> true textProxy.is( 'view:$textProxy' ); // -> false textProxy.is( 'range' ); // -> falseCopy codeNote: 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
-
is( type ) → this is ModelDocumentSelectioninheritedmodule:engine/model/markercollection~Marker#is:DOCUMENT_SELECTIONChecks whether the object is of type
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' ); // -> falseCopy codeParameters
type : 'documentSelection' | 'model:documentSelection'
Returns
this is ModelDocumentSelection
-
is( type ) → this is ModelNode | ModelText | ModelElement | ModelRootElementinheritedmodule:engine/model/markercollection~Marker#is:NODEChecks whether the object is of type
ModelNodeor its subclass.This method is useful when processing model objects that are of unknown type. For example, a function may return a
ModelDocumentFragmentor aModelNodethat 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 fragmentCopy codeSince this method is also available on a range of view objects, you can prefix the type of the object with
model:orview:to check, for example, if this is the model's or view's element:modelElement.is( 'model:element' ); // -> true modelElement.is( 'view:element' ); // -> falseCopy codeBy 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 preciseCopy codeParameters
type : 'node' | 'model:node'
Returns
this is ModelNode | ModelText | ModelElement | ModelRootElement
-
listenTo( emitter, event, callback, [ options ] ) → voidinheritedmodule:engine/model/markercollection~Marker#listenTo:BASE_EMITTERRegisters 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' );Copy codeAn event callback can stop the event and set the return value of the
firemethod.Type parameters
Parameters
emitter : EmitterThe object that fires the event.
event : TEvent[ 'name' ]The name of the event.
callback : GetCallback<TEvent>The function to be called on event.
[ options ] : GetCallbackOptions<TEvent>Additional options.
Returns
void
-
off( event, callback ) → voidinheritedmodule:engine/model/markercollection~Marker#offStops executing the callback on the given event. Shorthand for
this.stopListening( this, event, callback ).Parameters
event : stringThe name of the event.
callback : FunctionThe function to stop being called.
Returns
void
-
on( event, callback, [ options ] ) → voidinheritedmodule:engine/model/markercollection~Marker#onRegisters a callback function to be executed when an event is fired.
Shorthand for
this.listenTo( this, event, callback, options )(it makes the emitter listen on itself).Type parameters
Parameters
event : TEvent[ 'name' ]The name of the event.
callback : GetCallback<TEvent>The function to be called on event.
[ options ] : GetCallbackOptions<TEvent>Additional options.
Returns
void
-
once( event, callback, [ options ] ) → voidinheritedmodule:engine/model/markercollection~Marker#onceRegisters a callback function to be executed on the next time the event is fired only. This is similar to calling
onfollowed byoffin the callback.Type parameters
Parameters
event : TEvent[ 'name' ]The name of the event.
callback : GetCallback<TEvent>The function to be called on event.
[ options ] : GetCallbackOptions<TEvent>Additional options.
Returns
void
-
stopDelegating( [ event ], [ emitter ] ) → voidinheritedmodule:engine/model/markercollection~Marker#stopDelegatingStops 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
[ event ] : stringThe name of the event to stop delegating. If omitted, stops it all delegations.
[ emitter ] : Emitter(requires
event) The object to stop delegating a particular event to. If omitted, stops delegation ofeventto all emitters.
Returns
void
-
stopListening( [ emitter ], [ event ], [ callback ] ) → voidinheritedmodule:engine/model/markercollection~Marker#stopListening:BASE_STOPStops 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
[ emitter ] : EmitterThe object to stop listening to. If omitted, stops it for all objects.
[ event ] : string(Requires the
emitter) The name of the event to stop listening to. If omitted, stops it for all events fromemitter.[ callback ] : Function(Requires the
event) The function to be removed from the call list for the givenevent.
Returns
void
-
toJSON() → unknownmodule:engine/model/markercollection~Marker#toJSONConverts
Markerto plain object and returns it.Returns
unknownMarkerconverted to plain object.
-
_attachLiveRange( liveRange ) → ModelLiveRangeinternalmodule:engine/model/markercollection~Marker#_attachLiveRangeBinds new live range to the marker and detach the old one if is attached.
Parameters
liveRange : ModelLiveRangeLive range to attach
Returns
ModelLiveRangeAttached live range.
-
_detachLiveRange() → voidinternalmodule:engine/model/markercollection~Marker#_detachLiveRange
Events
-
change:content( eventInfo, range, data )module:engine/model/markercollection~Marker#event:change:contentFired whenever change on
ModelDocumentis done inside marker range. This is a delegated ModelLiveRange 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 : EventInfoAn object containing information about the fired event.
range : ModelRangedata : object
Related:
-
change:range( eventInfo, range, data )module:engine/model/markercollection~Marker#event:change:rangeFired whenever marker range is changed due to changes on
ModelDocument. This is a delegated ModelLiveRange 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 : EventInfoAn object containing information about the fired event.
range : ModelRangedata : object
Related: