UNPKG

@atlaskit/editor-plugin-ufo

Version:

Ufo plugin for @atlaskit/editor-core

61 lines (60 loc) 2.02 kB
/* eslint-disable @repo/internal/dom-events/no-unsafe-event-listeners */ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { abortAll } from '@atlaskit/react-ufo/interaction-metrics'; function bind(target, event, controller, listener) { // Safe check for rare cases where window doesn't exist if (!target || typeof target.addEventListener !== 'function') { return function () {}; } var options = { capture: true, passive: true, once: true, signal: controller.signal }; var listenerWrapper = function listenerWrapper(event) { if (event.isTrusted) { listener(); } }; target.addEventListener(event, listenerWrapper, options); return function unbind() { target.removeEventListener(event, listenerWrapper, options); }; } var AbortEvent = ['wheel', 'keydown', 'mousedown', 'pointerdown', 'pointerup', 'touchend', 'scroll', 'mouseover']; // eslint-disable-next-line @typescript-eslint/no-explicit-any export var abortUFOMeasurementOnFirstUserInteraction = function abortUFOMeasurementOnFirstUserInteraction() { if (typeof window.AbortController !== 'function') { return; } var unbindCallbacks = new Set(); var controller = new AbortController(); var unbindFirstInteractionEvents = function unbindFirstInteractionEvents() { controller.abort(); unbindCallbacks.forEach(function (cb) { cb(); }); unbindCallbacks.clear(); }; return new SafePlugin({ view: function view(_view) { if (!_view || !_view.dom) { return {}; } var dom = _view.dom; for (var _i = 0, _AbortEvent = AbortEvent; _i < _AbortEvent.length; _i++) { var abortEventName = _AbortEvent[_i]; unbindCallbacks.add(bind(dom, abortEventName, controller, function () { abortAll('new_interaction'); unbindFirstInteractionEvents(); })); } return { destroy: function destroy() { unbindFirstInteractionEvents(); } }; } }); };