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 Mar 6, 2020
1 parent f86c195 commit 0e7843c
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function gutenberg_provide_render_callback_with_block_object( $pre_render, $bloc
}
if ( isset( $block_type->attributes ) ) {
foreach ( $block_type->attributes as $attribute_name => $attribute_config ) {
if ( $attribute_config['context'] && isset( $block['attrs'][ $attribute_name ] ) ) {
if ( isset( $attribute_config['context'] ) && $attribute_config['context'] && isset( $block['attrs'][ $attribute_name ] ) ) {
if ( ! isset( $next_context[ $block['blockName'] ] ) ) {
$next_context[ $block['blockName'] ] = array();
}
Expand Down
16 changes: 12 additions & 4 deletions packages/block-editor/src/components/block-list/block-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ function Providers( { blockType, blockAttributes, children } ) {
attributeConfig.context &&
blockAttributes[ attributeName ] !== undefined
) {
const Provider = getContext( blockType.name, attributeName ).Provider;
const Provider = getContext( blockType.name, attributeName )
.Provider;
children = (
<Provider value={ blockAttributes[ attributeName ] }>{ children }</Provider>
<Provider value={ blockAttributes[ attributeName ] }>
{ children }
</Provider>
);
}
}
Expand All @@ -49,7 +52,9 @@ function BlockContext( {
const blockNames = castArray( blockNameOrBlockNames );
for ( const blockName of blockNames ) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const contextValue = useContext( getContext( blockName, attributeName ) );
const contextValue = useContext(
getContext( blockName, attributeName )
);
if ( contextValue !== undefined ) {
blockContext[ attributeName ] = contextValue;
}
Expand All @@ -60,7 +65,10 @@ function BlockContext( {
<Providers blockType={ blockType } blockAttributes={ blockAttributes }>
<Component
{ ...componentProps }
context={ useMemo( () => blockContext, Object.values( blockContext ) ) }
context={ useMemo(
() => blockContext,
Object.values( blockContext )
) }
/>
</Providers>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { compose, pure, ifCondition } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BlockContext from './block-context';
import _BlockContext from './block-context';
import BlockEdit from '../block-edit';
import BlockInvalidWarning from './block-invalid-warning';
import BlockCrashWarning from './block-crash-warning';
Expand Down Expand Up @@ -131,7 +131,7 @@ function BlockListBlock( {
// (InspectorControls, etc.) which are inside `BlockEdit` but not
// `BlockHTML`, even in HTML mode.
let blockEdit = (
<BlockContext
<_BlockContext
blockType={ blockType }
blockAttributes={ attributes }
Component={ BlockEdit }
Expand Down
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 } />;
}
44 changes: 31 additions & 13 deletions packages/block-library/src/post/edit/placeholder.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/**
* 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 +23,57 @@ 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 choosePost = useCallback(
() => setAttributes( { postType, postId } ),
[ postType, postId ]
);
const choosePostId = useCallback( () => setAttributes( { postId } ), [ 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 0e7843c

Please sign in to comment.