@wordpress/block-library
Version:
Block library for the WordPress editor.
132 lines (131 loc) • 6.28 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_blocks = require("@wordpress/blocks");
let _wordpress_hooks = require("@wordpress/hooks");
let ___navigation_link_shared_use_entity_binding_js = require("../navigation-link/shared/use-entity-binding.cjs");
// packages/block-library/src/navigation/menu-items-to-blocks.js
/**
* Convert a flat menu item structure to a nested blocks structure.
*
* @param {Object[]} menuItems An array of menu items.
*
* @return {WPBlock[]} An array of blocks.
*/
function menuItemsToBlocks(menuItems) {
if (!menuItems) return null;
const blocks = mapMenuItemsToBlocks(createDataTree(menuItems));
return (0, _wordpress_hooks.applyFilters)("blocks.navigation.__unstableMenuItemsToBlocks", blocks, menuItems);
}
/**
* A recursive function that maps menu item nodes to blocks.
*
* @param {WPNavMenuItem[]} menuItems An array of WPNavMenuItem items.
* @param {number} level An integer representing the nesting level.
* @return {Object} Object containing innerBlocks and mapping.
*/
function mapMenuItemsToBlocks(menuItems, level = 0) {
let mapping = {};
return {
innerBlocks: menuItems.toSorted((a, b) => a.menu_order - b.menu_order).map((menuItem) => {
if (menuItem.type === "block") {
const [block] = (0, _wordpress_blocks.parse)(menuItem.content.raw);
if (!block) return (0, _wordpress_blocks.createBlock)("core/freeform", { content: menuItem.content });
return block;
}
const blockType = menuItem.children?.length ? "core/navigation-submenu" : "core/navigation-link";
const attributes = menuItemToBlockAttributes(menuItem, blockType, level);
const { innerBlocks: nestedBlocks = [], mapping: nestedMapping = {} } = menuItem.children?.length ? mapMenuItemsToBlocks(menuItem.children, level + 1) : {};
mapping = {
...mapping,
...nestedMapping
};
const block = (0, _wordpress_blocks.createBlock)(blockType, attributes, nestedBlocks);
mapping[menuItem.id] = block.clientId;
return block;
}),
mapping
};
}
/**
* A WP nav_menu_item object.
* For more documentation on the individual fields present on a menu item please see:
* https://core.trac.wordpress.org/browser/tags/5.7.1/src/wp-includes/nav-menu.php#L789
*
* @typedef WPNavMenuItem
*
* @property {Object} title stores the raw and rendered versions of the title/label for this menu item.
* @property {Array} xfn the XFN relationships expressed in the link of this menu item.
* @property {Array} classes the HTML class attributes for this menu item.
* @property {string} attr_title the HTML title attribute for this menu item.
* @property {string} object The type of object originally represented, such as 'category', 'post', or 'attachment'.
* @property {string} object_id The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
* @property {string} description The description of this menu item.
* @property {string} url The URL to which this menu item points.
* @property {string} type The family of objects originally represented, such as 'post_type' or 'taxonomy'.
* @property {string} target The target attribute of the link element for this menu item.
*/
/**
* Convert block attributes to menu item.
*
* @param {WPNavMenuItem} menuItem the menu item to be converted to block attributes.
* @param {string} blockType The block type.
* @param {number} level An integer representing the nesting level.
* @return {Object} the block attributes converted from the WPNavMenuItem item.
*/
function menuItemToBlockAttributes({ title: menuItemTitleField, xfn, classes, attr_title, object, object_id, description, url, type: menuItemTypeField, target }, blockType, level) {
if (object && object === "post_tag") object = "tag";
const inferredKind = menuItemTypeField?.replace("_", "-") || "custom";
return {
label: menuItemTitleField?.rendered || "",
...object?.length && { type: object },
kind: inferredKind,
url: url || "",
...xfn?.length && xfn.join(" ").trim() && { rel: xfn.join(" ").trim() },
...classes?.length && classes.join(" ").trim() && { className: classes.join(" ").trim() },
...attr_title?.length && { title: attr_title },
...object_id && (inferredKind === "post-type" || inferredKind === "taxonomy") && {
id: object_id,
metadata: { bindings: (0, ___navigation_link_shared_use_entity_binding_js.buildNavigationLinkEntityBinding)(inferredKind) }
},
...description?.length && { description },
...target === "_blank" && { opensInNewTab: true },
...blockType === "core/navigation-submenu" && { isTopLevelItem: level === 0 },
...blockType === "core/navigation-link" && { isTopLevelLink: level === 0 }
};
}
/**
* Creates a nested, hierarchical tree representation from unstructured data that
* has an inherent relationship defined between individual items.
*
* For example, by default, each element in the dataset should have an `id` and
* `parent` property where the `parent` property indicates a relationship between
* the current item and another item with a matching `id` properties.
*
* This is useful for building linked lists of data from flat data structures.
*
* @param {Array} dataset linked data to be rearranged into a hierarchical tree based on relational fields.
* @param {string} id the property which uniquely identifies each entry within the array.
* @param {*} relation the property which identifies how the current item is related to other items in the data (if at all).
* @return {Array} a nested array of parent/child relationships
*/
function createDataTree(dataset, id = "id", relation = "parent") {
const hashTable = Object.create(null);
const dataTree = [];
for (const data of dataset) {
hashTable[data[id]] = {
...data,
children: []
};
if (data[relation]) {
hashTable[data[relation]] = hashTable[data[relation]] || {};
hashTable[data[relation]].children = hashTable[data[relation]].children || [];
hashTable[data[relation]].children.push(hashTable[data[id]]);
} else dataTree.push(hashTable[data[id]]);
}
return dataTree;
}
exports.default = menuItemsToBlocks;
//# sourceMappingURL=menu-items-to-blocks.cjs.map