From 1a3973f5ba3dee515539195e13b5b3cd3afd46e1 Mon Sep 17 00:00:00 2001 From: karthick-murugan Date: Mon, 18 Nov 2024 16:08:25 +0530 Subject: [PATCH] Stablize default controls --- .../reference-guides/data/data-core-blocks.md | 2 +- packages/blocks/src/store/selectors.js | 34 +++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/docs/reference-guides/data/data-core-blocks.md b/docs/reference-guides/data/data-core-blocks.md index 084c9c1d7a5fbc..af68ec5144b9b0 100644 --- a/docs/reference-guides/data/data-core-blocks.md +++ b/docs/reference-guides/data/data-core-blocks.md @@ -123,7 +123,7 @@ _Parameters_ - _state_ `Object`: Data state. - _nameOrType_ `(string|Object)`: Block name or type object -- _feature_ `Array|string`: Feature to retrieve +- _features_ `Array|string`: Feature to retrieve - _defaultSupports_ `*`: Default value to return if not explicitly defined _Returns_ diff --git a/packages/blocks/src/store/selectors.js b/packages/blocks/src/store/selectors.js index 79e88073ba20de..adf6fb1b5aa6a7 100644 --- a/packages/blocks/src/store/selectors.js +++ b/packages/blocks/src/store/selectors.js @@ -597,7 +597,7 @@ export const getChildBlockNames = createSelector( * * @param {Object} state Data state. * @param {(string|Object)} nameOrType Block name or type object - * @param {Array|string} feature Feature to retrieve + * @param {Array|string} features Feature to retrieve * @param {*} defaultSupports Default value to return if not * explicitly defined * @@ -629,7 +629,7 @@ export const getChildBlockNames = createSelector( export const getBlockSupport = ( state, nameOrType, - feature, + features, defaultSupports ) => { const blockType = getNormalizedBlockType( state, nameOrType ); @@ -637,11 +637,31 @@ export const getBlockSupport = ( return defaultSupports; } - return getValueFromObjectPath( - blockType.supports, - feature, - defaultSupports - ); + const featuresArray = Array.isArray( features ) ? features : [ features ]; + + let supportValue; + for ( const feature of featuresArray ) { + // Check for the feature directly + supportValue = getValueFromObjectPath( blockType.supports, feature ); + + // Check for `defaultControls` if the feature has it + if ( supportValue?.defaultControls ) { + return supportValue.defaultControls; + } + + // Check for `__experimentalDefaultControls` if `defaultControls` is not found + if ( supportValue?.__experimentalDefaultControls ) { + return supportValue.__experimentalDefaultControls; + } + + // If a value is found, return it + if ( supportValue !== undefined ) { + return supportValue; + } + } + + // Return the default value if none of the features are found + return defaultSupports; }; /**