UNPKG

@wordpress/block-library

Version:
88 lines (87 loc) • 3.42 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_blocks = require("@wordpress/blocks"); let _wordpress_data = require("@wordpress/data"); let _wordpress_block_editor = require("@wordpress/block-editor"); let ___navigation_link_shared_index_js = require("../navigation-link/shared/index.cjs"); // packages/block-library/src/page-list/use-convert-to-navigation-links.js /** * Converts an array of pages into a nested array of navigation link blocks. * * @param {Array} pages An array of pages. * * @return {Array} A nested array of navigation link blocks. */ function createNavigationLinks(pages = []) { const POST_TYPE_KIND = "post-type"; const linkMap = {}; const navigationLinks = []; pages.forEach(({ id, title, link: url, type, parent }) => { const innerBlocks = linkMap[id]?.innerBlocks ?? []; linkMap[id] = (0, _wordpress_blocks.createBlock)("core/navigation-link", { id, label: title.rendered, url, type, kind: POST_TYPE_KIND, metadata: { bindings: (0, ___navigation_link_shared_index_js.buildNavigationLinkEntityBinding)(POST_TYPE_KIND) } }, innerBlocks); if (!parent) navigationLinks.push(linkMap[id]); else { if (!linkMap[parent]) linkMap[parent] = { innerBlocks: [] }; linkMap[parent].innerBlocks.push(linkMap[id]); } }); return navigationLinks; } /** * Finds a navigation link block by id, recursively. * It might be possible to make this a more generic helper function. * * @param {Array} navigationLinks An array of navigation link blocks. * @param {number} id The id of the navigation link to find. * * @return {Object|null} The navigation link block with the given id. */ function findNavigationLinkById(navigationLinks, id) { for (const navigationLink of navigationLinks) { if (navigationLink.attributes.id === id) return navigationLink; if (navigationLink.innerBlocks && navigationLink.innerBlocks.length) { const foundNavigationLink = findNavigationLinkById(navigationLink.innerBlocks, id); if (foundNavigationLink) return foundNavigationLink; } } return null; } function convertToNavigationLinks(pages = [], parentPageID = null) { let navigationLinks = createNavigationLinks(pages); if (parentPageID) { const parentPage = findNavigationLinkById(navigationLinks, parentPageID); if (parentPage && parentPage.innerBlocks) navigationLinks = parentPage.innerBlocks; } const transformSubmenus = (listOfLinks) => { listOfLinks.forEach((block, index, listOfLinksArray) => { const { attributes, innerBlocks } = block; if (innerBlocks.length !== 0) { transformSubmenus(innerBlocks); listOfLinksArray[index] = (0, _wordpress_blocks.createBlock)("core/navigation-submenu", attributes, innerBlocks); } }); }; transformSubmenus(navigationLinks); return navigationLinks; } function useConvertToNavigationLinks({ clientId, pages, parentClientId, parentPageID }) { const { replaceBlock, selectBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); return () => { const navigationLinks = convertToNavigationLinks(pages, parentPageID); replaceBlock(clientId, navigationLinks); selectBlock(parentClientId); }; } exports.convertToNavigationLinks = convertToNavigationLinks; exports.useConvertToNavigationLinks = useConvertToNavigationLinks; //# sourceMappingURL=use-convert-to-navigation-links.cjs.map