From f9c6287d33d6a19476f0675b900d290d38cf6312 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Mon, 15 Mar 2021 12:39:59 -0700 Subject: [PATCH] update __experimentalDefaultBlock to also pass through block attributes --- .../src/components/block-actions/index.js | 6 +-- .../use-nested-settings-update.js | 5 ++- packages/block-editor/src/store/actions.js | 19 +++++++-- packages/block-editor/src/store/selectors.js | 12 +++--- .../block-editor/src/store/test/selectors.js | 39 +++++++++++++++++-- packages/block-library/src/buttons/edit.js | 2 +- packages/block-library/src/navigation/edit.js | 5 ++- 7 files changed, 68 insertions(+), 20 deletions(-) diff --git a/packages/block-editor/src/components/block-actions/index.js b/packages/block-editor/src/components/block-actions/index.js index 4fcd0e802c29ac..221170a079da7b 100644 --- a/packages/block-editor/src/components/block-actions/index.js +++ b/packages/block-editor/src/components/block-actions/index.js @@ -49,10 +49,8 @@ export default function BlockActions( { const defaultBlock = __experimentalGetDefaultBlockForAllowedBlocks( rootClientId ); - const canInsertDefaultBlock = canInsertBlockType( - defaultBlock, - rootClientId - ); + + const canInsertDefaultBlock = !! defaultBlock; const { removeBlocks, diff --git a/packages/block-editor/src/components/inner-blocks/use-nested-settings-update.js b/packages/block-editor/src/components/inner-blocks/use-nested-settings-update.js index ced84c9bb22ef4..bd09139c0eddcb 100644 --- a/packages/block-editor/src/components/inner-blocks/use-nested-settings-update.js +++ b/packages/block-editor/src/components/inner-blocks/use-nested-settings-update.js @@ -27,8 +27,9 @@ import { store as blockEditorStore } from '../../store'; * the child block. * @param {string} orientation The direction in which the block * should face. - * @param {string} __experimentalDefaultBlock The default block name. Used to insert blocks - * of this type, before/after blocks in inner blocks. + * @param {[]} __experimentalDefaultBlock The default block: [ blockName, { blockAttributes } ]. + * Used to insert blocks of this type, before/after blocks + * in inner blocks. */ export default function useNestedSettingsUpdate( clientId, diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index c642d94177f9f5..2eecf017858d40 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -1068,20 +1068,31 @@ export function selectionChange( * * @return {Object} Action object */ -export function* insertDefaultBlock( attributes, rootClientId, index ) { +export function* insertDefaultBlock( attributes = {}, rootClientId, index ) { // See if we specified a default for allowed blocks - const defaultBlockName = yield controls.select( + const defaultBlock = yield controls.select( blockEditorStoreName, '__experimentalGetDefaultBlockForAllowedBlocks', rootClientId ); // Abort if there is no default block type (if it has been unregistered). - if ( ! defaultBlockName ) { + if ( ! defaultBlock ) { return; } - const block = createBlock( defaultBlockName, attributes ); + const [ defaultBlockName, defaultBlockAttributes ] = defaultBlock; + + // prefer the non-empty block attributes + let blockAttributes = attributes; + if ( + Object.keys( attributes ).length === 0 && + Object.keys( defaultBlockAttributes ).length > 0 + ) { + blockAttributes = defaultBlockAttributes; + } + + const block = createBlock( defaultBlockName, blockAttributes ); return yield insertBlock( block, index, rootClientId ); } diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 0a2592a27b8910..a9cbc1cb30af70 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1847,7 +1847,7 @@ export function getBlockListSettings( state, clientId ) { * @param {Object} state Editor state. * @param {?string} clientId Block client ID. * - * @return {?string} default block name. + * @return {?[]} default block, [ blockName, { blockAttributes } ]. */ export function __experimentalGetDefaultBlockForAllowedBlocks( state, @@ -1855,14 +1855,16 @@ export function __experimentalGetDefaultBlockForAllowedBlocks( ) { const settings = getBlockListSettings( state, clientId ); - const defaultBlock = - settings?.__experimentalDefaultBlock ?? getDefaultBlockName(); + const [ + blockName, + blockAttributes = {}, + ] = settings?.__experimentalDefaultBlock ?? [ getDefaultBlockName() ]; - if ( ! canInsertBlockType( state, defaultBlock, clientId ) ) { + if ( ! canInsertBlockType( state, blockName, clientId ) ) { return; } - return defaultBlock; + return [ blockName, blockAttributes ]; } /** diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index 189ecb6b7c0acd..8c0ed24179beb4 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -3120,7 +3120,40 @@ describe( 'selectors', () => { 'core/test-block-b', 'core/test-block-c', ], - __experimentalDefaultBlock: 'core/test-block-c', + __experimentalDefaultBlock: [ 'core/test-block-c' ], + }, + }, + }; + expect( + __experimentalGetDefaultBlockForAllowedBlocks( + state, + 'testClientIdA' + ) + ).toEqual( [ 'core/test-block-c', {} ] ); + } ); + it( 'should return the default block and block attributes for allowed blocks', () => { + const state = { + blocks: { + byClientId: { + testClientIdA: { + name: 'core/test-block-a', + }, + }, + attributes: { + testClientIdA: {}, + }, + }, + settings: {}, + blockListSettings: { + testClientIdA: { + allowedBlocks: [ + 'core/test-block-b', + 'core/test-block-c', + ], + __experimentalDefaultBlock: [ + 'core/test-block-c', + { foo: 'foo', bar: 'bar' }, + ], }, }, }; @@ -3129,7 +3162,7 @@ describe( 'selectors', () => { state, 'testClientIdA' ) - ).toEqual( 'core/test-block-c' ); + ).toEqual( [ 'core/test-block-c', { foo: 'foo', bar: 'bar' } ] ); } ); it( 'should return the editor default block when block list default is not specified', () => { const state = { @@ -3159,7 +3192,7 @@ describe( 'selectors', () => { state, 'testClientIdA' ) - ).toEqual( 'core/test-block-default' ); + ).toEqual( [ 'core/test-block-default', {} ] ); } ); it( 'should return undefined when default is not specified and editor default is not supported', () => { const state = { diff --git a/packages/block-library/src/buttons/edit.js b/packages/block-library/src/buttons/edit.js index 49fb90d06309b3..c6bd7d75a03c18 100644 --- a/packages/block-library/src/buttons/edit.js +++ b/packages/block-library/src/buttons/edit.js @@ -33,7 +33,7 @@ function ButtonsEdit( { } ); const innerBlocksProps = useInnerBlocksProps( blockProps, { allowedBlocks: ALLOWED_BLOCKS, - __experimentalDefaultBlock: buttonBlockName, + __experimentalDefaultBlock: [ buttonBlockName ], template: BUTTONS_TEMPLATE, orientation, __experimentalLayout: { diff --git a/packages/block-library/src/navigation/edit.js b/packages/block-library/src/navigation/edit.js index 8f3e2a8c4e1d65..e4b4a30f71f944 100644 --- a/packages/block-library/src/navigation/edit.js +++ b/packages/block-library/src/navigation/edit.js @@ -72,7 +72,10 @@ function Navigation( { 'core/page-list', 'core/spacer', ], - __experimentalDefaultBlock: 'core/navigation-link', + __experimentalDefaultBlock: [ + 'core/navigation-link', + { type: 'page', kind: 'post-type' }, + ], orientation: attributes.orientation || 'horizontal', renderAppender: ( isImmediateParentOfSelectedBlock &&