UNPKG

@atlaskit/editor-plugin-ufo

Version:

Ufo plugin for @atlaskit/editor-core

68 lines (65 loc) 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.abortUFOMeasurementOnFirstUserInteraction = void 0; var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _interactionMetrics = require("@atlaskit/react-ufo/interaction-metrics"); /* eslint-disable @repo/internal/dom-events/no-unsafe-event-listeners */ 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 var abortUFOMeasurementOnFirstUserInteraction = exports.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.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 () { (0, _interactionMetrics.abortAll)('new_interaction'); unbindFirstInteractionEvents(); })); } return { destroy: function destroy() { unbindFirstInteractionEvents(); } }; } }); };