UNPKG

@atlaskit/editor-plugin-quick-insert

Version:

Quick insert plugin for @atlaskit/editor-core

191 lines (189 loc) • 10.7 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getRegistryElementBrowserItems = exports.getRecommendedTileSize = exports.getInitialRegistryElementBrowserSection = exports.createRegistryElementBrowserModel = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _createQuickInsertMatcher = require("@atlaskit/editor-common/quick-insert/create-quick-insert-matcher"); var _keys = require("@atlaskit/editor-common/quick-insert/keys"); var _normalizeQuickInsertMatchResult = require("../normalizeQuickInsertMatchResult"); function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var compareRanks = function compareRanks(left, right) { for (var index = 0; index < Math.max(left.length, right.length); index++) { var _left$index, _right$index; var difference = ((_left$index = left[index]) !== null && _left$index !== void 0 ? _left$index : 0) - ((_right$index = right[index]) !== null && _right$index !== void 0 ? _right$index : 0); if (difference !== 0) { return difference; } } return 0; }; var getChildren = function getChildren(components, parentKey, surfaceContext) { return components.filter(function (component) { var _component$parents, _component$isHidden; return ((_component$parents = component.parents) === null || _component$parents === void 0 ? void 0 : _component$parents.some(function (parent) { return parent.key === parentKey; })) && ((_component$isHidden = component.isHidden) === null || _component$isHidden === void 0 ? void 0 : _component$isHidden.call(component, { surfaceContext: surfaceContext })) !== true; }); }; var getParentRank = function getParentRank(registration, parentKey) { var _registration$parents, _registration$parents2; return (_registration$parents = (_registration$parents2 = registration.parents) === null || _registration$parents2 === void 0 || (_registration$parents2 = _registration$parents2.find(function (parent) { return parent.key === parentKey; })) === null || _registration$parents2 === void 0 ? void 0 : _registration$parents2.rank) !== null && _registration$parents !== void 0 ? _registration$parents : Number.MAX_SAFE_INTEGER; }; var _getDescendantItems = function getDescendantItems(components, parentKey, ancestorRanks, sectionKey, surfaceContext) { return getChildren(components, parentKey, surfaceContext).flatMap(function (component) { var ranks = [].concat((0, _toConsumableArray2.default)(ancestorRanks), [getParentRank(component, parentKey)]); if (component.type === 'menu-item') { return component.component ? [{ ranks: ranks, registration: component, sectionKeys: [sectionKey] }] : []; } return _getDescendantItems(components, component.key, ranks, sectionKey, surfaceContext); }); }; /** * Builds the Element Browser's view of the resolved Quick Insert registry. * The caller provides only components rooted at the Quick Insert menu surface. */ var createRegistryElementBrowserModel = exports.createRegistryElementBrowserModel = function createRegistryElementBrowserModel(components, _ref) { var footerKey = _ref.footerKey, overflowKeys = _ref.overflowKeys, _ref$recommendedRegis = _ref.recommendedRegistrations, recommendedRegistrations = _ref$recommendedRegis === void 0 ? [] : _ref$recommendedRegis, recommendedSectionKey = _ref.recommendedSectionKey, rootKey = _ref.rootKey, surfaceContext = _ref.surfaceContext; var excludedKeys = new Set([footerKey, recommendedSectionKey].concat((0, _toConsumableArray2.default)(overflowKeys))); var visibleComponents = components.filter(function (component) { return !excludedKeys.has(component.key); }); var sections = getChildren(visibleComponents, rootKey, surfaceContext).filter(function (component) { return component.type === 'menu-section'; }).map(function (section) { return { key: section.key, rank: getParentRank(section, rootKey) }; }).sort(function (left, right) { return left.rank - right.rank; }); var itemByKey = new Map(); sections.forEach(function (section) { _getDescendantItems(visibleComponents, section.key, [section.rank], section.key, surfaceContext).forEach(function (item) { var existing = itemByKey.get(item.registration.key); if (!existing) { itemByKey.set(item.registration.key, item); return; } var sectionKeys = (0, _toConsumableArray2.default)(new Set([].concat((0, _toConsumableArray2.default)(existing.sectionKeys), (0, _toConsumableArray2.default)(item.sectionKeys)))); itemByKey.set(item.registration.key, compareRanks(item.ranks, existing.ranks) < 0 ? _objectSpread(_objectSpread({}, item), {}, { sectionKeys: sectionKeys }) : _objectSpread(_objectSpread({}, existing), {}, { sectionKeys: sectionKeys })); }); }); var items = (0, _toConsumableArray2.default)(itemByKey.values()).sort(function (left, right) { return compareRanks(left.ranks, right.ranks); }); var fallbackItems = items.filter(function (_ref2) { var registration = _ref2.registration; return registration.key === _keys.ASK_ROVO_MENU_ITEM.key; }); var recommendedItems = (0, _toConsumableArray2.default)(new Set(recommendedRegistrations.map(function (_ref3) { var key = _ref3.key; return key; }))).flatMap(function (key) { var _itemByKey$get; return (_itemByKey$get = itemByKey.get(key)) !== null && _itemByKey$get !== void 0 ? _itemByKey$get : []; }); return { fallbackItems: fallbackItems.length > 0 ? fallbackItems : undefined, items: items, recommendedItems: recommendedItems, sections: sections }; }; /** * Lays out Recommended so every row is full: wide tiles take half a row and regular tiles a third. * An odd count of three or more ends with a row of three regular tiles; every other tile is wide. */ var getRecommendedTileSize = exports.getRecommendedTileSize = function getRecommendedTileSize(index, count) { return count >= 3 && count % 2 === 1 && index >= count - 3 ? 'regular' : 'wide'; }; var getInitialRegistryElementBrowserSection = exports.getInitialRegistryElementBrowserSection = function getInitialRegistryElementBrowserSection(model, requestedSection) { return model.sections.some(function (section) { return section.key === requestedSection; }) ? requestedSection : undefined; }; var getRegistryElementBrowserItems = exports.getRegistryElementBrowserItems = function getRegistryElementBrowserItems(_ref4) { var formatMessage = _ref4.formatMessage, model = _ref4.model, query = _ref4.query, section = _ref4.section; var withDescription = function withDescription(item) { var _createQuickInsertMat; return _objectSpread(_objectSpread({}, item), {}, { description: (_createQuickInsertMat = _createQuickInsertMatcher.createQuickInsertMatcher.getDescription) === null || _createQuickInsertMat === void 0 ? void 0 : _createQuickInsertMat.call(_createQuickInsertMatcher.createQuickInsertMatcher, item.registration.match, formatMessage) }); }; if (section === undefined && query === '') { var recommendedKeys = new Set(model.recommendedItems.map(function (_ref5) { var registration = _ref5.registration; return registration.key; })); return { items: model.items.filter(function (_ref6) { var registration = _ref6.registration; return !recommendedKeys.has(registration.key); }).map(withDescription), recommendedItems: model.recommendedItems.map(withDescription) }; } var normalizedQuery = query.trim(); var itemsWithDescriptions = (section ? model.items.filter(function (item) { return item.sectionKeys.includes(section); }) : model.items).map(withDescription); if (!normalizedQuery) { return { items: itemsWithDescriptions, recommendedItems: [] }; } var matches = itemsWithDescriptions.flatMap(function (item) { var _item$registration$ma, _item$registration; var result = (_item$registration$ma = (_item$registration = item.registration).match) === null || _item$registration$ma === void 0 ? void 0 : _item$registration$ma.call(_item$registration, { formatMessage: formatMessage, itemKey: item.registration.key, query: normalizedQuery }); return result ? [_objectSpread(_objectSpread({}, item), (0, _normalizeQuickInsertMatchResult.normalizeQuickInsertMatchResult)(result))] : []; }); var prioritized = matches.filter(function (_ref7) { var priorityGroup = _ref7.priorityGroup; return priorityGroup !== undefined; }); var unprioritized = matches.filter(function (_ref8) { var priorityGroup = _ref8.priorityGroup; return priorityGroup === undefined; }); return { items: [].concat((0, _toConsumableArray2.default)(prioritized.sort(function (left, right) { var _right$priorityGroup, _left$priorityGroup; return ((_right$priorityGroup = right.priorityGroup) !== null && _right$priorityGroup !== void 0 ? _right$priorityGroup : 0) - ((_left$priorityGroup = left.priorityGroup) !== null && _left$priorityGroup !== void 0 ? _left$priorityGroup : 0) || left.score - right.score || compareRanks(left.ranks, right.ranks) || left.registration.key.localeCompare(right.registration.key); })), (0, _toConsumableArray2.default)(unprioritized.sort(function (left, right) { var _left$ranks$, _right$ranks$; return ((_left$ranks$ = left.ranks[0]) !== null && _left$ranks$ !== void 0 ? _left$ranks$ : 0) - ((_right$ranks$ = right.ranks[0]) !== null && _right$ranks$ !== void 0 ? _right$ranks$ : 0) || left.score - right.score || compareRanks(left.ranks, right.ranks) || left.registration.key.localeCompare(right.registration.key); }))), recommendedItems: [] }; };