UNPKG

@wordpress/block-library

Version:
129 lines (121 loc) 4.17 kB
import { createElement } from "@wordpress/element"; /** * WordPress dependencies */ import { Button, Modal } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { useDispatch } from '@wordpress/data'; import { useEntityRecords } from '@wordpress/core-data'; import { createBlock as create } from '@wordpress/blocks'; import { store as blockEditorStore } from '@wordpress/block-editor'; const PAGE_FIELDS = ['id', 'title', 'link', 'type', 'parent']; const MAX_PAGE_COUNT = 100; export const convertSelectedBlockToNavigationLinks = _ref => { let { pages, clientId, replaceBlock, createBlock } = _ref; return () => { if (!pages) { return; } const linkMap = {}; const navigationLinks = []; pages.forEach(_ref2 => { var _linkMap$id$innerBloc, _linkMap$id; let { id, title, link: url, type, parent } = _ref2; // See if a placeholder exists. This is created if children appear before parents in list. const innerBlocks = (_linkMap$id$innerBloc = (_linkMap$id = linkMap[id]) === null || _linkMap$id === void 0 ? void 0 : _linkMap$id.innerBlocks) !== null && _linkMap$id$innerBloc !== void 0 ? _linkMap$id$innerBloc : []; linkMap[id] = createBlock('core/navigation-link', { id, label: title.rendered, url, type, kind: 'post-type' }, innerBlocks); if (!parent) { navigationLinks.push(linkMap[id]); } else { if (!linkMap[parent]) { // Use a placeholder if the child appears before parent in list. linkMap[parent] = { innerBlocks: [] }; } const parentLinkInnerBlocks = linkMap[parent].innerBlocks; parentLinkInnerBlocks.push(linkMap[id]); } }); // Transform all links with innerBlocks into Submenus. This can't be done // sooner because page objects have no information on their children. const transformSubmenus = listOfLinks => { listOfLinks.forEach((block, index, listOfLinksArray) => { const { attributes, innerBlocks } = block; if (innerBlocks.length !== 0) { transformSubmenus(innerBlocks); const transformedBlock = createBlock('core/navigation-submenu', attributes, innerBlocks); listOfLinksArray[index] = transformedBlock; } }); }; transformSubmenus(navigationLinks); replaceBlock(clientId, navigationLinks); }; }; export default function ConvertToLinksModal(_ref3) { let { onClose, clientId } = _ref3; const { records: pages, hasResolved: pagesFinished } = useEntityRecords('postType', 'page', { per_page: MAX_PAGE_COUNT, _fields: PAGE_FIELDS, // TODO: When https://core.trac.wordpress.org/ticket/39037 REST API support for multiple orderby // values is resolved, update 'orderby' to [ 'menu_order', 'post_title' ] to provide a consistent // sort. orderby: 'menu_order', order: 'asc' }); const { replaceBlock } = useDispatch(blockEditorStore); return createElement(Modal, { closeLabel: __('Close'), onRequestClose: onClose, title: __('Convert to links'), className: 'wp-block-page-list-modal', aria: { describedby: 'wp-block-page-list-modal__description' } }, createElement("p", { id: 'wp-block-page-list-modal__description' }, __('To edit this navigation menu, convert it to single page links. This allows you to add, re-order, remove items, or edit their labels.')), createElement("p", null, __("Note: if you add new pages to your site, you'll need to add them to your navigation menu.")), createElement("div", { className: "wp-block-page-list-modal-buttons" }, createElement(Button, { variant: "tertiary", onClick: onClose }, __('Cancel')), createElement(Button, { variant: "primary", disabled: !pagesFinished, onClick: convertSelectedBlockToNavigationLinks({ pages, replaceBlock, clientId, createBlock: create }) }, __('Convert')))); } //# sourceMappingURL=convert-to-links-modal.js.map