Skip to content

Commit

Permalink
Stablize default controls
Browse files Browse the repository at this point in the history
  • Loading branch information
karthick-murugan committed Nov 18, 2024
1 parent 49401da commit 1a3973f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/data/data-core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
34 changes: 27 additions & 7 deletions packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -629,19 +629,39 @@ export const getChildBlockNames = createSelector(
export const getBlockSupport = (
state,
nameOrType,
feature,
features,
defaultSupports
) => {
const blockType = getNormalizedBlockType( state, nameOrType );
if ( ! blockType?.supports ) {
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;
};

/**
Expand Down

0 comments on commit 1a3973f

Please sign in to comment.