@wordpress/block-library
Version:
Block library for the WordPress editor.
118 lines (107 loc) • 4.05 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createElement } from "@wordpress/element";
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useBlockProps, useInnerBlocksProps, useSetting, __experimentalRecursionProvider as RecursionProvider, __experimentalUseHasRecursion as useHasRecursion, store as blockEditorStore, Warning } from '@wordpress/block-editor';
import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { useCanEditEntity } from '../utils/hooks';
function ReadOnlyContent(_ref) {
let {
userCanEdit,
postType,
postId
} = _ref;
const [,, content] = useEntityProp('postType', postType, 'content', postId);
const blockProps = useBlockProps();
return content !== null && content !== void 0 && content.protected && !userCanEdit ? createElement("div", blockProps, createElement(Warning, null, __('This content is password protected.'))) : createElement("div", _extends({}, blockProps, {
dangerouslySetInnerHTML: {
__html: content === null || content === void 0 ? void 0 : content.rendered
}
}));
}
function EditableContent(_ref2) {
let {
layout,
context = {}
} = _ref2;
const {
postType,
postId
} = context;
const themeSupportsLayout = useSelect(select => {
var _getSettings;
const {
getSettings
} = select(blockEditorStore);
return (_getSettings = getSettings()) === null || _getSettings === void 0 ? void 0 : _getSettings.supportsLayout;
}, []);
const defaultLayout = useSetting('layout') || {};
const usedLayout = !!layout && layout.inherit ? defaultLayout : layout;
const [blocks, onInput, onChange] = useEntityBlockEditor('postType', postType, {
id: postId
});
const props = useInnerBlocksProps(useBlockProps({
className: 'entry-content'
}), {
value: blocks,
onInput,
onChange,
__experimentalLayout: themeSupportsLayout ? usedLayout : undefined
});
return createElement("div", props);
}
function Content(props) {
const {
context: {
queryId,
postType,
postId
} = {}
} = props;
const isDescendentOfQueryLoop = Number.isFinite(queryId);
const userCanEdit = useCanEditEntity('postType', postType, postId);
const isEditable = userCanEdit && !isDescendentOfQueryLoop;
return isEditable ? createElement(EditableContent, props) : createElement(ReadOnlyContent, {
userCanEdit: userCanEdit,
postType: postType,
postId: postId
});
}
function Placeholder() {
const blockProps = useBlockProps();
return createElement("div", blockProps, createElement("p", null, __('This is the Post Content block, it will display all the blocks in any single post or page.')), createElement("p", null, __('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.')), createElement("p", null, __('If there are any Custom Post Types registered at your site, the Post Content block can display the contents of those entries as well.')));
}
function RecursionError() {
const blockProps = useBlockProps();
return createElement("div", blockProps, createElement(Warning, null, __('Block cannot be rendered inside itself.')));
}
export default function PostContentEdit(_ref3) {
let {
context,
attributes
} = _ref3;
const {
postId: contextPostId,
postType: contextPostType
} = context;
const {
layout = {}
} = attributes;
const hasAlreadyRendered = useHasRecursion(contextPostId);
if (contextPostId && contextPostType && hasAlreadyRendered) {
return createElement(RecursionError, null);
}
return createElement(RecursionProvider, {
uniqueId: contextPostId
}, contextPostId && contextPostType ? createElement(Content, {
context: context,
layout: layout
}) : createElement(Placeholder, null));
}
//# sourceMappingURL=edit.js.map