Skip to content

Commit

Permalink
Wiriting flow: Backspace at beginning of first paragraph block preven…
Browse files Browse the repository at this point in the history
…ts block from being deleted (#56329)

* Writing flow: Backspace at beginning of first paragraph block prevents block from being deleted

* Use replaceBlocks

Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: draganescu <andraganescu@git.wordpress.org>
  • Loading branch information
4 people committed Apr 2, 2024
1 parent 9b465df commit f04edd2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,16 @@ 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 ) {
replaceBlocks( clientId, replacement );
}
}
}
},
Expand Down
47 changes: 47 additions & 0 deletions test/e2e/specs/editor/blocks/heading.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f04edd2

Please sign in to comment.