Class

AnnotationCollection (comments/annotations)

@ckeditor/ckeditor5-comments/src/annotations/annotationcollection

class

A collection of annotations.

It implements methods for managing annotations and creates a focus tracker for them to make it easier to manage the focus for all annotations.

AnnotationCollection fires event-focus when an annotation becomes focused and event-blur when all annotations lose focus.

Filtering

Properties

  • readonly inherited

    first : null | T

    Returns the first item from the collection or null when collection is empty.

  • readonly

    isFocused : boolean

    Equals to true when one of the annotation in the collection is focused.

  • readonly inherited

    last : null | T

    Returns the last item from the collection or null when collection is empty.

  • readonly inherited

    length : number

    The number of items available in the collection.

Methods

  • constructor( annotations )

    Parameters

    annotations : Iterable<Annotation>

    Initial annotations.

    Defaults to []

  • inherited

    Symbol.iterator() → Iterator<Annotation, any, undefined>

    Iterable interface.

    Returns

    Iterator<Annotation, any, undefined>
  • add( annotation ) → AnnotationCollection

    Adds an annotation to the collection.

    Parameters

    annotation : Annotation

    Returns

    AnnotationCollection
  • inherited

    addMany( items, [ index ] ) → AnnotationCollection

    Adds multiple items into the collection.

    Any item not containing an id will get an automatically generated one.

    Parameters

    items : Iterable<Annotation>
    [ index ] : number

    The position of the insertion. Items will be appended if no index is specified.

    Returns

    AnnotationCollection

    Fires

  • inherited

    bindTo( externalCollection ) → CollectionBindToChain<S, Annotation>

    Binds and synchronizes the collection with another one.

    The binding can be a simple factory:

    class FactoryClass {
    	public label: string;
    
    	constructor( data: { label: string } ) {
    		this.label = data.label;
    	}
    }
    
    const source = new Collection<{ label: string }>( { idProperty: 'label' } );
    const target = new Collection<FactoryClass>();
    
    target.bindTo( source ).as( FactoryClass );
    
    source.add( { label: 'foo' } );
    source.add( { label: 'bar' } );
    
    console.log( target.length ); // 2
    console.log( target.get( 1 ).label ); // 'bar'
    
    source.remove( 0 );
    console.log( target.length ); // 1
    console.log( target.get( 0 ).label ); // 'bar'
    

    or the factory driven by a custom callback:

    class FooClass {
    	public label: string;
    
    	constructor( data: { label: string } ) {
    		this.label = data.label;
    	}
    }
    
    class BarClass {
    	public label: string;
    
    	constructor( data: { label: string } ) {
    		this.label = data.label;
    	}
    }
    
    const source = new Collection<{ label: string }>( { idProperty: 'label' } );
    const target = new Collection<FooClass | BarClass>();
    
    target.bindTo( source ).using( ( item ) => {
    	if ( item.label == 'foo' ) {
    		return new FooClass( item );
    	} else {
    		return new BarClass( item );
    	}
    } );
    
    source.add( { label: 'foo' } );
    source.add( { label: 'bar' } );
    
    console.log( target.length ); // 2
    console.log( target.get( 0 ) instanceof FooClass ); // true
    console.log( target.get( 1 ) instanceof BarClass ); // true
    

    or the factory out of property name:

    const source = new Collection<{ nested: { value: string } }>();
    const target = new Collection<{ value: string }>();
    
    target.bindTo( source ).using( 'nested' );
    
    source.add( { nested: { value: 'foo' } } );
    source.add( { nested: { value: 'bar' } } );
    
    console.log( target.length ); // 2
    console.log( target.get( 0 ).value ); // 'foo'
    console.log( target.get( 1 ).value ); // 'bar'
    

    It's possible to skip specified items by returning null value:

    const source = new Collection<{ hidden: boolean }>();
    const target = new Collection<{ hidden: boolean }>();
    
    target.bindTo( source ).using( item => {
    	if ( item.hidden ) {
    		return null;
    	}
    
    	return item;
    } );
    
    source.add( { hidden: true } );
    source.add( { hidden: false } );
    
    console.log( source.length ); // 2
    console.log( target.length ); // 1
    

    Note: clear can be used to break the binding.

    Type parameters

    S : extends Record<string, any>

    The type of externalCollection element.

    Parameters

    externalCollection : Collection<S>

    A collection to be bound.

    Returns

    CollectionBindToChain<S, Annotation>

    The binding chain object.

  • inherited

    clear() → void

    Removes all items from the collection and destroys the binding created using bindTo.

    Returns

    void

    Fires

  • inherited

    delegate( events ) → EmitterMixinDelegateChain

    Delegates selected events to another Emitter. For instance:

    emitterA.delegate( 'eventX' ).to( emitterB );
    emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );
    

    then eventX is delegated (fired by) emitterB and emitterC along with data:

    emitterA.fire( 'eventX', data );
    

    and eventY is delegated (fired by) emitterC along with data:

    emitterA.fire( 'eventY', data );
    

    Parameters

    events : Array<string>

    Event names that will be delegated to another emitter.

    Returns

    EmitterMixinDelegateChain
  • destroy() → void

    Destroys all bindings and clears the collection.

    Returns

    void
  • inherited

    filter( callback, [ ctx ] ) → Array<Annotation>

    Returns an array with items for which the callback returned a true value.

    Parameters

    callback : ( Annotation, number ) => boolean
    [ ctx ] : any

    Context in which the callback will be called.

    Returns

    Array<Annotation>

    The array with matching items.

  • inherited

    find( callback, [ ctx ] ) → undefined | Annotation

    Finds the first item in the collection for which the callback returns a true value.

    Parameters

    callback : ( Annotation, number ) => boolean
    [ ctx ] : any

    Context in which the callback will be called.

    Returns

    undefined | Annotation

    The item for which callback returned a true value.

  • inherited

    fire( eventOrInfo, args ) → GetEventInfo<TEvent>[ 'return' ]

    Fires an event, executing all callbacks registered for it.

    The first parameter passed to callbacks is an EventInfo object, followed by the optional args provided in the fire() method call.

    Type parameters

    TEvent : extends BaseEvent

    The type describing the event. See BaseEvent.

    Parameters

    eventOrInfo : GetNameOrEventInfo<TEvent>

    The name of the event or EventInfo object 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 the evt.return's property (the event info is the first param of every callback).

  • inherited

    forEach( callback, [ ctx ] ) → void

    Performs the specified action for each item in the collection.

    Parameters

    callback : ( Annotation, number ) => unknown
    [ ctx ] : any

    Context in which the callback will be called.

    Returns

    void
  • inherited

    get( idOrIndex ) → null | Annotation

    Gets an item by its ID or index.

    Parameters

    idOrIndex : string | number

    The item ID or index in the collection.

    Returns

    null | Annotation

    The requested item or null if such item does not exist.

  • getByInnerView( innerView ) → undefined | Annotation

    Gets the annotation for a given annotation view's inner view.

    Parameters

    innerView : View<HTMLElement>

    Returns

    undefined | Annotation
  • getByView( view ) → undefined | Annotation

    Gets the annotation for a given annotation view.

    Parameters

    view : View<HTMLElement>

    Returns

    undefined | Annotation
  • inherited

    getIndex( itemOrId ) → number

    Gets an index of an item in the collection. When an item is not defined in the collection, the index will equal -1.

    Parameters

    itemOrId : string | Annotation

    The item or its ID in the collection.

    Returns

    number

    The index of a given item.

  • inherited

    has( itemOrId ) → boolean

    Returns a Boolean indicating whether the collection contains an item.

    Parameters

    itemOrId : string | Annotation

    The item or its ID in the collection.

    Returns

    boolean

    true if the collection contains the item, false otherwise.

  • inherited

    listenTo( emitter, event, callback, [ options ] ) → void

    Registers 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' );
    

    An event callback can stop the event and set the return value of the fire method.

    Type parameters

    TEvent : extends BaseEvent

    The type describing the event. See BaseEvent.

    Parameters

    emitter : Emitter

    The 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
  • inherited

    map( callback, [ ctx ] ) → Array<U>

    Executes the callback for each item in the collection and composes an array or values returned by this callback.

    Type parameters

    U

    The result type of the callback.

    Parameters

    callback : ( Annotation, number ) => U
    [ ctx ] : any

    Context in which the callback will be called.

    Returns

    Array<U>

    The result of mapping.

  • inherited

    off( event, callback ) → void

    Stops executing the callback on the given event. Shorthand for this.stopListening( this, event, callback ).

    Parameters

    event : string

    The name of the event.

    callback : Function

    The function to stop being called.

    Returns

    void
  • inherited

    on( event, callback, [ options ] ) → void

    Registers 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

    TEvent : extends BaseEvent

    The type descibing the event. See BaseEvent.

    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
  • inherited

    once( event, callback, [ options ] ) → void

    Registers a callback function to be executed on the next time the event is fired only. This is similar to calling on followed by off in the callback.

    Type parameters

    TEvent : extends BaseEvent

    The type descibing the event. See BaseEvent.

    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
  • refreshPositioning() → void

    Refreshes the positioning of all annotations and sorts them topmost and leftmost.

    Returns

    void
  • remove( annotation ) → Annotation

    Removes the annotation from the collection.

    Parameters

    annotation : Annotation

    Returns

    Annotation
  • inherited

    stopDelegating( [ event ], [ emitter ] ) → void

    Stops 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 ] : string

    The 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 of event to all emitters.

    Returns

    void
  • inherited

    stopListening( [ emitter ], [ event ], [ callback ] ) → void

    Stops 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 ] : Emitter

    The 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 from emitter.

    [ callback ] : Function

    (Requires the event) The function to be removed from the call list for the given event.

    Returns

    void

Events

  • inherited

    add( eventInfo, item, index )

    Fired when an item is added to the collection.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    item : T

    The added item.

    index : number

    An index where the addition occurred.

  • blur( eventInfo )

    Fired when all annotations become blurred.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

  • inherited

    change( eventInfo, data )

    Fired when the collection was changed due to adding or removing items.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    data : CollectionChangeEventData<T>

    Changed items.

  • focus( eventInfo, annotation )

    Fired when an annotation becomes active.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    annotation : Annotation

    An annotation that was focused.

  • inherited

    remove( eventInfo, item, index )

    Fired when an item is removed from the collection.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    item : T

    The removed item.

    index : number

    Index from which item was removed.