@wordpress/block-library
Version:
Block library for the WordPress editor.
127 lines (125 loc) • 5.14 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-create-navigation-menu.js
var use_create_navigation_menu_exports = {};
__export(use_create_navigation_menu_exports, {
CREATE_NAVIGATION_MENU_ERROR: () => CREATE_NAVIGATION_MENU_ERROR,
CREATE_NAVIGATION_MENU_IDLE: () => CREATE_NAVIGATION_MENU_IDLE,
CREATE_NAVIGATION_MENU_PENDING: () => CREATE_NAVIGATION_MENU_PENDING,
CREATE_NAVIGATION_MENU_SUCCESS: () => CREATE_NAVIGATION_MENU_SUCCESS,
default: () => useCreateNavigationMenu
});
module.exports = __toCommonJS(use_create_navigation_menu_exports);
var import_blocks = require("@wordpress/blocks");
var import_core_data = require("@wordpress/core-data");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_use_generate_default_navigation_title = __toESM(require("./use-generate-default-navigation-title"));
var CREATE_NAVIGATION_MENU_SUCCESS = "success";
var CREATE_NAVIGATION_MENU_ERROR = "error";
var CREATE_NAVIGATION_MENU_PENDING = "pending";
var CREATE_NAVIGATION_MENU_IDLE = "idle";
function useCreateNavigationMenu(clientId) {
const [status, setStatus] = (0, import_element.useState)(CREATE_NAVIGATION_MENU_IDLE);
const [value, setValue] = (0, import_element.useState)(null);
const [error, setError] = (0, import_element.useState)(null);
const { saveEntityRecord, editEntityRecord } = (0, import_data.useDispatch)(import_core_data.store);
const generateDefaultTitle = (0, import_use_generate_default_navigation_title.default)(clientId);
const create = (0, import_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, import_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]
);
return {
create,
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
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CREATE_NAVIGATION_MENU_ERROR,
CREATE_NAVIGATION_MENU_IDLE,
CREATE_NAVIGATION_MENU_PENDING,
CREATE_NAVIGATION_MENU_SUCCESS
});
//# sourceMappingURL=use-create-navigation-menu.js.map