Skip to content

Commit 1697aa7

Browse files
ellatrixyouknowriad
authored andcommitted
List: maintain nested list on parent item removal (WordPress#62949)
Co-authored-by: ellatrix <[email protected]> Co-authored-by: youknowriad <[email protected]>
1 parent f7fe45a commit 1697aa7

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

packages/block-library/src/list-item/hooks/use-merge.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,25 @@ export default function useMerge( clientId, onMerge ) {
9292
// list.
9393
const [ nestedListClientId ] = getBlockOrder( clientIdB );
9494
if ( nestedListClientId ) {
95-
moveBlocksToPosition(
96-
getBlockOrder( nestedListClientId ),
97-
nestedListClientId,
98-
getBlockRootClientId( clientIdA )
99-
);
95+
// If we are merging with the previous list item, and the
96+
// previous list item does not have nested list, move the
97+
// nested list to the previous list item.
98+
if (
99+
getPreviousBlockClientId( clientIdB ) === clientIdA &&
100+
! getBlockOrder( clientIdA ).length
101+
) {
102+
moveBlocksToPosition(
103+
[ nestedListClientId ],
104+
clientIdB,
105+
clientIdA
106+
);
107+
} else {
108+
moveBlocksToPosition(
109+
getBlockOrder( nestedListClientId ),
110+
nestedListClientId,
111+
getBlockRootClientId( clientIdA )
112+
);
113+
}
100114
}
101115
mergeBlocks( clientIdA, clientIdB );
102116
} );

test/e2e/specs/editor/blocks/list.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,4 +1549,64 @@ test.describe( 'List (@firefox)', () => {
15491549
await expect.poll( editor.getBlocks ).toMatchObject( end );
15501550
} );
15511551
} );
1552+
1553+
test( 'should leave nested list intact when deleting the parent item', async ( {
1554+
editor,
1555+
page,
1556+
} ) => {
1557+
await editor.insertBlock( {
1558+
name: 'core/list',
1559+
innerBlocks: [
1560+
{
1561+
name: 'core/list-item',
1562+
attributes: { content: '1' },
1563+
},
1564+
{
1565+
name: 'core/list-item',
1566+
attributes: { content: '' },
1567+
innerBlocks: [
1568+
{
1569+
name: 'core/list',
1570+
innerBlocks: [
1571+
{
1572+
name: 'core/list-item',
1573+
attributes: { content: 'a' },
1574+
},
1575+
],
1576+
},
1577+
],
1578+
},
1579+
{ name: 'core/list-item', attributes: { content: '3' } },
1580+
],
1581+
} );
1582+
1583+
await page.keyboard.press( 'ArrowDown' );
1584+
await page.keyboard.press( 'ArrowDown' );
1585+
await page.keyboard.press( 'ArrowDown' );
1586+
await page.keyboard.press( 'Backspace' );
1587+
1588+
expect( await editor.getBlocks() ).toMatchObject( [
1589+
{
1590+
name: 'core/list',
1591+
innerBlocks: [
1592+
{
1593+
name: 'core/list-item',
1594+
attributes: { content: '1' },
1595+
innerBlocks: [
1596+
{
1597+
name: 'core/list',
1598+
innerBlocks: [
1599+
{
1600+
name: 'core/list-item',
1601+
attributes: { content: 'a' },
1602+
},
1603+
],
1604+
},
1605+
],
1606+
},
1607+
{ name: 'core/list-item', attributes: { content: '3' } },
1608+
],
1609+
},
1610+
] );
1611+
} );
15521612
} );

0 commit comments

Comments
 (0)