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

Add block collections #17609

Merged
merged 4 commits into from
Jan 7, 2020
Merged

Conversation

scruffian
Copy link
Contributor

@scruffian scruffian commented Sep 26, 2019

Description

As proposed in #16866, this adds a new function registerBlockCollection( namespace, title, icon = {} );

This enables blocks to be added to both collections and categories, ensuring that blocks can be put in the category that they make the most sense in, but also make it possible to see all the blocks coming from a single source.

namespace is matched against a block prefix.
title shows in the block inserter.
icon will show alongside the title, if defined

How has this been tested?

Tested in Chrome on a .org installation with no other plugins.

Screenshots

Screenshot 2019-09-26 at 14 36 10

Types of changes

New feature (non-breaking change which adds functionality)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@gziolo gziolo added First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Feature] Block API API that allows to express the block paradigm. labels Sep 26, 2019
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

This is definitely a good direction. It raises some questions like:

  • what do we about all currently registered custom categories?
  • do we still want to allow to register custom categories?
  • how do we encourage plugin developers to use collections instead of categories?

We still need documentation, a note in the changelog and unit tests for new APIs.

* @param {Object} tite The title to show in the inserter
* @param {Object} icon The icon to show in the inserter
*
* @return {?WPBlock} The block, if it has been successfully registered;
Copy link
Member

Choose a reason for hiding this comment

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

The description for the return value needs to be updated.

