@atlaskit/editor-plugin-quick-insert
Version:
Quick insert plugin for @atlaskit/editor-core
82 lines (80 loc) • 3.81 kB
JavaScript
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 var MAX_RECOMMENDED_ITEMS = 5;
export var RECOMMENDED_SLOT_KEY_PREFIX = 'quick-insert-recommended-slot-';
export var isRecommendedSlotKey = function isRecommendedSlotKey(key) {
return key.startsWith(RECOMMENDED_SLOT_KEY_PREFIX);
};
var isRecommendationCandidate = function isRecommendationCandidate(component) {
return component.type === 'menu-item' && !isRecommendedSlotKey(component.key) && !isSectionOverflowItemKey(component.key) && !component.parents.some(function (parent) {
return isMenuFooterSectionKey(parent.key);
});
};
export var getRecommendedComponents = function getRecommendedComponents(_ref) {
var components = _ref.components,
context = _ref.context,
_ref$isRecommendedIte = _ref.isRecommendedItem,
isRecommendedItem = _ref$isRecommendedIte === void 0 ? defaultIsRecommendedItem : _ref$isRecommendedIte;
return components.filter(isRecommendationCandidate).flatMap(function (component, index) {
var _component$isHidden;
var 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: component,
index: index,
rank: result
}];
}).sort(function (first, second) {
return first.rank === second.rank ? first.index - second.index : first.rank - second.rank;
}).slice(0, MAX_RECOMMENDED_ITEMS).map(function (_ref2) {
var component = _ref2.component;
return component;
});
};
/** Keeps only the current menu-open recommendation snapshot. */
export var createRecommendedSnapshotCache = function createRecommendedSnapshotCache(_ref3) {
var api = _ref3.api,
isRecommendedItem = _ref3.isRecommendedItem;
var snapshot;
var getSnapshot = function getSnapshot(surfaceContext) {
var _surfaceContext$get, _snapshot;
var menuOpenId = surfaceContext === null || surfaceContext === 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 || (_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: isRecommendedItem
})
};
}
return snapshot.items;
};
return {
getSnapshot: getSnapshot
};
};