UNPKG

@atlaskit/editor-plugin-interactivity

Version:

Interactivity plugin for @atlaskit/editor-core

46 lines (44 loc) 1.37 kB
import { bindAll } from 'bind-event-listener'; import { INTERACTION_EVENT_TYPES } from './interaction-events'; /** * Reports the events the interactions with the editor are made of, as the browser dispatched * them. What they mean is the tracker's business. * * Event Timing cannot answer which interactions were with the editor, because it observes the * whole document, nor how many there were, because it reports none below 16 ms. */ export class EditorEventObserver { constructor(onEvent) { this.onEvent = onEvent; } /** * Called as the element the editor renders itself into changes: the editor only knows it after * its first render, and can replace it. */ observe(root) { const next = root !== null && root !== void 0 ? root : undefined; if (next === this.root) { return; } this.stop(); this.root = next; if (!next) { return; } this.unbind = bindAll(next, INTERACTION_EVENT_TYPES.map(type => ({ type, listener: this.onEvent })), // Capture, so a handler in the editor cannot stop an interaction from being reported. { capture: true, passive: true }); } stop() { var _this$unbind; (_this$unbind = this.unbind) === null || _this$unbind === void 0 ? void 0 : _this$unbind.call(this); this.unbind = undefined; this.root = undefined; } }