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: fix merging nested lists #52949

Merged
merged 2 commits into from
Jul 26, 2023
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
17 changes: 12 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 @@ -107,11 +107,18 @@ export default function useMerge( clientId, onMerge ) {
} else if ( previousBlockClientId ) {
const trailingId = getTrailingId( previousBlockClientId );
registry.batch( () => {
moveBlocksToPosition(
getBlockOrder( clientId ),
clientId,
previousBlockClientId
);
// When merging a list item with a previous trailing list
// item, we also need to move any nested list items. First,
// check if there's a listed list. If there's a nested list,
// append its nested list items to the trailing list.
const [ nestedListClientId ] = getBlockOrder( clientId );
ellatrix marked this conversation as resolved.
Show resolved Hide resolved
if ( nestedListClientId ) {
moveBlocksToPosition(
getBlockOrder( nestedListClientId ),
nestedListClientId,
getBlockRootClientId( trailingId )
);
}
mergeBlocks( trailingId, clientId );
} );
} else {
Expand Down
81 changes: 81 additions & 0 deletions test/e2e/specs/editor/blocks/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1367,4 +1367,85 @@ test.describe( 'List (@firefox)', () => {
<!-- /wp:list-item --></ul>
<!-- /wp:list -->` );
} );

test( 'should merge two list items with nested lists', async ( {
editor,
page,
} ) => {
await editor.insertBlock( {
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: '2' },
innerBlocks: [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: 'b' },
},
],
},
],
},
],
} );

// Navigate to the third item.
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'ArrowDown' );

await page.keyboard.press( 'Backspace' );

// Test caret position.
await page.keyboard.type( '‸' );

await expect.poll( 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‸2' },
},
{
name: 'core/list-item',
attributes: { content: 'b' },
},
],
},
],
},
],
},
] );
} );
} );