Skip to content

Commit

Permalink
Fix: deleting the last block triggers a focus loss. (#14189)
Browse files Browse the repository at this point in the history
## 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 #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.
  • Loading branch information
jorgefilipecosta authored and youknowriad committed Mar 6, 2019
1 parent 4a05fa4 commit b49fcbb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
11 changes: 11 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/block-editor/src/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ export default {
RESET_BLOCKS: [
validateBlocksToTemplate,
],
REMOVE_BLOCKS: [
ensureDefaultBlock,
],
REPLACE_BLOCKS: [
ensureDefaultBlock,
],
Expand Down
15 changes: 14 additions & 1 deletion packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
toggleBlockMode,
updateBlockListSettings,
} from '../actions';
import { select } from '../controls';

describe( 'actions', () => {
describe( 'resetBlocks', () => {
Expand Down Expand Up @@ -218,6 +219,10 @@ describe( 'actions', () => {
type: 'REMOVE_BLOCKS',
clientIds,
},
select(
'core/block-editor',
'getBlockCount',
),
] );
} );
} );
Expand All @@ -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 ) );
Expand All @@ -247,6 +256,10 @@ describe( 'actions', () => {
type: 'REMOVE_BLOCKS',
clientIds: [ clientId ],
},
select(
'core/block-editor',
'getBlockCount',
),
] );
} );
} );
Expand Down

0 comments on commit b49fcbb

Please sign in to comment.