diff --git a/packages/block-library/src/list-item/hooks/use-merge.js b/packages/block-library/src/list-item/hooks/use-merge.js index 4b56abb320d929..9ca4d5372ee6e8 100644 --- a/packages/block-library/src/list-item/hooks/use-merge.js +++ b/packages/block-library/src/list-item/hooks/use-merge.js @@ -92,11 +92,25 @@ export default function useMerge( clientId, onMerge ) { // list. const [ nestedListClientId ] = getBlockOrder( clientIdB ); if ( nestedListClientId ) { - moveBlocksToPosition( - getBlockOrder( nestedListClientId ), - nestedListClientId, - getBlockRootClientId( clientIdA ) - ); + // If we are merging with the previous list item, and the + // previous list item does not have nested list, move the + // nested list to the previous list item. + if ( + getPreviousBlockClientId( clientIdB ) === clientIdA && + ! getBlockOrder( clientIdA ).length + ) { + moveBlocksToPosition( + [ nestedListClientId ], + clientIdB, + clientIdA + ); + } else { + moveBlocksToPosition( + getBlockOrder( nestedListClientId ), + nestedListClientId, + getBlockRootClientId( clientIdA ) + ); + } } mergeBlocks( clientIdA, clientIdB ); } ); diff --git a/test/e2e/specs/editor/blocks/list.spec.js b/test/e2e/specs/editor/blocks/list.spec.js index 7c42ad7989aff9..16126cf9cd29f6 100644 --- a/test/e2e/specs/editor/blocks/list.spec.js +++ b/test/e2e/specs/editor/blocks/list.spec.js @@ -1549,4 +1549,64 @@ test.describe( 'List (@firefox)', () => { await expect.poll( editor.getBlocks ).toMatchObject( end ); } ); } ); + + test( 'should leave nested list intact when deleting the parent item', async ( { + editor, + page, + } ) => { + await editor.insertBlock( { + name: 'core/list', + innerBlocks: [ + { + name: 'core/list-item', + attributes: { content: '1' }, + }, + { + name: 'core/list-item', + attributes: { content: '' }, + innerBlocks: [ + { + name: 'core/list', + innerBlocks: [ + { + name: 'core/list-item', + attributes: { content: 'a' }, + }, + ], + }, + ], + }, + { name: 'core/list-item', attributes: { content: '3' } }, + ], + } ); + + await page.keyboard.press( 'ArrowDown' ); + await page.keyboard.press( 'ArrowDown' ); + await page.keyboard.press( 'ArrowDown' ); + await page.keyboard.press( 'Backspace' ); + + expect( await editor.getBlocks() ).toMatchObject( [ + { + name: 'core/list', + innerBlocks: [ + { + name: 'core/list-item', + attributes: { content: '1' }, + innerBlocks: [ + { + name: 'core/list', + innerBlocks: [ + { + name: 'core/list-item', + attributes: { content: 'a' }, + }, + ], + }, + ], + }, + { name: 'core/list-item', attributes: { content: '3' } }, + ], + }, + ] ); + } ); } );