@wordpress/block-library
Version:
Block library for the WordPress editor.
174 lines (172 loc) • 6.54 kB
JavaScript
;
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 __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-library/src/navigation/edit/use-convert-classic-menu-to-block-menu.js
var use_convert_classic_menu_to_block_menu_exports = {};
__export(use_convert_classic_menu_to_block_menu_exports, {
CLASSIC_MENU_CONVERSION_ERROR: () => CLASSIC_MENU_CONVERSION_ERROR,
CLASSIC_MENU_CONVERSION_IDLE: () => CLASSIC_MENU_CONVERSION_IDLE,
CLASSIC_MENU_CONVERSION_PENDING: () => CLASSIC_MENU_CONVERSION_PENDING,
CLASSIC_MENU_CONVERSION_SUCCESS: () => CLASSIC_MENU_CONVERSION_SUCCESS,
default: () => use_convert_classic_menu_to_block_menu_default
});
module.exports = __toCommonJS(use_convert_classic_menu_to_block_menu_exports);
var import_data = require("@wordpress/data");
var import_core_data = require("@wordpress/core-data");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_menu_items_to_blocks = __toESM(require("../menu-items-to-blocks.cjs"));
var CLASSIC_MENU_CONVERSION_SUCCESS = "success";
var CLASSIC_MENU_CONVERSION_ERROR = "error";
var CLASSIC_MENU_CONVERSION_PENDING = "pending";
var CLASSIC_MENU_CONVERSION_IDLE = "idle";
var classicMenuBeingConvertedId = null;
function useConvertClassicToBlockMenu(createNavigationMenu, { throwOnError = false } = {}) {
const registry = (0, import_data.useRegistry)();
const { editEntityRecord } = (0, import_data.useDispatch)(import_core_data.store);
const [status, setStatus] = (0, import_element.useState)(CLASSIC_MENU_CONVERSION_IDLE);
const [error, setError] = (0, import_element.useState)(null);
const convertClassicMenuToBlockMenu = (0, import_element.useCallback)(
async (menuId, menuName, postStatus = "publish") => {
let navigationMenu;
let classicMenuItems;
try {
classicMenuItems = await registry.resolveSelect(import_core_data.store).getMenuItems({
menus: menuId,
per_page: -1,
context: "view"
});
} catch (err) {
throw new Error(
(0, import_i18n.sprintf)(
// translators: %s: The name of a menu (e.g. Header menu).
(0, import_i18n.__)(`Unable to fetch classic menu "%s" from API.`),
menuName
),
{
cause: err
}
);
}
if (classicMenuItems === null) {
throw new Error(
(0, import_i18n.sprintf)(
// translators: %s: The name of a menu (e.g. Header menu).
(0, import_i18n.__)(`Unable to fetch classic menu "%s" from API.`),
menuName
)
);
}
const { innerBlocks } = (0, import_menu_items_to_blocks.default)(classicMenuItems);
try {
navigationMenu = await createNavigationMenu(
menuName,
innerBlocks,
postStatus
);
await editEntityRecord(
"postType",
"wp_navigation",
navigationMenu.id,
{
status: "publish"
},
{ throwOnError: true }
);
} catch (err) {
throw new Error(
(0, import_i18n.sprintf)(
// translators: %s: The name of a menu (e.g. Header menu).
(0, import_i18n.__)(`Unable to create Navigation Menu "%s".`),
menuName
),
{
cause: err
}
);
}
return navigationMenu;
},
[createNavigationMenu, editEntityRecord, registry]
);
const convert = (0, import_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, import_i18n.sprintf)(
// translators: %s: The name of a menu (e.g. Header menu).
(0, import_i18n.__)(`Unable to create Navigation Menu "%s".`),
menuName
),
{
cause: err
}
);
}
});
},
[convertClassicMenuToBlockMenu, throwOnError]
);
return {
convert,
status,
error
};
}
var use_convert_classic_menu_to_block_menu_default = useConvertClassicToBlockMenu;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CLASSIC_MENU_CONVERSION_ERROR,
CLASSIC_MENU_CONVERSION_IDLE,
CLASSIC_MENU_CONVERSION_PENDING,
CLASSIC_MENU_CONVERSION_SUCCESS
});
//# sourceMappingURL=use-convert-classic-menu-to-block-menu.cjs.map