-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Site Editor: Add new Post Comment Avatar block #35180
Conversation
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @c4rl0sbr4v0! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
The 96px is a default value that comes from WordPress core, but themes can override it through The percentage controls are perhaps a little confusing because one users gravatar source image might be a different size to anothers. Perhaps we don't need them?
Preserving aspect ratio makes sense to me. It might also be interesting to try making the Dimensions panel available here so that folks can adjust the padding value. The one other thing missing is the icon, we should probably add that to the library. Here's the svg:
|
@@ -0,0 +1,5 @@ | |||
.wp-block-post-comment-avatar img { |
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.
It seems we should be able to avoid editor-only styles for this one — what is it accomplishing?
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.
The ResizableBox creates a div
, this one the one that is getting the width and height based on options selected. Then we save those variables as attributes for the frontend display.
In order to get the img inside the div to grow as the user changes those variables, we add a width and height inherit class.
We have something similar on image block: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/image/editor.scss#L31-L39
We can look for another approach if needed of course!
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.
It's something we should consider form the perspective of the DX — adding a component like ResizableBox should not be forcing me to supply editor styles, the component itself should be handling that for me.
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 added a class to the main ResizableBox component in order for the next img to inherit the width. Let me know if that is ok. As that component is in a lot of different components of the editor :-)
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.
👋 - thanks for working on this Carlos!!
When adding a new core block, we need to also register it server side here: https://github.com/WordPress/gutenberg/blob/trunk/lib/blocks.php#L73.
Also you need to add a fixture for the auto generated tests to resolve the error:
Expected a fixture file called 'core__post-comment-avatar.html' or 'core__post-comment-avatar__*.html'.
When you're ready with the PR, create a core__post-comment-avatar.html
file here and then run npm run test-unit test/integration/full-content/full-content.test.js
. This will generate the json and parsed
versions. The new file should have the content(html) of the block when is inserted(use the code editor to see the output).
const { commentId } = context; | ||
|
||
// Maximum provided by the WordPress site. | ||
const maxImageWidth = 96; |
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 haven't checked this, by you might want to make sure there is no php filter for this value. WP has tons of filters 😄
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.
What is the API to run PHP filters from JS?
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 have a get_avatar hook that sets the size: https://developer.wordpress.org/reference/hooks/get_avatar/
Is this affecting the edit.js
code, right?
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 removed this code, instead, now we use the default values of block.json
file. I hope that will be a better approach!
In order to remove this. We have to add a new property on ImageSizeControl core block-editor library. Should we do this on this PR or better create a new issue and see what the community prefers? |
Good question. Maybe it makes sense to do that separately and give it the necessary attention? |
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.
Thanks for iterating on this @c4rl0sbr4v0 ! Avatar will have many nuances but we can see what can be done iteratively.
I guess this PR should take into account the imminent renaming of these blocks: #34994 (comment)
Also I would suggest not to bother with the fixtures until the block is ready 😄
}, [] ); | ||
|
||
useEffect( () => { | ||
setAttributes( { |
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.
When we setAttributes
we also add a step in history
(undo, redo). Since avatarUrls
is an array will always be a new reference, so it causes a history trap. You can check this by inserting the block and try to undo
. If you needed only this avatarUrls[ avatarUrls.length - 1 ]
which is a string
, this should be the dependency.
|
||
// Set default widht, height and url on first render. | ||
useEffect( () => { | ||
setDefaultSize( { defaultWidth: width, defaultHeight: height } ); |
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 haven't checked all the nuances with the avatars, but I think we should take into account the core handling. For example this filter: https://developer.wordpress.org/reference/functions/rest_get_avatar_sizes/, which is responsible for author_avatar_urls
value.
Also if we go this direction with a default value it shouldn't be the current width
as this can change from the control and would work as you expected on insertion only. If we end up not getting this dynamically, we should have a hardcoded value (if it makes sense).
const { url, alt, width, height, id, title } = attributes; | ||
return ( | ||
<img | ||
{ ...useBlockProps.save() } |
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 can pass props in useBlockProps.save
like className
.
const { comment } = useSelect( | ||
( select ) => { | ||
const { getEntityRecord } = select( coreStore ); | ||
return { |
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.
Nit: you can return the single value directly (not an object
).
Work moved to #35396. |
Moved to a cleanest PR -> PR #35396
We added a new Post Comment Avatar block. Related to issue #30575.
How has this been tested?
WordPress Version: 5.8.1
Gutenberg Version: 11.6.0-rc.1
Database: MySQL 8.0.16
Web Server: nginx
PHP Version: 7.3.5
Screenshots
Types of changes
Type Experimental
New feature Site Editor
Needs Accessibility Feedback (Although I tried with both keyboard and voice over and seems to be fine)
Needs Testing
Checklist: