@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 9.87 kB
Source Map (JSON)
{"version":3,"file":"menu-items-to-blocks.cjs","names":["applyFilters","parse","createBlock","buildNavigationLinkEntityBinding"],"sources":["../../src/navigation/menu-items-to-blocks.js"],"sourcesContent":["import { createBlock, parse } from '@wordpress/blocks';\nimport { applyFilters } from '@wordpress/hooks';\nimport { buildNavigationLinkEntityBinding } from '../navigation-link/shared/use-entity-binding';\n\n/**\n * Convert a flat menu item structure to a nested blocks structure.\n *\n * @param {Object[]} menuItems An array of menu items.\n *\n * @return {WPBlock[]} An array of blocks.\n */\nexport default function menuItemsToBlocks( menuItems ) {\n\tif ( ! menuItems ) {\n\t\treturn null;\n\t}\n\n\tconst menuTree = createDataTree( menuItems );\n\tconst blocks = mapMenuItemsToBlocks( menuTree );\n\treturn applyFilters(\n\t\t'blocks.navigation.__unstableMenuItemsToBlocks',\n\t\tblocks,\n\t\tmenuItems\n\t);\n}\n\n/**\n * A recursive function that maps menu item nodes to blocks.\n *\n * @param {WPNavMenuItem[]} menuItems An array of WPNavMenuItem items.\n * @param {number} level An integer representing the nesting level.\n * @return {Object} Object containing innerBlocks and mapping.\n */\nfunction mapMenuItemsToBlocks( menuItems, level = 0 ) {\n\tlet mapping = {};\n\n\t// The menuItem should be in menu_order sort order.\n\tconst sortedItems = menuItems.toSorted(\n\t\t( a, b ) => a.menu_order - b.menu_order\n\t);\n\n\tconst innerBlocks = sortedItems.map( ( menuItem ) => {\n\t\tif ( menuItem.type === 'block' ) {\n\t\t\tconst [ block ] = parse( menuItem.content.raw );\n\n\t\t\tif ( ! block ) {\n\t\t\t\treturn createBlock( 'core/freeform', {\n\t\t\t\t\tcontent: menuItem.content,\n\t\t\t\t} );\n\t\t\t}\n\n\t\t\treturn block;\n\t\t}\n\n\t\tconst blockType = menuItem.children?.length\n\t\t\t? 'core/navigation-submenu'\n\t\t\t: 'core/navigation-link';\n\n\t\tconst attributes = menuItemToBlockAttributes(\n\t\t\tmenuItem,\n\t\t\tblockType,\n\t\t\tlevel\n\t\t);\n\n\t\t// If there are children recurse to build those nested blocks.\n\t\tconst {\n\t\t\tinnerBlocks: nestedBlocks = [], // alias to avoid shadowing\n\t\t\tmapping: nestedMapping = {}, // alias to avoid shadowing\n\t\t} = menuItem.children?.length\n\t\t\t? mapMenuItemsToBlocks( menuItem.children, level + 1 )\n\t\t\t: {};\n\n\t\t// Update parent mapping with nested mapping.\n\t\tmapping = {\n\t\t\t...mapping,\n\t\t\t...nestedMapping,\n\t\t};\n\n\t\t// Create block with nested \"innerBlocks\".\n\t\tconst block = createBlock( blockType, attributes, nestedBlocks );\n\n\t\t// Create mapping for menuItem -> block.\n\t\tmapping[ menuItem.id ] = block.clientId;\n\n\t\treturn block;\n\t} );\n\n\treturn {\n\t\tinnerBlocks,\n\t\tmapping,\n\t};\n}\n\n/**\n * A WP nav_menu_item object.\n * For more documentation on the individual fields present on a menu item please see:\n * https://core.trac.wordpress.org/browser/tags/5.7.1/src/wp-includes/nav-menu.php#L789\n *\n * @typedef WPNavMenuItem\n *\n * @property {Object} title stores the raw and rendered versions of the title/label for this menu item.\n * @property {Array} xfn the XFN relationships expressed in the link of this menu item.\n * @property {Array} classes the HTML class attributes for this menu item.\n * @property {string} attr_title the HTML title attribute for this menu item.\n * @property {string} object The type of object originally represented, such as 'category', 'post', or 'attachment'.\n * @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.\n * @property {string} description The description of this menu item.\n * @property {string} url The URL to which this menu item points.\n * @property {string} type The family of objects originally represented, such as 'post_type' or 'taxonomy'.\n * @property {string} target The target attribute of the link element for this menu item.\n */\n\n/**\n * Convert block attributes to menu item.\n *\n * @param {WPNavMenuItem} menuItem the menu item to be converted to block attributes.\n * @param {string} blockType The block type.\n * @param {number} level An integer representing the nesting level.\n * @return {Object} the block attributes converted from the WPNavMenuItem item.\n */\nfunction menuItemToBlockAttributes(\n\t{\n\t\ttitle: menuItemTitleField,\n\t\txfn,\n\t\tclasses,\n\t\t// eslint-disable-next-line camelcase\n\t\tattr_title,\n\t\tobject,\n\t\t// eslint-disable-next-line camelcase\n\t\tobject_id,\n\t\tdescription,\n\t\turl,\n\t\ttype: menuItemTypeField,\n\t\ttarget,\n\t},\n\tblockType,\n\tlevel\n) {\n\t// For historical reasons, the `core/navigation-link` variation type is `tag`\n\t// whereas WP Core expects `post_tag` as the `object` type.\n\t// To avoid writing a block migration we perform a conversion here.\n\t// See also inverse equivalent in `blockAttributesToMenuItem`.\n\tif ( object && object === 'post_tag' ) {\n\t\tobject = 'tag';\n\t}\n\n\tconst inferredKind = menuItemTypeField?.replace( '_', '-' ) || 'custom';\n\n\treturn {\n\t\tlabel: menuItemTitleField?.rendered || '',\n\t\t...( object?.length && {\n\t\t\ttype: object,\n\t\t} ),\n\t\tkind: inferredKind,\n\t\turl: url || '',\n\t\t...( xfn?.length &&\n\t\t\txfn.join( ' ' ).trim() && {\n\t\t\t\trel: xfn.join( ' ' ).trim(),\n\t\t\t} ),\n\t\t...( classes?.length &&\n\t\t\tclasses.join( ' ' ).trim() && {\n\t\t\t\tclassName: classes.join( ' ' ).trim(),\n\t\t\t} ),\n\t\t/* eslint-disable camelcase */\n\t\t...( attr_title?.length && {\n\t\t\ttitle: attr_title,\n\t\t} ),\n\t\t...( object_id &&\n\t\t\t( inferredKind === 'post-type' || inferredKind === 'taxonomy' ) && {\n\t\t\t\tid: object_id,\n\t\t\t\tmetadata: {\n\t\t\t\t\tbindings: buildNavigationLinkEntityBinding( inferredKind ),\n\t\t\t\t},\n\t\t\t} ),\n\t\t/* eslint-enable camelcase */\n\t\t...( description?.length && {\n\t\t\tdescription,\n\t\t} ),\n\t\t...( target === '_blank' && {\n\t\t\topensInNewTab: true,\n\t\t} ),\n\t\t...( blockType === 'core/navigation-submenu' && {\n\t\t\tisTopLevelItem: level === 0,\n\t\t} ),\n\t\t...( blockType === 'core/navigation-link' && {\n\t\t\tisTopLevelLink: level === 0,\n\t\t} ),\n\t};\n}\n\n/**\n * Creates a nested, hierarchical tree representation from unstructured data that\n * has an inherent relationship defined between individual items.\n *\n * For example, by default, each element in the dataset should have an `id` and\n * `parent` property where the `parent` property indicates a relationship between\n * the current item and another item with a matching `id` properties.\n *\n * This is useful for building linked lists of data from flat data structures.\n *\n * @param {Array} dataset linked data to be rearranged into a hierarchical tree based on relational fields.\n * @param {string} id the property which uniquely identifies each entry within the array.\n * @param {*} relation the property which identifies how the current item is related to other items in the data (if at all).\n * @return {Array} a nested array of parent/child relationships\n */\nfunction createDataTree( dataset, id = 'id', relation = 'parent' ) {\n\tconst hashTable = Object.create( null );\n\tconst dataTree = [];\n\n\tfor ( const data of dataset ) {\n\t\thashTable[ data[ id ] ] = {\n\t\t\t...data,\n\t\t\tchildren: [],\n\t\t};\n\t\tif ( data[ relation ] ) {\n\t\t\thashTable[ data[ relation ] ] = hashTable[ data[ relation ] ] || {};\n\t\t\thashTable[ data[ relation ] ].children =\n\t\t\t\thashTable[ data[ relation ] ].children || [];\n\t\t\thashTable[ data[ relation ] ].children.push(\n\t\t\t\thashTable[ data[ id ] ]\n\t\t\t);\n\t\t} else {\n\t\t\tdataTree.push( hashTable[ data[ id ] ] );\n\t\t}\n\t}\n\n\treturn dataTree;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAWA,SAAwB,kBAAmB,WAAY;CACtD,IAAK,CAAE,WACN,OAAO;CAIR,MAAM,SAAS,qBADE,eAAgB,SACW,CAAE;CAC9C,QAAA,GAAOA,iBAAAA,aAAAA,CACN,iDACA,QACA,SACD;AACD;;;;;;;;AASA,SAAS,qBAAsB,WAAW,QAAQ,GAAI;CACrD,IAAI,UAAU,CAAC;CAqDf,OAAO;EACN,aAnDmB,UAAU,UAC3B,GAAG,MAAO,EAAE,aAAa,EAAE,UAGA,CAAC,CAAC,KAAO,aAAc;GACpD,IAAK,SAAS,SAAS,SAAU;IAChC,MAAM,CAAE,UAAA,GAAUC,kBAAAA,MAAAA,CAAO,SAAS,QAAQ,GAAI;IAE9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