Skip to content

Commit

Permalink
Merging blocks: allow x to be merged into wrapper blocks (quote, list…
Browse files Browse the repository at this point in the history
…, group...) (#42780)
  • Loading branch information
ellatrix authored Aug 12, 2022
1 parent 6268912 commit c416bf8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
36 changes: 32 additions & 4 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,21 +1002,49 @@ 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
);
// Only focus the previous block if it's not mergeable.
if ( blocksWithTheSameType?.length !== 1 ) {
dispatch.selectBlock( blockA.clientId );
return;
}
const [ blockWithSameType ] = blocksWithTheSameType;
if ( blockWithSameType.innerBlocks.length < 1 ) {
dispatch.selectBlock( blockA.clientId );
return;
}
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
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>
<!-- /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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- wp:group -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>1</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:paragraph -->
<p>2</p>
<!-- /wp:paragraph -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- wp:group -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>1</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>2</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->
17 changes: 17 additions & 0 deletions test/e2e/specs/editor/blocks/group.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,21 @@ test.describe( 'Group', () => {

expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );

test( 'can merge into group with Backspace', async ( { editor, page } ) => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '1' );
await editor.transformBlockTo( 'core/group' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '2' );

// Confirm last paragraph is outside of group.
expect( await editor.getEditedPostContent() ).toMatchSnapshot();

// Merge the last paragraph into the group.
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'Backspace' );

expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );
} );

0 comments on commit c416bf8

Please sign in to comment.