From a8b19126bdffcb33858910db4766882ceb113a6e Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Tue, 21 Nov 2023 01:09:32 +0900 Subject: [PATCH 1/2] Writing flow: Backspace at beginning of first paragraph block prevents block from being deleted --- .../src/components/block-list/block.js | 13 ++++- test/e2e/specs/editor/blocks/heading.spec.js | 47 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/block-list/block.js b/packages/block-editor/src/components/block-list/block.js index 69fe316112618..a66af43ea9cec 100644 --- a/packages/block-editor/src/components/block-list/block.js +++ b/packages/block-editor/src/components/block-list/block.js @@ -451,8 +451,17 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { } moveFirstItemUp( rootClientId ); - } else { - removeBlock( clientId ); + } else if ( + getBlockName( clientId ) !== getDefaultBlockName() + ) { + const replacement = switchToBlockType( + getBlock( clientId ), + getDefaultBlockName() + ); + if ( replacement && replacement.length ) { + insertBlocks( replacement, getBlockIndex( clientId ) ); + removeBlock( clientId, false ); + } } } }, diff --git a/test/e2e/specs/editor/blocks/heading.spec.js b/test/e2e/specs/editor/blocks/heading.spec.js index f0271a8f6e897..358efd28874d7 100644 --- a/test/e2e/specs/editor/blocks/heading.spec.js +++ b/test/e2e/specs/editor/blocks/heading.spec.js @@ -119,6 +119,53 @@ test.describe( 'Heading', () => { ] ); } ); + test( 'should create a empty paragraph block when pressing backspace at the beginning of the first empty heading block', async ( { + editor, + page, + } ) => { + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '## a' ); + await page.keyboard.press( 'Backspace' ); + await page.keyboard.press( 'Backspace' ); + + await expect.poll( editor.getBlocks ).toEqual( [] ); + } ); + + test( 'should transform to a paragraph block when pressing backspace at the beginning of the first heading block', async ( { + editor, + page, + } ) => { + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '## a' ); + await page.keyboard.press( 'ArrowLeft' ); + await page.keyboard.press( 'Backspace' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: 'a' }, + }, + ] ); + } ); + + test( 'should keep the heading when there is an empty paragraph block before and backspace is pressed at the start', async ( { + editor, + page, + } ) => { + await page.keyboard.press( 'Enter' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '## a' ); + await page.keyboard.press( 'ArrowLeft' ); + await page.keyboard.press( 'Backspace' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/heading', + attributes: { content: 'a', level: 2 }, + }, + ] ); + } ); + test( 'should correctly apply custom colors', async ( { editor, page, From 76485973fd39176ffd8a0b27c9d0ecc64e837c6c Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Mon, 1 Apr 2024 20:33:08 +0900 Subject: [PATCH 2/2] Use replaceBlocks --- packages/block-editor/src/components/block-list/block.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/block-list/block.js b/packages/block-editor/src/components/block-list/block.js index 4bb8f69f8ecfd..febc52aca401a 100644 --- a/packages/block-editor/src/components/block-list/block.js +++ b/packages/block-editor/src/components/block-list/block.js @@ -467,8 +467,7 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { getDefaultBlockName() ); if ( replacement && replacement.length ) { - insertBlocks( replacement, getBlockIndex( clientId ) ); - removeBlock( clientId, false ); + replaceBlocks( clientId, replacement ); } } }