-
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
Add block collections #17609
Add block collections #17609
Conversation
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.
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; |
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 description for the return value needs to be updated.
@@ -30,6 +30,7 @@ import { | |||
} from '@wordpress/components'; | |||
import { | |||
getCategories, | |||
getCollections, |
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.
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. |
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.
Documentation is missing here.
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 it, I guess this will update automatically?
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.
npm run docs:build
should update this document.
Thanks for the review @gziolo. Some thoughts:
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. |
@gziolo I added a test. Is that sufficient? |
@@ -1,4 +1,4 @@ | |||
/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */ | |||
/* eslint no-8: [ 'error', { allow: [ 'error' ] } ] */ |
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 doesn't look like an expected change.
@youknowriad and @mtias - can you share your feedback on this PR? |
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.
Left a small comment but I like the addition 👍
*/ | ||
export function getCollections() { | ||
return select( 'core/blocks' ).getCollections(); | ||
} |
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.
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.
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.
Happy to make the change, but I'm not sure what it should be. I just copied the other (bad) examples!
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 idea is that we don't really need this function.
components can call select( 'core/blocks' ).getCollections()
directly in withSelect
or useSelect
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.
Done!
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?
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 ) { |
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.
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 ) { |
gutenberg/packages/plugins/src/api/index.js
Line 109 in f8b62bd
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.
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.
Done!
c1c94f8
to
966bf5a
Compare
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. |
d4df845
to
7a1da8d
Compare
@youknowriad I've rebased this and fixed all the conflicts. Thanks for looking. |
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 collection appears as documented, the test collection still needs to be removed.
packages/block-library/src/index.js
Outdated
@@ -146,6 +148,12 @@ export const registerCoreBlocks = () => { | |||
video, | |||
].forEach( registerBlock ); | |||
|
|||
// This line is just for testing | |||
registerBlockCollection( 'core', { | |||
title: 'Core', |
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.
As this seems ready to land, this test collection should be removed before we merge
@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`. |
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 think the wording is incorrect since it's in fact two arguments.
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 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.
(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 |
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.
This comma separation is not a valid syntax for JSDoc.
* | ||
* @param {Object} state Data state. | ||
* | ||
* @return {Array} Collections list. |
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.
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.
Would you mind a rebase here so we can consider merging it? |
08c388c
to
b3a7162
Compare
@youknowriad I rebased this and fixed the incorrect docs. Thanks! |
Thanks for your work here @scruffian |
How does one test this? Is there any coverage in end-to-end tests? |
Related: #22279 |
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 definedHow has this been tested?
Tested in Chrome on a .org installation with no other plugins.
Screenshots
Types of changes
New feature (non-breaking change which adds functionality)
Checklist: