From 3d385da4e56772148a3b1e087f9668c37bdfb5bf Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Tue, 5 Nov 2019 13:17:15 +0100 Subject: [PATCH] Blocks: Add support for the default block pattern --- packages/block-library/src/columns/edit.js | 9 ++-- packages/blocks/src/store/selectors.js | 27 ++++++++++- packages/blocks/src/store/test/selectors.js | 51 ++++++++++++++++++++- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/columns/edit.js b/packages/block-library/src/columns/edit.js index 54ba8fbd4003b6..1e08ee01b7f20f 100644 --- a/packages/block-library/src/columns/edit.js +++ b/packages/block-library/src/columns/edit.js @@ -53,10 +53,11 @@ export function ColumnsEdit( { } ) { const { verticalAlignment } = attributes; - const { count, patterns } = useSelect( ( select ) => { + const { count, defaultPattern, patterns } = useSelect( ( select ) => { return { count: select( 'core/block-editor' ).getBlockCount( clientId ), patterns: select( 'core/blocks' ).__experimentalGetBlockPatterns( 'core/columns' ), + defaultPattern: select( 'core/blocks' ).__experimentalGetDefaultBlockPattern( 'core/columns' ), }; } ); const [ template, setTemplate ] = useState( getColumnsTemplate( count ) ); @@ -106,11 +107,7 @@ export function ColumnsEdit( {
{ - if ( nextTemplate === undefined ) { - nextTemplate = getColumnsTemplate( 2 ); - } - + __experimentalOnSelectTemplateOption={ ( nextTemplate = defaultPattern.innerBlocks ) => { setTemplate( nextTemplate ); setForceUseTemplate( true ); } } diff --git a/packages/blocks/src/store/selectors.js b/packages/blocks/src/store/selectors.js index 55ef8655199726..48308f4fc498da 100644 --- a/packages/blocks/src/store/selectors.js +++ b/packages/blocks/src/store/selectors.js @@ -2,7 +2,17 @@ * External dependencies */ import createSelector from 'rememo'; -import { filter, get, includes, map, some, flow, deburr } from 'lodash'; +import { + deburr, + filter, + find, + first, + flow, + get, + includes, + map, + some, +} from 'lodash'; /** * Given a block name or block type object, returns the corresponding @@ -69,6 +79,21 @@ export function __experimentalGetBlockPatterns( state, blockName ) { return state.blockPatterns[ blockName ]; } +/** + * Returns the default block pattern for the given block type. + * If there is no default pattern set, it returns the first item. + * + * @param {Object} state Data state. + * @param {string} blockName Block type name. + * + * @return {?WPBlockPattern} The default block pattern. + */ +export function __experimentalGetDefaultBlockPattern( state, blockName ) { + const patterns = __experimentalGetBlockPatterns( state, blockName ); + + return find( patterns, 'isDefault' ) || first( patterns ); +} + /** * Returns all the available categories. * diff --git a/packages/blocks/src/store/test/selectors.js b/packages/blocks/src/store/test/selectors.js index 05fc5a23040a85..6392de23bd63e6 100644 --- a/packages/blocks/src/store/test/selectors.js +++ b/packages/blocks/src/store/test/selectors.js @@ -1,10 +1,16 @@ +/** + * External dependencies + */ +import deepFreeze from 'deep-freeze'; + /** * Internal dependencies */ import { getChildBlockNames, - isMatchingSearchTerm, + __experimentalGetDefaultBlockPattern, getGroupingBlockName, + isMatchingSearchTerm, } from '../selectors'; describe( 'selectors', () => { @@ -139,6 +145,49 @@ describe( 'selectors', () => { } ); } ); + describe( '__experimentalGetDefaultBlockPattern', () => { + const blockName = 'block/name'; + const createBlockPatternsState = ( patterns ) => { + return deepFreeze( { + blockPatterns: { + [ blockName ]: patterns, + }, + } ); + }; + const firstBlockPattern = { + name: 'first-block-pattern', + }; + const secondBlockPattern = { + name: 'second-block-pattern', + }; + + it( 'should return the default pattern when set', () => { + const defaultBlockPattern = { + ...secondBlockPattern, + isDefault: true, + }; + const state = createBlockPatternsState( [ + firstBlockPattern, + defaultBlockPattern, + ] ); + + const result = __experimentalGetDefaultBlockPattern( state, blockName ); + + expect( result ).toEqual( defaultBlockPattern ); + } ); + + it( 'should return the first pattern when no default pattern set', () => { + const state = createBlockPatternsState( [ + firstBlockPattern, + secondBlockPattern, + ] ); + + const result = __experimentalGetDefaultBlockPattern( state, blockName ); + + expect( result ).toEqual( firstBlockPattern ); + } ); + } ); + describe( 'isMatchingSearchTerm', () => { const name = 'core/paragraph'; const blockType = {