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

Blocks: Tag Cloud #4994

Closed
wants to merge 16 commits into from
Closed

Blocks: Tag Cloud #4994

wants to merge 16 commits into from

Conversation

jahvi
Copy link
Contributor

@jahvi jahvi commented Feb 10, 2018

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:

  • Right now I'm populating the select box with all taxonomies but I should just be getting only the ones registered with show_tagcloud as true, however I'm not sure how to do this, can this be done through the rest API?
  • To render the actual tag clous, should I "reimplement" the 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):

motion

Types of changes

New feature: tag cloud block

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code has proper inline documentation.

@jahvi jahvi mentioned this pull request Feb 10, 2018
@melchoyce
Copy link
Contributor

Hey @jahvi! Thanks for working on this.

What if the block had a placeholder when you first add it to your post?

image

@jahvi
Copy link
Contributor Author

jahvi commented Feb 10, 2018

Thanks for the feedback @melchoyce that looks great! I'll work on that for the next update.

@jahvi jahvi force-pushed the add/block-tag-cloud branch from a223832 to a57ce61 Compare February 11, 2018 13:40
@jahvi
Copy link
Contributor Author

jahvi commented Feb 11, 2018

I just improved the block a bit by adding a placeholder, alignment and server side rendering. Still tons left to do but it should be more usable now.

motion

@jahvi jahvi force-pushed the add/block-tag-cloud branch from 5f358e4 to c90cecb Compare February 17, 2018 09:07
@jahvi
Copy link
Contributor Author

jahvi commented Feb 17, 2018

Hey there, I just added some basic styles and managed to display the terms in the editor, I'm not very proud of the way I'm calculating the font sizes as it's almost a copy of how this is rendered on PHP but really open to suggestions.

motion

getTaxonomies() {
const taxonomies = this.props.taxonomies.data;

if ( ! taxonomies ) {
Copy link
Member

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, () => '' );
[]

const taxonomies = this.props.taxonomies.data;
const taxonomy = taxonomies[ taxonomySlug ];

setAttributes( { taxonomySlug, taxonomyRestBase: taxonomy.rest_base } );
Copy link
Member

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

Copy link
Contributor Author

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!

className={ className }
/>
) : (
[]
Copy link
Member

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.

@@ -0,0 +1,13 @@
.gutenberg .wp-block-tag-cloud {
a {
display: inline-block;
Copy link
Member

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

taxonomyRestBase: {
type: 'string',
},
align: {
Copy link
Member

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.

*/
function gutenberg_render_block_core_tag_cloud( $attributes ) {
$align = 'center';
if ( isset( $attributes['align'] ) && in_array( $attributes['align'], array( 'left', 'right', 'full' ), true ) ) {
Copy link
Member

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 }`;
Copy link
Member

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;
} );

Copy link
Contributor Author

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.

@jahvi
Copy link
Contributor Author

jahvi commented Feb 19, 2018

Thanks for your feedback @aduth I just updated my PR.

...taxonomies,
];
const terms = termList.data ? (
<TermList
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

@jahvi the ServerSideRendercomponent has landed with #5495

Do you want to refactor your PR to make use of it?

Copy link
Contributor Author

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

Copy link
Contributor Author

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 🙏🏽

@jahvi jahvi force-pushed the add/block-tag-cloud branch 2 times, most recently from 3ac4710 to 7ac38ae Compare March 4, 2018 15:53
@jahvi jahvi force-pushed the add/block-tag-cloud branch from 7ac38ae to 9029500 Compare March 25, 2018 14:07
@jahvi
Copy link
Contributor Author

jahvi commented Mar 25, 2018

Just added this to make sure only taxonomies with show_cloud = true are displayed in the options, and added some some fixtures to make test pass.

@jahvi jahvi force-pushed the add/block-tag-cloud branch from fee27ae to c8a4bed Compare May 4, 2018 17:48
@tofumatt
Copy link
Member

tofumatt commented Jul 5, 2018

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.

@tofumatt tofumatt closed this Jul 5, 2018
@jahvi
Copy link
Contributor Author

jahvi commented Jul 7, 2018

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 withAPIData HOC is giving me the following error:

react-dom.min.82e21c65.js:110 TypeError: Cannot read property 'getAllResponseHeaders' of undefined
    at getResponseHeaders (request.js:59)
    at Object.<anonymous> (request.js:87)
    at Object.<anonymous> (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,underscore,backbone,moxiejs,plupload,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-&load[]=sortable&ver=4.9.7:2)
    at i (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,underscore,backbone,moxiejs,plupload,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-&load[]=sortable&ver=4.9.7:2)
    at Object.add [as done] (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,underscore,backbone,moxiejs,plupload,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-&load[]=sortable&ver=4.9.7:2)
    at Array.<anonymous> (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,underscore,backbone,moxiejs,plupload,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-&load[]=sortable&ver=4.9.7:2)
    at Function.each (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,underscore,backbone,moxiejs,plupload,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-&load[]=sortable&ver=4.9.7:2)
    at Object.<anonymous> (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,underscore,backbone,moxiejs,plupload,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-&load[]=sortable&ver=4.9.7:2)
    at Function.a.Deferred (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,underscore,backbone,moxiejs,plupload,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-&load[]=sortable&ver=4.9.7:9)
    at Object.then (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,underscore,backbone,moxiejs,plupload,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-&load[]=sortable&ver=4.9.7:2)

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.

@jahvi
Copy link
Contributor Author

jahvi commented Jul 10, 2018

I managed to make the tag cloud block work with the new withSelect HOC so it should be ready for review, can this PR be opened again so I can push the new code or should I submit a new one?

@tofumatt
Copy link
Member

Looks like the branch was force-pushed to so I can't reopen this PR:

screenshot 2018-07-10 19 07 12

If you open a new PR we can look at it, feel free to reference this one as well just to keep track of things 😄

@jahvi jahvi mentioned this pull request Jul 10, 2018
4 tasks
@jahvi
Copy link
Contributor Author

jahvi commented Jul 10, 2018

No problem, I just created a new PR #7875

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants