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

useEditorFeature: take block context into account #24416

Merged
merged 2 commits into from
Aug 31, 2020
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
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