diff --git a/packages/block-library/src/post-content/block.json b/packages/block-library/src/post-content/block.json index d04c4d474093e6..9162bc75e3e87c 100644 --- a/packages/block-library/src/post-content/block.json +++ b/packages/block-library/src/post-content/block.json @@ -1,5 +1,5 @@ { "name": "core/post-content", "category": "layout", - "context": [ "postId" ] + "context": [ "postId", "postType" ] } diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 3336e0d8199676..99247da7980084 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -1,9 +1,18 @@ -export default function PostContentEdit() { - return ( -

- { - 'Welcome to WordPress and the wonderful world of blocks. This content represents how a post would look when editing block templates.' - } -

- ); +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import PostContentInnerBlocks from './inner-blocks'; + +export default function PostContentEdit( { context: { postId, postType } } ) { + if ( postId && postType ) { + return ( + + ); + } + return

{ __( 'This is a placeholder for post content.' ) }

; } diff --git a/packages/block-library/src/post-content/inner-blocks.js b/packages/block-library/src/post-content/inner-blocks.js new file mode 100644 index 00000000000000..e42d00b35d1c95 --- /dev/null +++ b/packages/block-library/src/post-content/inner-blocks.js @@ -0,0 +1,20 @@ +/** + * WordPress dependencies + */ +import { useEntityBlockEditor } from '@wordpress/core-data'; +import { InnerBlocks } from '@wordpress/block-editor'; + +export default function PostContentInnerBlocks( { postType, postId } ) { + const [ blocks, onInput, onChange ] = useEntityBlockEditor( + 'postType', + postType, + { id: postId } + ); + return ( + + ); +} diff --git a/packages/core-data/src/entity-provider.js b/packages/core-data/src/entity-provider.js index d2d0f23569ca4c..82732aa2c01006 100644 --- a/packages/core-data/src/entity-provider.js +++ b/packages/core-data/src/entity-provider.js @@ -74,16 +74,19 @@ export function useEntityId( kind, type ) { * specified property of the nearest provided * entity of the specified type. * - * @param {string} kind The entity kind. - * @param {string} type The entity type. - * @param {string} prop The property name. + * @param {string} kind The entity kind. + * @param {string} type The entity type. + * @param {string} prop The property name. + * @param {string} [_id] An entity ID to use instead of the context-provided one. * * @return {[*, Function]} A tuple where the first item is the * property value and the second is the * setter. */ -export function useEntityProp( kind, type, prop ) { - const id = useEntityId( kind, type ); +export function useEntityProp( kind, type, prop, _id ) { + const providerId = useEntityId( kind, type ); + const id = _id ?? providerId; + const { value, fullValue } = useSelect( ( select ) => { const { getEntityRecord, getEditedEntityRecord } = select( 'core' ); @@ -128,18 +131,31 @@ export function useEntityProp( kind, type, prop ) { * @param {Object} [options.initialEdits] Initial edits object for the entity record. * @param {string} [options.blocksProp='blocks'] The name of the entity prop that holds the blocks array. * @param {string} [options.contentProp='content'] The name of the entity prop that holds the serialized blocks. + * @param {string} [options.id] An entity ID to use instead of the context-provided one. * * @return {[WPBlock[], Function, Function]} The block array and setters. */ export function useEntityBlockEditor( kind, type, - { initialEdits, blocksProp = 'blocks', contentProp = 'content' } = {} + { + initialEdits, + blocksProp = 'blocks', + contentProp = 'content', + id: _id, + } = {} ) { - const [ content, setContent ] = useEntityProp( kind, type, contentProp ); + const providerId = useEntityId( kind, type ); + const id = _id ?? providerId; + + const [ content, setContent ] = useEntityProp( + kind, + type, + contentProp, + id + ); const { editEntityRecord } = useDispatch( 'core' ); - const id = useEntityId( kind, type ); const initialBlocks = useMemo( () => { if ( initialEdits ) { editEntityRecord( kind, type, id, initialEdits, { @@ -157,7 +173,8 @@ export function useEntityBlockEditor( const [ blocks = initialBlocks, onInput ] = useEntityProp( kind, type, - blocksProp + blocksProp, + id ); const onChange = useCallback(