-
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
Block API: Support optional block category #22192
Changes from all commits
7e53fc4
018ffe6
eb34898
d95fefd
a28d026
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ function InserterBlockList( { | |
} ) { | ||
const { | ||
categories, | ||
defaultCategory, | ||
collections, | ||
items, | ||
rootChildBlocks, | ||
|
@@ -73,6 +74,7 @@ function InserterBlockList( { | |
); | ||
const { | ||
getCategories, | ||
getDefaultCategory, | ||
getCollections, | ||
getChildBlockNames, | ||
} = select( 'core/blocks' ); | ||
|
@@ -81,6 +83,7 @@ function InserterBlockList( { | |
|
||
return { | ||
categories: getCategories(), | ||
defaultCategory: getDefaultCategory(), | ||
collections: getCollections(), | ||
rootChildBlocks: getChildBlockNames( rootBlockName ), | ||
items: getInserterItems( rootClientId ), | ||
|
@@ -209,7 +212,12 @@ function InserterBlockList( { | |
|
||
{ ! hasChildItems && | ||
map( categories, ( category ) => { | ||
const categoryItems = itemsPerCategory[ category.slug ]; | ||
const categoryItems = | ||
itemsPerCategory[ | ||
category === defaultCategory | ||
? undefined | ||
: category.slug | ||
]; | ||
Comment on lines
+216
to
+220
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works, but it's a bit strange, because it's assuming there's a string key
|
||
if ( ! categoryItems || ! categoryItems.length ) { | ||
return null; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,7 +354,19 @@ Returns all the block categories. | |
|
||
_Returns_ | ||
|
||
- `Array<Object>`: Block categories. | ||
- `Array<WPBlockTypeCategory>`: Block categories. | ||
|
||
<a name="getCategory" href="#getCategory">#</a> **getCategory** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably be consistent here with |
||
|
||
Returns a single category by slug. | ||
|
||
_Parameters_ | ||
|
||
- _slug_ `string`: Category slug. | ||
|
||
_Returns_ | ||
|
||
- `(WPBlockTypeCategory|undefined)`: Block category, if exists. | ||
|
||
<a name="getChildBlockNames" href="#getChildBlockNames">#</a> **getChildBlockNames** | ||
|
||
|
@@ -687,7 +699,7 @@ Sets the block categories. | |
|
||
_Parameters_ | ||
|
||
- _categories_ `Array<Object>`: Block categories. | ||
- _categories_ `Array<WPBlockTypeCategory>`: Block categories. | ||
|
||
<a name="setDefaultBlockName" href="#setDefaultBlockName">#</a> **setDefaultBlockName** | ||
|
||
|
@@ -789,7 +801,7 @@ Updates a category. | |
_Parameters_ | ||
|
||
- _slug_ `string`: Block category slug. | ||
- _category_ `Object`: Object containing the category properties that should be updated. | ||
- _category_ `WPBlockTypeCategory`: Object containing the category properties that should be updated. | ||
|
||
<a name="withBlockContentContext" href="#withBlockContentContext">#</a> **withBlockContentContext** | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ import { DEPRECATED_ENTRY_KEYS } from './constants'; | |
* @property {string} name Block type's namespaced name. | ||
* @property {string} title Human-readable block type label. | ||
* @property {string} description A detailed block type description. | ||
* @property {string} category Block type category classification, | ||
* @property {string} [category] Block type category classification, | ||
* used in search interfaces to arrange | ||
* block types by category. | ||
* @property {WPBlockTypeIcon} [icon] Block type icon. | ||
|
@@ -203,10 +203,6 @@ export function registerBlockType( name, settings ) { | |
console.error( 'The "edit" property must be a valid function.' ); | ||
return; | ||
} | ||
if ( ! ( 'category' in settings ) ) { | ||
console.error( 'The block "' + name + '" must have a category.' ); | ||
return; | ||
} | ||
if ( | ||
'category' in settings && | ||
! some( select( 'core/blocks' ).getCategories(), { | ||
Comment on lines
207
to
208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess to support the use-case described at #19279 (comment), we probably want to make sure that if a block type attempts to register with a category which doesn't actually exist, it should be treated the same as if it were not assigned a category at all. Rather than as it exists here currently, where an error will occur. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,26 @@ import { combineReducers } from '@wordpress/data'; | |
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Module Constants | ||
* @typedef WPBlockTypeCategory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm on the fence about whether to call this See also: #19279 (comment) † Accuracy as in: Consider that we don't call |
||
* | ||
* @property {string} slug Unique identifier. | ||
* @property {string} title Human-readable label. | ||
*/ | ||
|
||
/** | ||
* Default category, used in absence of any assigned category for block type. | ||
* | ||
* @type {WPBlockTypeCategory} | ||
*/ | ||
export const DEFAULT_CATEGORY = { | ||
slug: 'uncategorized', | ||
title: __( 'Uncategorized' ), | ||
}; | ||
|
||
/** | ||
* Default set of block type categories. | ||
* | ||
* @type {WPBlockTypeCategory[]} | ||
*/ | ||
export const DEFAULT_CATEGORIES = [ | ||
{ slug: 'common', title: __( 'Common blocks' ) }, | ||
|
@@ -29,6 +48,7 @@ export const DEFAULT_CATEGORIES = [ | |
{ slug: 'widgets', title: __( 'Widgets' ) }, | ||
{ slug: 'embed', title: __( 'Embeds' ) }, | ||
{ slug: 'reusable', title: __( 'Reusable blocks' ) }, | ||
DEFAULT_CATEGORY, | ||
]; | ||
|
||
/** | ||
|
@@ -199,10 +219,10 @@ export const groupingBlockName = createBlockNameSetterReducer( | |
/** | ||
* Reducer managing the categories | ||
* | ||
* @param {Object} state Current state. | ||
* @param {Object} action Dispatched action. | ||
* @param {WPBlockTypeCategory[]} state Current state. | ||
* @param {Object} action Dispatched action. | ||
* | ||
* @return {Object} Updated state. | ||
* @return {WPBlockTypeCategory[]} Updated state. | ||
*/ | ||
export function categories( state = DEFAULT_CATEGORIES, action ) { | ||
switch ( action.type ) { | ||
|
@@ -229,6 +249,17 @@ export function categories( state = DEFAULT_CATEGORIES, action ) { | |
return state; | ||
} | ||
|
||
/** | ||
* Reducer managing the default category slug. | ||
* | ||
* @param {string} state Current state. | ||
* | ||
* @return {string} Updated state. | ||
*/ | ||
export function defaultCategory( state = DEFAULT_CATEGORY.slug ) { | ||
return state; | ||
} | ||
|
||
export function collections( state = {}, action ) { | ||
switch ( action.type ) { | ||
case 'ADD_BLOCK_COLLECTION': | ||
|
@@ -254,5 +285,6 @@ export default combineReducers( { | |
unregisteredFallbackBlockName, | ||
groupingBlockName, | ||
categories, | ||
defaultCategory, | ||
collections, | ||
} ); |
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's the reasoning for actually registering a default category instead of just having a nullable category and just group blocks with a "null" or inexistant category together in the inserter? It might be simpler and avoids having to define a "default category"?
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 mentioned in the original comment:
It may also require a bit more "explicit" handling in the components where the groupings are arranged, since it wouldn't be able to be treated just the same as any other category.
In the end though, it still may be the simplest option here 🤷 Worth exploring at least.
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.
Still in progress, but mostly completed, and seems quite a bit simpler indeed: #22280