diff --git a/packages/block-editor/src/components/block-list/block.js b/packages/block-editor/src/components/block-list/block.js index 3980dd7b2aead3..c385ea4cd6367f 100644 --- a/packages/block-editor/src/components/block-list/block.js +++ b/packages/block-editor/src/components/block-list/block.js @@ -376,26 +376,26 @@ 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 ), @@ -403,16 +403,16 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { changeSelection ); removeBlock( firstClientId, false ); - } ); + } } - } - if ( - ! getBlockOrder( _clientId ).length && - isUnmodifiedBlock( getBlock( _clientId ) ) - ) { - removeBlock( _clientId, false ); - } + if ( + ! getBlockOrder( _clientId ).length && + isUnmodifiedBlock( getBlock( _clientId ) ) + ) { + removeBlock( _clientId, false ); + } + } ); } } diff --git a/packages/block-library/src/list-item/transforms.js b/packages/block-library/src/list-item/transforms.js index 6e05f8501b5a3b..7918b45a6b0279 100644 --- a/packages/block-library/src/list-item/transforms.js +++ b/packages/block-library/src/list-item/transforms.js @@ -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 ) ), + ], }, ], }; diff --git a/test/e2e/specs/editor/blocks/list.spec.js b/test/e2e/specs/editor/blocks/list.spec.js index bdf366fd73f47b..a4af98f0ba0578 100644 --- a/test/e2e/specs/editor/blocks/list.spec.js +++ b/test/e2e/specs/editor/blocks/list.spec.js @@ -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,