Skip to content

Commit

Permalink
getBlockTransformItems: Support single block object (#40718)
Browse files Browse the repository at this point in the history
* getBlockTransformItems: Support single block object
* Update navigation link
* Add unit tests
  • Loading branch information
Mamaduka committed Apr 29, 2022
1 parent fae087b commit b186cb5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ Items are returned ordered descendingly by their 'frecency'.
_Parameters_

- _state_ `Object`: Editor state.
- _blocks_ `Object|Object[]`: Block object or array objects.
- _rootClientId_ `?string`: Optional root client ID of block list.

_Returns_
Expand Down
26 changes: 14 additions & 12 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2050,23 +2050,25 @@ export const getInserterItems = createSelector(
*
* Items are returned ordered descendingly by their 'frecency'.
*
* @param {Object} state Editor state.
* @param {?string} rootClientId Optional root client ID of block list.
* @param {Object} state Editor state.
* @param {Object|Object[]} blocks Block object or array objects.
* @param {?string} rootClientId Optional root client ID of block list.
*
* @return {WPEditorTransformItem[]} Items that appear in inserter.
*
* @typedef {Object} WPEditorTransformItem
* @property {string} id Unique identifier for the item.
* @property {string} name The type of block to create.
* @property {string} title Title of the item, as it appears in the inserter.
* @property {string} icon Dashicon for the item, as it appears in the inserter.
* @property {boolean} isDisabled Whether or not the user should be prevented from inserting
* this item.
* @property {number} frecency Heuristic that combines frequency and recency.
* @property {string} id Unique identifier for the item.
* @property {string} name The type of block to create.
* @property {string} title Title of the item, as it appears in the inserter.
* @property {string} icon Dashicon for the item, as it appears in the inserter.
* @property {boolean} isDisabled Whether or not the user should be prevented from inserting
* this item.
* @property {number} frecency Heuristic that combines frequency and recency.
*/
export const getBlockTransformItems = createSelector(
( state, blocks, rootClientId = null ) => {
const [ sourceBlock ] = blocks;
const normalizedBlocks = castArray( blocks );
const [ sourceBlock ] = normalizedBlocks;
const buildBlockTypeTransformItem = buildBlockTypeItem( state, {
buildScope: 'transform',
} );
Expand All @@ -2088,11 +2090,11 @@ export const getBlockTransformItems = createSelector(
isDisabled: false,
name: '*',
title: __( 'Unwrap' ),
icon: itemsByName[ sourceBlock.name ]?.icon,
icon: itemsByName[ sourceBlock?.name ]?.icon,
};

const possibleTransforms = getPossibleBlockTransformations(
blocks
normalizedBlocks
).reduce( ( accumulator, block ) => {
if ( block === '*' ) {
accumulator.push( itemsByName[ '*' ] );
Expand Down
17 changes: 17 additions & 0 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3093,6 +3093,23 @@ describe( 'selectors', () => {
] )
);
} );
it( 'should support single block object', () => {
const state = {
blocks: {
byClientId: {},
attributes: {},
order: {},
parents: {},
cache: {},
},
settings: {},
preferences: {},
blockListSettings: {},
};
const block = { name: 'core/with-tranforms-a' };
const items = getBlockTransformItems( state, block );
expect( items ).toHaveLength( 2 );
} );
it( 'should return only eligible blocks for transformation - `allowedBlocks`', () => {
const state = {
blocks: {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ function LinkControlTransforms( { clientId, replace } ) {
return {
getBlock: _getBlock,
blockTransforms: getBlockTransformItems(
[ _getBlock( clientId ) ],
_getBlock( clientId ),
getBlockRootClientId( clientId )
),
};
Expand Down

0 comments on commit b186cb5

Please sign in to comment.