Skip to content

Commit

Permalink
List: maintain nested list on parent item removal (#62949)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
  • Loading branch information
3 people authored and gutenbergplugin committed Jul 2, 2024
1 parent 7398659 commit a530166
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/block-library/src/list-item/hooks/use-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} );
Expand Down
60 changes: 60 additions & 0 deletions test/e2e/specs/editor/blocks/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' } },
],
},
] );
} );
} );

0 comments on commit a530166

Please sign in to comment.