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

[Block Library - Post Comment Content]: Fix block's edit function #35190

Merged
merged 3 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions packages/block-library/src/post-comment-content/edit.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { RawHTML } from '@wordpress/element';
import { useEntityProp } from '@wordpress/core-data';
import { useBlockProps } from '@wordpress/block-editor';

// TODO: JSDOC types
export default function Edit( { attributes, context } ) {
const { className } = attributes;
const { commentId } = context;
import { useBlockProps, Warning } from '@wordpress/block-editor';
import { Disabled } from '@wordpress/components';

export default function Edit( { context: { commentId } } ) {
const blockProps = useBlockProps();
const [ content ] = useEntityProp(
'root',
'comment',
'content',
commentId
);

if ( ! content?.rendered ) {
return (
<div { ...blockProps }>
<Warning>{ __( 'Comment has no content.' ) }</Warning>
</div>
);
}
return (
<div { ...useBlockProps() }>
<p className={ className }>{ content }</p>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this used to work somehow before? 🤔

The API seems to return an object from the start?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ntsekouras, I just saw that we are working on the same issue, although it seems that you have advanced more than me 😃 😅 #35183

Did this used to work somehow before? 🤔

Wondering the same ― for me, it wasn't working. Which totally makes sense, as the object the useEntityProp hook returns comes from the REST API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey David 👋 - sorry for this... I was reviewing the post avatar PR and noticed that it was broken.

I had seen that there was activity to the issue and my PR just fixes the breakage without the rest enhancements in the issue - that's why I just mentioned it as related..

I guess we can land this PR and you handle all the other stuff (including the renaming 😄 ). What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no problem! I'll wait for this PR to be merged. 👍

<div { ...blockProps }>
<Disabled>
<RawHTML key="html">{ content.rendered }</RawHTML>
</Disabled>
</div>
);
}
9 changes: 6 additions & 3 deletions packages/block-library/src/post-comment-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ function render_block_core_post_comment_content( $attributes, $content, $block )
if ( ! isset( $block->context['commentId'] ) ) {
return '';
}
$wrapper_attributes = get_block_wrapper_attributes();
$comment_text = get_comment_text( $block->context['commentId'] );
if ( ! $comment_text ) {
return '';
}
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
get_comment_text( $block->context['commentId'] )
get_block_wrapper_attributes(),
$comment_text
);
}

Expand Down
21 changes: 14 additions & 7 deletions packages/block-library/src/post-comment/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ import { __ } from '@wordpress/i18n';
import { Placeholder, TextControl, Button } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { blockDefault } from '@wordpress/icons';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
import {
useBlockProps,
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
} from '@wordpress/block-editor';

const ALLOWED_BLOCKS = [
'core/post-comment-content',
'core/post-comment-author',
'core/post-comment-date',
];
const TEMPLATE = [
[ 'core/post-comment-content' ],
[ 'core/post-comment-author' ],
];

// TODO: JSDOC types
export default function Edit( { attributes, setAttributes } ) {
const { commentId } = attributes;
const [ commentIdInput, setCommentIdInput ] = useState( commentId );
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
allowedBlocks: ALLOWED_BLOCKS,
} );

if ( ! commentId ) {
return (
Expand Down Expand Up @@ -48,9 +59,5 @@ export default function Edit( { attributes, setAttributes } ) {
);
}

return (
<div { ...blockProps }>
<InnerBlocks allowedBlocks={ ALLOWED_BLOCKS } />
</div>
);
return <div { ...innerBlocksProps } />;
}