@wordpress/block-library
Version:
Block library for the WordPress editor.
228 lines (227 loc) • 10.7 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_data = require("@wordpress/data");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_components = require("@wordpress/components");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_core_data = require("@wordpress/core-data");
let _wordpress_element = require("@wordpress/element");
let _wordpress_notices = require("@wordpress/notices");
let __placeholder_jsx = require("./placeholder.cjs");
__placeholder_jsx = __toESM(__placeholder_jsx);
let __selection_modal_jsx = require("./selection-modal.cjs");
__selection_modal_jsx = __toESM(__selection_modal_jsx);
let __advanced_controls_jsx = require("./advanced-controls.cjs");
let __inner_blocks_jsx = require("./inner-blocks.cjs");
__inner_blocks_jsx = __toESM(__inner_blocks_jsx);
let __utils_create_template_part_id_js = require("./utils/create-template-part-id.cjs");
let ______utils_edit_original_inspector_button_tsx = require("../../utils/edit-original-inspector-button.cjs");
______utils_edit_original_inspector_button_tsx = __toESM(______utils_edit_original_inspector_button_tsx);
let __utils_hooks_js = require("./utils/hooks.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/template-part/edit/index.jsx
const SUPPORTED_AREAS = [
"header",
"footer",
"navigation-overlay"
];
function ReplaceButton({ isEntityAvailable, area, templatePartId, isTemplatePartSelectionOpen, setIsTemplatePartSelectionOpen }) {
const { templateParts } = (0, __utils_hooks_js.useAlternativeTemplateParts)(area, templatePartId);
const hasReplacements = !!templateParts.length;
if (!(isEntityAvailable && hasReplacements && SUPPORTED_AREAS.includes(area))) return null;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuItem, {
onClick: () => {
setIsTemplatePartSelectionOpen(true);
},
"aria-expanded": isTemplatePartSelectionOpen,
"aria-haspopup": "dialog",
children: (0, _wordpress_i18n.__)("Replace")
});
}
function TemplatesList({ area, clientId, isEntityAvailable, onSelect }) {
const blockPatterns = (0, __utils_hooks_js.useAlternativeBlockPatterns)(area, clientId);
if (!(isEntityAvailable && !!blockPatterns.length && SUPPORTED_AREAS.includes(area))) return null;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.PanelBody, {
title: (0, _wordpress_i18n.__)("Design"),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.__experimentalBlockPatternsList, {
label: (0, _wordpress_i18n.__)("Templates"),
blockPatterns,
onClickPattern: onSelect,
showTitlesAsTooltip: true
})
});
}
function TemplatePartEdit({ attributes, setAttributes, clientId }) {
const { createSuccessNotice } = (0, _wordpress_data.useDispatch)(_wordpress_notices.store);
const { editEntityRecord } = (0, _wordpress_data.useDispatch)(_wordpress_core_data.store);
const currentTheme = (0, _wordpress_data.useSelect)((select) => select(_wordpress_core_data.store).getCurrentTheme()?.stylesheet, []);
const { slug, theme = currentTheme, tagName, layout = {} } = attributes;
const templatePartId = (0, __utils_create_template_part_id_js.createTemplatePartId)(theme, slug);
const hasAlreadyRendered = (0, _wordpress_block_editor.useHasRecursion)(templatePartId);
const [isTemplatePartSelectionOpen, setIsTemplatePartSelectionOpen] = (0, _wordpress_element.useState)(false);
const { isResolved, hasInnerBlocks, isMissing, area, onNavigateToEntityRecord, title, canUserEdit, canUserEditBlock } = (0, _wordpress_data.useSelect)((select) => {
const { getEditedEntityRecord, hasFinishedResolution } = select(_wordpress_core_data.store);
const { getBlockCount, getSettings, canEditBlock } = select(_wordpress_block_editor.store);
const getEntityArgs = [
"postType",
"wp_template_part",
templatePartId
];
const entityRecord = templatePartId ? getEditedEntityRecord(...getEntityArgs) : null;
const _area = entityRecord?.area || attributes.area;
const hasResolvedEntity = templatePartId ? hasFinishedResolution("getEditedEntityRecord", getEntityArgs) : false;
const _canUserEdit = hasResolvedEntity ? select(_wordpress_core_data.store).canUser("update", {
kind: "postType",
name: "wp_template_part",
id: templatePartId
}) : false;
return {
hasInnerBlocks: getBlockCount(clientId) > 0,
isResolved: hasResolvedEntity,
isMissing: hasResolvedEntity && (!entityRecord || Object.keys(entityRecord).length === 0),
area: _area,
onNavigateToEntityRecord: getSettings().onNavigateToEntityRecord,
title: entityRecord?.title,
canUserEdit: !!_canUserEdit,
canUserEditBlock: canEditBlock(clientId)
};
}, [
templatePartId,
attributes.area,
clientId
]);
const areaObject = (0, __utils_hooks_js.useTemplatePartArea)(area);
const blockProps = (0, _wordpress_block_editor.useBlockProps)();
const isPlaceholder = !slug;
const isEntityAvailable = !isPlaceholder && !isMissing && isResolved;
const TagName = tagName || areaObject.tagName;
const onPatternSelect = async (pattern) => {
await editEntityRecord("postType", "wp_template_part", templatePartId, {
blocks: pattern.blocks,
content: (0, _wordpress_blocks.serialize)(pattern.blocks)
});
createSuccessNotice((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Template Part \"%s\" updated."), title || slug), { type: "snackbar" });
};
if (!hasInnerBlocks && (slug && !theme || slug && isMissing)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TagName, {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.Warning, { children: (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Template part has been deleted or is unavailable: %s"), slug) })
});
if (isEntityAvailable && hasAlreadyRendered) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TagName, {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.Warning, { children: (0, _wordpress_i18n.__)("Block cannot be rendered inside itself.") })
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_block_editor.RecursionProvider, {
uniqueId: templatePartId,
children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(______utils_edit_original_inspector_button_tsx.default, {
clientId,
postType: "wp_template_part",
postId: templatePartId
}),
isEntityAvailable && onNavigateToEntityRecord && canUserEdit && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, {
group: "other",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, {
onClick: () => {
onNavigateToEntityRecord({
postId: templatePartId,
postType: "wp_template_part"
});
},
children: (0, _wordpress_i18n.__)("Edit original")
})
}),
canUserEdit && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, {
group: "advanced",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__advanced_controls_jsx.TemplatePartAdvancedControls, {
tagName,
setAttributes,
isEntityAvailable,
templatePartId,
defaultWrapper: areaObject.tagName,
hasInnerBlocks,
clientId
})
}),
isPlaceholder && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TagName, {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__placeholder_jsx.default, {
area: attributes.area,
templatePartId,
clientId,
setAttributes,
onOpenSelectionModal: () => setIsTemplatePartSelectionOpen(true)
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockSettingsMenuControls, { children: ({ selectedClientIds }) => {
if (!canUserEditBlock || !(selectedClientIds.length === 1 && clientId === selectedClientIds[0])) return null;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ReplaceButton, {
isEntityAvailable,
area,
clientId,
templatePartId,
isTemplatePartSelectionOpen,
setIsTemplatePartSelectionOpen
});
} }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, {
group: "settings",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TemplatesList, {
area,
clientId,
isEntityAvailable,
onSelect: (pattern) => onPatternSelect(pattern)
})
}),
isEntityAvailable && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__inner_blocks_jsx.default, {
tagName: TagName,
blockProps,
postId: templatePartId,
hasInnerBlocks,
layout
}),
!isPlaceholder && !isResolved && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TagName, {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Spinner, {})
})
]
}), isTemplatePartSelectionOpen && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Modal, {
overlayClassName: "block-editor-template-part__selection-modal",
title: (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Choose a %s"), areaObject.label.toLowerCase()),
onRequestClose: () => setIsTemplatePartSelectionOpen(false),
isFullScreen: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__selection_modal_jsx.default, {
templatePartId,
clientId,
area,
setAttributes,
onClose: () => setIsTemplatePartSelectionOpen(false)
})
})] });
}
exports.default = TemplatePartEdit;
//# sourceMappingURL=index.cjs.map