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

Transforms: add group unwrap #42685

Merged
merged 4 commits into from
Jul 29, 2022
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
7 changes: 7 additions & 0 deletions packages/block-library/src/group/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ const transforms = {
},
},
],
to: [
{
type: 'block',
blocks: [ '*' ],
transform: ( attributes, innerBlocks ) => innerBlocks,
},
],
};

export default transforms;
17 changes: 12 additions & 5 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ const isPossibleTransformForSource = ( transform, direction, blocks ) => {
// a Grouping block.
if (
! isMultiBlock &&
direction === 'from' &&
isContainerGroupBlock( sourceBlock.name ) &&
isContainerGroupBlock( transform.blockName )
) {
Expand Down Expand Up @@ -490,8 +491,7 @@ export function switchToBlockType( blocks, name ) {
transformationsTo,
( t ) =>
t.type === 'block' &&
( isWildcardBlockTransform( t ) ||
Copy link
Member Author

Choose a reason for hiding this comment

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

Wildcard to transforms (a.k.a. unwrap transforms) don't make sense to be considered when looking for a specific block transform.

E.g. if the quote block registers an unwrap transform, it shouldn't be considered when you transform a quote to a particular other block like pull quote.

The wildcard transforms only make sense from "from" transforms, which means transforming from random mix of blocks to the block that registers it.

E.g. when a quote block registers a "from" wildcard transform, it should be considered when switching block type to quote.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add this to the transforms docs?

t.blocks.indexOf( name ) !== -1 ) &&
t.blocks.indexOf( name ) !== -1 &&
( ! isMultiBlock || t.isMultiBlock ) &&
maybeCheckTransformIsMatch( t, blocksArray )
) ||
Expand Down Expand Up @@ -555,9 +555,16 @@ export function switchToBlockType( blocks, name ) {
return null;
}

const hasSwitchedBlock =
name === '*' ||
some( transformationResults, ( result ) => result.name === name );
// When unwrapping blocks (`switchToBlockType( wrapperblocks, '*' )`), do
// not run filters on the unwrapped blocks. They shoud remain as they are.
if ( name === '*' ) {
return transformationResults;
}

const hasSwitchedBlock = some(
transformationResults,
( result ) => result.name === name
);

// Ensure that at least one block object returned by the transformation has
// the expected "destination" block type.
Expand Down
35 changes: 35 additions & 0 deletions packages/e2e-tests/specs/editor/blocks/group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
searchForBlock,
getEditedPostContent,
createNewPost,
pressKeyWithModifier,
transformBlockTo,
} from '@wordpress/e2e-test-utils';

describe( 'Group', () => {
Expand Down Expand Up @@ -40,4 +42,37 @@ describe( 'Group', () => {

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

it( 'can wrap in group and unwrap group', async () => {
await clickBlockAppender();
await page.keyboard.type( '1' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '2' );
await pressKeyWithModifier( 'shift', 'ArrowUp' );
await transformBlockTo( 'Group' );

expect( await getEditedPostContent() ).toMatchInlineSnapshot( `
"<!-- wp:group -->
<div class=\\"wp-block-group\\"><!-- wp:paragraph -->
<p>1</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>2</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->"
` );

await transformBlockTo( 'Unwrap' );

expect( await getEditedPostContent() ).toMatchInlineSnapshot( `
"<!-- wp:paragraph -->
<p>1</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>2</p>
<!-- /wp:paragraph -->"
` );
} );
} );