From 5274a897d5afe813d2556a64d623fff843c56326 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Fri, 5 Mar 2021 08:45:54 -0800 Subject: [PATCH] Define default blocks for social-links and buttons, update __experimentalGetDefaultBlockForAllowedBlocks to use canInsertBlockType --- packages/block-editor/src/store/selectors.js | 8 +- .../block-editor/src/store/test/selectors.js | 82 +++++++++++-------- packages/block-library/src/buttons/edit.js | 1 + .../block-library/src/social-links/edit.js | 1 + 4 files changed, 54 insertions(+), 38 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 93812af2d44bb..6088c9743501f 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1854,13 +1854,15 @@ export function __experimentalGetDefaultBlockForAllowedBlocks( ) { const settings = getBlockListSettings( state, clientId ); - if ( ! settings?.__experimentalDefaultBlock || ! settings?.allowedBlocks ) { + if ( ! settings?.__experimentalDefaultBlock ) { return; } if ( - ! settings?.allowedBlocks.includes( - settings?.__experimentalDefaultBlock + ! canInsertBlockType( + state, + settings.__experimentalDefaultBlock, + clientId ) ) { return; diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index 3009667a9f1ec..b45d1d0c194ce 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -3089,14 +3089,24 @@ describe( 'selectors', () => { describe( '__experimentalGetDefaultBlockForAllowedBlocks', () => { it( 'should return the default block for allowed blocks', () => { const state = { + blocks: { + byClientId: { + testClientIdA: { + name: 'core/test-block-a', + }, + }, + attributes: { + testClientIdA: {}, + }, + }, + settings: {}, blockListSettings: { testClientIdA: { - allowedBlocks: [ 'test/foo', 'test/bar' ], - __experimentalDefaultBlock: 'test/foo', - }, - testClientIdB: { - allowedBlocks: [ 'test/cats', 'test/dogs' ], - __experimentalDefaultBlock: 'test/cats', + allowedBlocks: [ + 'core/test-block-b', + 'core/test-block-c', + ], + __experimentalDefaultBlock: 'core/test-block-c', }, }, }; @@ -3105,23 +3115,27 @@ describe( 'selectors', () => { state, 'testClientIdA' ) - ).toEqual( 'test/foo' ); - expect( - __experimentalGetDefaultBlockForAllowedBlocks( - state, - 'testClientIdB' - ) - ).toEqual( 'test/cats' ); + ).toEqual( 'core/test-block-c' ); } ); - it( 'should return undefined when not specified', () => { + it( 'should return undefined when default is not specified', () => { const state = { + blocks: { + byClientId: { + testClientIdA: { + name: 'core/test-block-a', + }, + }, + attributes: { + testClientIdA: {}, + }, + }, + settings: {}, blockListSettings: { testClientIdA: { - allowedBlocks: [ 'test/foo', 'test/bar' ], - __experimentalDefaultBlock: 'test/foo', - }, - testClientIdB: { - allowedBlocks: [ 'test/cats', 'test/dogs' ], + allowedBlocks: [ + 'core/test-block-b', + 'core/test-block-c', + ], }, }, }; @@ -3134,30 +3148,28 @@ describe( 'selectors', () => { } ); it( 'should return undefined when default block is not in allowedBlocks', () => { const state = { + blocks: { + byClientId: { + testClientIdA: { + name: 'core/test-block-a', + }, + }, + attributes: { + testClientIdA: {}, + }, + }, + settings: {}, blockListSettings: { testClientIdA: { - allowedBlocks: [ 'test/foo', 'test/bar' ], - __experimentalDefaultBlock: 'test/foo', - }, - testClientIdB: { - allowedBlocks: [ 'test/cats', 'test/dogs' ], - __experimentalDefaultBlock: 'test/birds', + allowedBlocks: [ 'core/test-block-b' ], + __experimentalDefaultBlock: 'core/test-block-c', }, }, }; expect( __experimentalGetDefaultBlockForAllowedBlocks( state, - 'testClientIdB' - ) - ).toEqual( undefined ); - } ); - it( 'should return undefined when block list settings is not specified', () => { - const state = { blockListSettings: {} }; - expect( - __experimentalGetDefaultBlockForAllowedBlocks( - state, - 'testClientIdB' + 'testClientIdA' ) ).toEqual( undefined ); } ); diff --git a/packages/block-library/src/buttons/edit.js b/packages/block-library/src/buttons/edit.js index 886f89d40d14e..49fb90d06309b 100644 --- a/packages/block-library/src/buttons/edit.js +++ b/packages/block-library/src/buttons/edit.js @@ -33,6 +33,7 @@ function ButtonsEdit( { } ); const innerBlocksProps = useInnerBlocksProps( blockProps, { allowedBlocks: ALLOWED_BLOCKS, + __experimentalDefaultBlock: buttonBlockName, template: BUTTONS_TEMPLATE, orientation, __experimentalLayout: { diff --git a/packages/block-library/src/social-links/edit.js b/packages/block-library/src/social-links/edit.js index 44abbf168c701..561f5c693150e 100644 --- a/packages/block-library/src/social-links/edit.js +++ b/packages/block-library/src/social-links/edit.js @@ -94,6 +94,7 @@ export function SocialLinksEdit( props ) { const blockProps = useBlockProps( { className } ); const innerBlocksProps = useInnerBlocksProps( blockProps, { allowedBlocks: ALLOWED_BLOCKS, + __experimentalDefaultBlock: 'core/social-link', orientation: 'horizontal', placeholder: SocialPlaceholder, templateLock: false,