Skip to content
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
35 changes: 27 additions & 8 deletions packages/block-library/src/list-item/hooks/use-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function useMerge( clientId, onMerge ) {
getPreviousBlockClientId,
getNextBlockClientId,
getBlockOrder,
getBlockIndex,
getBlockRootClientId,
getBlockName,
getBlock,
Expand Down Expand Up @@ -60,6 +61,18 @@ export default function useMerge( clientId, onMerge ) {
clientIdB,
clientIdA
);
} else if (
getParentListItemId( clientIdB ) === clientIdA
) {
// Merging into the parent item's own line: the
// children take the item's place in its list, one
// level up, since their line moved there.
moveBlocksToPosition(
getBlockOrder( nestedListClientId ),
nestedListClientId,
getBlockRootClientId( clientIdB ),
getBlockIndex( clientIdB ) + 1
);
} else {
moveBlocksToPosition(
getBlockOrder( nestedListClientId ),
Expand All @@ -68,7 +81,13 @@ export default function useMerge( clientId, onMerge ) {
);
}
}
const listId = getBlockRootClientId( clientIdB );
mergeBlocks( clientIdA, clientIdB );
// Merging the last item of a nested list into its parent
// line leaves the list block empty.
if ( ! getBlockOrder( listId ).length ) {
removeBlock( listId, false );
}
} );
}

Expand Down Expand Up @@ -126,24 +145,24 @@ export default function useMerge( clientId, onMerge ) {
}
}
}
} else if ( getParentListItemId( nextBlockClientId ) ) {
outdentListItem( nextBlockClientId );
} else {
mergeWithNested( clientId, nextBlockClientId );
}
} else {
// Merging is only done from the top level. For lowel levels, the
// list item is outdented instead.
if ( getParentListItemId( clientId ) ) {
outdentListItem( clientId );
return;
}
// Merge into the previous line: the trailing item of the
// previous sibling, or the parent item's own line for a first
// child.
const previousBlockClientId = getPreviousBlockClientId( clientId );
if ( previousBlockClientId ) {
const trailingId = getTrailingId( previousBlockClientId );
mergeWithNested( trailingId, clientId );
return;
}
const parentListItemId = getParentListItemId( clientId );
if ( parentListItemId ) {
mergeWithNested( parentListItemId, clientId );
return;
}

const blockOrder = getBlockOrder( clientId );
if (
Expand Down
201 changes: 177 additions & 24 deletions test/e2e/specs/editor/blocks/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,26 +1014,25 @@ test.describe( 'List (@firefox)', () => {
<!-- /wp:list -->`
);

await page.keyboard.press( 'Backspace' );
await page.keyboard.press( 'Backspace' ); // Should be at level 1.
await page.keyboard.press( 'Backspace' ); // Should delete "i".

await expect.poll( editor.getEditedPostContent ).toBe(
`<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>1<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>a</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>a<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->`
);

await page.keyboard.press( 'Backspace' ); // Should be at level 0.
await page.keyboard.press( 'Backspace' ); // Should merge into "a".

await expect.poll( editor.getEditedPostContent ).toBe(
`<!-- wp:list -->
Expand All @@ -1043,59 +1042,49 @@ test.describe( 'List (@firefox)', () => {
<li>a</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->`
);

await page.keyboard.press( 'Backspace' ); // Should be at level 1.
await page.keyboard.press( 'Backspace' ); // Should delete "a".

await expect.poll( editor.getEditedPostContent ).toBe(
`<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>1<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>a</li>
<li></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->`
);

await page.keyboard.press( 'Backspace' );
await page.keyboard.press( 'Backspace' ); // Should be at level 0.
await page.keyboard.press( 'Backspace' ); // Should merge into "1".

await expect.poll( editor.getEditedPostContent ).toBe(
`<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>1</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->`
);

await page.keyboard.press( 'Backspace' ); // Should be at level 0.
await page.keyboard.press( 'Backspace' ); // Should delete "1".

await expect.poll( editor.getEditedPostContent ).toBe(
`<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>1</li>
<li></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->`
);

await page.keyboard.press( 'Backspace' );
await page.keyboard.press( 'Backspace' ); // Should remove list.

await expect.poll( editor.getEditedPostContent ).toBe( '' );

// That's 9 key presses to create the list, and 9 key presses to remove
// That's 9 key presses to create the list, and 6 key presses to remove
// the list. ;)
} );

Expand Down Expand Up @@ -1136,7 +1125,7 @@ test.describe( 'List (@firefox)', () => {
<!-- /wp:list -->`
);

await page.keyboard.press( 'Backspace' );
await page.keyboard.press( 'Shift+Tab' );
// Type to also verify the caret stays in the outdented item.
await page.keyboard.type( 'x' );

Expand All @@ -1161,6 +1150,170 @@ test.describe( 'List (@firefox)', () => {
);
} );

test( 'should try to preserve the indentation level of nested items as their parent gets merged', async ( {
editor,
page,
} ) => {
await editor.canvas
.locator( 'role=document[name="Add default block"i]' )
.click();

await page.keyboard.type( '* a' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( ' b' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( ' c' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'd' );
await page.keyboard.press( 'Enter' );
// Enter on an empty item outdents it, one level per press.
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'e' );

await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: 'a' },
innerBlocks: [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: 'b' },
innerBlocks: [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: {
content: 'c',
},
},
{
name: 'core/list-item',
attributes: {
content: 'd',
},
},
],
},
],
},
],
},
],
},
{
name: 'core/list-item',
attributes: { content: 'e' },
},
],
},
] );

// Place caret to the right of list-item "c"
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowRight' );

// Backspace should empty the text of that list-item, and nothing else
await page.keyboard.press( 'Backspace' );
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: 'a' },
innerBlocks: [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: 'b' },
innerBlocks: [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: {
content: '',
},
},
{
name: 'core/list-item',
attributes: {
content: 'd',
},
},
],
},
],
},
],
},
],
},
{
name: 'core/list-item',
attributes: { content: 'e' },
},
],
},
] );

// Now that the item is empty, backspace merges it into "b", and
// "d" keeps its original indentation level as a child of "b".
await page.keyboard.press( 'Backspace' );
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: 'a' },
innerBlocks: [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: 'b' },
innerBlocks: [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: {
content: 'd',
},
},
],
},
],
},
],
},
],
},
{
name: 'core/list-item',
attributes: { content: 'e' },
},
],
},
] );
} );

test( 'should place the caret in the right place with nested list', async ( {
editor,
page,
Expand Down
Loading