From 627c696a09721fb2f5ef477ca0c252a4655b817d Mon Sep 17 00:00:00 2001 From: Ella Date: Wed, 22 May 2024 21:04:48 +0200 Subject: [PATCH 1/2] Writing flow: remove first empty paragraph on Backspace --- .../src/components/block-list/block.js | 9 ++++++ .../specs/editor/various/writing-flow.spec.js | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/packages/block-editor/src/components/block-list/block.js b/packages/block-editor/src/components/block-list/block.js index cbb1b769b5336..f4722292a935e 100644 --- a/packages/block-editor/src/components/block-list/block.js +++ b/packages/block-editor/src/components/block-list/block.js @@ -264,6 +264,7 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { __unstableMarkLastChangeAsPersistent, moveBlocksToPosition, removeBlock, + selectBlock, } = dispatch( blockEditorStore ); // Do not add new properties here, use `useDispatch` instead to avoid @@ -472,6 +473,14 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { if ( replacement && replacement.length ) { replaceBlocks( clientId, replacement ); } + } else { + const nextBlockClientId = getNextBlockClientId( clientId ); + if ( nextBlockClientId ) { + registry.batch( () => { + removeBlock( clientId ); + selectBlock( nextBlockClientId ); + } ); + } } } }, diff --git a/test/e2e/specs/editor/various/writing-flow.spec.js b/test/e2e/specs/editor/various/writing-flow.spec.js index 2dee26255c103..2eee3cad2b73f 100644 --- a/test/e2e/specs/editor/various/writing-flow.spec.js +++ b/test/e2e/specs/editor/various/writing-flow.spec.js @@ -595,6 +595,37 @@ test.describe( 'Writing Flow (@firefox, @webkit)', () => { ] ); } ); + test( 'should remove first empty paragraph on Backspace', async ( { + editor, + page, + } ) => { + await page.keyboard.press( 'Enter' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '2' ); + await page.keyboard.press( 'ArrowUp' ); + + // Ensure setup is correct. + expect( await editor.getBlocks() ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: '' }, + }, + { + name: 'core/paragraph', + attributes: { content: '2' }, + }, + ] ); + + await page.keyboard.press( 'Backspace' ); + + expect( await editor.getBlocks() ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: '2' }, + }, + ] ); + } ); + test( 'should merge paragraphs', async ( { editor, page } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1' ); From 3f2127a41b1ff7f352f21672197bcb3439f5a306 Mon Sep 17 00:00:00 2001 From: Ella Date: Wed, 22 May 2024 21:27:02 +0200 Subject: [PATCH 2/2] Fix post content in site editor too --- .../src/components/block-list/block.js | 53 ++++++++++++------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/packages/block-editor/src/components/block-list/block.js b/packages/block-editor/src/components/block-list/block.js index f4722292a935e..88f834586992f 100644 --- a/packages/block-editor/src/components/block-list/block.js +++ b/packages/block-editor/src/components/block-list/block.js @@ -306,6 +306,28 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { canInsertBlockType, } = registry.select( blockEditorStore ); + function switchToDefaultOrRemove() { + const block = getBlock( clientId ); + const defaultBlockName = getDefaultBlockName(); + if ( getBlockName( clientId ) !== defaultBlockName ) { + const replacement = switchToBlockType( + block, + defaultBlockName + ); + if ( replacement && replacement.length ) { + replaceBlocks( clientId, replacement ); + } + } else if ( isUnmodifiedDefaultBlock( block ) ) { + const nextBlockClientId = getNextBlockClientId( clientId ); + if ( nextBlockClientId ) { + registry.batch( () => { + removeBlock( clientId ); + selectBlock( nextBlockClientId ); + } ); + } + } + } + /** * Moves the block with clientId up one level. If the block type * cannot be inserted at the new location, it will be attempted to @@ -345,7 +367,16 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { getDefaultBlockName() ); - if ( replacement && replacement.length ) { + if ( + replacement && + replacement.length && + replacement.every( ( block ) => + canInsertBlockType( + block.name, + targetRootClientId + ) + ) + ) { insertBlocks( replacement, getBlockIndex( _clientId ), @@ -353,6 +384,8 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { changeSelection ); removeBlock( firstClientId, false ); + } else { + switchToDefaultOrRemove(); } } @@ -463,24 +496,8 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { } moveFirstItemUp( rootClientId ); - } else if ( - getBlockName( clientId ) !== getDefaultBlockName() - ) { - const replacement = switchToBlockType( - getBlock( clientId ), - getDefaultBlockName() - ); - if ( replacement && replacement.length ) { - replaceBlocks( clientId, replacement ); - } } else { - const nextBlockClientId = getNextBlockClientId( clientId ); - if ( nextBlockClientId ) { - registry.batch( () => { - removeBlock( clientId ); - selectBlock( nextBlockClientId ); - } ); - } + switchToDefaultOrRemove(); } } },