Skip to content

Commit

Permalink
Refactor code to use "supports" from blocks for overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed May 20, 2020
1 parent a31af8a commit be46697
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 48 deletions.
9 changes: 0 additions & 9 deletions lib/experimental-default-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,5 @@
"typography": {
"dropCap": false
}
},
"blocks": {
"core/paragraph": {
"features": {
"typography": {
"dropCap": true
}
}
}
}
}
10 changes: 2 additions & 8 deletions lib/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
40 changes: 15 additions & 25 deletions packages/block-editor/src/components/use-editor-feature/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, has } from 'lodash';
import { get } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) {
Expand Down
6 changes: 5 additions & 1 deletion packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
};

/**
Expand Down
54 changes: 53 additions & 1 deletion packages/blocks/src/store/test/selectors.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,73 @@
/**
* External dependencies
*/
import { omit } from 'lodash';
import { keyBy, omit } from 'lodash';

import deepFreeze from 'deep-freeze';

/**
* Internal dependencies
*/
import {
getBlockSupport,
getChildBlockNames,
getDefaultBlockVariation,
getGroupingBlockName,
isMatchingSearchTerm,
} 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 = {};
Expand Down
5 changes: 2 additions & 3 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit be46697

Please sign in to comment.