Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FSE: Add entity editor to post content block #22473

Merged
merged 8 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/block-library/src/post-content/block.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "core/post-content",
"category": "layout",
"context": [ "postId" ]
"context": [ "postId", "postType" ]
}
25 changes: 19 additions & 6 deletions packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
export default function PostContentEdit() {
/**
* WordPress dependencies
*/
import { EntityProvider } from '@wordpress/core-data';
noahtallen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Internal dependencies
*/
import PostContentInnerBlocks from './inner-blocks';

export default function PostContentEdit( { context: { postId, postType } } ) {
if ( postId && postType ) {
return (
<EntityProvider kind="postType" type={ postType } id={ postId }>
<PostContentInnerBlocks postType={ postType } />
</EntityProvider>
);
}
return (
<p>
{
'Welcome to WordPress and the wonderful world of blocks. This content represents how a post would look when editing block templates.'
}
</p>
<p>{ 'Try setting the active page or post via the page selector.' }</p>
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
);
}
29 changes: 29 additions & 0 deletions packages/block-library/src/post-content/inner-blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* WordPress dependencies
*/
import { useEntityBlockEditor } from '@wordpress/core-data';
import { InnerBlocks } from '@wordpress/block-editor';

/**
* Renders the content of the current post in a controlled inner block area.
*
* @param {Object} Props Component props.
* @param {string} Props.postType The postType to use for the entity blocks.
* @return {Function} Controlled InnerBlocks of the current post.
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
*/
export default function PostContentInnerBlocks( { postType } ) {
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
postType,
{
initialEdits: { status: 'publish' },
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
}
);
return (
<InnerBlocks
value={ blocks }
onInput={ onInput }
onChange={ onChange }
/>
);
}