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: Post comment author]: Add link settings and block supports #35595
Changes from 6 commits
848bb3a
3c0e791
95a0735
e1824c7
4d53bf0
f9c26e4
db11cc3
bd3ff13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,24 @@ | |
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { PanelBody, ToggleControl } from '@wordpress/components'; | ||
|
||
export default function Edit( { attributes, context } ) { | ||
const { className } = attributes; | ||
/** | ||
* Renders the `core/post-comment-author` block on the editor. | ||
* | ||
* @param {Object} props React props. | ||
* @param {Object} props.setAttributes Callback for updating block attributes. | ||
* @param {Object} props.attributes Block attributes. | ||
* @param {Object} props.context Inherited context. | ||
* | ||
* @return {JSX.Element} React element. | ||
*/ | ||
export default function Edit( { attributes, context, setAttributes } ) { | ||
const { className, isLink, linkTarget } = attributes; | ||
const { commentId } = context; | ||
const blockProps = useBlockProps( { className } ); | ||
|
||
const displayName = useSelect( | ||
( select ) => { | ||
|
@@ -21,15 +33,45 @@ export default function Edit( { attributes, context } ) { | |
const user = getEntityRecord( 'root', 'user', comment.author ); | ||
return user?.name ?? __( 'Anonymous' ); | ||
} | ||
|
||
return authorName ?? ''; | ||
}, | ||
[ commentId ] | ||
); | ||
|
||
const displayAuthor = isLink ? ( | ||
<a | ||
href="#comment-author-pseudo-link" | ||
onClick={ ( event ) => event.preventDefault() } | ||
> | ||
{ displayName } | ||
</a> | ||
) : ( | ||
<p>{ displayName }</p> | ||
); | ||
|
||
return ( | ||
<div { ...useBlockProps() }> | ||
<p className={ className }>{ displayName }</p> | ||
</div> | ||
<> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Link settings' ) }> | ||
<ToggleControl | ||
label={ __( 'Link to authors URL' ) } | ||
onChange={ () => setAttributes( { isLink: ! isLink } ) } | ||
checked={ isLink } | ||
/> | ||
{ isLink && ( | ||
<ToggleControl | ||
label={ __( 'Open in new tab' ) } | ||
onChange={ ( value ) => | ||
setAttributes( { | ||
linkTarget: value ? '_blank' : '_self', | ||
} ) | ||
} | ||
checked={ linkTarget === '_blank' } | ||
/> | ||
) } | ||
</PanelBody> | ||
</InspectorControls> | ||
<div { ...blockProps }>{ displayAuthor }</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The HTML generated doesn’t use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was, but now that you mention, I will edit in order to have it again. Is there any guide about how a block html structure should be? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My best answer would be to ask theme experts @carolinan and @aristath for advice 😃 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, we can iterate on it since we don't save the HTML output in the post/theme content, but use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like there is no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we remove the p on the editor the comments are all misaligned due to padding issues. What would be a better approach?
All feedback will be appreciated! I will leave by the moment as first option, as it was previously. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leave the old behavior and address it seperately. |
||
</> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { SVG, Path, Circle } from '@wordpress/primitives'; | ||
|
||
const commentAuthor = ( | ||
<SVG viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="#FFFFFF"> | ||
<Path | ||
d="M18 4H6c-1.1 0-2 .9-2 2v12.9c0 .6.5 1.1 1.1 1.1.3 0 .5-.1.8-.3L8.5 17H18c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm.5 11c0 .3-.2.5-.5.5H7.9l-2.4 2.4V6c0-.3.2-.5.5-.5h12c.3 0 .5.2.5.5v9z" | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
/> | ||
<Path | ||
d="M15 15V15C15 13.8954 14.1046 13 13 13L11 13C9.89543 13 9 13.8954 9 15V15" | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
/> | ||
<Circle cx="12" cy="9" r="2" fillRule="evenodd" clipRule="evenodd" /> | ||
</SVG> | ||
); | ||
|
||
export default commentAuthor; |
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.
Should it also reset the link target when link gets disabled?
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.
I copied that attribute from Post Title block. There is no reset when you enable/disable the link.
Of course I can add a reset, but maybe we could create an issue to unify on all blocks with the same attribute to have the same behaviour?
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.
New issue for unification sounds like a good plan 👍