UNPKG

@atlaskit/editor-plugin-quick-insert

Version:

Quick insert plugin for @atlaskit/editor-core

226 lines 10.7 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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; } import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { getQuickInsertMenuItemParents } from '@atlaskit/editor-common/quick-insert/get-menu-item-parents'; import { BLOCK_TEMPLATES_SECTION, DATA_AND_CHARTS_SECTION, EMBED_SECTION, MEDIA_SECTION, OTHER_SECTION, ROVO_SECTION, STRUCTURE_SECTION, TEXT_FORMATTING_SECTION } from '@atlaskit/editor-common/quick-insert/keys'; export var QUICK_INSERT_ITEMS_ANALYTICS_DELAY_MS = 10000; var GUID_ONLY_APP_KEY_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; var isGuidOnlyAppKey = function isGuidOnlyAppKey(appKey) { return GUID_ONLY_APP_KEY_REGEX.test(appKey); }; var sectionCategoryKeys = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, BLOCK_TEMPLATES_SECTION.key, 'blockTemplates'), DATA_AND_CHARTS_SECTION.key, 'dataAndCharts'), EMBED_SECTION.key, 'embed'), MEDIA_SECTION.key, 'media'), OTHER_SECTION.key, 'other'), ROVO_SECTION.key, 'rovo'), STRUCTURE_SECTION.key, 'structure'), 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); }; export var calculateQuickInsertItemsAttributes = function calculateQuickInsertItemsAttributes(_ref) { var providedItems = _ref.providedItems; var emptyCategoryInformation = function emptyCategoryInformation() { return { internalMacroCount: 0, internalAppCount: 0, internalAppMacroMax: 0, ecosystemMacroCount: 0, ecosystemAppCount: 0, ecosystemAppMacroMax: 0, allMacroCount: 0, allAppCount: 0, allAppMacroMax: 0 }; }; var emptyCategoryCounts = { blockTemplates: emptyCategoryInformation(), dataAndCharts: emptyCategoryInformation(), embed: emptyCategoryInformation(), media: emptyCategoryInformation(), other: emptyCategoryInformation(), rovo: emptyCategoryInformation(), structure: emptyCategoryInformation(), textFormatting: emptyCategoryInformation() }; var emptyLegacyCategoryCounts = { admin: emptyCategoryInformation(), ai: emptyCategoryInformation(), 'block-templates': emptyCategoryInformation(), communication: emptyCategoryInformation(), 'confluence-content': emptyCategoryInformation(), 'data-and-charts': emptyCategoryInformation(), development: emptyCategoryInformation(), embed: emptyCategoryInformation(), 'external-content': emptyCategoryInformation(), formatting: emptyCategoryInformation(), media: emptyCategoryInformation(), navigation: emptyCategoryInformation(), other: emptyCategoryInformation(), reporting: emptyCategoryInformation(), skills: emptyCategoryInformation(), structure: emptyCategoryInformation(), 'text-formatting': emptyCategoryInformation(), uncategorized: emptyCategoryInformation(), visuals: emptyCategoryInformation() }; var attributes = { category: emptyCategoryCounts, legacyCategory: emptyLegacyCategoryCounts, allCategories: emptyCategoryInformation() }; var appMacroCounts = new WeakMap(); var getAppMacroCounts = function getAppMacroCounts(category) { var existingCounts = appMacroCounts.get(category); if (existingCounts) { return existingCounts; } var counts = { all: new Map(), internal: new Map(), ecosystem: new Map() }; appMacroCounts.set(category, counts); return counts; }; var countAppMacro = function countAppMacro(appKey, counts) { var _counts$get; var appMacroCount = ((_counts$get = counts.get(appKey)) !== null && _counts$get !== void 0 ? _counts$get : 0) + 1; counts.set(appKey, appMacroCount); return appMacroCount; }; var countCategoryItem = function countCategoryItem(category, item) { var _item$app; category.allMacroCount += 1; var _ref2 = (_item$app = item.app) !== null && _item$app !== void 0 ? _item$app : {}, _ref2$key = _ref2.key, appKey = _ref2$key === void 0 ? 'unknown' : _ref2$key, source = _ref2.source; var appMacroKey = isGuidOnlyAppKey(appKey) ? 'unknown' : appKey; var counts = getAppMacroCounts(category); var allAppMacroCount = countAppMacro(appMacroKey, counts.all); category.allAppCount = counts.all.size; category.allAppMacroMax = Math.max(category.allAppMacroMax, allAppMacroCount); if (source === 'internal') { category.internalMacroCount += 1; var internalAppMacroCount = countAppMacro(appMacroKey, counts.internal); category.internalAppCount = counts.internal.size; category.internalAppMacroMax = Math.max(category.internalAppMacroMax, internalAppMacroCount); } else if (source === 'ecosystem') { category.ecosystemMacroCount += 1; var ecosystemAppMacroCount = countAppMacro(appMacroKey, counts.ecosystem); category.ecosystemAppCount = counts.ecosystem.size; category.ecosystemAppMacroMax = Math.max(category.ecosystemAppMacroMax, ecosystemAppMacroCount); } }; var countItem = function countItem(item) { var _item$categories, _categories$map$filte; countCategoryItem(attributes.allCategories, item); var categories = (_item$categories = item.categories) !== null && _item$categories !== void 0 ? _item$categories : []; var normalizedCategories = new Set((_categories$map$filte = categories === null || categories === void 0 ? void 0 : categories.map(function (category) { return category.trim().toLowerCase(); }).filter(Boolean)) !== null && _categories$map$filte !== void 0 ? _categories$map$filte : []); var hasUnknownCategory = normalizedCategories.size === 0; var _iterator = _createForOfIteratorHelper(normalizedCategories), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var category = _step.value; if (isLegacyCategoryKey(category)) { countCategoryItem(attributes.legacyCategory[category], item); } else { hasUnknownCategory = true; } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } if (hasUnknownCategory) { countCategoryItem(attributes.legacyCategory.uncategorized, item); } var _iterator2 = _createForOfIteratorHelper(getQuickInsertMenuItemParents({ category: item.category, legacyCategories: item.categories, rank: 0 })), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var key = _step2.value.key; var categoryKey = sectionCategoryKeys[key]; if (!categoryKey) { continue; } countCategoryItem(attributes.category[categoryKey], item); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } }; var _iterator3 = _createForOfIteratorHelper(providedItems), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var item = _step3.value; countItem(item); } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } return attributes; }; export var createQuickInsertItemsAnalyticsScheduler = function createQuickInsertItemsAnalyticsScheduler(dispatchAnalyticsEvent) { var timeoutId; var animationFrameId; var idleCallbackId; var destroyed = false; var scheduled = false; var emit = function emit(options) { if (destroyed) { return; } dispatchAnalyticsEvent({ action: ACTION.QUICK_INSERT_INFORMATION, actionSubject: ACTION_SUBJECT.QUICK_INSERT, attributes: calculateQuickInsertItemsAttributes(options), eventType: 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(options) { 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(options); }); } else if (typeof window.requestAnimationFrame === 'function') { animationFrameId = window.requestAnimationFrame(function () { return emit(options); }); } }, QUICK_INSERT_ITEMS_ANALYTICS_DELAY_MS); } }; };