-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block Library: Implement Post block and Post blocks editing. #19572
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
32b0304
Block Library: Implement Post block.
epiqueras 69842d6
Block Library: Implement Post Content block editor.
epiqueras acc41ba
Block Library: Implement Post Title block editor.
epiqueras 3e575d8
Block Library: Implement Post block save.
epiqueras 9461576
Lib: Implement server-side block context.
epiqueras 4b61fb3
Block Editor: Implement client-side block context.
epiqueras ed895e4
Editor: Provide root Post block context.
epiqueras 963a64f
Core Data: Support passing a specific ID to entity hooks instead of r…
epiqueras 2cf7ce4
Block Library: Implement Post blocks context server-side.
epiqueras 3aaff0b
Block Library: Implement Post blocks context client-side.
epiqueras 56eec83
Block Editor: Resolve error if block type for context provider undefined
aduth 4fa6936
Block Library: Post: Register from metadata
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/block-editor/src/components/block-list/block-context.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { castArray } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext, useContext, useMemo } from '@wordpress/element'; | ||
|
||
const context = {}; | ||
const getContext = ( blockName, attributeName ) => { | ||
if ( ! context[ blockName ] ) { | ||
context[ blockName ] = {}; | ||
} | ||
if ( ! context[ blockName ][ attributeName ] ) { | ||
context[ blockName ][ attributeName ] = createContext(); | ||
} | ||
return context[ blockName ][ attributeName ]; | ||
}; | ||
|
||
function Providers( { blockType, blockAttributes, children } ) { | ||
if ( blockType ) { | ||
for ( const [ attributeName, attributeConfig ] of Object.entries( | ||
blockType.attributes | ||
) ) { | ||
if ( | ||
attributeConfig.context && | ||
blockAttributes[ attributeName ] !== undefined | ||
) { | ||
const Provider = getContext( blockType.name, attributeName ) | ||
.Provider; | ||
children = ( | ||
<Provider value={ blockAttributes[ attributeName ] }> | ||
{ children } | ||
</Provider> | ||
); | ||
} | ||
} | ||
} | ||
|
||
return children; | ||
} | ||
|
||
function BlockContext( { | ||
blockType, | ||
blockAttributes, | ||
Component, | ||
...componentProps | ||
} ) { | ||
const blockContext = {}; | ||
if ( blockType.context ) { | ||
for ( const [ attributeName, blockNameOrBlockNames ] of Object.entries( | ||
blockType.context | ||
) ) { | ||
const blockNames = castArray( blockNameOrBlockNames ); | ||
for ( const blockName of blockNames ) { | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
const contextValue = useContext( | ||
getContext( blockName, attributeName ) | ||
); | ||
if ( contextValue !== undefined ) { | ||
blockContext[ attributeName ] = contextValue; | ||
} | ||
} | ||
} | ||
} | ||
return ( | ||
<Providers blockType={ blockType } blockAttributes={ blockAttributes }> | ||
<Component | ||
{ ...componentProps } | ||
context={ useMemo( | ||
() => blockContext, | ||
Object.values( blockContext ) | ||
) } | ||
/> | ||
</Providers> | ||
); | ||
} | ||
BlockContext.Providers = Providers; | ||
export default BlockContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,30 @@ | ||
export default function PostContentEdit() { | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEntityBlockEditor } from '@wordpress/core-data'; | ||
import { InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
function PostContentInnerBlocks( { postType, postId } ) { | ||
const [ blocks, onInput, onChange ] = useEntityBlockEditor( | ||
'postType', | ||
postType, | ||
{ | ||
id: postId, | ||
} | ||
); | ||
return ( | ||
<p> | ||
{ | ||
'Welcome to WordPress and the wonderful world of blocks. This content represents how a post would look when editing block templates.' | ||
} | ||
</p> | ||
<InnerBlocks | ||
__experimentalBlocks={ blocks } | ||
onInput={ onInput } | ||
onChange={ onChange } | ||
/> | ||
); | ||
} | ||
|
||
export default function PostContentEdit( { context: { postType, postId } } ) { | ||
if ( ! postType || ! postId ) { | ||
return 'Post Content Placeholder'; | ||
} | ||
|
||
return <PostContentInnerBlocks postType={ postType } postId={ postId } />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,32 @@ | ||
export default function PostTitleEdit() { | ||
return <h2>{ 'Hello world!' }</h2>; | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
import { RichText } from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
function PostTitleInput( { postType, postId } ) { | ||
const [ title, setTitle ] = useEntityProp( | ||
'postType', | ||
postType, | ||
'title', | ||
postId | ||
); | ||
return ( | ||
<RichText | ||
tagName="h2" | ||
placeholder={ __( 'Post Title' ) } | ||
value={ title } | ||
onChange={ setTitle } | ||
allowedFormats={ [] } | ||
/> | ||
); | ||
} | ||
|
||
export default function PostTitleEdit( { context: { postType, postId } } ) { | ||
if ( ! postType || ! postId ) { | ||
return 'Post Title Placeholder'; | ||
} | ||
|
||
return <PostTitleInput postType={ postType } postId={ postId } />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I don't think we should target blocks explicitly here instead of should have "context names". What happens if I want to build an alternative post block with an alternative UI to select posts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You filter the children you want to support.