@wordpress/block-library
Version:
Block library for the WordPress editor.
257 lines (255 loc) • 10.6 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-library/src/breadcrumbs/edit.js
var edit_exports = {};
__export(edit_exports, {
default: () => BreadcrumbEdit
});
module.exports = __toCommonJS(edit_exports);
var import_i18n = require("@wordpress/i18n");
var import_block_editor = require("@wordpress/block-editor");
var import_components = require("@wordpress/components");
var import_data = require("@wordpress/data");
var import_core_data = require("@wordpress/core-data");
var import_element = require("@wordpress/element");
var import_server_side_render = require("@wordpress/server-side-render");
var import_hooks = require("../utils/hooks");
var import_jsx_runtime = require("react/jsx-runtime");
var separatorDefaultValue = "/";
function BreadcrumbEdit({
attributes,
setAttributes,
context: { postId, postType, templateSlug }
}) {
const {
separator,
showHomeItem,
showCurrentItem,
prefersTaxonomy,
showOnHomePage
} = attributes;
const {
post,
isPostTypeHierarchical,
postTypeHasTaxonomies,
hasTermsAssigned,
isLoading
} = (0, import_data.useSelect)(
(select) => {
if (!postType) {
return {};
}
const _post = select(import_core_data.store).getEntityRecord(
"postType",
postType,
postId
);
const postTypeObject = select(import_core_data.store).getPostType(postType);
const _postTypeHasTaxonomies = postTypeObject && postTypeObject.taxonomies.length;
let taxonomies;
if (_postTypeHasTaxonomies) {
taxonomies = select(import_core_data.store).getTaxonomies({
type: postType,
per_page: -1
});
}
return {
post: _post,
isPostTypeHierarchical: postTypeObject?.hierarchical,
postTypeHasTaxonomies: _postTypeHasTaxonomies,
hasTermsAssigned: _post && (taxonomies || []).filter(
({ visibility }) => visibility?.publicly_queryable
).some((taxonomy) => {
return !!_post[taxonomy.rest_base]?.length;
}),
isLoading: postId && !_post || !postTypeObject || _postTypeHasTaxonomies && !taxonomies
};
},
[postType, postId]
);
const [invalidationKey, setInvalidationKey] = (0, import_element.useState)(0);
(0, import_element.useEffect)(() => {
setInvalidationKey((c) => c + 1);
}, [post]);
const blockProps = (0, import_block_editor.useBlockProps)();
const dropdownMenuProps = (0, import_hooks.useToolsPanelDropdownMenuProps)();
const { content } = (0, import_server_side_render.useServerSideRender)({
attributes,
skipBlockSupportAttributes: true,
block: "core/breadcrumbs",
urlQueryArgs: { post_id: postId, invalidationKey }
});
if (isLoading) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...blockProps, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Spinner, {}) });
}
let _showTerms;
if (!isPostTypeHierarchical && !post?.parent) {
_showTerms = true;
} else if (!postTypeHasTaxonomies) {
_showTerms = false;
} else {
_showTerms = prefersTaxonomy;
}
let placeholder = null;
const showPlaceholder = !postId || !postType || // When `templateSlug` is set only show placeholder if the post type is not.
// This is needed because when we are showing the template in post editor we
// want to show the real breadcrumbs if we have the post type.
templateSlug && !postType || !_showTerms && !isPostTypeHierarchical || _showTerms && !hasTermsAssigned;
if (showPlaceholder) {
const placeholderItems = [];
if (showHomeItem) {
placeholderItems.push((0, import_i18n.__)("Home"));
}
if (templateSlug && !postId) {
placeholderItems.push((0, import_i18n.__)("Page"));
} else if (_showTerms) {
placeholderItems.push((0, import_i18n.__)("Category"));
} else {
placeholderItems.push((0, import_i18n.__)("Ancestor"), (0, import_i18n.__)("Parent"));
}
placeholder = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"nav",
{
style: {
"--separator": `'${separator}'`
},
inert: "true",
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("ol", { children: [
placeholderItems.map((text, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("a", { href: `#breadcrumbs-pseudo-link-${index}`, children: text }) }, index)),
showCurrentItem && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { "aria-current": "page", children: (0, import_i18n.__)("Current") }) })
] })
}
);
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
import_components.__experimentalToolsPanel,
{
label: (0, import_i18n.__)("Settings"),
resetAll: () => {
setAttributes({
separator: separatorDefaultValue,
showHomeItem: true,
showCurrentItem: true
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.__experimentalToolsPanelItem,
{
label: (0, import_i18n.__)("Show home breadcrumb"),
isShownByDefault: true,
hasValue: () => !showHomeItem,
onDeselect: () => setAttributes({
showHomeItem: true
}),
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.ToggleControl,
{
__nextHasNoMarginBottom: true,
label: (0, import_i18n.__)("Show home breadcrumb"),
onChange: (value) => setAttributes({ showHomeItem: value }),
checked: showHomeItem
}
)
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.__experimentalToolsPanelItem,
{
label: (0, import_i18n.__)("Show current breadcrumb"),
isShownByDefault: true,
hasValue: () => !showCurrentItem,
onDeselect: () => setAttributes({
showCurrentItem: true
}),
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.ToggleControl,
{
__nextHasNoMarginBottom: true,
label: (0, import_i18n.__)("Show current breadcrumb"),
onChange: (value) => setAttributes({ showCurrentItem: value }),
checked: showCurrentItem
}
)
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.__experimentalToolsPanelItem,
{
label: (0, import_i18n.__)("Separator"),
isShownByDefault: true,
hasValue: () => separator !== separatorDefaultValue,
onDeselect: () => setAttributes({
separator: separatorDefaultValue
}),
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.TextControl,
{
__nextHasNoMarginBottom: true,
__next40pxDefaultSize: true,
autoComplete: "off",
label: (0, import_i18n.__)("Separator"),
value: separator,
onChange: (value) => setAttributes({ separator: value }),
onBlur: () => {
if (!separator) {
setAttributes({
separator: separatorDefaultValue
});
}
}
}
)
}
)
]
}
) }),
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_block_editor.InspectorControls, { group: "advanced", children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.CheckboxControl,
{
__nextHasNoMarginBottom: true,
label: (0, import_i18n.__)("Show on homepage"),
checked: showOnHomePage,
onChange: (value) => setAttributes({ showOnHomePage: value }),
help: (0, import_i18n.__)(
"If this breadcrumbs block appears in a template or template part that\u2019s shown on the homepage, enable this option to display the breadcrumb trail. Otherwise, this setting has no effect."
)
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.CheckboxControl,
{
__nextHasNoMarginBottom: true,
label: (0, import_i18n.__)("Prefer taxonomy terms"),
checked: prefersTaxonomy,
onChange: (value) => setAttributes({ prefersTaxonomy: value }),
help: (0, import_i18n.__)(
"The exact type of breadcrumbs shown will vary automatically depending on the page in which this block is displayed. In the specific case of a hierarchical post type with taxonomies, the breadcrumbs can either reflect its post hierarchy (default) or the hierarchy of its assigned taxonomy terms."
)
}
)
] }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...blockProps, children: showPlaceholder ? placeholder : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_element.RawHTML, { inert: "true", children: content }) })
] });
}
//# sourceMappingURL=edit.js.map