Sign up (with export icon)

DomTargetsListener

Api-class iconclass

Keeps emitter's callback attached to exactly the DOM event targets returned by getTargets(), for code whose targets are known only later or change over time.

To listen to the events of the whole editor UI, use listenToEventRoots instead, which follows the editor's registry on its own. Use this listener for particular elements, such as the positioning target of a balloon or the limiter of a sticky panel: return document and their event roots from getTargets(). Unlike the registry listeners, it does not listen to document on its own.

The targets are read once, when the listener is created. Call refresh whenever they may have changed: it calls getTargets() again, attaches the callback to the targets that appeared and detaches it from the targets that disappeared, leaving the others alone. Call destroy to detach the callback from every target:

const scrollListener = new DomTargetsListener( () => [ document, ...getEventRoots( limiterElement ) ], {
	emitter: view,
	event: 'scroll',
	listenerOptions: { useCapture: true },
	callback: onScroll
} );

// For example once the limiter was moved to another shadow root.
scrollListener.refresh();

// For example when the view is destroyed.
scrollListener.destroy();
Copy code

Only this callback is ever detached, so other listeners of the same emitter on the same targets are not affected.

Type parameters

Properties

Methods

  • Chevron-right icon

    constructor( getTargets, options )

    Creates the listener and attaches the callback to the targets returned by getTargets().

    Type parameters

    TEvent: extends keyof DomEventMap

    Parameters

    getTargets: () => Iterable<EventTarget | null | undefined>

    Returns the targets that should be listened to. null and undefined entries are skipped.

    options: DomTargetsListenerOptions<TEvent>

    Describes the listener to attach to each target.

  • Chevron-right icon

    destroy() → void

    Detaches the callback from every target.

    Returns

    void
  • Chevron-right icon

    refresh() → void

    Reads the targets again: attaches the callback to the targets that appeared and detaches it from the targets that disappeared.

    Returns

    void
  • Chevron-right icon

    _attachTo( targets ) → void
    Lock iconprivate

    Attaches the callback to exactly the given targets.

    Parameters

    targets: Set<EventTarget>

    Returns

    void