UNPKG

@wordpress/block-library

Version:
147 lines (128 loc) 4.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertSelectedBlockToNavigationLinks = void 0; exports.default = ConvertToLinksModal; var _element = require("@wordpress/element"); var _components = require("@wordpress/components"); var _i18n = require("@wordpress/i18n"); var _data = require("@wordpress/data"); var _coreData = require("@wordpress/core-data"); var _blocks = require("@wordpress/blocks"); var _blockEditor = require("@wordpress/block-editor"); /** * WordPress dependencies */ const PAGE_FIELDS = ['id', 'title', 'link', 'type', 'parent']; const MAX_PAGE_COUNT = 100; 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); }; }; exports.convertSelectedBlockToNavigationLinks = convertSelectedBlockToNavigationLinks; function ConvertToLinksModal(_ref3) { let { onClose, clientId } = _ref3; const { records: pages, hasResolved: pagesFinished } = (0, _coreData.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 } = (0, _data.useDispatch)(_blockEditor.store); return (0, _element.createElement)(_components.Modal, { closeLabel: (0, _i18n.__)('Close'), onRequestClose: onClose, title: (0, _i18n.__)('Convert to links'), className: 'wp-block-page-list-modal', aria: { describedby: 'wp-block-page-list-modal__description' } }, (0, _element.createElement)("p", { id: 'wp-block-page-list-modal__description' }, (0, _i18n.__)('To edit this navigation menu, convert it to single page links. This allows you to add, re-order, remove items, or edit their labels.')), (0, _element.createElement)("p", null, (0, _i18n.__)("Note: if you add new pages to your site, you'll need to add them to your navigation menu.")), (0, _element.createElement)("div", { className: "wp-block-page-list-modal-buttons" }, (0, _element.createElement)(_components.Button, { variant: "tertiary", onClick: onClose }, (0, _i18n.__)('Cancel')), (0, _element.createElement)(_components.Button, { variant: "primary", disabled: !pagesFinished, onClick: convertSelectedBlockToNavigationLinks({ pages, replaceBlock, clientId, createBlock: _blocks.createBlock }) }, (0, _i18n.__)('Convert')))); } //# sourceMappingURL=convert-to-links-modal.js.map