-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add block variations transformation in block switcher (#50139)
* Add block variations transformation in block switcher * move variation transforms to the top of the list * fix error on multi select * add e2e test * Update test/e2e/specs/editor/various/block-switcher-test.spec.js Co-authored-by: Kai Hao <kevin830726@gmail.com> * update e2e test * Update test/e2e/specs/editor/various/block-switcher-test.spec.js Co-authored-by: Kai Hao <kevin830726@gmail.com> --------- Co-authored-by: Kai Hao <kevin830726@gmail.com>
- Loading branch information
1 parent
01248b8
commit 8d9752e
Showing
4 changed files
with
225 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/block-editor/src/components/block-switcher/block-variation-transformations.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { MenuItem } from '@wordpress/components'; | ||
import { | ||
getBlockMenuDefaultClassName, | ||
cloneBlock, | ||
store as blocksStore, | ||
} from '@wordpress/blocks'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useState, useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
import BlockIcon from '../block-icon'; | ||
import PreviewBlockPopover from './preview-block-popover'; | ||
|
||
const EMPTY_OBJECT = {}; | ||
|
||
export function useBlockVariationTransforms( { clientIds, blocks } ) { | ||
const { activeBlockVariation, blockVariationTransformations } = useSelect( | ||
( select ) => { | ||
const { | ||
getBlockRootClientId, | ||
getBlockAttributes, | ||
canRemoveBlocks, | ||
} = select( blockEditorStore ); | ||
const { getActiveBlockVariation, getBlockVariations } = | ||
select( blocksStore ); | ||
const rootClientId = getBlockRootClientId( | ||
Array.isArray( clientIds ) ? clientIds[ 0 ] : clientIds | ||
); | ||
const canRemove = canRemoveBlocks( clientIds, rootClientId ); | ||
// Only handle single selected blocks for now. | ||
if ( blocks.length !== 1 || ! canRemove ) { | ||
return EMPTY_OBJECT; | ||
} | ||
const [ firstBlock ] = blocks; | ||
return { | ||
blockVariationTransformations: getBlockVariations( | ||
firstBlock.name, | ||
'transform' | ||
), | ||
activeBlockVariation: getActiveBlockVariation( | ||
firstBlock.name, | ||
getBlockAttributes( firstBlock.clientId ) | ||
), | ||
}; | ||
}, | ||
[ clientIds, blocks ] | ||
); | ||
const transformations = useMemo( () => { | ||
return blockVariationTransformations?.filter( | ||
( { name } ) => name !== activeBlockVariation?.name | ||
); | ||
}, [ blockVariationTransformations, activeBlockVariation ] ); | ||
return transformations; | ||
} | ||
|
||
const BlockVariationTransformations = ( { | ||
transformations, | ||
onSelect, | ||
blocks, | ||
} ) => { | ||
const [ hoveredTransformItemName, setHoveredTransformItemName ] = | ||
useState(); | ||
return ( | ||
<> | ||
{ hoveredTransformItemName && ( | ||
<PreviewBlockPopover | ||
blocks={ cloneBlock( | ||
blocks[ 0 ], | ||
transformations.find( | ||
( { name } ) => name === hoveredTransformItemName | ||
).attributes | ||
) } | ||
/> | ||
) } | ||
{ transformations?.map( ( item ) => ( | ||
<BlockVariationTranformationItem | ||
key={ item.name } | ||
item={ item } | ||
onSelect={ onSelect } | ||
setHoveredTransformItemName={ setHoveredTransformItemName } | ||
/> | ||
) ) } | ||
</> | ||
); | ||
}; | ||
|
||
function BlockVariationTranformationItem( { | ||
item, | ||
onSelect, | ||
setHoveredTransformItemName, | ||
} ) { | ||
const { name, icon, title } = item; | ||
return ( | ||
<MenuItem | ||
className={ getBlockMenuDefaultClassName( name ) } | ||
onClick={ ( event ) => { | ||
event.preventDefault(); | ||
onSelect( name ); | ||
} } | ||
onMouseLeave={ () => setHoveredTransformItemName( null ) } | ||
onMouseEnter={ () => setHoveredTransformItemName( name ) } | ||
> | ||
<BlockIcon icon={ icon } showColors /> | ||
{ title } | ||
</MenuItem> | ||
); | ||
} | ||
|
||
export default BlockVariationTransformations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); | ||
|
||
test.describe( 'Block Switcher', () => { | ||
test.beforeEach( async ( { admin } ) => { | ||
await admin.createNewPost(); | ||
} ); | ||
|
||
test( 'Block variation transforms', async ( { editor, page } ) => { | ||
// This is the `stack` Group variation. | ||
await editor.insertBlock( { | ||
name: 'core/group', | ||
attributes: { | ||
layout: { | ||
type: 'flex', | ||
orientation: 'vertical', | ||
}, | ||
}, | ||
innerBlocks: [ | ||
{ | ||
name: 'core/paragraph', | ||
attributes: { content: '1' }, | ||
}, | ||
], | ||
} ); | ||
// Transform to `Stack` variation. | ||
await editor.clickBlockToolbarButton( 'Stack' ); | ||
const variations = page | ||
.getByRole( 'menu', { name: 'Stack' } ) | ||
.getByRole( 'group' ); | ||
await expect( | ||
variations.getByRole( 'menuitem', { name: 'Stack' } ) | ||
).toBeHidden(); | ||
await variations.getByRole( 'menuitem', { name: 'Row' } ).click(); | ||
await expect.poll( editor.getBlocks ).toMatchObject( [ | ||
{ | ||
name: 'core/group', | ||
attributes: expect.objectContaining( { | ||
layout: { | ||
type: 'flex', | ||
flexWrap: 'nowrap', | ||
orientation: undefined, | ||
}, | ||
} ), | ||
innerBlocks: [ | ||
{ | ||
name: 'core/paragraph', | ||
attributes: { content: '1' }, | ||
}, | ||
], | ||
}, | ||
] ); | ||
await editor.clickBlockToolbarButton( 'Row' ); | ||
await expect( | ||
page.locator( 'role=menuitem[name="Stack"i]' ) | ||
).toBeVisible(); | ||
} ); | ||
} ); |