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

Block API: Allow block registration without category #22280

Merged
merged 6 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/block-editor/src/components/inserter/block-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ function InserterBlockList( {
return filter( filteredItems, { category: 'reusable' } );
}, [ filteredItems ] );

const uncategorizedItems = useMemo( () => {
aduth marked this conversation as resolved.
Show resolved Hide resolved
return filter( filteredItems, ( item ) => ! item.category );
aduth marked this conversation as resolved.
Show resolved Hide resolved
}, [ filteredItems ] );

const itemsPerCategory = useMemo( () => {
const getCategoryIndex = ( item ) => {
return findIndex(
Expand All @@ -145,7 +149,10 @@ function InserterBlockList( {

return flow(
( itemList ) =>
filter( itemList, ( item ) => item.category !== 'reusable' ),
filter(
itemList,
( item ) => item.category && item.category !== 'reusable'
),
( itemList ) => sortBy( itemList, getCategoryIndex ),
( itemList ) => groupBy( itemList, 'category' )
)( filteredItems );
Expand Down Expand Up @@ -228,6 +235,19 @@ function InserterBlockList( {
);
} ) }

{ ! hasChildItems && uncategorizedItems.length && (
<InserterPanel
className="block-editor-inserter__uncategorized-blocks-panel"
title={ __( 'Uncategorized' ) }
aduth marked this conversation as resolved.
Show resolved Hide resolved
>
<BlockTypesList
items={ uncategorizedItems }
onSelect={ onSelectItem }
onHover={ onHover }
/>
</InserterPanel>
) }

{ ! hasChildItems &&
map( collections, ( collection, namespace ) => {
const collectionItems = itemsPerCollection[ namespace ];
Expand Down
16 changes: 8 additions & 8 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */
/* eslint no-console: [ 'error', { allow: [ 'error', 'warn' ] } ] */

/**
* External dependencies
Expand Down Expand Up @@ -203,20 +203,20 @@ 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(), {
slug: settings.category,
} )
) {
console.error(
'The block "' + name + '" must have a registered category.'
console.warn(
'The block "' +
name +
'" is registered with an invalid category "' +
settings.category +
'".'
);
return;
delete settings.category;
}
if ( ! ( 'title' in settings ) || settings.title === '' ) {
console.error( 'The block "' + name + '" must have a title.' );
Expand Down
25 changes: 5 additions & 20 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,7 @@ describe( 'blocks', () => {
expect( block ).toBeUndefined();
} );

it( 'should reject blocks without category', () => {
const blockType = {
settingName: 'settingValue',
save: noop,
title: 'block title',
},
block = registerBlockType(
'my-plugin/fancy-block-8',
blockType
);
expect( console ).toHaveErroredWith(
'The block "my-plugin/fancy-block-8" must have a category.'
);
expect( block ).toBeUndefined();
} );

it( 'should reject blocks with non registered category.', () => {
it( 'should unset category of blocks with non registered category.', () => {
const blockType = {
save: noop,
category: 'custom-category-slug',
Expand All @@ -199,10 +183,11 @@ describe( 'blocks', () => {
'my-plugin/fancy-block-9',
blockType
);
expect( console ).toHaveErroredWith(
'The block "my-plugin/fancy-block-9" must have a registered category.'
expect( console ).toHaveWarnedWith(
'The block "my-plugin/fancy-block-9" is registered with an invalid category "custom-category-slug".'
);
expect( block ).toBeUndefined();
expect( block ).not.toBeUndefined();
expect( block.category ).toBeUndefined();
} );

it( 'should reject blocks without title', () => {
Expand Down