Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: deleting the last block triggers a focus loss. #14189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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