From b49fcbb80378f7b7493334d22f39e7030e106782 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 1 Mar 2019 22:04:11 +0000 Subject: [PATCH] Fix: deleting the last block triggers a focus loss. (#14189) ## Description This PR fixes a problem: if the post contains one block and it is removed the focus is lost. This is a regression that happened when removeBlocks action was refactored to be a generator. We had an effect that inserts the default block during remove blocks action when certain conditions are met, this effect stopped working. This PR removes the effect and makes sure everything is handled by the removeBlocks action creator. End to end test available at https://github.com/WordPress/gutenberg/pull/14191. ## How has this been tested? I created a new post. I wrote something in a paragraph I removed the paragraph using the remove button in the side menu and I verified the default block was added. --- packages/block-editor/src/store/actions.js | 11 +++++++++++ packages/block-editor/src/store/effects.js | 3 --- packages/block-editor/src/store/test/actions.js | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) 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', + ), ] ); } ); } );