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: remove first empty paragraph on Backspace #61889

Merged
merged 2 commits into from
May 31, 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
48 changes: 37 additions & 11 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -305,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
Expand Down Expand Up @@ -344,14 +367,25 @@ 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 ),
targetRootClientId,
changeSelection
);
removeBlock( firstClientId, false );
} else {
switchToDefaultOrRemove();
Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add a test for the site editor as well.

}
}

Expand Down Expand Up @@ -462,16 +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 {
switchToDefaultOrRemove();
}
}
},
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/specs/editor/various/writing-flow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
Loading