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 forward merging of nested list #55121

Merged
merged 1 commit into from
Oct 10, 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
43 changes: 20 additions & 23 deletions packages/block-library/src/list-item/hooks/use-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ export default function useMerge( clientId, onMerge ) {
}

return ( forward ) => {
function mergeWithNested( clientIdA, clientIdB ) {
registry.batch( () => {
// When merging a sub list item with a higher next list item, we
// also need to move any nested list items. Check if there's a
// listed list, and append its nested list items to the current
// list.
const [ nestedListClientId ] = getBlockOrder( clientIdB );
if ( nestedListClientId ) {
moveBlocksToPosition(
getBlockOrder( nestedListClientId ),
nestedListClientId,
getBlockRootClientId( clientIdA )
);
}
mergeBlocks( clientIdA, clientIdB );
} );
}

if ( forward ) {
const nextBlockClientId = getNextId( clientId );

Expand All @@ -87,14 +105,7 @@ export default function useMerge( clientId, onMerge ) {
if ( getParentListItemId( nextBlockClientId ) ) {
outdentListItem( nextBlockClientId );
} else {
registry.batch( () => {
moveBlocksToPosition(
getBlockOrder( nextBlockClientId ),
nextBlockClientId,
getPreviousBlockClientId( nextBlockClientId )
);
mergeBlocks( clientId, nextBlockClientId );
} );
mergeWithNested( clientId, nextBlockClientId );
}
} else {
// Merging is only done from the top level. For lowel levels, the
Expand All @@ -104,21 +115,7 @@ export default function useMerge( clientId, onMerge ) {
outdentListItem( clientId );
} else if ( previousBlockClientId ) {
const trailingId = getTrailingId( previousBlockClientId );
registry.batch( () => {
// 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 );
if ( nestedListClientId ) {
moveBlocksToPosition(
getBlockOrder( nestedListClientId ),
nestedListClientId,
getBlockRootClientId( trailingId )
);
}
mergeBlocks( trailingId, clientId );
} );
mergeWithNested( trailingId, clientId );
} else {
onMerge( forward );
}
Expand Down
64 changes: 42 additions & 22 deletions test/e2e/specs/editor/blocks/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1361,11 +1361,8 @@ test.describe( 'List (@firefox)', () => {
<!-- /wp:list -->` );
} );

test( 'should merge two list items with nested lists', async ( {
editor,
page,
} ) => {
await editor.insertBlock( {
test.describe( 'should merge two list items with nested lists', () => {
const start = {
name: 'core/list',
innerBlocks: [
{
Expand Down Expand Up @@ -1399,22 +1396,8 @@ test.describe( 'List (@firefox)', () => {
],
},
],
} );

// 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( [
};
const end = [
{
name: 'core/list',
innerBlocks: [
Expand All @@ -1439,6 +1422,43 @@ test.describe( 'List (@firefox)', () => {
},
],
},
] );
];

test( 'Backspace', async ( { editor, page } ) => {
await editor.insertBlock( start );

// Navigate to the start of 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( end );
} );

test( 'Delete (forward)', async ( { editor, page } ) => {
await editor.insertBlock( start );

// Navigate to the end of the second 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( 'ArrowRight' );

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

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

await expect.poll( editor.getBlocks ).toMatchObject( end );
} );
} );
} );