Skip to content

Commit

Permalink
useEditorFeature: take block context into account (#24416)
Browse files Browse the repository at this point in the history
* Take editor features data from blocks as well

* Make linter happy
  • Loading branch information
nosolosw authored Aug 31, 2020
1 parent be09692 commit 379719f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
23 changes: 14 additions & 9 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,22 +596,27 @@ function gutenberg_experimental_global_styles_enqueue_assets() {
* @return array Default features config for the editor.
*/
function gutenberg_experimental_global_styles_get_editor_features( $config ) {
if (
empty( $config['global']['features'] ) ||
! is_array( $config['global']['features'] )
) {
$config['global']['features'] = array();
$features = array();
foreach ( array_keys( $config ) as $context ) {
if (
empty( $config[ $context ]['features'] ) ||
! is_array( $config[ $context ]['features'] )
) {
$features[ $context ] = array();
} else {
$features[ $context ] = $config[ $context ]['features'];
}
}

// Deprecated theme supports.
if ( get_theme_support( 'disable-custom-colors' ) ) {
if ( ! isset( $config['global']['features']['color'] ) ) {
$config['global']['features']['color'] = array();
if ( ! isset( $features['global']['color'] ) ) {
$features['global']['color'] = array();
}
$config['global']['features']['color']['custom'] = false;
$features['global']['color']['custom'] = false;
}

return $config['global']['features'];
return $features;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions packages/block-editor/src/components/use-editor-feature/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export default function useEditorFeature( featurePath ) {

const setting = useSelect(
( select ) => {
const path = `__experimentalFeatures.${ featurePath }`;

// 1 - Use deprecated settings, if available.
const settings = select( 'core/block-editor' ).getSettings();
const deprecatedSettingsValue = deprecatedFlags[ featurePath ]
Expand All @@ -49,8 +47,14 @@ export default function useEditorFeature( featurePath ) {
return deprecatedSettingsValue;
}

// 2 - Use global __experimentalFeatures otherwise.
return get( settings, path );
// 2 - Use __experimental features otherwise.
// We cascade to the global value if the block one is not available.
//
// TODO: make it work for blocks that define multiple selectors
// such as core/heading or core/post-title.
const globalPath = `__experimentalFeatures.global.${ featurePath }`;
const blockPath = `__experimentalFeatures.${ blockName }.${ featurePath }`;
return get( settings, blockPath ) ?? get( settings, globalPath );
},
[ blockName, featurePath ]
);
Expand Down

0 comments on commit 379719f

Please sign in to comment.