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 extra undo/redo step when removing or replacing all blocks #54457

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 12 additions & 10 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ function getBlocksWithDefaultStylesApplied( blocks, blockEditorSettings ) {
*/
export const replaceBlocks =
( clientIds, blocks, indexToSelect, initialPosition = 0, meta ) =>
( { select, dispatch } ) => {
( { select, dispatch, registry } ) => {
/* eslint-enable jsdoc/valid-types */
clientIds = castArray( clientIds );
blocks = getBlocksWithDefaultStylesApplied(
Expand All @@ -394,16 +394,18 @@ export const replaceBlocks =
return;
}
}
dispatch( {
type: 'REPLACE_BLOCKS',
clientIds,
blocks,
time: Date.now(),
indexToSelect,
initialPosition,
meta,
registry.batch( () => {
dispatch( {
type: 'REPLACE_BLOCKS',
clientIds,
blocks,
time: Date.now(),
indexToSelect,
initialPosition,
meta,
} );
dispatch.ensureDefaultBlock();
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
} );
dispatch.ensureDefaultBlock();
};

/**
Expand Down
13 changes: 7 additions & 6 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function showBlockInterface() {
*/
export const privateRemoveBlocks =
( clientIds, selectPrevious = true, forceRemove = false ) =>
( { select, dispatch } ) => {
( { select, dispatch, registry } ) => {
if ( ! clientIds || ! clientIds.length ) {
return;
}
Expand Down Expand Up @@ -154,11 +154,12 @@ export const privateRemoveBlocks =
dispatch.selectPreviousBlock( clientIds[ 0 ], selectPrevious );
}

dispatch( { type: 'REMOVE_BLOCKS', clientIds } );

// 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.
dispatch( ensureDefaultBlock() );
registry.batch( () => {
dispatch( { type: 'REMOVE_BLOCKS', clientIds } );
// 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.
dispatch( ensureDefaultBlock() );
} );
};

/**
Expand Down
27 changes: 19 additions & 8 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ describe( 'actions', () => {
};
const dispatch = jest.fn();
dispatch.ensureDefaultBlock = jest.fn();
const registry = createRegistry();

replaceBlock( 'chicken', block )( { select, dispatch } );
replaceBlock( 'chicken', block )( { select, dispatch, registry } );

expect( dispatch ).toHaveBeenCalledWith( {
type: 'REPLACE_BLOCKS',
Expand Down Expand Up @@ -285,8 +286,12 @@ describe( 'actions', () => {
};
const dispatch = jest.fn();
dispatch.ensureDefaultBlock = jest.fn();
const registry = createRegistry();

replaceBlocks( [ 'chicken' ], blocks )( { select, dispatch } );
replaceBlocks(
[ 'chicken' ],
blocks
)( { select, dispatch, registry } );

expect( dispatch ).toHaveBeenCalledWith( {
type: 'REPLACE_BLOCKS',
Expand Down Expand Up @@ -319,14 +324,15 @@ describe( 'actions', () => {
};
const dispatch = jest.fn();
dispatch.ensureDefaultBlock = jest.fn();
const registry = createRegistry();

replaceBlocks(
[ 'chicken' ],
blocks,
null,
null,
meta
)( { select, dispatch } );
)( { select, dispatch, registry } );

expect( dispatch ).toHaveBeenCalledWith( {
type: 'REPLACE_BLOCKS',
Expand Down Expand Up @@ -628,8 +634,9 @@ describe( 'actions', () => {
const dispatch = Object.assign( jest.fn(), {
selectPreviousBlock: jest.fn(),
} );
const registry = createRegistry();

removeBlocks( clientIds )( { select, dispatch } );
removeBlocks( clientIds )( { select, dispatch, registry } );

expect( dispatch.selectPreviousBlock ).toHaveBeenCalledWith(
clientId,
Expand Down Expand Up @@ -739,8 +746,8 @@ describe( 'actions', () => {
const dispatch = Object.assign( jest.fn(), {
selectPreviousBlock: jest.fn(),
} );

removeBlock( clientId )( { select, dispatch } );
const registry = createRegistry();
removeBlock( clientId )( { select, dispatch, registry } );

expect( dispatch.selectPreviousBlock ).toHaveBeenCalledWith(
clientId,
Expand All @@ -753,7 +760,7 @@ describe( 'actions', () => {
} );
} );

it( 'should dispatch REMOVE_BLOCKS action, opting out of select previous', () => {
it( 'should dispatch REMOVE_BLOCKS action, opting out of select previous', async () => {
const clientId = 'myclientid';

const select = {
Expand All @@ -765,7 +772,11 @@ describe( 'actions', () => {
selectPreviousBlock: jest.fn(),
} );

removeBlocks( [ clientId ], false )( { select, dispatch } );
const registry = createRegistry();
removeBlocks(
[ clientId ],
false
)( { select, dispatch, registry } );

expect( dispatch.selectPreviousBlock ).not.toHaveBeenCalled();

Expand Down
Loading