forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Post Comment block (WordPress#24781)
Laying the foundation for a block-based post comment building. Adds a Post Comment block which requires a Comment ID input for now. The Post Comment block provides a context for the inner blocks. As an example, also implemented a very simple Post Comment Content block, which uses the context provided by the Post Comment block.
- Loading branch information
1 parent
beced05
commit a1201ab
Showing
19 changed files
with
288 additions
and
17 deletions.
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
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,8 @@ | ||
{ | ||
"name": "core/post-comment-content", | ||
"category": "design", | ||
"usesContext": [ "commentId" ], | ||
"supports": { | ||
"html": false | ||
} | ||
} |
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,19 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
|
||
// TODO: JSDOC types | ||
export default function Edit( { attributes, context } ) { | ||
const { className } = attributes; | ||
const { commentId } = context; | ||
|
||
const [ content ] = useEntityProp( | ||
'root', | ||
'comment', | ||
'content', | ||
commentId | ||
); | ||
|
||
return <p className={ className }>{ content }</p>; | ||
} |
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,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { alignJustify as icon } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
|
||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'Post Comment Content' ), | ||
description: __( 'Post Comment Content' ), | ||
icon, | ||
edit, | ||
parent: [ 'core/post-comment' ], | ||
}; |
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,35 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/post-comment-content` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/post-comment-content` block on the server. | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block default content. | ||
* @param WP_Block $block Block instance. | ||
* @return string Return the post comment's content. | ||
*/ | ||
function render_block_core_post_comment_content( $attributes, $content, $block ) { | ||
if ( ! isset( $block->context['commentId'] ) ) { | ||
return ''; | ||
} | ||
|
||
return sprintf( '<div>%1$s</div>', get_comment_text( $block->context['commentId'] ) ); | ||
} | ||
|
||
/** | ||
* Registers the `core/post-comment-content` block on the server. | ||
*/ | ||
function register_block_core_post_comment_content() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/post-comment-content', | ||
array( | ||
'render_callback' => 'render_block_core_post_comment_content', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_post_comment_content' ); |
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,15 @@ | ||
{ | ||
"name": "core/post-comment", | ||
"category": "design", | ||
"attributes": { | ||
"commentId": { | ||
"type": "number" | ||
} | ||
}, | ||
"providesContext": { | ||
"commentId": "commentId" | ||
}, | ||
"supports": { | ||
"html": false | ||
} | ||
} |
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,46 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { Placeholder, TextControl, Button } from '@wordpress/components'; | ||
import { useState } from '@wordpress/element'; | ||
import { blockDefault } from '@wordpress/icons'; | ||
import { InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
const ALLOWED_BLOCKS = [ [ 'core/post-comment-content' ] ]; | ||
|
||
// TODO: JSDOC types | ||
export default function Edit( { className, attributes, setAttributes } ) { | ||
const { commentId } = attributes; | ||
const [ commentIdInput, setCommentIdInput ] = useState( commentId ); | ||
|
||
if ( ! commentId ) { | ||
return ( | ||
<Placeholder | ||
icon={ blockDefault } | ||
label={ __( 'Post Comment' ) } | ||
instructions={ __( 'Input post comment ID' ) } | ||
> | ||
<TextControl | ||
value={ commentId } | ||
onChange={ ( val ) => setCommentIdInput( parseInt( val ) ) } | ||
/> | ||
|
||
<Button | ||
isPrimary | ||
onClick={ () => { | ||
setAttributes( { commentId: commentIdInput } ); | ||
} } | ||
> | ||
{ __( 'Save' ) } | ||
</Button> | ||
</Placeholder> | ||
); | ||
} | ||
|
||
return ( | ||
<div className={ className }> | ||
<InnerBlocks allowedBlocks={ ALLOWED_BLOCKS } /> | ||
</div> | ||
); | ||
} |
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,23 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { comment as icon } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
import save from './save'; | ||
|
||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'Post Comment' ), | ||
description: __( 'Post Comment' ), | ||
icon, | ||
edit, | ||
save, | ||
}; |
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,17 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/post-comment` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Registers the `core/post-comment` block on the server. | ||
* We need to do this to make context available for inner blocks. | ||
*/ | ||
function register_block_core_post_comment() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/post-comment' | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_post_comment' ); |
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,14 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
export default function save( { attributes } ) { | ||
const { className } = attributes; | ||
|
||
return ( | ||
<div className={ className }> | ||
<InnerBlocks.Content /> | ||
</div> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/fixtures/blocks/core__post-comment-content.html
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 @@ | ||
<!-- wp:post-comment-content /--> |
10 changes: 10 additions & 0 deletions
10
packages/e2e-tests/fixtures/blocks/core__post-comment-content.json
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,10 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/post-comment-content", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
18 changes: 18 additions & 0 deletions
18
packages/e2e-tests/fixtures/blocks/core__post-comment-content.parsed.json
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,18 @@ | ||
[ | ||
{ | ||
"blockName": "core/post-comment-content", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/fixtures/blocks/core__post-comment-content.serialized.html
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 @@ | ||
<!-- wp:post-comment-content /--> |
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,3 @@ | ||
<!-- wp:core/post-comment --> | ||
<div class="wp-block-post-comment"></div> | ||
<!-- /wp:core/post-comment --> |
10 changes: 10 additions & 0 deletions
10
packages/e2e-tests/fixtures/blocks/core__post-comment.json
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,10 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/post-comment", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "<div class=\"wp-block-post-comment\"></div>" | ||
} | ||
] |
20 changes: 20 additions & 0 deletions
20
packages/e2e-tests/fixtures/blocks/core__post-comment.parsed.json
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,20 @@ | ||
[ | ||
{ | ||
"blockName": "core/post-comment", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n<div class=\"wp-block-post-comment\"></div>\n", | ||
"innerContent": [ | ||
"\n<div class=\"wp-block-post-comment\"></div>\n" | ||
] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
3 changes: 3 additions & 0 deletions
3
packages/e2e-tests/fixtures/blocks/core__post-comment.serialized.html
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,3 @@ | ||
<!-- wp:post-comment --> | ||
<div class="wp-block-post-comment"></div> | ||
<!-- /wp:post-comment --> |