UNPKG

@wordpress/block-library

Version:
109 lines (108 loc) • 5.34 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let _wordpress_data = require("@wordpress/data"); let _wordpress_core_data = require("@wordpress/core-data"); let _wordpress_element = require("@wordpress/element"); let _wordpress_i18n = require("@wordpress/i18n"); let ___menu_items_to_blocks_js = require("../menu-items-to-blocks.cjs"); ___menu_items_to_blocks_js = __toESM(___menu_items_to_blocks_js); // packages/block-library/src/navigation/edit/use-convert-classic-menu-to-block-menu.js const CLASSIC_MENU_CONVERSION_SUCCESS = "success"; const CLASSIC_MENU_CONVERSION_ERROR = "error"; const CLASSIC_MENU_CONVERSION_PENDING = "pending"; const CLASSIC_MENU_CONVERSION_IDLE = "idle"; let classicMenuBeingConvertedId = null; function useConvertClassicToBlockMenu(createNavigationMenu, { throwOnError = false } = {}) { const registry = (0, _wordpress_data.useRegistry)(); const { editEntityRecord } = (0, _wordpress_data.useDispatch)(_wordpress_core_data.store); const [status, setStatus] = (0, _wordpress_element.useState)(CLASSIC_MENU_CONVERSION_IDLE); const [error, setError] = (0, _wordpress_element.useState)(null); const convertClassicMenuToBlockMenu = (0, _wordpress_element.useCallback)(async (menuId, menuName, postStatus = "publish") => { let navigationMenu; let classicMenuItems; try { classicMenuItems = await registry.resolveSelect(_wordpress_core_data.store).getMenuItems({ menus: menuId, per_page: -1, context: "view" }); } catch (err) { throw new Error((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)(`Unable to fetch classic menu "%s" from API.`), menuName), { cause: err }); } if (classicMenuItems === null) throw new Error((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)(`Unable to fetch classic menu "%s" from API.`), menuName)); const { innerBlocks } = (0, ___menu_items_to_blocks_js.default)(classicMenuItems); try { navigationMenu = await createNavigationMenu(menuName, innerBlocks, postStatus); /** * Immediately trigger editEntityRecord to change the wp_navigation post status to 'publish'. * This status change causes the menu to be displayed on the front of the site and sets the post state to be "dirty". * The problem being solved is if saveEditedEntityRecord was used here, the menu would be updated on the frontend and the editor _automatically_, * without user interaction. * If the user abandons the site editor without saving, there would still be a wp_navigation post created as draft. */ await editEntityRecord("postType", "wp_navigation", navigationMenu.id, { status: "publish" }, { throwOnError: true }); } catch (err) { throw new Error((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)(`Unable to create Navigation Menu "%s".`), menuName), { cause: err }); } return navigationMenu; }, [ createNavigationMenu, editEntityRecord, registry ]); return { convert: (0, _wordpress_element.useCallback)(async (menuId, menuName, postStatus) => { if (classicMenuBeingConvertedId === menuId) return; classicMenuBeingConvertedId = menuId; if (!menuId || !menuName) { setError("Unable to convert menu. Missing menu details."); setStatus(CLASSIC_MENU_CONVERSION_ERROR); return; } setStatus(CLASSIC_MENU_CONVERSION_PENDING); setError(null); return await convertClassicMenuToBlockMenu(menuId, menuName, postStatus).then((navigationMenu) => { setStatus(CLASSIC_MENU_CONVERSION_SUCCESS); classicMenuBeingConvertedId = null; return navigationMenu; }).catch((err) => { setError(err?.message); setStatus(CLASSIC_MENU_CONVERSION_ERROR); classicMenuBeingConvertedId = null; if (throwOnError) throw new Error((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)(`Unable to create Navigation Menu "%s".`), menuName), { cause: err }); }); }, [convertClassicMenuToBlockMenu, throwOnError]), status, error }; } exports.CLASSIC_MENU_CONVERSION_ERROR = CLASSIC_MENU_CONVERSION_ERROR; exports.CLASSIC_MENU_CONVERSION_IDLE = CLASSIC_MENU_CONVERSION_IDLE; exports.CLASSIC_MENU_CONVERSION_PENDING = CLASSIC_MENU_CONVERSION_PENDING; exports.CLASSIC_MENU_CONVERSION_SUCCESS = CLASSIC_MENU_CONVERSION_SUCCESS; exports.default = useConvertClassicToBlockMenu; //# sourceMappingURL=use-convert-classic-menu-to-block-menu.cjs.map