UNPKG

@atlaskit/editor-plugin-quick-insert

Version:

Quick insert plugin for @atlaskit/editor-core

215 lines (214 loc) 8.38 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.createQuickInsertItemsAnalyticsScheduler = exports.calculateQuickInsertItemsAttributes = exports.QUICK_INSERT_ITEMS_ANALYTICS_DELAY_MS = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _analytics = require("@atlaskit/editor-common/analytics"); var _getMenuItemParents = require("@atlaskit/editor-common/quick-insert/get-menu-item-parents"); var _keys = require("@atlaskit/editor-common/quick-insert/keys"); function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var QUICK_INSERT_ITEMS_ANALYTICS_DELAY_MS = exports.QUICK_INSERT_ITEMS_ANALYTICS_DELAY_MS = 10000; var sectionCategoryKeys = (0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)({}, _keys.BLOCK_TEMPLATES_SECTION.key, 'blockTemplates'), _keys.DATA_AND_CHARTS_SECTION.key, 'dataAndCharts'), _keys.EMBED_SECTION.key, 'embed'), _keys.MEDIA_SECTION.key, 'media'), _keys.OTHER_SECTION.key, 'other'), _keys.ROVO_SECTION.key, 'rovo'), _keys.STRUCTURE_SECTION.key, 'structure'), _keys.TEXT_FORMATTING_SECTION.key, 'textFormatting'); var legacyCategoryKeys = new Set(['admin', 'ai', 'block-templates', 'communication', 'confluence-content', 'data-and-charts', 'development', 'embed', 'external-content', 'formatting', 'media', 'navigation', 'other', 'reporting', 'skills', 'structure', 'text-formatting', 'visuals']); var isLegacyCategoryKey = function isLegacyCategoryKey(category) { return legacyCategoryKeys.has(category); }; var calculateQuickInsertItemsAttributes = exports.calculateQuickInsertItemsAttributes = function calculateQuickInsertItemsAttributes(items) { var attributes = { category: { blockTemplates: { appCount: 0 }, dataAndCharts: { appCount: 0 }, embed: { appCount: 0 }, media: { appCount: 0 }, other: { appCount: 0 }, rovo: { appCount: 0 }, structure: { appCount: 0 }, textFormatting: { appCount: 0 } }, legacyCategory: { admin: { appCount: 0 }, ai: { appCount: 0 }, 'block-templates': { appCount: 0 }, communication: { appCount: 0 }, 'confluence-content': { appCount: 0 }, 'data-and-charts': { appCount: 0 }, development: { appCount: 0 }, embed: { appCount: 0 }, 'external-content': { appCount: 0 }, formatting: { appCount: 0 }, media: { appCount: 0 }, navigation: { appCount: 0 }, other: { appCount: 0 }, reporting: { appCount: 0 }, skills: { appCount: 0 }, structure: { appCount: 0 }, 'text-formatting': { appCount: 0 }, uncategorized: { appCount: 0 }, visuals: { appCount: 0 } } }; var _iterator = _createForOfIteratorHelper(items), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _item$categories$map$, _item$categories; var item = _step.value; var normalizedCategories = new Set((_item$categories$map$ = (_item$categories = item.categories) === null || _item$categories === void 0 ? void 0 : _item$categories.map(function (category) { return category.trim().toLowerCase(); }).filter(Boolean)) !== null && _item$categories$map$ !== void 0 ? _item$categories$map$ : []); var hasUnknownCategory = normalizedCategories.size === 0; var _iterator2 = _createForOfIteratorHelper(normalizedCategories), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var category = _step2.value; if (isLegacyCategoryKey(category)) { attributes.legacyCategory[category].appCount += 1; } else { hasUnknownCategory = true; } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } if (hasUnknownCategory) { attributes.legacyCategory.uncategorized.appCount += 1; } var _iterator3 = _createForOfIteratorHelper((0, _getMenuItemParents.getQuickInsertMenuItemParents)({ categories: item.categories, rank: 0 })), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var key = _step3.value.key; var categoryKey = sectionCategoryKeys[key]; if (categoryKey) { attributes.category[categoryKey].appCount += 1; } } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return attributes; }; var createQuickInsertItemsAnalyticsScheduler = exports.createQuickInsertItemsAnalyticsScheduler = function createQuickInsertItemsAnalyticsScheduler(dispatchAnalyticsEvent) { var timeoutId; var animationFrameId; var idleCallbackId; var destroyed = false; var scheduled = false; var emit = function emit(items) { if (destroyed) { return; } dispatchAnalyticsEvent({ action: _analytics.ACTION.QUICK_INSERT_INFORMATION, actionSubject: _analytics.ACTION_SUBJECT.QUICK_INSERT, attributes: calculateQuickInsertItemsAttributes(items), eventType: _analytics.EVENT_TYPE.OPERATIONAL }); }; return { destroy: function destroy() { destroyed = true; if (timeoutId !== undefined) { clearTimeout(timeoutId); } if (animationFrameId !== undefined && typeof window !== 'undefined') { window.cancelAnimationFrame(animationFrameId); } if (idleCallbackId !== undefined && typeof window !== 'undefined' && typeof window.cancelIdleCallback === 'function') { window.cancelIdleCallback(idleCallbackId); } }, schedule: function schedule(items) { if (scheduled || destroyed) { return; } scheduled = true; timeoutId = setTimeout(function () { if (destroyed || typeof window === 'undefined') { return; } if (typeof window.requestIdleCallback === 'function') { idleCallbackId = window.requestIdleCallback(function () { return emit(items); }); } else if (typeof window.requestAnimationFrame === 'function') { animationFrameId = window.requestAnimationFrame(function () { return emit(items); }); } }, QUICK_INSERT_ITEMS_ANALYTICS_DELAY_MS); } }; };