@@ -30,6 +30,7 @@ import {
} from '@wordpress/components';
import {
getCategories,
getCollections,
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be more reliable to get both collections and categories through withSelect HOC?

@@ -195,6 +207,10 @@ _Returns_

<!-- START TOKEN(Autogenerated actions) -->

<a name="addBlockCollection" href="#addBlockCollection">#</a> **addBlockCollection**

Undocumented declaration.
Copy link
Member

Choose a reason for hiding this comment

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

Documentation is missing here.

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 added it, I guess this will update automatically?

Copy link
Member

Choose a reason for hiding this comment

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

npm run docs:build should update this document.

@scruffian
Copy link
Contributor Author

Thanks for the review @gziolo. Some thoughts:

do we still want to allow to register custom categories?
what do we about all currently registered custom categories?

I think there is still value in the custom categories. The way I see it this just adds another option for block developers, but they can continue doing it how they are without a problem.

how do we encourage plugin developers to use collections instead of categories?

I'll add it to the docs.

@scruffian
Copy link
Contributor Author

Another question this raises is, should we should blocks in both sections when you search, for example:

Screenshot 2019-09-26 at 17 18 59

@scruffian
Copy link
Contributor Author

@gziolo I added a test. Is that sufficient?

@@ -1,4 +1,4 @@
/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */
/* eslint no-8: [ 'error', { allow: [ 'error' ] } ] */
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't look like an expected change.

@gziolo
Copy link
Member

gziolo commented Oct 7, 2019

@youknowriad and @mtias - can you share your feedback on this PR?

@gziolo gziolo requested a review from mtias October 7, 2019 10:37
@gziolo gziolo added Needs Technical Feedback Needs testing from a developer perspective. [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible labels Oct 7, 2019
Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Left a small comment but I like the addition 👍

*/
export function getCollections() {
return select( 'core/blocks' ).getCollections();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary? Can't we rely on selectors instead? (Selectors are reactive and not global functions).
I know we do the same mistake for other things like "categories", "block types"... but ideally, we don't continue the trend for new APIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to make the change, but I'm not sure what it should be. I just copied the other (bad) examples!

Copy link
Contributor

Choose a reason for hiding this comment

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

The idea is that we don't really need this function.

components can call select( 'core/blocks' ).getCollections() directly in withSelect or useSelect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@youknowriad youknowriad added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Oct 7, 2019
@gziolo
Copy link
Member

gziolo commented Oct 7, 2019

One more question, how this would work for blocks registered on the server? Do you think this should be something which should be handled only on the front-end?

registerBlockCollection( namespace, title, icon = {} );

One more thing, as far as I remember, all the APIs that register something take an object as 2nd param. Should we follow the same pattern here? In general, it's more flexible as it allows to easily introduce more options in the future.

* @param {Object} icon The icon to show in the inserter
*
*/
export function registerBlockCollection( namespace, title, icon ) {
Copy link
Member

Choose a reason for hiding this comment

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

All the existing public API methods which register something use settings object as 2nd param, it would be great to align, see:

export function registerFormatType( name, settings ) {

export function registerPlugin( name, settings ) {

export function registerBlockType( name, settings ) {

All of them offer unregister* function as well which returns the registered object for more control for 3rd party plugings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@gziolo gziolo removed Needs Technical Feedback Needs testing from a developer perspective. [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible labels Oct 25, 2019
@scruffian
Copy link
Contributor Author

One more question, how this would work for blocks registered on the server? Do you think this should be something which should be handled only on the front-end?

I think having it front-end only is fine for now. We can add a server side option for it if there's a demand.

@scruffian
Copy link
Contributor Author

@youknowriad I've rebased this and fixed all the conflicts. Thanks for looking.

Copy link
Contributor

@draganescu draganescu left a comment

Choose a reason for hiding this comment

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

The collection appears as documented, the test collection still needs to be removed.

@@ -146,6 +148,12 @@ export const registerCoreBlocks = () => {
video,
].forEach( registerBlock );

// This line is just for testing
registerBlockCollection( 'core', {
title: 'Core',
Copy link
Contributor

Choose a reason for hiding this comment

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

As this seems ready to land, this test collection should be removed before we merge

@scruffian
Copy link
Contributor Author

@draganescu thanks, done :)


Blocks can be added to collections, grouping together all blocks from the same origin

`registerBlockCollection` takes three parameters `namespace`, `title` and `icon`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the wording is incorrect since it's in fact two arguments.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Thanks for your work here, this is looking good. Ideally, we add an e2e test (plugin test) to validate this API. It could also be done as a follow-up.

@youknowriad
Copy link
Contributor

(Let me know when it's rebased... so I can merge)

* Registers a new block collection to group blocks in the same namespace in the inserter.
*
* @param {string} namespace The namespace to group blocks by in the inserter; corresponds to the block namespace
* @param {Object} title, icon The title to show in the inserter, The icon to show in the inserter
Copy link
Member

Choose a reason for hiding this comment

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

This comma separation is not a valid syntax for JSDoc.

*
* @param {Object} state Data state.
*
* @return {Array} Collections list.
Copy link
Member

Choose a reason for hiding this comment

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

Based on the state shape, I don't think this returns an array. Should it be returning an array though? That seems like what I might expect to receive as a consumer.

@youknowriad
Copy link
Contributor

Would you mind a rebase here so we can consider merging it?

@scruffian
Copy link
Contributor Author

@youknowriad I rebased this and fixed the incorrect docs. Thanks!

@youknowriad youknowriad merged commit 0cb146d into WordPress:master Jan 7, 2020
@youknowriad
Copy link
Contributor

Thanks for your work here @scruffian

@scruffian scruffian deleted the add/block-collections branch January 7, 2020 10:26
@ellatrix ellatrix added this to the Gutenberg 7.3 milestone Jan 20, 2020
@jorgefilipecosta jorgefilipecosta mentioned this pull request Feb 12, 2020
23 tasks
@youknowriad youknowriad removed the Needs Dev Note Requires a developer note for a major WordPress release cycle label Apr 8, 2020
@aduth
Copy link
Member

aduth commented May 11, 2020

How does one test this? Is there any coverage in end-to-end tests?

@aduth
Copy link
Member

aduth commented May 11, 2020

Should a collection item search result be counted in the announced message?

image

Edit: I guess technically there are only two options present, though it's a bit more confusing in how there are four buttons for those two options.

@aduth
Copy link
Member

aduth commented May 11, 2020

Should reusable blocks be shown before or after block collections in the inserter?

image

@aduth
Copy link
Member

aduth commented May 11, 2020

Should a collection item search result be counted in the announced message?

Related: #22279

@mtias
Copy link
Member

mtias commented May 12, 2020

These collections are not meant to be shown as regular categories, so they should not show up in searches as one:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants