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

Merging blocks: allow x to be merged into wrapper blocks (quote, list, group...) #42780

Merged
merged 5 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Merging blocks: allow x to be merged into wrapper blocks (quote, list…
…, group...)
  • Loading branch information
ellatrix committed Aug 12, 2022
commit 095df179c0b744da8ce5daf9e0f529c314840e38
37 changes: 32 additions & 5 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,21 +1002,41 @@ export const __unstableExpandSelection =
*/
export const mergeBlocks =
( firstBlockClientId, secondBlockClientId ) =>
( { select, dispatch } ) => {
( { registry, select, dispatch } ) => {
const blocks = [ firstBlockClientId, secondBlockClientId ];
dispatch( { type: 'MERGE_BLOCKS', blocks } );

const [ clientIdA, clientIdB ] = blocks;
const blockA = select.getBlock( clientIdA );
const blockAType = getBlockType( blockA.name );

// Only focus the previous block if it's not mergeable.
if ( ! blockAType ) return;

const blockB = select.getBlock( clientIdB );

if ( blockAType && ! blockAType.merge ) {
dispatch.selectBlock( blockA.clientId );
// If there's no merge function defined, attempt merging inner
// blocks.
const blocksWithTheSameType = switchToBlockType(
blockB,
blockAType.name
);
if ( blocksWithTheSameType?.length !== 1 ) return;
const [ blockWithSameType ] = blocksWithTheSameType;
registry.batch( () => {
dispatch.insertBlocks(
blockWithSameType.innerBlocks,
undefined,
clientIdA
);
dispatch.removeBlock( clientIdB );
dispatch.selectBlock(
blockWithSameType.innerBlocks[ 0 ].clientId
);
} );
return;
}

const blockB = select.getBlock( clientIdB );
const blockBType = getBlockType( blockB.name );
const { clientId, attributeKey, offset } = select.getSelectionStart();
const selectedBlockType =
Expand Down Expand Up @@ -1075,8 +1095,15 @@ export const mergeBlocks =
? [ cloneB ]
: switchToBlockType( cloneB, blockA.name );

// If the block types can not match, do nothing.
if ( ! blocksWithTheSameType || ! blocksWithTheSameType.length ) {
// Fall back to moving the block inside the previous
// block.
dispatch.moveBlocksToPosition(
[ clientIdA ],
select.getBlockRootClientId( clientIdA ),
clientIdB,
0
);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ exports[`Quote can be split at the end 2`] = `
"<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><!-- wp:paragraph -->
<p>1</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
Copy link
Contributor

Choose a reason for hiding this comment

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

@ellatrix I think there should be a '2' here. The test types it, but it's missing. It looks like you may have committed the result of a failed test.

I've pushed a PR that updates the snapshot to its correct version - #43407. I expect the test is flakey and will still need to be fixed, but at least it won't be a false positive.

<!-- /wp:paragraph --></blockquote>
<!-- /wp:quote -->"
`;
3 changes: 2 additions & 1 deletion packages/e2e-tests/specs/editor/blocks/quote.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ describe( 'Quote', () => {
expect( await getEditedPostContent() ).toMatchSnapshot();

await page.keyboard.press( 'Backspace' );
await page.keyboard.type( '2' );

// Expect the paragraph to be deleted.
// Expect the paragraph to be merged into the quote block.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

Expand Down