Skip to content

Commit

Permalink
General fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Oct 9, 2023
1 parent 0daa67b commit cba9be1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* WordPress dependencies
*/
import { isUnmodifiedBlock } from '@wordpress/blocks';
import { Platform } from '@wordpress/element';

const castArray = ( maybeArray ) =>
Expand Down Expand Up @@ -150,14 +151,27 @@ export const privateRemoveBlocks =
}
}

const blockOrder = select.getBlockOrder( rootClientId );
const removeRoot =
blockOrder.length === clientIds.length &&
isUnmodifiedBlock(
select.__unstableGetBlockWithoutInnerBlocks( rootClientId )
);

if ( selectPrevious ) {
dispatch.selectPreviousBlock( clientIds[ 0 ], selectPrevious );
dispatch.selectPreviousBlock(
removeRoot ? rootClientId : clientIds[ 0 ],
selectPrevious
);
}

// We're batching these two actions because an extra `undo/redo` step can
// be created, based on whether we insert a default block or not.
registry.batch( () => {
dispatch( { type: 'REMOVE_BLOCKS', clientIds } );
dispatch( {
type: 'REMOVE_BLOCKS',
clientIds: removeRoot ? [ rootClientId ] : 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
15 changes: 15 additions & 0 deletions test/e2e/specs/editor/blocks/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,21 @@ test.describe( 'List (@firefox)', () => {
);
} );

test( 'remove empty list graciously through UI', async ( {
editor,
page,
} ) => {
await editor.canvas
.locator( 'role=button[name="Add default block"i]' )
.click();
await page.keyboard.type( '* 1' );

await editor.clickBlockToolbarButton( 'Options' );
await page.getByRole( 'menuitem', { name: 'Delete' } ).click();

expect( await editor.getEditedPostContent() ).toBe( '' );
} );

test( 'should not change the contents when you change the list type to Ordered', async ( {
editor,
page,
Expand Down

0 comments on commit cba9be1

Please sign in to comment.