Skip to content

Commit

Permalink
Writing flow: remove first empty paragraph on Backspace (WordPress#61889
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ellatrix authored and carstingaxion committed Jun 4, 2024
1 parent f565d01 commit 47a56b8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
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 @@ -265,6 +265,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 @@ -306,6 +307,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 @@ -345,14 +368,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();
}
}

Expand Down Expand Up @@ -463,16 +497,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

0 comments on commit 47a56b8

Please sign in to comment.