From 53c84d592968b7666a02645bc2f575db6f67de52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Mon, 15 May 2023 16:28:04 +0300 Subject: [PATCH 1/2] List block: fix merging nested list into paragraph --- .../src/components/block-list/block.js | 56 +++++++++---------- .../block-library/src/list-item/transforms.js | 6 +- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/packages/block-editor/src/components/block-list/block.js b/packages/block-editor/src/components/block-list/block.js index 3980dd7b2aead..c385ea4cd6367 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 6e05f8501b5a3..7918b45a6b027 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 ) ), + ], }, ], }; From de5f2841cb662c9f4027ff861f49e6398b0c20f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Mon, 15 May 2023 17:18:13 +0300 Subject: [PATCH 2/2] Add e2e test --- test/e2e/specs/editor/blocks/list.spec.js | 65 +++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/e2e/specs/editor/blocks/list.spec.js b/test/e2e/specs/editor/blocks/list.spec.js index bdf366fd73f47..a4af98f0ba057 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,