diff --git a/lib/experimental-default-theme.json b/lib/experimental-default-theme.json index 6a32720a417bce..a11c0e15fc7ddd 100644 --- a/lib/experimental-default-theme.json +++ b/lib/experimental-default-theme.json @@ -10,14 +10,5 @@ "typography": { "dropCap": false } - }, - "blocks": { - "core/paragraph": { - "features": { - "typography": { - "dropCap": true - } - } - } } } diff --git a/lib/theme.php b/lib/theme.php index 9801367bf4e3c7..564c92df2e7e5f 100644 --- a/lib/theme.php +++ b/lib/theme.php @@ -43,17 +43,11 @@ function gutenberg_experimental_get_theme_config( $config_path, $section_name = * @return array Filtered editor settings. */ function gutenberg_extend_settings_features( $settings ) { - $theme_features = gutenberg_experimental_get_theme_config( + $theme_features = gutenberg_experimental_get_theme_config( __DIR__ . '/experimental-default-theme.json', 'features' ); - $settings['__experimentalFeaturesConfig'] = $theme_features; - - $theme_blocks = gutenberg_experimental_get_theme_config( - __DIR__ . '/experimental-default-theme.json', - 'blocks' - ); - $settings['__experimentalBlocksConfig'] = $theme_blocks; + $settings['__experimentalFeatures'] = $theme_features; return $settings; } diff --git a/packages/block-editor/src/components/use-editor-feature/index.js b/packages/block-editor/src/components/use-editor-feature/index.js index 33843400125d19..86c71975e6ed64 100644 --- a/packages/block-editor/src/components/use-editor-feature/index.js +++ b/packages/block-editor/src/components/use-editor-feature/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { get, has } from 'lodash'; +import { get } from 'lodash'; /** * WordPress dependencies @@ -17,45 +17,35 @@ import { useBlockEditContext } from '../block-edit'; * Hook that retrieves the setting for the given editor feature. * It works with nested objects using by finding the value at path. * - * @param {string} featurePath The path to the feature. + * @param {string} featurePath The path to the feature. + * @param {*} defaultValue Default value to return if not + * explicitly defined. * * @return {any} Returns the value defined for the setting. * * @example * ```js - * const isEnabled = useEditorFeature( 'typography.dropCap' ); + * const isEnabled = useEditorFeature( 'typography.dropCap', false ); * ``` */ -export default function useEditorFeature( featurePath ) { +export default function useEditorFeature( featurePath, defaultValue ) { const { name: blockName } = useBlockEditContext(); + const path = `__experimentalFeatures.${ featurePath }`; const setting = useSelect( ( select ) => { - const { getSettings } = select( 'core/block-editor' ); + const { getBlockSupport } = select( 'core/blocks' ); - const path = featurePath.split( '.' ); - if ( - has( getSettings(), [ - '__experimentalBlocksConfig', - blockName, - 'features', - ...path, - ] ) - ) { - return get( getSettings(), [ - '__experimentalBlocksConfig', - blockName, - 'features', - ...path, - ] ); + const blockSupportValue = getBlockSupport( blockName, path ); + if ( blockSupportValue !== undefined ) { + return blockSupportValue; } - return get( getSettings(), [ - '__experimentalFeaturesConfig', - ...path, - ] ); + const { getSettings } = select( 'core/block-editor' ); + + return get( getSettings(), path, defaultValue ); }, - [ blockName, featurePath ] + [ blockName, path ] ); return setting; diff --git a/packages/block-library/src/paragraph/edit.js b/packages/block-library/src/paragraph/edit.js index 2f9f60cd1390b5..0b00fa9bc37cc1 100644 --- a/packages/block-library/src/paragraph/edit.js +++ b/packages/block-library/src/paragraph/edit.js @@ -57,7 +57,7 @@ function ParagraphRTLToolbar( { direction, setDirection } ) { } function useDropCap( isDropCap, fontSize, styleFontSize ) { - const isDisabled = ! useEditorFeature( 'typography.dropCap' ); + const isDisabled = ! useEditorFeature( 'typography.dropCap', false ); const [ minimumHeight, setMinimumHeight ] = useState(); diff --git a/packages/block-library/src/paragraph/index.js b/packages/block-library/src/paragraph/index.js index e1711a67de97d2..127e7e9c9b9ea0 100644 --- a/packages/block-library/src/paragraph/index.js +++ b/packages/block-library/src/paragraph/index.js @@ -48,6 +48,11 @@ export const settings = { __experimentalColor: Platform.OS === 'web', __experimentalLineHeight: true, __experimentalFontSize: true, + __experimentalFeatures: { + typography: { + dropCap: true, + }, + }, }, __experimentalLabel( attributes, { context } ) { if ( context === 'accessibility' ) { diff --git a/packages/blocks/src/store/selectors.js b/packages/blocks/src/store/selectors.js index c896128834429d..a0a2d02bdd3838 100644 --- a/packages/blocks/src/store/selectors.js +++ b/packages/blocks/src/store/selectors.js @@ -217,7 +217,11 @@ export const getBlockSupport = ( ) => { const blockType = getNormalizedBlockType( state, nameOrType ); - return get( blockType, [ 'supports', feature ], defaultSupports ); + return get( + blockType, + [ 'supports', ...feature.split( '.' ) ], + defaultSupports + ); }; /** diff --git a/packages/blocks/src/store/test/selectors.js b/packages/blocks/src/store/test/selectors.js index 636228f7835acb..daea899178fdf0 100644 --- a/packages/blocks/src/store/test/selectors.js +++ b/packages/blocks/src/store/test/selectors.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { omit } from 'lodash'; +import { keyBy, omit } from 'lodash'; import deepFreeze from 'deep-freeze'; @@ -9,6 +9,7 @@ import deepFreeze from 'deep-freeze'; * Internal dependencies */ import { + getBlockSupport, getChildBlockNames, getDefaultBlockVariation, getGroupingBlockName, @@ -16,6 +17,57 @@ import { } from '../selectors'; describe( 'selectors', () => { + describe( 'getBlockSupport', () => { + const blockName = 'block/name'; + const getState = ( blocks ) => { + return deepFreeze( { + blockTypes: keyBy( blocks, 'name' ), + } ); + }; + + it( 'returns default value when config entry not found', () => { + const state = getState( [] ); + + expect( + getBlockSupport( state, blockName, 'unknown', 'default' ) + ).toBe( 'default' ); + } ); + + it( 'returns value when config found but falsy', () => { + const state = getState( [ + { + name: blockName, + supports: { + falsy: '', + }, + }, + ] ); + + expect( + getBlockSupport( state, blockName, 'falsy', 'default' ) + ).toBe( '' ); + } ); + + it( 'works with configs stored as nested objects', () => { + const state = getState( [ + { + name: blockName, + supports: { + features: { + foo: { + bar: 'value', + }, + }, + }, + }, + ] ); + + expect( + getBlockSupport( state, blockName, 'features.foo.bar' ) + ).toBe( 'value' ); + } ); + } ); + describe( 'getChildBlockNames', () => { it( 'should return an empty array if state is empty', () => { const state = {}; diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index 6b62e6aa5c5c9e..074f93fe2ae1ff 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -113,14 +113,13 @@ class EditorProvider extends Component { ...pick( settings, [ '__experimentalBlockDirectory', '__experimentalBlockPatterns', - '__experimentalBlocksConfig', + '__experimentalBlockPatternCategories', '__experimentalDisableCustomUnits', '__experimentalDisableCustomLineHeight', '__experimentalEnableLegacyWidgetBlock', '__experimentalEnableFullSiteEditing', '__experimentalEnableFullSiteEditingDemo', - '__experimentalFeaturesConfig', - '__experimentalEnableFullSiteEditingDemo', + '__experimentalFeatures', '__experimentalGlobalStylesUserEntityId', '__experimentalGlobalStylesBase', '__experimentalPreferredStyleVariations',