Skip to content

Commit

Permalink
use CreateSelector and declare WPBlockDisplayInformation typedef
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Dec 15, 2020
1 parent 64b7512 commit 324d6a7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
*/
import { useSelect } from '@wordpress/data';

/**
* @typedef {Object} BlockDisplayInformation Contains block's information for display reasons.
* @property {string} title Block's title or block variation match's title, if found.
* @property {JSX.Element} icon Block's icon or block variation match's icon, if found.
* @property {string} description Block's description or block variation match's description, if found.
*/
/** @typedef {import('@wordpress/blocks').WPBlockDisplayInformation} WPBlockDisplayInformation */

/**
* Hook used to try to find a matching block variation and return
Expand All @@ -21,7 +16,7 @@ import { useSelect } from '@wordpress/data';
* the returned information come from the Block Type.
*
* @param {string} clientId Block's client id.
* @return {BlockDisplayInformation} Block's display information.
* @return {WPBlockDisplayInformation} Block's display information.
*
*/

Expand Down
10 changes: 10 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ import { store as blocksStore } from '../store';
* then no preview is shown.
*/

/**
* A subset of `WPBlock` type. Contains basic block's information for display reasons.
*
* @typedef {Object} WPBlockDisplayInformation
*
* @property {string} title Human-readable block type label.
* @property {JSX.Element} icon Block type icon.
* @property {string} description A detailed block type description.
*/

/**
* Mapping of legacy category slugs to their latest normal values, used to
* accommodate updates of the default set of block categories.
Expand Down
54 changes: 33 additions & 21 deletions packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {

/** @typedef {import('../api/registration').WPBlockVariation} WPBlockVariation */
/** @typedef {import('../api/registration').WPBlockVariationScope} WPBlockVariationScope */
/** @typedef {import('../api/registration').WPBlockDisplayInformation} WPBlockDisplayInformation */
/** @typedef {import('./reducer').WPBlockCategory} WPBlockCategory */

/**
Expand Down Expand Up @@ -95,29 +96,40 @@ export function getBlockVariations( state, blockName, scope ) {
} );
}

// TODO check createSelector
// TODOjsdoc
/**
* Returns an array with the child blocks of a given block.
*
* @param {Object} state Data state.
* @param {string} name Block type name.
* @param {Object} attributes Block's attributes
*
* @return {WPBlockDisplayInformation} Block's display information.
*/
// TODO tests
// TODO check performance
export const getBlockDisplayInformation = ( state, name, attributes ) => {
const variations = state.blockVariations[ name ];
const blockType = getBlockType( state, name );
const blockTypeInfo = {
title: blockType.title,
icon: blockType.icon,
description: blockType.description,
};
if ( ! variations || ! blockType?.variationMatcher ) return blockTypeInfo;
const match = variations.find( ( variation ) =>
blockType.variationMatcher( attributes, variation )
);
if ( ! match ) return blockTypeInfo;
return {
title: match.title || blockType.title,
icon: match.icon || blockType.icon,
description: match.description || blockType.description,
};
};
export const getBlockDisplayInformation = createSelector(
( state, name, attributes ) => {
const variations = state.blockVariations[ name ];
const blockType = getBlockType( state, name );
const blockTypeInfo = {
title: blockType.title,
icon: blockType.icon,
description: blockType.description,
};
if ( ! variations || ! blockType?.variationMatcher )
return blockTypeInfo;
const match = variations.find( ( variation ) =>
blockType.variationMatcher( attributes, variation )
);
if ( ! match ) return blockTypeInfo;
return {
title: match.title || blockType.title,
icon: match.icon || blockType.icon,
description: match.description || blockType.description,
};
},
( state ) => [ state.blockTypes, state.blockVariations ]
);

/**
* Returns the default block variation for the given block type.
Expand Down

0 comments on commit 324d6a7

Please sign in to comment.