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

getBlockTransformItems: Support single block object #40718

Merged
merged 3 commits into from
Apr 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
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