Skip to content

Commit

Permalink
update __experimentalGetDefaultBlockForAllowedBlocks to fallback to e…
Browse files Browse the repository at this point in the history
…ditor default block when insertion is supported
  • Loading branch information
gwwar committed Mar 8, 2021
1 parent 7d4bcec commit 3e97524
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
8 changes: 4 additions & 4 deletions packages/block-editor/src/components/block-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function BlockActions( {
getTemplateLock,
__experimentalGetDefaultBlockForAllowedBlocks,
} = useSelect( ( select ) => select( blockEditorStore ), [] );
const { getDefaultBlockName, getGroupingBlockName } = useSelect(
const { getGroupingBlockName } = useSelect(
( select ) => select( blocksStore ),
[]
);
Expand All @@ -46,9 +46,9 @@ export default function BlockActions( {
);
} );

const defaultBlock =
__experimentalGetDefaultBlockForAllowedBlocks( rootClientId ) ??
getDefaultBlockName();
const defaultBlock = __experimentalGetDefaultBlockForAllowedBlocks(
rootClientId
);
const canInsertDefaultBlock = canInsertBlockType(
defaultBlock,
rootClientId
Expand Down
4 changes: 1 addition & 3 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
createBlock,
doBlocksMatchTemplate,
getBlockType,
getDefaultBlockName,
hasBlockSupport,
switchToBlockType,
synchronizeBlocksWithTemplate,
Expand Down Expand Up @@ -1071,14 +1070,13 @@ export function selectionChange(
*/
export function* insertDefaultBlock( attributes, rootClientId, index ) {
// See if we specified a default for allowed blocks
const defaultAllowedBlock = yield controls.select(
const defaultBlockName = yield controls.select(
blockEditorStoreName,
'__experimentalGetDefaultBlockForAllowedBlocks',
rootClientId
);

// Abort if there is no default block type (if it has been unregistered).
const defaultBlockName = defaultAllowedBlock || getDefaultBlockName();
if ( ! defaultBlockName ) {
return;
}
Expand Down
16 changes: 5 additions & 11 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import createSelector from 'rememo';
import {
getBlockType,
getBlockTypes,
getDefaultBlockName,
hasBlockSupport,
getPossibleBlockTransformations,
parse,
Expand Down Expand Up @@ -1854,21 +1855,14 @@ export function __experimentalGetDefaultBlockForAllowedBlocks(
) {
const settings = getBlockListSettings( state, clientId );

if ( ! settings?.__experimentalDefaultBlock ) {
return;
}
const defaultBlock =
settings?.__experimentalDefaultBlock ?? getDefaultBlockName();

if (
! canInsertBlockType(
state,
settings.__experimentalDefaultBlock,
clientId
)
) {
if ( ! canInsertBlockType( state, defaultBlock, clientId ) ) {
return;
}

return settings?.__experimentalDefaultBlock;
return defaultBlock;
}

/**
Expand Down
46 changes: 45 additions & 1 deletion packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { filter } from 'lodash';
* WordPress dependencies
*/
import {
setDefaultBlockName,
registerBlockType,
unregisterBlockType,
setFreeformContentHandlerName,
Expand Down Expand Up @@ -121,6 +122,14 @@ describe( 'selectors', () => {
parent: [ 'core/test-block-b' ],
} );

registerBlockType( 'core/test-block-default', {
save: ( props ) => props.attributes.text,
category: 'text',
title: 'Test Block Default',
icon: 'test',
keywords: [ 'testing' ],
} );

registerBlockType( 'core/test-freeform', {
save: ( props ) => <RawHTML>{ props.attributes.content }</RawHTML>,
category: 'text',
Expand All @@ -143,6 +152,7 @@ describe( 'selectors', () => {
} );

setFreeformContentHandlerName( 'core/test-freeform' );
setDefaultBlockName( 'core/test-block-default' );

cachedSelectors.forEach( ( { clear } ) => clear() );
} );
Expand All @@ -153,7 +163,9 @@ describe( 'selectors', () => {
unregisterBlockType( 'core/test-block-b' );
unregisterBlockType( 'core/test-block-c' );
unregisterBlockType( 'core/test-freeform' );
unregisterBlockType( 'core/test-block-default' );
unregisterBlockType( 'core/post-content-child' );
setDefaultBlockName( null );

setFreeformContentHandlerName( undefined );
} );
Expand Down Expand Up @@ -2682,6 +2694,7 @@ describe( 'selectors', () => {
expect( firstBlockFirstCall.map( ( item ) => item.id ) ).toEqual( [
'core/test-block-a',
'core/test-block-b',
'core/test-block-default',
'core/test-freeform',
'core/block/1',
'core/block/2',
Expand All @@ -2696,6 +2709,7 @@ describe( 'selectors', () => {
expect( secondBlockFirstCall.map( ( item ) => item.id ) ).toEqual( [
'core/test-block-a',
'core/test-block-b',
'core/test-block-default',
'core/test-freeform',
'core/block/1',
'core/block/2',
Expand Down Expand Up @@ -3117,7 +3131,37 @@ describe( 'selectors', () => {
)
).toEqual( 'core/test-block-c' );
} );
it( 'should return undefined when default is not specified', () => {
it( 'should return the editor default block when block list default is not specified', () => {
const state = {
blocks: {
byClientId: {
testClientIdA: {
name: 'core/test-block-a',
},
},
attributes: {
testClientIdA: {},
},
},
settings: {},
blockListSettings: {
testClientIdA: {
allowedBlocks: [
'core/test-block-b',
'core/test-block-c',
'core/test-block-default',
],
},
},
};
expect(
__experimentalGetDefaultBlockForAllowedBlocks(
state,
'testClientIdA'
)
).toEqual( 'core/test-block-default' );
} );
it( 'should return undefined when default is not specified and editor default is not supported', () => {
const state = {
blocks: {
byClientId: {
Expand Down

0 comments on commit 3e97524

Please sign in to comment.