ShadowRootRegistry
Keeps track of the shadow roots hosting a set of registered DOM nodes (for example an editor UI's editables, toolbars and menu bars, or a feature's own UI), so that consumers can, for instance, inject styles or listeners into every shadow root those nodes currently live in.
It also keeps track of the wider set of the event roots of those nodes – the shadow roots plus the documents the nodes are connected to – for consumers that listen to DOM events of those nodes.
Properties
_rootsByNode: Map<Node, Array<Document | ShadowRoot>>privatemodule:utils/dom/shadowrootregistry~ShadowRootRegistry#_rootsByNodeThe event roots of each registered node, as of the last time that node was looked at: the shadow roots hosting it and the document it is connected to. Empty for a node that was detached then. Re-derived for every node by each
refresh, so a node that has since been attached, or moved to another tree, is picked up. The union of the values isgetEventRoots, and its shadow roots aregetShadowRoots.
Methods
constructor( args )inheritedmodule:utils/dom/shadowrootregistry~ShadowRootRegistry#constructordelegate( events ) → EmitterMixinDelegateChaininheritedmodule:utils/dom/shadowrootregistry~ShadowRootRegistry#delegateDelegates selected events to another
Emitter. For instance:
Copy codeemitterA.delegate( 'eventX' ).to( emitterB ); emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );then
eventXis delegated (fired by)emitterBandemitterCalong withdata:
Copy codeemitterA.fire( 'eventX', data );and
eventYis delegated (fired by)emitterCalong withdata:
Copy codeemitterA.fire( 'eventY', data );Parameters
events: Array<string>Event names that will be delegated to another emitter.
Returns
fire( eventOrInfo, args ) → GetEventInfo<TEvent>[ 'return' ]inheritedmodule:utils/dom/shadowrootregistry~ShadowRootRegistry#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).
getEventRoots() → Set<Document | ShadowRoot>module:utils/dom/shadowrootregistry~ShadowRootRegistry#getEventRootsReturns the event roots of the currently registered nodes: every shadow root hosting at least one of them, as in
getShadowRoots, and every document at least one of them is connected to.See
getEventRootsfor what an event root is.Returns
Set<Document | ShadowRoot>
getShadowRoots() → Set<ShadowRoot>module:utils/dom/shadowrootregistry~ShadowRootRegistry#getShadowRootsReturns the shadow roots currently hosting at least one registered node.
Returns
Set<ShadowRoot>
listenTo( emitter, event, callback, options? ) → voidinheritedmodule:utils/dom/shadowrootregistry~ShadowRootRegistry#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.
Copy code// 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
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:utils/dom/shadowrootregistry~ShadowRootRegistry#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:utils/dom/shadowrootregistry~ShadowRootRegistry#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:utils/dom/shadowrootregistry~ShadowRootRegistry#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
refresh() → voidmodule:utils/dom/shadowrootregistry~ShadowRootRegistry#refreshRe-derives the shadow roots of every registered node, firing event-add for the roots that started hosting one and event-remove for those that stopped. Fires event-changeEventRoots if the event roots changed.
Call it whenever a registered node may have been attached, detached, or moved – for example on every editor UI update. Nodes are re-derived rather than resolved once, because the tree a node lives in is not fixed: an editable mounted by the integrator after the editor was created, or moved into a fullscreen container in another tree, ends up somewhere its roots were never derived from.
Returns
void
registerNode( node ) → voidmodule:utils/dom/shadowrootregistry~ShadowRootRegistry#registerNodeRegisters a node, so the shadow roots hosting it become a part of
getShadowRoots.The node does not have to be attached to the DOM yet; while it is not, it simply contributes no shadow root. The next
refreshpicks it up once it is attached.Parameters
node: Node
Returns
void
stopDelegating( event?, emitter? ) → voidinheritedmodule:utils/dom/shadowrootregistry~ShadowRootRegistry#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:utils/dom/shadowrootregistry~ShadowRootRegistry#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
unregisterNode( node ) → voidmodule:utils/dom/shadowrootregistry~ShadowRootRegistry#unregisterNodeUnregisters a previously registered node. Shadow roots that no longer host any registered node stop being a part of
getShadowRoots.Parameters
node: Node
Returns
void
destroy() → voidinternalmodule:utils/dom/shadowrootregistry~ShadowRootRegistry#destroyUnregisters every currently registered node at once, firing event-remove for every shadow root that consequently stops being tracked, and event-changeEventRoots if any event root was tracked.
Returns
void
_fireRootChanges( previousRoots ) → voidprivatemodule:utils/dom/shadowrootregistry~ShadowRootRegistry#_fireRootChangesFires event-add and event-remove for the difference between the shadow roots in the given set of tracked event roots and the current ones, followed by a single event-changeEventRoots if the event roots differ at all.
Parameters
previousRoots: Set<Document | ShadowRoot>
Returns
void
Events
add( eventInfo, root )module:utils/dom/shadowrootregistry~ShadowRootRegistry#event:addFired when a shadow root starts hosting a registered node.
Parameters
eventInfo: EventInfoAn object containing information about the fired event.
root: ShadowRootThe shadow root that started being tracked.
changeEventRoots( eventInfo )module:utils/dom/shadowrootregistry~ShadowRootRegistry#event:changeEventRootsFired when the event roots of the registered nodes change: a shadow root or a document starts or stops hosting a registered node. Fired once per change, after any event-add and event-remove events, so read the current roots with
getEventRoots.Parameters
eventInfo: EventInfoAn object containing information about the fired event.
remove( eventInfo, root )module:utils/dom/shadowrootregistry~ShadowRootRegistry#event:removeFired when a shadow root no longer hosts any registered node.
Parameters
eventInfo: EventInfoAn object containing information about the fired event.
root: ShadowRootThe shadow root that stopped being tracked.