utils/dom/shadowrootregistry
Classes
module:utils/dom/shadowrootregistry~ShadowRootRegistry
Type Definitions
module:utils/dom/shadowrootregistry~ShadowRootRegistryAddEventmodule:utils/dom/shadowrootregistry~ShadowRootRegistryChangeEventRootsEventmodule:utils/dom/shadowrootregistry~ShadowRootRegistryRemoveEvent
Functions
listenToEventRoots( registry, options ) → () => voidmodule:utils/dom/shadowrootregistry~listenToEventRootsAttaches
emitter's callback to the globaldocumentand to every event root tracked byregistry, and keeps that up to date – attaching to roots that appear later, detaching from roots that disappear – for as long as the returned function is not called.It replaces a pair of a
documentlistener andlistenToShadowRoots:
Copy codeconst stopListening = listenToEventRoots( editor.ui.shadowRootRegistry, { emitter: view, event: 'scroll', listenerOptions: { useCapture: true }, callback: repositionOnScroll } );The global
documentis always listened to, even if no registered node lives in it, so the callback receives the same events as a listener ondocumentwould, plus the events of the other event roots. Each root is listened to once, no matter how many registered nodes it hosts.As with
listenToShadowRoots, useuseCapture: truefor events that do not bubble, such asscroll.Call the returned function to detach. There is nothing to refresh, as the listener follows the registry on its own.
Type parameters
TEvent: extends keyof DomEventMap
Parameters
registry: ShadowRootRegistryThe registry whose event roots should be listened to.
options: DomTargetsListenerOptions<TEvent>Describes the listener to attach to each event root.
Returns
() => void
listenToShadowRoots( registry, options ) → () => voidmodule:utils/dom/shadowrootregistry~listenToShadowRootsSome DOM events never reach a listener on
documentif they originate inside a shadow root —scrollandmouseenter/mouseleave, for instance, do not cross a shadow boundary at all. This function is how code that needs those events anyway reacts to them for every shadow root a registry is currently tracking: it attachesemitter's callback directly to each one tracked byregistry, and keeps that up to date — attaching to roots that appear later, detaching from roots that disappear — for as long as the returned function is not called.Attach it alongside the
documentlistener rather than instead of it — one covers the light DOM, the other each shadow root:
Copy codeview.listenTo( global.document, 'scroll', repositionOnScroll, { useCapture: true } ); const stopListening = listenToShadowRoots( editor.ui.shadowRootRegistry, { emitter: view, event: 'scroll', listenerOptions: { useCapture: true }, callback: repositionOnScroll } );To cover both with a single call, use
listenToEventRootsinstead.The
useCaptureabove is not incidental. The events this function exists for do not bubble when fired on an element, so a listener on a shadow root only sees one that originates inside that root during the capture phase. Without it the listener is attached but never fires.Call the returned function to detach. There is nothing to refresh, as the listener follows the registry on its own.
Type parameters
TEvent: extends keyof DomEventMap
Parameters
registry: ShadowRootRegistryThe registry whose shadow roots should be listened to.
options: DomTargetsListenerOptions<TEvent>Describes the listener to attach to each shadow root.
Returns
() => void