Skip to content

Commit

Permalink
Block Library: Implement Post blocks context client-side.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Jan 19, 2020
1 parent ec65bc5 commit 1101065
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 39 deletions.
6 changes: 5 additions & 1 deletion packages/block-library/src/post-content/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"name": "core/post-content",
"category": "layout"
"category": "layout",
"context": {
"postType": "core/post",
"postId": "core/post"
}
}
18 changes: 12 additions & 6 deletions packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/**
* WordPress dependencies
*/
import { useEntityBlockEditor, useEntityId } from '@wordpress/core-data';
import { useEntityBlockEditor } from '@wordpress/core-data';
import { InnerBlocks } from '@wordpress/block-editor';

function PostContentInnerBlocks() {
const [ blocks, onInput, onChange ] = useEntityBlockEditor( 'postType', 'post' );
function PostContentInnerBlocks( { postType, postId } ) {
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
postType,
{
id: postId,
}
);
return (
<InnerBlocks
__experimentalBlocks={ blocks }
Expand All @@ -15,9 +21,9 @@ function PostContentInnerBlocks() {
);
}

export default function PostContentEdit() {
if ( ! useEntityId( 'postType', 'post' ) ) {
export default function PostContentEdit( { context: { postType, postId } } ) {
if ( ! postType || ! postId ) {
return 'Post Content Placeholder';
}
return <PostContentInnerBlocks />;
return <PostContentInnerBlocks postType={ postType } postId={ postId } />;
}
6 changes: 5 additions & 1 deletion packages/block-library/src/post-title/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"name": "core/post-title",
"category": "layout"
"category": "layout",
"context": {
"postType": "core/post",
"postId": "core/post"
}
}
17 changes: 11 additions & 6 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/**
* WordPress dependencies
*/
import { useEntityProp, useEntityId } from '@wordpress/core-data';
import { useEntityProp } from '@wordpress/core-data';
import { RichText } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

function PostTitleInput() {
const [ title, setTitle ] = useEntityProp( 'postType', 'post', 'title' );
function PostTitleInput( { postType, postId } ) {
const [ title, setTitle ] = useEntityProp(
'postType',
postType,
'title',
postId
);
return (
<RichText
tagName="h2"
Expand All @@ -18,9 +23,9 @@ function PostTitleInput() {
);
}

export default function PostTitleEdit() {
if ( ! useEntityId( 'postType', 'post' ) ) {
export default function PostTitleEdit( { context: { postType, postId } } ) {
if ( ! postType || ! postId ) {
return 'Post Title Placeholder';
}
return <PostTitleInput />;
return <PostTitleInput postType={ postType } postId={ postId } />;
}
12 changes: 11 additions & 1 deletion packages/block-library/src/post/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
{
"name": "core/post",
"category": "layout"
"category": "layout",
"attributes": {
"postType": {
"type": "string",
"context": true
},
"postId": {
"type": "string",
"context": true
}
}
}
20 changes: 9 additions & 11 deletions packages/block-library/src/post/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,33 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { EntityProvider } from '@wordpress/core-data';
import { InnerBlocks } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import PostPlaceholder from './placeholder';

export default function PostEdit( { attributes: { postId }, setAttributes } ) {
export default function PostEdit( {
attributes: { postType, postId },
setAttributes,
} ) {
const loaded = useSelect(
( select ) => {
if ( ! postId ) {
if ( ! postType || ! postId ) {
return false;
}
return Boolean(
select( 'core' ).getEntityRecord( 'postType', 'post', postId )
select( 'core' ).getEntityRecord( 'postType', postType, postId )
);
},
[ postId ]
[ postType, postId ]
);
if ( postId ) {
if ( postType && postId ) {
if ( ! loaded ) {
return null;
}
return (
<EntityProvider kind="postType" type="post" id={ postId }>
<InnerBlocks />
</EntityProvider>
);
return <InnerBlocks />;
}
return <PostPlaceholder setAttributes={ setAttributes } />;
}
38 changes: 25 additions & 13 deletions packages/block-library/src/post/edit/placeholder.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* WordPress dependencies
*/
import { useEntityBlockEditor, EntityProvider } from '@wordpress/core-data';
import { useEntityBlockEditor } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
import { BlockPreview } from '@wordpress/block-editor';
import { useState, useCallback } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { Placeholder, TextControl, Button } from '@wordpress/components';

function PostPreview() {
const [ blocks ] = useEntityBlockEditor( 'postType', 'post' );
function PostPreview( { postType, postId } ) {
const [ blocks ] = useEntityBlockEditor( 'postType', postType, { id: postId } );
return (
<div className="wp-block-post__placeholder-preview">
<div className="wp-block-post__placeholder-preview-title">
Expand All @@ -21,41 +21,53 @@ function PostPreview() {
}

export default function PostPlaceholder( { setAttributes } ) {
const [ postType, setPostType ] = useState( 'post' );
const [ postId, setPostId ] = useState();
const preview = useSelect(
( select ) => {
if ( ! postId ) {
if ( ! postType || ! postId ) {
return null;
}
const post = select( 'core' ).getEntityRecord( 'postType', 'post', postId );
const post = select( 'core' ).getEntityRecord( 'postType', postType, postId );
if ( post ) {
return (
<EntityProvider kind="postType" type="post" id={ postId }>
<PostPreview />
</EntityProvider>
);
return <PostPreview postType={ postType } postId={ postId } />;
}
},
[ postId ]
[ postType, postId ]
);
const choosePostId = useCallback( () => setAttributes( { postId } ), [ postId ] );
const choosePost = useCallback( () => setAttributes( { postType, postId } ), [
postType,
postId,
] );
return (
<Placeholder
icon="media-document"
label={ __( 'Post' ) }
instructions={ __( 'Choose a post by post ID.' ) }
>
<div className="wp-block-post__placeholder-input-container">
<TextControl
label={ __( 'Post Type' ) }
placeholder={ __( 'post' ) }
value={ postType }
onChange={ setPostType }
className="wp-block-post__placeholder-input"
/>
<TextControl
label={ __( 'Post ID' ) }
placeholder={ __( '1' ) }
value={ postId }
onChange={ setPostId }
help={ preview === undefined && __( 'Post not found.' ) }
className="wp-block-post__placeholder-input"
/>
</div>
{ preview }
<Button isPrimary disabled={ ! postId || ! preview } onClick={ choosePostId }>
<Button
isPrimary
disabled={ ! postType || ! postId || ! preview }
onClick={ choosePost }
>
{ __( 'Choose' ) }
</Button>
</Placeholder>
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/post/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
width: 100%;
}

.wp-block-post__placeholder-input {
margin: 5px;
}

.wp-block-post__placeholder-preview {
margin-bottom: 15px;
width: 100%;
Expand Down

0 comments on commit 1101065

Please sign in to comment.