Skip to content

Commit b186cb5

Browse files
authored
getBlockTransformItems: Support single block object (#40718)
* getBlockTransformItems: Support single block object * Update navigation link * Add unit tests
1 parent fae087b commit b186cb5

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

docs/reference-guides/data/data-core-block-editor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ Items are returned ordered descendingly by their 'frecency'.
428428
_Parameters_
429429

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

433434
_Returns_

packages/block-editor/src/store/selectors.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,23 +2050,25 @@ export const getInserterItems = createSelector(
20502050
*
20512051
* Items are returned ordered descendingly by their 'frecency'.
20522052
*
2053-
* @param {Object} state Editor state.
2054-
* @param {?string} rootClientId Optional root client ID of block list.
2053+
* @param {Object} state Editor state.
2054+
* @param {Object|Object[]} blocks Block object or array objects.
2055+
* @param {?string} rootClientId Optional root client ID of block list.
20552056
*
20562057
* @return {WPEditorTransformItem[]} Items that appear in inserter.
20572058
*
20582059
* @typedef {Object} WPEditorTransformItem
2059-
* @property {string} id Unique identifier for the item.
2060-
* @property {string} name The type of block to create.
2061-
* @property {string} title Title of the item, as it appears in the inserter.
2062-
* @property {string} icon Dashicon for the item, as it appears in the inserter.
2063-
* @property {boolean} isDisabled Whether or not the user should be prevented from inserting
2064-
* this item.
2065-
* @property {number} frecency Heuristic that combines frequency and recency.
2060+
* @property {string} id Unique identifier for the item.
2061+
* @property {string} name The type of block to create.
2062+
* @property {string} title Title of the item, as it appears in the inserter.
2063+
* @property {string} icon Dashicon for the item, as it appears in the inserter.
2064+
* @property {boolean} isDisabled Whether or not the user should be prevented from inserting
2065+
* this item.
2066+
* @property {number} frecency Heuristic that combines frequency and recency.
20662067
*/
20672068
export const getBlockTransformItems = createSelector(
20682069
( state, blocks, rootClientId = null ) => {
2069-
const [ sourceBlock ] = blocks;
2070+
const normalizedBlocks = castArray( blocks );
2071+
const [ sourceBlock ] = normalizedBlocks;
20702072
const buildBlockTypeTransformItem = buildBlockTypeItem( state, {
20712073
buildScope: 'transform',
20722074
} );
@@ -2088,11 +2090,11 @@ export const getBlockTransformItems = createSelector(
20882090
isDisabled: false,
20892091
name: '*',
20902092
title: __( 'Unwrap' ),
2091-
icon: itemsByName[ sourceBlock.name ]?.icon,
2093+
icon: itemsByName[ sourceBlock?.name ]?.icon,
20922094
};
20932095

20942096
const possibleTransforms = getPossibleBlockTransformations(
2095-
blocks
2097+
normalizedBlocks
20962098
).reduce( ( accumulator, block ) => {
20972099
if ( block === '*' ) {
20982100
accumulator.push( itemsByName[ '*' ] );

packages/block-editor/src/store/test/selectors.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,6 +3093,23 @@ describe( 'selectors', () => {
30933093
] )
30943094
);
30953095
} );
3096+
it( 'should support single block object', () => {
3097+
const state = {
3098+
blocks: {
3099+
byClientId: {},
3100+
attributes: {},
3101+
order: {},
3102+
parents: {},
3103+
cache: {},
3104+
},
3105+
settings: {},
3106+
preferences: {},
3107+
blockListSettings: {},
3108+
};
3109+
const block = { name: 'core/with-tranforms-a' };
3110+
const items = getBlockTransformItems( state, block );
3111+
expect( items ).toHaveLength( 2 );
3112+
} );
30963113
it( 'should return only eligible blocks for transformation - `allowedBlocks`', () => {
30973114
const state = {
30983115
blocks: {

packages/block-library/src/navigation-link/edit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ function LinkControlTransforms( { clientId, replace } ) {
354354
return {
355355
getBlock: _getBlock,
356356
blockTransforms: getBlockTransformItems(
357-
[ _getBlock( clientId ) ],
357+
_getBlock( clientId ),
358358
getBlockRootClientId( clientId )
359359
),
360360
};

0 commit comments

Comments
 (0)