@atlaskit/editor-plugin-quick-insert
Version:
Quick insert plugin for @atlaskit/editor-core
143 lines (141 loc) • 7.84 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getRegistryElementBrowserItems = 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");
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 isVisible = function isVisible(registration) {
var _registration$isHidde;
return ((_registration$isHidde = registration.isHidden) === null || _registration$isHidde === void 0 ? void 0 : _registration$isHidde.call(registration)) !== true;
};
var getChildren = function getChildren(components, parentKey) {
return components.filter(function (component) {
var _component$parents;
return isVisible(component) && ((_component$parents = component.parents) === null || _component$parents === void 0 ? void 0 : _component$parents.some(function (parent) {
return parent.key === parentKey;
}));
});
};
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) {
return getChildren(components, parentKey).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);
});
};
/**
* Builds the Element Browser's immutable open-time view of the 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,
recommendedSectionKey = _ref.recommendedSectionKey,
rootKey = _ref.rootKey;
var excludedKeys = new Set([footerKey, recommendedSectionKey].concat((0, _toConsumableArray2.default)(overflowKeys)));
var visibleComponents = components.filter(function (component) {
return isVisible(component) && !excludedKeys.has(component.key);
});
var sections = getChildren(visibleComponents, rootKey).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).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;
});
return {
fallbackItems: fallbackItems.length > 0 ? fallbackItems : undefined,
items: items,
sections: sections
};
};
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(_ref3) {
var formatMessage = _ref3.formatMessage,
model = _ref3.model,
query = _ref3.query,
section = _ref3.section;
var normalizedQuery = query.trim();
var items = section ? model.items.filter(function (item) {
return item.sectionKeys.includes(section);
}) : model.items;
var itemsWithDescriptions = items.map(function (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 (!normalizedQuery) {
return itemsWithDescriptions;
}
return 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,
query: normalizedQuery
});
return result ? [_objectSpread(_objectSpread({}, item), {}, {
score: Math.min(1, Math.max(0, result.score))
})] : [];
}).sort(function (left, right) {
return left.score - right.score || compareRanks(left.ranks, right.ranks) || left.registration.key.localeCompare(right.registration.key);
});
};