diff --git a/packages/block-editor/src/components/block-actions/index.js b/packages/block-editor/src/components/block-actions/index.js index 361e0e322e73c..4fcd0e802c29a 100644 --- a/packages/block-editor/src/components/block-actions/index.js +++ b/packages/block-editor/src/components/block-actions/index.js @@ -31,7 +31,7 @@ export default function BlockActions( { getTemplateLock, __experimentalGetDefaultBlockForAllowedBlocks, } = useSelect( ( select ) => select( blockEditorStore ), [] ); - const { getDefaultBlockName, getGroupingBlockName } = useSelect( + const { getGroupingBlockName } = useSelect( ( select ) => select( blocksStore ), [] ); @@ -46,9 +46,9 @@ export default function BlockActions( { ); } ); - const defaultBlock = - __experimentalGetDefaultBlockForAllowedBlocks( rootClientId ) ?? - getDefaultBlockName(); + const defaultBlock = __experimentalGetDefaultBlockForAllowedBlocks( + rootClientId + ); const canInsertDefaultBlock = canInsertBlockType( defaultBlock, rootClientId diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index 6273f465cebaf..c642d94177f9f 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -12,7 +12,6 @@ import { createBlock, doBlocksMatchTemplate, getBlockType, - getDefaultBlockName, hasBlockSupport, switchToBlockType, synchronizeBlocksWithTemplate, @@ -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; } diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 6088c9743501f..0a2592a27b891 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -25,6 +25,7 @@ import createSelector from 'rememo'; import { getBlockType, getBlockTypes, + getDefaultBlockName, hasBlockSupport, getPossibleBlockTransformations, parse, @@ -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; } /** diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index b45d1d0c194ce..189ecb6b7c0ac 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -7,6 +7,7 @@ import { filter } from 'lodash'; * WordPress dependencies */ import { + setDefaultBlockName, registerBlockType, unregisterBlockType, setFreeformContentHandlerName, @@ -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 ) => { props.attributes.content }, category: 'text', @@ -143,6 +152,7 @@ describe( 'selectors', () => { } ); setFreeformContentHandlerName( 'core/test-freeform' ); + setDefaultBlockName( 'core/test-block-default' ); cachedSelectors.forEach( ( { clear } ) => clear() ); } ); @@ -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 ); } ); @@ -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', @@ -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', @@ -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: {