Skip to content

Commit

Permalink
Block Editor: Allow control over drop cap feature with `useEditorFeat…
Browse files Browse the repository at this point in the history
…ure` helper (#22291)

* Block Editor: Integrate theme config with useEditorFeature

* Remove the default value param from useEditorFeature
  • Loading branch information
gziolo committed May 27, 2020
1 parent 55b8f7e commit c31475a
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 24 deletions.
14 changes: 0 additions & 14 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,6 @@ function register_block_type_from_metadata( $path, $args = array() ) {
}
}

/**
* Extends block editor settings to determine whether to use drop cap feature.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_extend_settings_drop_cap( $settings ) {
$settings['__experimentalDisableDropCap'] = false;
return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_extend_settings_drop_cap' );


/**
* Extends block editor settings to include a list of image dimensions per size.
*
Expand Down
49 changes: 49 additions & 0 deletions lib/editor-features.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Experimental editor features config functionality.
*
* @package gutenberg
*/

/**
* Returns the default config for editor features,
* or an empty array if none found.
*
* @return array Default features config for the editor.
*/
function gutenberg_experimental_get_editor_features_config() {
$empty_config = array();
$config_path = __DIR__ . '/experimental-default-theme.json';
if ( ! file_exists( $config_path ) ) {
return $empty_config;
}

$theme_config = json_decode(
@file_get_contents( $config_path ),
true
);
if (
empty( $theme_config['global']['features'] ) ||
! is_array( $theme_config['global']['features'] )
) {
return $empty_config;
}

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


/**
* Extends editor settings with the features loaded from default config.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_extend_settings_editor_features( $settings ) {
$editor_features = gutenberg_experimental_get_editor_features_config();
$settings['__experimentalFeatures'] = $editor_features;

return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_extend_settings_editor_features' );
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
"value": "linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)"
}
]
},
"features": {
"typography": {
"dropCap": false
}
}
}
}
2 changes: 1 addition & 1 deletion lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function gutenberg_experimental_global_styles_get_user_cpt_id() {
*/
function gutenberg_experimental_global_styles_get_core() {
$config = gutenberg_experimental_global_styles_get_from_file(
dirname( dirname( __FILE__ ) ) . '/experimental-default-global-styles.json'
__DIR__ . '/experimental-default-theme.json'
);

return $config;
Expand Down
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ function gutenberg_is_experiment_enabled( $name ) {
require dirname( __FILE__ ) . '/experiments-page.php';
require dirname( __FILE__ ) . '/customizer.php';
require dirname( __FILE__ ) . '/edit-site-page.php';
require dirname( __FILE__ ) . '/editor-features.php';
require dirname( __FILE__ ) . '/global-styles.php';
34 changes: 30 additions & 4 deletions packages/block-editor/src/components/use-editor-feature/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
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} featureName The name of the feature.
* @param {string} featurePath The path to the feature.
*
* @return {any} Returns the value defined for the setting.
*
* @example
* ```js
* const isEnabled = useEditorFeature( 'typography.dropCap' );
* ```
*/
export default function useEditorFeature( featureName ) {
export default function useEditorFeature( featurePath ) {
const { name: blockName } = useBlockEditContext();
const path = `__experimentalFeatures.${ featurePath }`;

const setting = useSelect(
( select ) => {
const { getBlockSupport } = select( 'core/blocks' );

const blockSupportValue = getBlockSupport( blockName, path );
if ( blockSupportValue !== undefined ) {
return blockSupportValue;
}

const { getSettings } = select( 'core/block-editor' );

return getSettings()[ featureName ];
return get( getSettings(), path );
},
[ featureName ]
[ blockName, path ]
);

return setting;
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/paragraph/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"lightBlockWrapper": true,
"__experimentalColor": true,
"__experimentalLineHeight": true,
"__experimentalFontSize": true
"__experimentalFontSize": true,
"__experimentalFeatures": {
"typography": {
"dropCap": true
}
}
}
}
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( '__experimentalDisableDropCap' );
const isDisabled = ! useEditorFeature( 'typography.dropCap' );

const [ minimumHeight, setMinimumHeight ] = useState();

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
2 changes: 1 addition & 1 deletion packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ class EditorProvider extends Component {
'__experimentalBlockPatternCategories',
'__experimentalDisableCustomUnits',
'__experimentalDisableCustomLineHeight',
'__experimentalDisableDropCap',
'__experimentalEnableLegacyWidgetBlock',
'__experimentalEnableFullSiteEditing',
'__experimentalEnableFullSiteEditingDemo',
'__experimentalFeatures',
'__experimentalGlobalStylesUserEntityId',
'__experimentalGlobalStylesBase',
'__experimentalPreferredStyleVariations',
Expand Down

0 comments on commit c31475a

Please sign in to comment.