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

Block Editor: Allow control over drop cap feature with useEditorFeature helper #22291

Merged
merged 2 commits into from
May 27, 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
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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you suggested, I'm thinking that we can move the theme.json (both core and theme) to PHP. This also solves one potential code path in which we return the empty config without any values. We can do this in a follow-up as we also have to do it for global styles.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's take care of it separately. I'm still unsure whether this JSON file shouldn't be used as a default for @wordpress/block-editor settings object:

https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/store/defaults.js

There is also this question whether should I put some defaults there as well for features 😅

if ( ! file_exists( $config_path ) ) {
return $empty_config;
}

$theme_config = json_decode(
@file_get_contents( $config_path ),
gziolo marked this conversation as resolved.
Show resolved Hide resolved
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": {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
"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' );
gziolo marked this conversation as resolved.
Show resolved Hide resolved

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