Skip to content

Commit

Permalink
List block: fix merging nested list into paragraph (#50634)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored May 16, 2023
1 parent 744ab2c commit 4adde6c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 30 deletions.
56 changes: 28 additions & 28 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,43 +376,43 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => {
) {
removeBlock( _clientId );
} else {
if (
canInsertBlockType(
getBlockName( firstClientId ),
targetRootClientId
)
) {
moveBlocksToPosition(
[ firstClientId ],
_clientId,
targetRootClientId,
getBlockIndex( _clientId )
);
} else {
const replacement = switchToBlockType(
getBlock( firstClientId ),
getDefaultBlockName()
);

if ( replacement && replacement.length ) {
registry.batch( () => {
registry.batch( () => {
if (
canInsertBlockType(
getBlockName( firstClientId ),
targetRootClientId
)
) {
moveBlocksToPosition(
[ firstClientId ],
_clientId,
targetRootClientId,
getBlockIndex( _clientId )
);
} else {
const replacement = switchToBlockType(
getBlock( firstClientId ),
getDefaultBlockName()
);

if ( replacement && replacement.length ) {
insertBlocks(
replacement,
getBlockIndex( _clientId ),
targetRootClientId,
changeSelection
);
removeBlock( firstClientId, false );
} );
}
}
}

if (
! getBlockOrder( _clientId ).length &&
isUnmodifiedBlock( getBlock( _clientId ) )
) {
removeBlock( _clientId, false );
}
if (
! getBlockOrder( _clientId ).length &&
isUnmodifiedBlock( getBlock( _clientId ) )
) {
removeBlock( _clientId, false );
}
} );
}
}

Expand Down
6 changes: 4 additions & 2 deletions packages/block-library/src/list-item/transforms.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import { createBlock, cloneBlock } from '@wordpress/blocks';

const transforms = {
to: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
transform: ( attributes, innerBlocks = [] ) => [
createBlock( 'core/paragraph', attributes ),
...innerBlocks.map( ( block ) => cloneBlock( block ) ),
],
},
],
};
Expand Down
65 changes: 65 additions & 0 deletions test/e2e/specs/editor/blocks/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,71 @@ test.describe( 'List (@firefox)', () => {
);
} );

test( 'should keep nested list items when merging with paragraph', async ( {
editor,
page,
pageUtils,
} ) => {
const startingContent = [
{
name: 'core/paragraph',
attributes: { content: 'p' },
},
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: '1' },
innerBlocks: [
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: 'i' },
},
],
},
],
},
],
},
];
for ( const block of startingContent ) {
await editor.insertBlock( block );
}

// Move the caret in front of "1" in the first list item.
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'Backspace' );

await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/paragraph',
attributes: { content: 'p' },
},
{
name: 'core/paragraph',
attributes: { content: '1' },
},
{
name: 'core/list',
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: 'i' },
},
],
},
] );

await pageUtils.pressKeys( 'primary+z' );

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

test( 'should split into two ordered lists with paragraph', async ( {
editor,
page,
Expand Down

1 comment on commit 4adde6c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 4adde6c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4989426196
📝 Reported issues:

Please sign in to comment.