@wordpress/block-library
Version:
Block library for the WordPress editor.
154 lines (153 loc) • 7.16 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_block_editor = require("@wordpress/block-editor");
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 ___utils_hooks_js = require("../utils/hooks.cjs");
let ___lock_unlock_ts = require("../lock-unlock.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/post-content/edit.jsx
const { HTMLElementControl } = (0, ___lock_unlock_ts.unlock)(_wordpress_block_editor.privateApis);
function ReadOnlyContent({ parentLayout, layoutClassNames, userCanEdit, postType, postId, tagName: TagName = "div" }) {
const [, , content] = (0, _wordpress_core_data.useEntityProp)("postType", postType, "content", postId);
const blockProps = (0, _wordpress_block_editor.useBlockProps)({ className: layoutClassNames });
const blocks = (0, _wordpress_element.useMemo)(() => {
return content?.raw ? (0, _wordpress_blocks.parse)(content.raw) : [];
}, [content?.raw]);
const blockPreviewProps = (0, _wordpress_block_editor.__experimentalUseBlockPreview)({
blocks,
props: blockProps,
layout: parentLayout
});
if (userCanEdit) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...blockPreviewProps });
return content?.protected ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TagName, {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.Warning, { children: (0, _wordpress_i18n.__)("This content is password protected.") })
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TagName, {
...blockProps,
dangerouslySetInnerHTML: { __html: content?.rendered }
});
}
function EditableContentLoaded({ blockProps, blocks, onInput, onChange, tagName: TagName = "div" }) {
const props = (0, _wordpress_block_editor.useInnerBlocksProps)(blockProps, {
value: blocks,
onInput,
onChange
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TagName, { ...props });
}
function EditableContent({ context = {}, tagName: TagName = "div" }) {
const { postType, postId } = context;
const [blocks, onInput, onChange] = (0, _wordpress_core_data.useEntityBlockEditor)("postType", postType, { id: postId });
const hasLoadedRecord = (0, _wordpress_data.useSelect)((select) => select(_wordpress_core_data.store).getEntityRecord("postType", postType, postId) !== void 0 || select(_wordpress_core_data.store).hasFinishedResolution("getEntityRecord", [
"postType",
postType,
postId
]), [postType, postId]);
const blockProps = (0, _wordpress_block_editor.useBlockProps)({ className: "entry-content" });
if (!hasLoadedRecord) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TagName, { ...blockProps });
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditableContentLoaded, {
blockProps,
blocks,
onInput,
onChange,
tagName: TagName
});
}
function Content(props) {
const { context: { queryId, postType, postId } = {}, layoutClassNames, tagName } = props;
const userCanEdit = (0, ___utils_hooks_js.useCanEditEntity)("postType", postType, postId);
if (userCanEdit === void 0) return null;
return userCanEdit && !Number.isFinite(queryId) ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditableContent, { ...props }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ReadOnlyContent, {
parentLayout: props.parentLayout,
layoutClassNames,
userCanEdit,
postType,
postId,
tagName
});
}
function Placeholder({ layoutClassNames }) {
const blockProps = (0, _wordpress_block_editor.useBlockProps)({ className: layoutClassNames });
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
...blockProps,
children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: (0, _wordpress_i18n.__)("This is the Content block, it will display all the blocks in any single post or page.") }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: (0, _wordpress_i18n.__)("That might be a simple arrangement like consecutive paragraphs in a blog post, or a more elaborate composition that includes image galleries, videos, tables, columns, and any other block types.") }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: (0, _wordpress_i18n.__)("If there are any Custom Post Types registered at your site, the Content block can display the contents of those entries as well.") })
]
});
}
function RecursionError() {
const blockProps = (0, _wordpress_block_editor.useBlockProps)();
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.Warning, { children: (0, _wordpress_i18n.__)("Block cannot be rendered inside itself.") })
});
}
/**
* Render inspector controls for the PostContent block.
*
* @param {Object} props Component props.
* @param {string} props.tagName The HTML tag name.
* @param {Function} props.onSelectTagName onChange function for the SelectControl.
* @param {string} props.clientId The client ID of the current block.
*
* @return {React.JSX.Element} The control group.
*/
function PostContentEditControls({ tagName, onSelectTagName, clientId }) {
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, {
group: "advanced",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(HTMLElementControl, {
tagName,
onChange: onSelectTagName,
clientId,
options: [
{
label: (0, _wordpress_i18n.__)("Default (<div>)"),
value: "div"
},
{
label: "<main>",
value: "main"
},
{
label: "<section>",
value: "section"
},
{
label: "<article>",
value: "article"
}
]
})
});
}
function PostContentEdit({ context, attributes: { tagName = "div" }, setAttributes, clientId, __unstableLayoutClassNames: layoutClassNames, __unstableParentLayout: parentLayout }) {
const { postId: contextPostId, postType: contextPostType } = context;
const hasAlreadyRendered = (0, _wordpress_block_editor.useHasRecursion)(contextPostId);
if (contextPostId && contextPostType && hasAlreadyRendered) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RecursionError, {});
const handleSelectTagName = (value) => {
setAttributes({ tagName: value });
};
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(PostContentEditControls, {
tagName,
onSelectTagName: handleSelectTagName,
clientId
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.RecursionProvider, {
uniqueId: contextPostId,
children: contextPostId && contextPostType ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Content, {
context,
parentLayout,
layoutClassNames
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Placeholder, { layoutClassNames })
})] });
}
exports.default = PostContentEdit;
//# sourceMappingURL=edit.cjs.map