@atlaskit/editor-plugin-quick-insert
Version:
Quick insert plugin for @atlaskit/editor-core
184 lines (183 loc) • 9.95 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
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) { _defineProperty(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; }
import { createQuickInsertMatcher } from '@atlaskit/editor-common/quick-insert/create-quick-insert-matcher';
import { ASK_ROVO_MENU_ITEM } from '@atlaskit/editor-common/quick-insert/keys';
import { normalizeQuickInsertMatchResult } from '../normalizeQuickInsertMatchResult';
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(_toConsumableArray(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.
*/
export var 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(_toConsumableArray(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 = _toConsumableArray(new Set([].concat(_toConsumableArray(existing.sectionKeys), _toConsumableArray(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 = _toConsumableArray(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 === ASK_ROVO_MENU_ITEM.key;
});
var recommendedItems = _toConsumableArray(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.
*/
export var getRecommendedTileSize = function getRecommendedTileSize(index, count) {
return count >= 3 && count % 2 === 1 && index >= count - 3 ? 'regular' : 'wide';
};
export var getInitialRegistryElementBrowserSection = function getInitialRegistryElementBrowserSection(model, requestedSection) {
return model.sections.some(function (section) {
return section.key === requestedSection;
}) ? requestedSection : undefined;
};
export var 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.getDescription) === null || _createQuickInsertMat === void 0 ? void 0 : _createQuickInsertMat.call(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), 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(_toConsumableArray(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);
})), _toConsumableArray(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: []
};
};