diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index a94a526f4a5da..07f150f53d21a 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -393,6 +393,17 @@ export function* removeBlocks( clientIds, selectPrevious = true ) { type: 'REMOVE_BLOCKS', clientIds, }; + + const count = yield select( + 'core/block-editor', + 'getBlockCount', + ); + + // To avoid a focus loss when removing the last block, assure there is + // always a default block if the last of the blocks have been removed. + if ( count === 0 ) { + yield insertDefaultBlock(); + } } /** diff --git a/packages/block-editor/src/store/effects.js b/packages/block-editor/src/store/effects.js index f02ef6fbd7231..ca46e1afb8f4a 100644 --- a/packages/block-editor/src/store/effects.js +++ b/packages/block-editor/src/store/effects.js @@ -127,9 +127,6 @@ export default { RESET_BLOCKS: [ validateBlocksToTemplate, ], - REMOVE_BLOCKS: [ - ensureDefaultBlock, - ], REPLACE_BLOCKS: [ ensureDefaultBlock, ], diff --git a/packages/block-editor/src/store/test/actions.js b/packages/block-editor/src/store/test/actions.js index 3ae9039505356..2ca6839dc57e2 100644 --- a/packages/block-editor/src/store/test/actions.js +++ b/packages/block-editor/src/store/test/actions.js @@ -28,6 +28,7 @@ import { toggleBlockMode, updateBlockListSettings, } from '../actions'; +import { select } from '../controls'; describe( 'actions', () => { describe( 'resetBlocks', () => { @@ -218,6 +219,10 @@ describe( 'actions', () => { type: 'REMOVE_BLOCKS', clientIds, }, + select( + 'core/block-editor', + 'getBlockCount', + ), ] ); } ); } ); @@ -234,10 +239,14 @@ describe( 'actions', () => { type: 'REMOVE_BLOCKS', clientIds: [ clientId ], }, + select( + 'core/block-editor', + 'getBlockCount', + ), ] ); } ); - it( 'should return REMOVE_BLOCKS action, opting out of remove previous', () => { + it( 'should return REMOVE_BLOCKS action, opting out of select previous', () => { const clientId = 'myclientid'; const actions = Array.from( removeBlock( clientId, false ) ); @@ -247,6 +256,10 @@ describe( 'actions', () => { type: 'REMOVE_BLOCKS', clientIds: [ clientId ], }, + select( + 'core/block-editor', + 'getBlockCount', + ), ] ); } ); } );