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: preserve block when merging into empty paragraph #55134

Merged
merged 7 commits into from
Oct 9, 2023
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
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 ) {
Copy link
Member

Choose a reason for hiding this comment

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

With this change in practice, this condition else if ( onRemove && isEmpty( value ) && isReverse ) {is never executed right ? We pass an onMerge on the block list so if ( onMerge ) { will always be truth, if that's the case would it make sense to remove the condition?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not all blocks may pass an onMerge function to RichText, they may only pass onRemove.

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
Loading