@atlaskit/editor-plugin-quick-insert
Version:
Quick insert plugin for @atlaskit/editor-core
88 lines (85 loc) • 4.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isRecommendedSlotKey = exports.getRecommendedComponents = exports.createRecommendedSnapshotCache = exports.RECOMMENDED_SLOT_KEY_PREFIX = exports.MAX_RECOMMENDED_ITEMS = void 0;
var _monitoring = require("@atlaskit/editor-common/monitoring");
var _isRecommendedItem = require("@atlaskit/editor-common/quick-insert/is-recommended-item");
var _keys = require("@atlaskit/editor-common/quick-insert/keys");
var _typeAheadIsMenuFooterSectionKey = require("@atlaskit/editor-common/type-ahead-is-menu-footer-section-key");
var _typeAheadIsSectionOverflowItemKey = require("@atlaskit/editor-common/type-ahead-is-section-overflow-item-key");
var _typeAheadSurfaceContext = require("@atlaskit/editor-common/type-ahead-surface-context");
var MAX_RECOMMENDED_ITEMS = exports.MAX_RECOMMENDED_ITEMS = 5;
var RECOMMENDED_SLOT_KEY_PREFIX = exports.RECOMMENDED_SLOT_KEY_PREFIX = 'quick-insert-recommended-slot-';
var isRecommendedSlotKey = exports.isRecommendedSlotKey = function isRecommendedSlotKey(key) {
return key.startsWith(RECOMMENDED_SLOT_KEY_PREFIX);
};
var isRecommendationCandidate = function isRecommendationCandidate(component) {
return component.type === 'menu-item' && !isRecommendedSlotKey(component.key) && !(0, _typeAheadIsSectionOverflowItemKey.isSectionOverflowItemKey)(component.key) && !component.parents.some(function (parent) {
return (0, _typeAheadIsMenuFooterSectionKey.isMenuFooterSectionKey)(parent.key);
});
};
var getRecommendedComponents = exports.getRecommendedComponents = function getRecommendedComponents(_ref) {
var components = _ref.components,
context = _ref.context,
_ref$isRecommendedIte = _ref.isRecommendedItem,
isRecommendedItem = _ref$isRecommendedIte === void 0 ? _isRecommendedItem.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 (0, _monitoring.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. */
var createRecommendedSnapshotCache = exports.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(_typeAheadSurfaceContext.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(_keys.MENU.key)) !== null && _api$uiControlRegistr !== void 0 ? _api$uiControlRegistr : [],
context: surfaceContext,
isRecommendedItem: isRecommendedItem
})
};
}
return snapshot.items;
};
return {
getSnapshot: getSnapshot
};
};