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

List: maintain nested list on parent item removal #62949

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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' } },
],
},
] );
} );
} );
Loading