import { __ } from '@wordpress/i18n'; import { InspectorControls, useBlockProps, useInnerBlocksProps, RecursionProvider, useHasRecursion, Warning, privateApis as blockEditorPrivateApis, __experimentalUseBlockPreview as useBlockPreview, } from '@wordpress/block-editor'; import { parse } from '@wordpress/blocks'; import type { Block, BlockEditProps } from '@wordpress/blocks'; import { useEntityProp, useEntityBlockEditor, store as coreStore, } from '@wordpress/core-data'; import { useSelect } from '@wordpress/data'; import { useMemo } from '@wordpress/element'; import { useCanEditEntity } from '../utils/hooks'; import { unlock } from '../lock-unlock'; import type { PostContentAttributes, PostContentBlockContext } from './types'; const { HTMLElementControl } = unlock( blockEditorPrivateApis ); type TagNameType = 'div' | 'main' | 'section' | 'article'; function ReadOnlyContent( { parentLayout, layoutClassNames, userCanEdit, postType, postId, tagName: TagName = 'div', }: { parentLayout?: Record< string, unknown >; layoutClassNames?: string; userCanEdit: boolean; postType?: string; postId?: number; tagName?: TagNameType; } ) { const [ , , content ] = useEntityProp< unknown, { raw?: string; rendered?: string; protected?: boolean } >( 'postType', postType!, 'content', postId ); const blockProps = useBlockProps( { className: layoutClassNames } ); const blocks = useMemo( () => { return content?.raw ? parse( content.raw ) : []; }, [ content?.raw ] ); const blockPreviewProps = useBlockPreview( { blocks, props: blockProps, layout: parentLayout, } ); if ( userCanEdit ) { /* * Rendering the block preview using the raw content blocks allows for * block support styles to be generated and applied by the editor. * * The preview using the raw blocks can only be presented to users with * edit permissions for the post to prevent potential exposure of private * block content. */ return
; } return content?.protected ? ( { __( 'This content is password protected.' ) } ) : ( ); } function EditableContentLoaded( { blockProps, blocks, onInput, onChange, tagName: TagName = 'div', }: { blockProps: Record< string, unknown >; blocks: Block[] | undefined; onInput: ReturnType< typeof useEntityBlockEditor >[ 1 ]; onChange: ReturnType< typeof useEntityBlockEditor >[ 2 ]; tagName?: TagNameType; } ) { const props = useInnerBlocksProps( blockProps, { value: blocks, onInput, onChange, } ); return ; } function EditableContent( { context = {}, tagName: TagName = 'div', }: { context?: PostContentBlockContext; tagName?: TagNameType; } ) { const { postType, postId } = context; const [ blocks, onInput, onChange ] = useEntityBlockEditor( 'postType', postType!, { id: postId } ); // Wait for the entity record before mounting the inner blocks area, so // that an empty `blocks` value means an empty post rather than one that // has not loaded yet. const hasLoadedRecord = useSelect( ( select ) => select( coreStore ).getEntityRecord( 'postType', postType!, postId! ) !== undefined || select( coreStore ).hasFinishedResolution( 'getEntityRecord', [ 'postType', postType, postId, ] ), [ postType, postId ] ); const blockProps = useBlockProps( { className: 'entry-content' } ); if ( ! hasLoadedRecord ) { return ; } return ( ); } function Content( props: { context?: PostContentBlockContext; parentLayout?: Record< string, unknown >; layoutClassNames?: string; tagName?: TagNameType; } ) { const { context: { queryId, postType, postId } = {}, layoutClassNames, tagName, } = props; const userCanEdit = useCanEditEntity( 'postType', postType!, postId! ); if ( userCanEdit === undefined ) { return null; } const isDescendentOfQueryLoop = Number.isFinite( queryId ); const isEditable = userCanEdit && ! isDescendentOfQueryLoop; return isEditable ? ( ) : ( ); } function Placeholder( { layoutClassNames }: { layoutClassNames?: string } ) { const blockProps = useBlockProps( { className: layoutClassNames } ); return (

{ __( 'This is the Content block, it will display all the blocks in any single post or page.' ) }

{ __( '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.' ) }

{ __( '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 = useBlockProps(); return (
{ __( 'Block cannot be rendered inside itself.' ) }
); } /** * Render inspector controls for the PostContent block. * * @param props Component props. * @param props.tagName The HTML tag name. * @param props.onSelectTagName onChange function for the SelectControl. * @param props.clientId The client ID of the current block. * * @return The control group. */ function PostContentEditControls( { tagName, onSelectTagName, clientId, }: { tagName: string; onSelectTagName: ( value: string ) => void; clientId: string; } ) { return ( )' ), value: 'div' }, { label: '
', value: 'main' }, { label: '
', value: 'section' }, { label: '
', value: 'article' }, ] } /> ); } export default function PostContentEdit( { context, attributes: { tagName = 'div' }, setAttributes, clientId, __unstableLayoutClassNames: layoutClassNames, __unstableParentLayout: parentLayout, }: BlockEditProps< PostContentAttributes > & { context: PostContentBlockContext; __unstableLayoutClassNames?: string; __unstableParentLayout?: Record< string, unknown >; } ) { const { postId: contextPostId, postType: contextPostType } = context; const hasAlreadyRendered = useHasRecursion( contextPostId ); if ( contextPostId && contextPostType && hasAlreadyRendered ) { return ; } const handleSelectTagName = ( value: string ) => { setAttributes( { tagName: value } ); }; return ( <> { contextPostId && contextPostType ? ( ) : ( ) } ); }