Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe just not do anything? I'm not sure if pressing Backspace at the start of a heading should convert it to a paragraph? Are there any editors that do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Google Docs and MS Word don't seem to transform a heading into a paragraph. Regarding Notion, it seems to transform a heading into a paragraph.

9cd90d08815c9c736a142187f7cb9fcb.mp4

Personally, I like the current behavior of the Backspace key. If the backspace key does nothing, we will need to transform the existing heading into a paragraph or delete it in order to start a new article again from a paragraph. This seems a little cumbersome to me.

} 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
Loading