@wordpress/block-library
Version:
Block library for the WordPress editor.
94 lines (93 loc) • 4.22 kB
JavaScript
;
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_blocks = require("@wordpress/blocks");
let _wordpress_core_data = require("@wordpress/core-data");
let _wordpress_data = require("@wordpress/data");
let _wordpress_element = require("@wordpress/element");
let __use_generate_default_navigation_title_js = require("./use-generate-default-navigation-title.cjs");
__use_generate_default_navigation_title_js = __toESM(__use_generate_default_navigation_title_js);
// packages/block-library/src/navigation/edit/use-create-navigation-menu.js
const CREATE_NAVIGATION_MENU_SUCCESS = "success";
const CREATE_NAVIGATION_MENU_ERROR = "error";
const CREATE_NAVIGATION_MENU_PENDING = "pending";
const CREATE_NAVIGATION_MENU_IDLE = "idle";
function useCreateNavigationMenu(clientId) {
const [status, setStatus] = (0, _wordpress_element.useState)(CREATE_NAVIGATION_MENU_IDLE);
const [value, setValue] = (0, _wordpress_element.useState)(null);
const [error, setError] = (0, _wordpress_element.useState)(null);
const { saveEntityRecord, editEntityRecord } = (0, _wordpress_data.useDispatch)(_wordpress_core_data.store);
const generateDefaultTitle = (0, __use_generate_default_navigation_title_js.default)(clientId);
return {
create: (0, _wordpress_element.useCallback)(async (title = null, blocks = [], postStatus) => {
if (title && typeof title !== "string") {
setError("Invalid title supplied when creating Navigation Menu.");
setStatus(CREATE_NAVIGATION_MENU_ERROR);
throw new Error(`Value of supplied title argument was not a string.`);
}
setStatus(CREATE_NAVIGATION_MENU_PENDING);
setValue(null);
setError(null);
if (!title) title = await generateDefaultTitle().catch((err) => {
setError(err?.message);
setStatus(CREATE_NAVIGATION_MENU_ERROR);
throw new Error("Failed to create title when saving new Navigation Menu.", { cause: err });
});
const record = {
title,
content: (0, _wordpress_blocks.serialize)(blocks),
status: postStatus
};
return saveEntityRecord("postType", "wp_navigation", record).then((response) => {
setValue(response);
setStatus(CREATE_NAVIGATION_MENU_SUCCESS);
if (postStatus !== "publish") editEntityRecord("postType", "wp_navigation", response.id, { status: "publish" });
return response;
}).catch((err) => {
setError(err?.message);
setStatus(CREATE_NAVIGATION_MENU_ERROR);
throw new Error("Unable to save new Navigation Menu", { cause: err });
});
}, [
saveEntityRecord,
editEntityRecord,
generateDefaultTitle
]),
status,
value,
error,
isIdle: status === CREATE_NAVIGATION_MENU_IDLE,
isPending: status === CREATE_NAVIGATION_MENU_PENDING,
isSuccess: status === CREATE_NAVIGATION_MENU_SUCCESS,
isError: status === CREATE_NAVIGATION_MENU_ERROR
};
}
exports.CREATE_NAVIGATION_MENU_ERROR = CREATE_NAVIGATION_MENU_ERROR;
exports.CREATE_NAVIGATION_MENU_IDLE = CREATE_NAVIGATION_MENU_IDLE;
exports.CREATE_NAVIGATION_MENU_PENDING = CREATE_NAVIGATION_MENU_PENDING;
exports.CREATE_NAVIGATION_MENU_SUCCESS = CREATE_NAVIGATION_MENU_SUCCESS;
exports.default = useCreateNavigationMenu;
//# sourceMappingURL=use-create-navigation-menu.cjs.map