UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

118 lines (112 loc) 5.6 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } /** * Type guard to check if a component has a parent * * @param component The block menu component to check * @returns True if the component has a parent, false otherwise */ var hasParent = function hasParent(component) { return 'parent' in component && !!component.parent; }; /** * Type guard to identify top-level sections (sections without a parent) * * @param component The block menu component to check * @returns True if the component is a top-level section, false otherwise */ var isTopLevelSection = function isTopLevelSection(component) { return component.type === 'block-menu-section' && !hasParent(component); }; /** * Gets all top-level sections (those without a parent) sorted by rank * * @param components All registered block menu components * @returns Sorted array of top-level sections */ export var getSortedTopLevelSections = function getSortedTopLevelSections(components) { return components.filter(isTopLevelSection).sort(function (a, b) { return (a.rank || 0) - (b.rank || 0); }); }; /** * Generates a unique key from a key and type * Used to lookup children in the childrenMap * * @param key The component's key * @param type The component's type * @returns A unique string key combining type and key */ export var getChildrenMapKey = function getChildrenMapKey(key, type) { return "".concat(type, ":").concat(key); }; /** * Builds a map of parent keys to their sorted children * This enables efficient hierarchical rendering of the menu structure * * @param components All registered block menu components * @returns Map where keys are parent identifiers and values are sorted child components */ export var buildChildrenMap = function buildChildrenMap(components) { var childrenMap = new Map(); var _iterator = _createForOfIteratorHelper(components), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var component = _step.value; // Only components with parents can be children if ('parent' in component && !!component.parent) { var childrenMapKey = getChildrenMapKey(component.parent.key, component.parent.type); var existing = childrenMap.get(childrenMapKey) || []; existing.push(component); childrenMap.set(childrenMapKey, existing); } } // Sort children by their rank within their parent } catch (err) { _iterator.e(err); } finally { _iterator.f(); } var _iterator2 = _createForOfIteratorHelper(childrenMap.entries()), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var _step2$value = _slicedToArray(_step2.value, 2), children = _step2$value[1]; children.sort(function (a, b) { var rankA = hasParent(a) ? a.parent.rank || 0 : 0; var rankB = hasParent(b) ? b.parent.rank || 0 : 0; return rankA - rankB; }); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } return childrenMap; }; /** * Determines whether a component will render based on its type and children * * Rules: * - An item will not render if it has isHidden that returns true OR if its component returns null (fallback) * - A nested menu will render if at least one section, that has at least one registered child * - A section will render if it has at least one registered child component that will render * */ var _willComponentRender = function willComponentRender(registeredComponent, childrenMap) { if (registeredComponent.type === 'block-menu-item') { var _registeredComponent$; return !(registeredComponent !== null && registeredComponent !== void 0 && (_registeredComponent$ = registeredComponent.isHidden) !== null && _registeredComponent$ !== void 0 && _registeredComponent$.call(registeredComponent)); } var childrenMapKey = getChildrenMapKey(registeredComponent.key, registeredComponent.type); var registeredComponents = childrenMap.get(childrenMapKey) || []; return registeredComponents.some(function (childComponent) { return _willComponentRender(childComponent, childrenMap); }); }; export { _willComponentRender as willComponentRender };