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: Add useEditorFeature hook to simplify access to editor features #21646

Merged
merged 2 commits into from
Apr 24, 2020

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Apr 16, 2020

Description

Implements the ideas shared in https://make.wordpress.org/core/2020/01/23/controlling-the-block-editor/.

Part of #20588.

Supersedes: #20623.
Related to: #21490, #20290.

Work in progress, all APIs are experimental.

This PR introduced a new experimental setting for drop cap: __experimentalDisableDropCap. It follows conventions used for other settings that were added recently like:

  • __experimentalDisableCustomLineHeight
  • __experimentalDisableCustomUnits

It also replaces existing hooks used for the listed settings with the new useEditorFeature hook.

For the initial pass, the implementation of useEditorFeature is a tiny wrapper over the getSettings selector.

The first next step would be to add a way to define block specific overrides and tackle it internally inside the hook based on the detected BlockEditContext.

There is also this general challenge about creating a good naming schema for all the features we plan to support. We also should seek ways to integrate it with Global Styles and theme.json config.

How has this been tested?

Set the setting for __experimentalDisableDropCap to true on the server and make sure that there is no drop cap control displayed in the sidebar for Paragraph block.

<?php

function gutenberg_override_settings_drop_cap( $settings ) {
	$settings['__experimentalDisableDropCap'] = true;
	return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_override_settings_drop_cap' );

I also tested that the drop cap feature when enabled still works as before. The same applies to custom line hight and custom units.

Types of changes

New feature (non-breaking change which adds functionality).

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@gziolo gziolo self-assigned this Apr 16, 2020
@gziolo gziolo requested a review from youknowriad April 16, 2020 12:09
@gziolo gziolo added [Package] Block editor /packages/block-editor [Block] Paragraph Affects the Paragraph Block labels Apr 16, 2020
@gziolo gziolo requested a review from ItsJonQ April 16, 2020 12:10
@github-actions
Copy link

github-actions bot commented Apr 16, 2020

Size Change: +79 B (0%)

Total Size: 845 kB

Filename Size Change
build/block-editor/index.js 105 kB +40 B (0%)
build/block-library/index.js 113 kB +37 B (0%)
build/editor/index.js 43.4 kB +2 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/api-fetch/index.js 4.08 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.24 kB 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 761 B 0 B
build/block-editor/style-rtl.css 10.1 kB 0 B
build/block-editor/style.css 10.1 kB 0 B
build/block-library/editor-rtl.css 7.13 kB 0 B
build/block-library/editor.css 7.13 kB 0 B
build/block-library/style-rtl.css 7.19 kB 0 B
build/block-library/style.css 7.19 kB 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 57.7 kB 0 B
build/components/index.js 198 kB 0 B
build/components/style-rtl.css 16.9 kB 0 B
build/components/style.css 16.9 kB 0 B
build/compose/index.js 6.66 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.43 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 569 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/index.js 3.54 kB 0 B
build/edit-navigation/style-rtl.css 485 B 0 B
build/edit-navigation/style.css 485 B 0 B
build/edit-post/index.js 28.2 kB 0 B
build/edit-post/style-rtl.css 12.5 kB 0 B
build/edit-post/style.css 12.5 kB 0 B
build/edit-site/index.js 10.8 kB 0 B
build/edit-site/style-rtl.css 5.25 kB 0 B
build/edit-site/style.css 5.24 kB 0 B
build/edit-widgets/index.js 8.33 kB 0 B
build/edit-widgets/style-rtl.css 5 kB 0 B
build/edit-widgets/style.css 5 kB 0 B
build/editor/editor-styles-rtl.css 428 B 0 B
build/editor/editor-styles.css 431 B 0 B
build/editor/style-rtl.css 3.27 kB 0 B
build/editor/style.css 3.27 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.32 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 711 B 0 B
build/keyboard-shortcuts/index.js 2.51 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.12 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 5.29 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.67 kB 0 B
build/primitives/index.js 1.49 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.84 kB 0 B
build/rich-text/index.js 14.8 kB 0 B
build/server-side-render/index.js 2.67 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@gziolo gziolo added the [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible label Apr 16, 2020
const isDisabled = useIsLineHeightControlsDisabled();
const isDisabled = useEditorFeature(
'__experimentalDisableCustomLineHeight'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the API a lot, and the fact that we can implement any heuristics to get the value of that config in the hook without changing how the API is used.

I'm not sure yet about the naming. useEditorFeature, useEditorSetting, useEditorConfig ? How will it scale to nested configs like typography, colors? Why is __experimentalDisableCustomLineHeight a top level config and not nested under "typography" (same for drop cap).

cc @mtias @aduth @mcsf and others.

Copy link

Choose a reason for hiding this comment

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

I like the API a lot, and the fact that we can implement any heuristics to get the value of that config in the hook without changing how the API is used.

🤘

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure yet about the naming. useEditorFeature, useEditorSetting, useEditorConfig ?

If the thing we're referring to here is the same thing we assign via <Editor settings={ ... } />, or block_editor_settings filter, I'd very much like to see "setting" or "settings" as part of the name.

How will it scale to nested configs like typography, colors? Why is __experimentalDisableCustomLineHeight a top level config and not nested under "typography" (same for drop cap).

Do we have nested settings? Can we not? 😛

Copy link
Member Author

Choose a reason for hiding this comment

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

If we fly with useSetting then we need those nested groups to make it all easier. We discussed groups like typography, colors so far. I'm sure we need more. The only question is whether we name it feature, setting, support or whatever :)

Copy link
Contributor

Choose a reason for hiding this comment

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

for context, this hook is supposed to get its values from the "config" property in the proposed format in the end. #21583 (comment)

This config is probably going to end up in the block-editor getSettings selector somehow, I haven't give it much though yet about how, whether it's just 1 - 1 or if there's some intermediate mapping that happens.

I don't think the block-editor settings and the "config" of a block-editor are the exact same things, block-editor can have more settings and themes can have more "config" though.

Copy link
Member Author

@gziolo gziolo Apr 17, 2020

Choose a reason for hiding this comment

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

It feels like all we need is proper namespacing to make it easy to inject all settings from the theme.json config. Wherever we have duplication, we can find a nice way to migrate it behind the scenes before sending it to the front.

To give an example, the following would still work on PHP side:

$settings = array(
    'disableCustomColors'    => get_theme_support( 'disable-custom-colors' ),
    'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),		
);

You could also override it with theme.json:

{ 
    "config": {
        "colors": {
            "disableCustomColors": false
        },
        "typography": {
            "disableCustomFontSizes": false
        }
    }
}

The challenge would be to pick what takes precedence, theme.json or get_theme_support here, or the top-level disableCustomFontSizes vs typography. disableCustomFontSizes. Before sending it to the client, we would remove duplicates from the top-level using some conflict resolution strategy.

On the client, we need to keep old settings for backward compatibility but I would solve it by doing internal mapping in the getSettings selector:

const mappings = {
    disableCustomColors: 'colors.disableCustomColors',
    disableCustomFontSizes: 'typography.disableCustomFontSizes',
};

I hope I didn't miss anything.

lib/compat.php Outdated Show resolved Hide resolved
@gziolo gziolo marked this pull request as ready for review April 17, 2020 08:11
@gziolo
Copy link
Member Author

gziolo commented Apr 17, 2020

We just need to settle on the name of the hook and it should be ready to go. I picked __experimentalUseEditorFeature but it might be less confusing if we name it __experimentalUseBlockSetting to indicate that it can have per-block overrides but is sourced from settings.

Aside: should we expose those settings from the server using REST API endpoint to make it possible to use it with standalone clients like the mobile app?

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Let's keep this moving, let's merge this first step as it's experimental, and we try to scale it on follow-up PRs.

@gziolo gziolo removed the [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible label Apr 24, 2020
@gziolo gziolo merged commit f46c7fc into master Apr 24, 2020
@gziolo gziolo deleted the add/use-feature branch April 24, 2020 10:42
@github-actions github-actions bot added this to the Gutenberg 8.0 milestone Apr 24, 2020
@ellatrix ellatrix mentioned this pull request Jun 16, 2020
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Paragraph Affects the Paragraph Block [Package] Block editor /packages/block-editor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants