Skip to content

Commit

Permalink
update __experimentalDefaultBlock to also pass through block attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed Mar 16, 2021
1 parent ca82047 commit f9c6287
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 20 deletions.
6 changes: 2 additions & 4 deletions packages/block-editor/src/components/block-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ export default function BlockActions( {
const defaultBlock = __experimentalGetDefaultBlockForAllowedBlocks(
rootClientId
);
const canInsertDefaultBlock = canInsertBlockType(
defaultBlock,
rootClientId
);

const canInsertDefaultBlock = !! defaultBlock;

const {
removeBlocks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 15 additions & 4 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
12 changes: 7 additions & 5 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1847,22 +1847,24 @@ 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,
clientId
) {
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 ];
}

/**
Expand Down
39 changes: 36 additions & 3 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
],
},
},
};
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/buttons/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function ButtonsEdit( {
} );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: buttonBlockName,
__experimentalDefaultBlock: [ buttonBlockName ],
template: BUTTONS_TEMPLATE,
orientation,
__experimentalLayout: {
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down

0 comments on commit f9c6287

Please sign in to comment.