From 4efbda6bb5f24d9d55cb8e3eea36294c147d7eaf Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 19 Jun 2019 10:25:58 +0100 Subject: [PATCH] Registers the core/group Block as the default block for handling "grouping" interactions (#15774) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adds grouping interaction to state Enables the ability register a given block as the block which handles “grouping” interactions within the editor. Related https://github.com/WordPress/gutenberg/pull/14908 * Registers grouping block * Auto updated README * Adds test for get/set Grouping block * Update documentation with clearer descriptions * Adds reducer and selector tests * Updates “Blog” for correct “Block” in docs --- .../developers/data/data-core-blocks.md | 26 +++++++++++++++++++ packages/block-library/src/index.js | 5 ++++ packages/blocks/README.md | 20 ++++++++++++-- packages/blocks/src/api/index.js | 2 ++ packages/blocks/src/api/registration.js | 22 ++++++++++++++-- packages/blocks/src/api/test/registration.js | 16 ++++++++++++ packages/blocks/src/store/actions.js | 16 ++++++++++++ packages/blocks/src/store/reducer.js | 2 ++ packages/blocks/src/store/selectors.js | 11 ++++++++ packages/blocks/src/store/test/reducer.js | 25 ++++++++++++++++++ packages/blocks/src/store/test/selectors.js | 11 ++++++++ 11 files changed, 152 insertions(+), 4 deletions(-) diff --git a/docs/designers-developers/developers/data/data-core-blocks.md b/docs/designers-developers/developers/data/data-core-blocks.md index 5256587043602d..b1d377ed11a6fe 100644 --- a/docs/designers-developers/developers/data/data-core-blocks.md +++ b/docs/designers-developers/developers/data/data-core-blocks.md @@ -108,6 +108,18 @@ _Returns_ - `?string`: Name of the block for handling non-block content. +# **getGroupingBlockName** + +Returns the name of the block for handling unregistered blocks. + +_Parameters_ + +- _state_ `Object`: Data state. + +_Returns_ + +- `?string`: Name of the block for handling unregistered blocks. + # **getUnregisteredFallbackBlockName** Returns the name of the block for handling unregistered blocks. @@ -270,6 +282,20 @@ _Returns_ - `Object`: Action object. +# **setGroupingBlockName** + +Returns an action object used to set the name of the block used +when grouping other blocks +eg: in "Group/Ungroup" interactions + +_Parameters_ + +- _name_ `string`: Block name. + +_Returns_ + +- `Object`: Action object. + # **setUnregisteredFallbackBlockName** Returns an action object used to set the name of the block used as a fallback diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 9724f9d9293e33..a9a9b78f34ca92 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -9,6 +9,7 @@ import { setDefaultBlockName, setFreeformContentHandlerName, setUnregisteredTypeHandlerName, + setGroupingBlockName, unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase } from '@wordpress/blocks'; @@ -139,4 +140,8 @@ export const registerCoreBlocks = () => { setFreeformContentHandlerName( classic.name ); } setUnregisteredTypeHandlerName( missing.name ); + + if ( group ) { + setGroupingBlockName( group.name ); + } }; diff --git a/packages/blocks/README.md b/packages/blocks/README.md index b7a8e5fd70c9dd..cf83bc0e3b0870 100644 --- a/packages/blocks/README.md +++ b/packages/blocks/README.md @@ -370,7 +370,15 @@ handler has been defined. _Returns_ -- `?string`: Blog name. +- `?string`: Block name. + +# **getGroupingBlockName** + +Retrieves name of block used for handling grouping interactions. + +_Returns_ + +- `?string`: Block name. # **getPhrasingContentSchema** @@ -434,7 +442,7 @@ handler has been defined. _Returns_ -- `?string`: Blog name. +- `?string`: Block name. # **hasBlockSupport** @@ -660,6 +668,14 @@ _Parameters_ - _blockName_ `string`: Block name. +# **setGroupingBlockName** + +Assigns name of block for handling block grouping interactions. + +_Parameters_ + +- _name_ `string`: Block name. + # **setUnregisteredTypeHandlerName** Assigns name of block handling unregistered block types. diff --git a/packages/blocks/src/api/index.js b/packages/blocks/src/api/index.js index c13b28f4903ca0..68e60ed43952b1 100644 --- a/packages/blocks/src/api/index.js +++ b/packages/blocks/src/api/index.js @@ -35,6 +35,8 @@ export { getUnregisteredTypeHandlerName, setDefaultBlockName, getDefaultBlockName, + setGroupingBlockName, + getGroupingBlockName, getBlockType, getBlockTypes, getBlockSupport, diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 8f1155fbce3400..978cac89d52f63 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -193,12 +193,21 @@ export function setFreeformContentHandlerName( blockName ) { * Retrieves name of block handling non-block content, or undefined if no * handler has been defined. * - * @return {?string} Blog name. + * @return {?string} Block name. */ export function getFreeformContentHandlerName() { return select( 'core/blocks' ).getFreeformFallbackBlockName(); } +/** + * Retrieves name of block used for handling grouping interactions. + * + * @return {?string} Block name. + */ +export function getGroupingBlockName() { + return select( 'core/blocks' ).getGroupingBlockName(); +} + /** * Assigns name of block handling unregistered block types. * @@ -212,7 +221,7 @@ export function setUnregisteredTypeHandlerName( blockName ) { * Retrieves name of block handling unregistered block types, or undefined if no * handler has been defined. * - * @return {?string} Blog name. + * @return {?string} Block name. */ export function getUnregisteredTypeHandlerName() { return select( 'core/blocks' ).getUnregisteredFallbackBlockName(); @@ -227,6 +236,15 @@ export function setDefaultBlockName( name ) { dispatch( 'core/blocks' ).setDefaultBlockName( name ); } +/** + * Assigns name of block for handling block grouping interactions. + * + * @param {string} name Block name. + */ +export function setGroupingBlockName( name ) { + dispatch( 'core/blocks' ).setGroupingBlockName( name ); +} + /** * Retrieves the default block name. * diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 05cc3140779eaa..90d1c4760d1ee4 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -22,6 +22,8 @@ import { getUnregisteredTypeHandlerName, setDefaultBlockName, getDefaultBlockName, + getGroupingBlockName, + setGroupingBlockName, getBlockType, getBlockTypes, getBlockSupport, @@ -415,6 +417,20 @@ describe( 'blocks', () => { } ); } ); + describe( 'getGroupingBlockName()', () => { + it( 'defaults to undefined', () => { + expect( getGroupingBlockName() ).toBeNull(); + } ); + } ); + + describe( 'setGroupingBlockName()', () => { + it( 'assigns default block name', () => { + setGroupingBlockName( 'core/test-block' ); + + expect( getGroupingBlockName() ).toBe( 'core/test-block' ); + } ); + } ); + describe( 'getBlockType()', () => { it( 'should return { name, save } for blocks with minimum settings', () => { registerBlockType( 'core/test-block', defaultBlockSettings ); diff --git a/packages/blocks/src/store/actions.js b/packages/blocks/src/store/actions.js index 8f2d9c834d26dc..220ae9be0ea0d3 100644 --- a/packages/blocks/src/store/actions.js +++ b/packages/blocks/src/store/actions.js @@ -107,6 +107,22 @@ export function setUnregisteredFallbackBlockName( name ) { }; } +/** + * Returns an action object used to set the name of the block used + * when grouping other blocks + * eg: in "Group/Ungroup" interactions + * + * @param {string} name Block name. + * + * @return {Object} Action object. + */ +export function setGroupingBlockName( name ) { + return { + type: 'SET_GROUPING_BLOCK_NAME', + name, + }; +} + /** * Returns an action object used to set block categories. * diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index bfeeec10ec1e10..4e3131ca7b62e0 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -124,6 +124,7 @@ export function createBlockNameSetterReducer( setActionType ) { export const defaultBlockName = createBlockNameSetterReducer( 'SET_DEFAULT_BLOCK_NAME' ); export const freeformFallbackBlockName = createBlockNameSetterReducer( 'SET_FREEFORM_FALLBACK_BLOCK_NAME' ); export const unregisteredFallbackBlockName = createBlockNameSetterReducer( 'SET_UNREGISTERED_FALLBACK_BLOCK_NAME' ); +export const groupingBlockName = createBlockNameSetterReducer( 'SET_GROUPING_BLOCK_NAME' ); /** * Reducer managing the categories @@ -164,5 +165,6 @@ export default combineReducers( { defaultBlockName, freeformFallbackBlockName, unregisteredFallbackBlockName, + groupingBlockName, categories, } ); diff --git a/packages/blocks/src/store/selectors.js b/packages/blocks/src/store/selectors.js index 97d323c65fb168..25fe40ca9f6111 100644 --- a/packages/blocks/src/store/selectors.js +++ b/packages/blocks/src/store/selectors.js @@ -101,6 +101,17 @@ export function getUnregisteredFallbackBlockName( state ) { return state.unregisteredFallbackBlockName; } +/** + * Returns the name of the block for handling unregistered blocks. + * + * @param {Object} state Data state. + * + * @return {string?} Name of the block for handling unregistered blocks. + */ +export function getGroupingBlockName( state ) { + return state.groupingBlockName; +} + /** * Returns an array with the child blocks of a given block. * diff --git a/packages/blocks/src/store/test/reducer.js b/packages/blocks/src/store/test/reducer.js index 249b86ea6f197a..346bb2e9aab1bf 100644 --- a/packages/blocks/src/store/test/reducer.js +++ b/packages/blocks/src/store/test/reducer.js @@ -13,6 +13,7 @@ import { defaultBlockName, freeformFallbackBlockName, unregisteredFallbackBlockName, + groupingBlockName, DEFAULT_CATEGORIES, } from '../reducer'; @@ -183,6 +184,30 @@ describe( 'freeformFallbackBlockName', () => { } ); } ); +describe( 'groupingBlockName', () => { + it( 'should return null as default state', () => { + expect( groupingBlockName( undefined, {} ) ).toBeNull(); + } ); + + it( 'should set the grouping block name', () => { + const state = groupingBlockName( null, { + type: 'SET_GROUPING_BLOCK_NAME', + name: 'core/group', + } ); + + expect( state ).toBe( 'core/group' ); + } ); + + it( 'should reset the group fallback block name', () => { + const state = groupingBlockName( 'core/group', { + type: 'REMOVE_BLOCK_TYPES', + names: [ 'core/group' ], + } ); + + expect( state ).toBeNull(); + } ); +} ); + describe( 'unregisteredFallbackBlockName', () => { it( 'should return null as default state', () => { expect( unregisteredFallbackBlockName( undefined, {} ) ).toBeNull(); diff --git a/packages/blocks/src/store/test/selectors.js b/packages/blocks/src/store/test/selectors.js index 2f37ed006042c0..05fc5a23040a85 100644 --- a/packages/blocks/src/store/test/selectors.js +++ b/packages/blocks/src/store/test/selectors.js @@ -4,6 +4,7 @@ import { getChildBlockNames, isMatchingSearchTerm, + getGroupingBlockName, } from '../selectors'; describe( 'selectors', () => { @@ -199,4 +200,14 @@ describe( 'selectors', () => { } ); } ); } ); + + describe( 'getGroupingBlockName', () => { + it( 'returns the grouping block name from state', () => { + const state = { + groupingBlockName: 'core/group', + }; + + expect( getGroupingBlockName( state ) ).toEqual( 'core/group' ); + } ); + } ); } );