UNPKG

@wordpress/block-library

Version:
1 lines • 5.93 kB
{"version":3,"file":"use-convert-to-navigation-links.cjs","names":["createBlock","buildNavigationLinkEntityBinding","useDispatch","blockEditorStore"],"sources":["../../src/page-list/use-convert-to-navigation-links.js"],"sourcesContent":["import { createBlock } from '@wordpress/blocks';\nimport { useDispatch } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { buildNavigationLinkEntityBinding } from '../navigation-link/shared';\n\n/**\n * Converts an array of pages into a nested array of navigation link blocks.\n *\n * @param {Array} pages An array of pages.\n *\n * @return {Array} A nested array of navigation link blocks.\n */\nfunction createNavigationLinks( pages = [] ) {\n\tconst POST_TYPE_KIND = 'post-type';\n\tconst linkMap = {};\n\tconst navigationLinks = [];\n\tpages.forEach( ( { id, title, link: url, type, parent } ) => {\n\t\t// See if a placeholder exists. This is created if children appear before parents in list.\n\t\tconst innerBlocks = linkMap[ id ]?.innerBlocks ?? [];\n\t\tlinkMap[ id ] = createBlock(\n\t\t\t'core/navigation-link',\n\t\t\t{\n\t\t\t\tid,\n\t\t\t\tlabel: title.rendered,\n\t\t\t\turl,\n\t\t\t\ttype,\n\t\t\t\tkind: POST_TYPE_KIND,\n\t\t\t\tmetadata: {\n\t\t\t\t\tbindings:\n\t\t\t\t\t\tbuildNavigationLinkEntityBinding( POST_TYPE_KIND ),\n\t\t\t\t},\n\t\t\t},\n\t\t\tinnerBlocks\n\t\t);\n\n\t\tif ( ! parent ) {\n\t\t\tnavigationLinks.push( linkMap[ id ] );\n\t\t} else {\n\t\t\tif ( ! linkMap[ parent ] ) {\n\t\t\t\t// Use a placeholder if the child appears before parent in list.\n\t\t\t\tlinkMap[ parent ] = { innerBlocks: [] };\n\t\t\t}\n\t\t\t// Although these variables are not referenced, they are needed to store the innerBlocks in memory.\n\t\t\tconst parentLinkInnerBlocks = linkMap[ parent ].innerBlocks;\n\t\t\tparentLinkInnerBlocks.push( linkMap[ id ] );\n\t\t}\n\t} );\n\n\treturn navigationLinks;\n}\n\n/**\n * Finds a navigation link block by id, recursively.\n * It might be possible to make this a more generic helper function.\n *\n * @param {Array} navigationLinks An array of navigation link blocks.\n * @param {number} id The id of the navigation link to find.\n *\n * @return {Object|null} The navigation link block with the given id.\n */\nfunction findNavigationLinkById( navigationLinks, id ) {\n\tfor ( const navigationLink of navigationLinks ) {\n\t\t// Is this the link we're looking for?\n\t\tif ( navigationLink.attributes.id === id ) {\n\t\t\treturn navigationLink;\n\t\t}\n\n\t\t// If not does it have innerBlocks?\n\t\tif ( navigationLink.innerBlocks && navigationLink.innerBlocks.length ) {\n\t\t\tconst foundNavigationLink = findNavigationLinkById(\n\t\t\t\tnavigationLink.innerBlocks,\n\t\t\t\tid\n\t\t\t);\n\n\t\t\tif ( foundNavigationLink ) {\n\t\t\t\treturn foundNavigationLink;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn null;\n}\n\nexport function convertToNavigationLinks( pages = [], parentPageID = null ) {\n\tlet navigationLinks = createNavigationLinks( pages );\n\n\t// If a parent page ID is provided, only return the children of that page.\n\tif ( parentPageID ) {\n\t\tconst parentPage = findNavigationLinkById(\n\t\t\tnavigationLinks,\n\t\t\tparentPageID\n\t\t);\n\t\tif ( parentPage && parentPage.innerBlocks ) {\n\t\t\tnavigationLinks = parentPage.innerBlocks;\n\t\t}\n\t}\n\n\t// Transform all links with innerBlocks into Submenus. This can't be done\n\t// sooner because page objects have no information on their children.\n\tconst transformSubmenus = ( listOfLinks ) => {\n\t\tlistOfLinks.forEach( ( block, index, listOfLinksArray ) => {\n\t\t\tconst { attributes, innerBlocks } = block;\n\t\t\tif ( innerBlocks.length !== 0 ) {\n\t\t\t\ttransformSubmenus( innerBlocks );\n\t\t\t\tconst transformedBlock = createBlock(\n\t\t\t\t\t'core/navigation-submenu',\n\t\t\t\t\tattributes,\n\t\t\t\t\tinnerBlocks\n\t\t\t\t);\n\t\t\t\tlistOfLinksArray[ index ] = transformedBlock;\n\t\t\t}\n\t\t} );\n\t};\n\n\ttransformSubmenus( navigationLinks );\n\treturn navigationLinks;\n}\n\nexport function useConvertToNavigationLinks( {\n\tclientId,\n\tpages,\n\tparentClientId,\n\tparentPageID,\n} ) {\n\tconst { replaceBlock, selectBlock } = useDispatch( blockEditorStore );\n\n\treturn () => {\n\t\tconst navigationLinks = convertToNavigationLinks( pages, parentPageID );\n\n\t\t// Replace the Page List block with the Navigation Links.\n\t\treplaceBlock( clientId, navigationLinks );\n\n\t\t// Select the Navigation block to reveal the changes.\n\t\tselectBlock( parentClientId );\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAYA,SAAS,sBAAuB,QAAQ,CAAC,GAAI;CAC5C,MAAM,iBAAiB;CACvB,MAAM,UAAU,CAAC;CACjB,MAAM,kBAAkB,CAAC;CACzB,MAAM,SAAW,EAAE,IAAI,OAAO,MAAM,KAAK,MAAM,aAAc;EAE5D,MAAM,cAAc,QAAS,GAAI,EAAE,eAAe,CAAC;EACnD,QAAS,OAAA,GAAOA,kBAAAA,YAAAA,CACf,wBACA;GACC;GACA,OAAO,MAAM;GACb;GACA;GACA,MAAM;GACN,UAAU,EACT,WAAA,GACCC,mCAAAA,iCAAAA,CAAkC,cAAe,EACnD;EACD,GACA,WACD;EAEA,IAAK,CAAE,QACN,gBAAgB,KAAM,QAAS,GAAK;OAC9B;GACN,IAAK,CAAE,QAAS,SAEf,QAAS,UAAW,EAAE,aAAa,CAAC,EAAE;GAIvC,QADuC,OAAQ,CAAC,YAC1B,KAAM,QAAS,GAAK;EAC3C;CACD,CAAE;CAEF,OAAO;AACR;;;;;;;;;;AAWA,SAAS,uBAAwB,iBAAiB,IAAK;CACtD,KAAM,MAAM,kBAAkB,iBAAkB;EAE/C,IAAK,eAAe,WAAW,OAAO,IACrC,OAAO;EAIR,IAAK,eAAe,eAAe,eAAe,YAAY,QAAS;GACtE,MAAM,sBAAsB,uBAC3B,eAAe,aACf,EACD;GAEA,IAAK,qBACJ,OAAO;EAET;CACD;CAEA,OAAO;AACR;AAEA,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