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 block: fix merging nested list into paragraph #50634

Merged
merged 2 commits into from
May 16, 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
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