Skip to content

Commit

Permalink
Writing flow: preserve block when merging into empty paragraph (#55134)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Oct 9, 2023
1 parent 4ce1636 commit eb77e48
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => {
}

moveFirstItemUp( rootClientId );
} else {
removeBlock( clientId );
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function useDelete( props ) {
// an intentional user interaction distinguishing between Backspace and
// Delete to remove the empty field, but also to avoid merge & remove
// causing destruction of two fields (merge, then removed merged).
if ( onRemove && isEmpty( value ) && isReverse ) {
else if ( onRemove && isEmpty( value ) && isReverse ) {
onRemove( ! isReverse );
}

Expand Down
33 changes: 25 additions & 8 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
switchToBlockType,
synchronizeBlocksWithTemplate,
getBlockSupport,
isUnmodifiedDefaultBlock,
} from '@wordpress/blocks';
import { speak } from '@wordpress/a11y';
import { __, _n, sprintf } from '@wordpress/i18n';
Expand Down Expand Up @@ -1013,17 +1014,12 @@ export const mergeBlocks =

if ( ! blockAType ) return;

const blockB = select.getBlock( clientIdB );

if (
! blockAType.merge &&
! getBlockSupport( blockA.name, '__experimentalOnMerge' )
getBlockSupport( blockA.name, '__experimentalOnMerge' )
) {
dispatch.selectBlock( blockA.clientId );
return;
}

const blockB = select.getBlock( clientIdB );

if ( ! blockAType.merge ) {
// If there's no merge function defined, attempt merging inner
// blocks.
const blocksWithTheSameType = switchToBlockType(
Expand Down Expand Up @@ -1090,6 +1086,27 @@ export const mergeBlocks =
return;
}

if ( isUnmodifiedDefaultBlock( blockA ) ) {
dispatch.removeBlock(
clientIdA,
select.isBlockSelected( clientIdA )
);
return;
}

if ( isUnmodifiedDefaultBlock( blockB ) ) {
dispatch.removeBlock(
clientIdB,
select.isBlockSelected( clientIdB )
);
return;
}

if ( ! blockAType.merge ) {
dispatch.selectBlock( blockA.clientId );
return;
}

const blockBType = getBlockType( blockB.name );
const { clientId, attributeKey, offset } = select.getSelectionStart();
const selectedBlockType =
Expand Down

1 comment on commit eb77e48

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in eb77e48.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6459869474
📝 Reported issues:

Please sign in to comment.