-
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
Blocks: Tag Cloud #4994
Blocks: Tag Cloud #4994
Conversation
Hey @jahvi! Thanks for working on this. What if the block had a placeholder when you first add it to your post? |
Thanks for the feedback @melchoyce that looks great! I'll work on that for the next update. |
a223832
to
a57ce61
Compare
5f358e4
to
c90cecb
Compare
blocks/library/tag-cloud/block.js
Outdated
getTaxonomies() { | ||
const taxonomies = this.props.taxonomies.data; | ||
|
||
if ( ! taxonomies ) { |
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.
Minor: Lodash's _.map
, which you're using, will gracefully handle undefined
values:
n_ > _.map( undefined, () => '' );
[]
blocks/library/tag-cloud/block.js
Outdated
const taxonomies = this.props.taxonomies.data; | ||
const taxonomy = taxonomies[ taxonomySlug ]; | ||
|
||
setAttributes( { taxonomySlug, taxonomyRestBase: taxonomy.rest_base } ); |
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 can't help but think that taxonomyRestBase
is redundant with taxonomySlug
.
You might be able to eliminate it in favor of just the slug, taking advantage of the taxonomy
helper included in the argument passed to withAPIData
callback:
https://github.com/WordPress/gutenberg/tree/master/components/higher-order/with-api-data#usage
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 I wasn't aware of the taxonomy
helper, it does simplify things a lot!
blocks/library/tag-cloud/block.js
Outdated
className={ className } | ||
/> | ||
) : ( | ||
[] |
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.
Minor: It may be easier for React to reconcile† this in the render output if it were expressed as an empty value, e.g. null
or undefined
:
let terms;
if ( termList.data ) {
terms = (
<TermList
terms={ termList.data }
showTagCounts={ showTagCounts }
className={ className }
/>
);
}
Or:
const terms = termList.data ? (
<TermList
terms={ termList.data }
showTagCounts={ showTagCounts }
className={ className }
/>
) : null;
† The assumption being that it shortcuts to avoid iterating over an empty value.
blocks/library/tag-cloud/editor.scss
Outdated
@@ -0,0 +1,13 @@ | |||
.gutenberg .wp-block-tag-cloud { | |||
a { | |||
display: inline-block; |
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.
These spaces should be tabs.
Use tabs, not spaces, to indent each property.
https://make.wordpress.org/core/handbook/best-practices/coding-standards/css/#structure
blocks/library/tag-cloud/index.js
Outdated
taxonomyRestBase: { | ||
type: 'string', | ||
}, | ||
align: { |
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.
With #4069, align
is now simpler to implement for a block via supports
.
blocks/library/tag-cloud/index.php
Outdated
*/ | ||
function gutenberg_render_block_core_tag_cloud( $attributes ) { | ||
$align = 'center'; | ||
if ( isset( $attributes['align'] ) && in_array( $attributes['align'], array( 'left', 'right', 'full' ), true ) ) { |
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 wouldn't expect align
should work here, because you have no attribute schema defined on the server. See the Latest Posts block implementation as an example where attributes are to be defined on the server. These will automatically load into the client, so you don't need to duplicate there.
Eventually the idea is that all blocks will be defined this way : #2751
terms.forEach( ( term, i ) => { | ||
const countWeight = term.count - lowestCount; | ||
const fontSize = MIN + ( countWeight * fontStep ); | ||
terms[ i ].fontScale = `${ fontSize }${ UNIT }`; |
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're mutating the original terms
reference array here which, based on your usage of the return value termFontSizes
below, I don't think is expected.
This might be a good candidate for an Array#map
(returning an array of font sizes) or Array#reduce
(returning an object of term name => font size).
// Calculate font size for each term
return terms.map( ( term, i ) => {
const countWeight = term.count - lowestCount;
const fontSize = MIN + ( countWeight * fontStep );
return fontSize;
} );
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 pointing it out, I chose to use map
since the terms order stays consistent I figured it would make the code simpler.
Thanks for your feedback @aduth I just updated my PR. |
blocks/library/tag-cloud/block.js
Outdated
...taxonomies, | ||
]; | ||
const terms = termList.data ? ( | ||
<TermList |
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.
We could avoid having to implement TermList
which is a duplication of wp_tag_cloud()
if we used ServerSideRender
proposed in #780.
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 agree, not sure what the status of that proposal is but it would have
been useful.
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.
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.
Ah that's interesting, I'll refactor my PR then
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.
Just updated the code to use the new ServerSideRender
component, it does simplify things quite a lot 🙏🏽
3ac4710
to
7ac38ae
Compare
7ac38ae
to
9029500
Compare
Just added this to make sure only taxonomies with |
It looks like this PR has stalled–I've linked it to the issue so we can use it as prior art, but because it hasn't been worked on in three months I'm going to close it. @jahvi If that's my bad and you were waiting on someone to review something, please let us know and we can re-open this/keep iterating. |
Hey @tofumatt I just rebased the latest branches onto this to try to bring this PR back to life (not sure why my latest changes don't show, maybe because PR is closed) I'm having an issue now where my use of the
From what I read this HOC is going to be deprecated but I thought it would be safe to use for now as other components are still using it, can anyone advice what the problem might be? I guess something changed in the HOC implementation that prevents this from working as it did before but comparing it with the other components I can't see any major differences. |
I managed to make the tag cloud block work with the new |
No problem, I just created a new PR #7875 |
Description
This is my attempt at implementing the tag cloud widget as a Gutenberg block, it's still a work in progress but I wanted to get some initial feedback while I'm at it.
Fixes #1801.
I tried to follow the "Categories" block for consistency but there are some areas I'm not sure about:
show_tagcloud
astrue
, however I'm not sure how to do this, can this be done through the rest API?wp_generate_tag_cloud
PHP function in my JS block or is there a better way to get the generated markup?Any feedback would be appreciated! 😀
How Has This Been Tested?
Tested in the latest master branch.
Screenshots (jpeg or gifs if applicable):
Types of changes
New feature: tag cloud block
Checklist: