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

Registers the core/group Block as the default block for handling "grouping" interactions #15774

Merged
merged 7 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions docs/designers-developers/developers/data/data-core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ _Returns_

- `?string`: Name of the block for handling non-block content.

<a name="getGroupingBlockName" href="#getGroupingBlockName">#</a> **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.

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

Returns the name of the block for handling unregistered blocks.
Expand Down Expand Up @@ -270,6 +282,20 @@ _Returns_

- `Object`: Action object.

<a name="setGroupingBlockName" href="#setGroupingBlockName">#</a> **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.

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

Returns an action object used to set the name of the block used as a fallback
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
setDefaultBlockName,
setFreeformContentHandlerName,
setUnregisteredTypeHandlerName,
setGroupingBlockName,
unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase
} from '@wordpress/blocks';

Expand Down Expand Up @@ -135,4 +136,8 @@ export const registerCoreBlocks = () => {
setFreeformContentHandlerName( classic.name );
}
setUnregisteredTypeHandlerName( missing.name );

if ( group ) {
Copy link
Member

Choose a reason for hiding this comment

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

When would this not be true?

Copy link
Contributor

Choose a reason for hiding this comment

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

I was browsing the file and came to ask the same thing. :) @getdave

Copy link
Member

Choose a reason for hiding this comment

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

Pull request at #22563

setGroupingBlockName( group.name );
}
};
20 changes: 18 additions & 2 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,15 @@ handler has been defined.

_Returns_

- `?string`: Blog name.
- `?string`: Block name.

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

Retrieves name of block used for handling grouping interactions.

_Returns_

- `?string`: Block name.

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

Expand Down Expand Up @@ -434,7 +442,7 @@ handler has been defined.

_Returns_

- `?string`: Blog name.
- `?string`: Block name.

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

Expand Down Expand Up @@ -660,6 +668,14 @@ _Parameters_

- _blockName_ `string`: Block name.

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

Assigns name of block for handling block grouping interactions.

_Parameters_

- _name_ `string`: Block name.

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

Assigns name of block handling unregistered block types.
Expand Down
2 changes: 2 additions & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export {
getUnregisteredTypeHandlerName,
setDefaultBlockName,
getDefaultBlockName,
setGroupingBlockName,
getGroupingBlockName,
getBlockType,
getBlockTypes,
getBlockSupport,
Expand Down
22 changes: 20 additions & 2 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
Expand All @@ -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.
*
Expand Down
16 changes: 16 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
getUnregisteredTypeHandlerName,
setDefaultBlockName,
getDefaultBlockName,
getGroupingBlockName,
setGroupingBlockName,
getBlockType,
getBlockTypes,
getBlockSupport,
Expand Down Expand Up @@ -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 );
Expand Down
16 changes: 16 additions & 0 deletions packages/blocks/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -164,5 +165,6 @@ export default combineReducers( {
defaultBlockName,
freeformFallbackBlockName,
unregisteredFallbackBlockName,
groupingBlockName,
categories,
} );
11 changes: 11 additions & 0 deletions packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
25 changes: 25 additions & 0 deletions packages/blocks/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
defaultBlockName,
freeformFallbackBlockName,
unregisteredFallbackBlockName,
groupingBlockName,
DEFAULT_CATEGORIES,
} from '../reducer';

Expand Down Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions packages/blocks/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {
getChildBlockNames,
isMatchingSearchTerm,
getGroupingBlockName,
} from '../selectors';

describe( 'selectors', () => {
Expand Down Expand Up @@ -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' );
} );
} );
} );