UNPKG

@atlaskit/editor-plugin-quick-insert

Version:

Quick insert plugin for @atlaskit/editor-core

74 lines (72 loc) 3.31 kB
import { logException } from '@atlaskit/editor-common/monitoring'; import { defaultIsRecommendedItem } from '@atlaskit/editor-common/quick-insert/is-recommended-item'; import { MENU } from '@atlaskit/editor-common/quick-insert/keys'; import { isMenuFooterSectionKey } from '@atlaskit/editor-common/type-ahead-is-menu-footer-section-key'; import { isSectionOverflowItemKey } from '@atlaskit/editor-common/type-ahead-is-section-overflow-item-key'; import { TYPE_AHEAD_SURFACE_CONTEXT } from '@atlaskit/editor-common/type-ahead-surface-context'; export const MAX_RECOMMENDED_ITEMS = 5; export const RECOMMENDED_SLOT_KEY_PREFIX = 'quick-insert-recommended-slot-'; export const isRecommendedSlotKey = key => key.startsWith(RECOMMENDED_SLOT_KEY_PREFIX); const isRecommendationCandidate = component => component.type === 'menu-item' && !isRecommendedSlotKey(component.key) && !isSectionOverflowItemKey(component.key) && !component.parents.some(parent => isMenuFooterSectionKey(parent.key)); export const getRecommendedComponents = ({ components, context, isRecommendedItem = defaultIsRecommendedItem }) => { return components.filter(isRecommendationCandidate).flatMap((component, index) => { var _component$isHidden; const result = isRecommendedItem({ itemKey: component.key }); if (result === null) { return []; } if (Number.isNaN(result)) { void logException(new Error('Invalid Quick Insert recommended item rank'), { location: 'editor-plugin-quick-insert/getRecommendedComponents' }); return []; } if ((_component$isHidden = component.isHidden) !== null && _component$isHidden !== void 0 && _component$isHidden.call(component, { surfaceContext: context })) { return []; } return [{ component, index, rank: result }]; }).sort((first, second) => first.rank === second.rank ? first.index - second.index : first.rank - second.rank).slice(0, MAX_RECOMMENDED_ITEMS).map(({ component }) => component); }; /** Keeps only the current menu-open recommendation snapshot. */ export const createRecommendedSnapshotCache = ({ api, isRecommendedItem }) => { let snapshot; const getSnapshot = surfaceContext => { var _surfaceContext$get, _snapshot; const menuOpenId = surfaceContext === null || surfaceContext === void 0 ? void 0 : (_surfaceContext$get = surfaceContext.get(TYPE_AHEAD_SURFACE_CONTEXT)) === null || _surfaceContext$get === void 0 ? void 0 : _surfaceContext$get.menuOpenId; if (!surfaceContext || !menuOpenId) { return []; } if (((_snapshot = snapshot) === null || _snapshot === void 0 ? void 0 : _snapshot.id) !== menuOpenId) { var _api$uiControlRegistr, _api$uiControlRegistr2; snapshot = { id: menuOpenId, items: getRecommendedComponents({ components: (_api$uiControlRegistr = api === null || api === void 0 ? void 0 : (_api$uiControlRegistr2 = api.uiControlRegistry) === null || _api$uiControlRegistr2 === void 0 ? void 0 : _api$uiControlRegistr2.actions.getComponents(MENU.key)) !== null && _api$uiControlRegistr !== void 0 ? _api$uiControlRegistr : [], context: surfaceContext, isRecommendedItem }) }; } return snapshot.items; }; return { getSnapshot }; };