Skip to content

Commit

Permalink
Define default blocks for social-links and buttons, update __experime…
Browse files Browse the repository at this point in the history
…ntalGetDefaultBlockForAllowedBlocks to use canInsertBlockType
  • Loading branch information
gwwar committed Mar 5, 2021
1 parent 81e9228 commit 5274a89
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 38 deletions.
8 changes: 5 additions & 3 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
82 changes: 47 additions & 35 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
};
Expand All @@ -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',
],
},
},
};
Expand All @@ -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 );
} );
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/buttons/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function ButtonsEdit( {
} );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: buttonBlockName,
template: BUTTONS_TEMPLATE,
orientation,
__experimentalLayout: {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/social-links/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5274a89

Please sign in to comment.