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

Allow to identify styles that are added to the editor settings #1326

Closed
wants to merge 4 commits into from
Closed

Allow to identify styles that are added to the editor settings #1326

wants to merge 4 commits into from

Conversation

oandregal
Copy link
Member

@oandregal oandregal commented Jun 1, 2021

Part of https://core.trac.wordpress.org/ticket/53175

Global settings are filterable by hooking into the block_editor_settings filter. Global styles in the front end are filterable as well by getting the global-styles style handle (see). However, there's no mechanism to filter the global styles that come with the editor.

This PR allows third-party to identify the styles that are going to be sent to the editor, so they can filter them.

In looking at making it general, this has been added to all the styles we add to the settings.

How to test

  • Using WordPress 5.8, activate a theme with theme.json support.
    • Alternatively, using the TwentyTwentyOne theme, create a file named theme.json in the top-level folder of the theme and paste the following contents {"version": 1}.
  • Load the post editor. Open the devtools and in the inspector search for --wp--preset--color.
  • The expected result is that there are two occurrences: the wp-edit-post-js-after script and within a <style> tag in the body of the document, just before the editor-styles-wrapper node.

Now, install and activate the Gutenberg plugin from the plugin repository and do the same. What happens is that there will be three occurrences, the wp-edit-post-js-after embedded script and two identical <style> tags. With this PR there's only one <style> tag added.

Changes introduced

It adds a __unstableType key to the styles we add to the editor settings, so they can be inspected by others. In Gutenberg, we'll take advantage of that (as in WordPress/gutenberg#32377) to remove the global styles that come with WordPress core.

Alternatives I've considered

I haven't seen many options to do something like this. A similar approach could be to add some sort of metadata in the first lines of the stylesheet instead. While feasible it seems more error-prone than this.

@oandregal oandregal changed the title Allow to filter global styles in the editor Allow to identify styles that are added to the editor settings Jun 2, 2021
@oandregal
Copy link
Member Author

Pushed a change to make this general and use a __unstableType that is added to styles that come from core (__unstableType = core), the theme (__unstableType = theme), or global styles (__unstableType = globalStyles).

Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

Left some questions/food for thought but the direction seems good 👍

$editor_settings['styles'][] = array(
'css' => $theme_json->get_stylesheet( 'css_variables' ),
'__experimentalNoWrapper' => true,
'__unstableType' => 'globalStyles',
Copy link
Member

Choose a reason for hiding this comment

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

"Global styles" has several origins it could be core, theme, and user. Do you think it would make sense/would be useful to also have origin indicators for global styles? It would be complex given how our theme.json merge works.

Copy link
Member Author

Choose a reason for hiding this comment

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

I considered it, but the concept of "origin" doesn't match well this use case: globalStyles is the result of merging three different sources of data.

'baseURL' => get_theme_file_uri( $style ),
'css' => file_get_contents( $file ),
'baseURL' => get_theme_file_uri( $style ),
'__unstableType' => 'theme',
Copy link
Member

Choose a reason for hiding this comment

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

Previously when working on presets we used the concept of origins to differentiate presets coming from core vs theme vs user. Styles are all the same type/structure they just have different sources/origins do you think it would make sense to name the property __unstableOrigin.

Copy link
Member Author

Choose a reason for hiding this comment

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

See comment above #1326 (comment) I'd rather keep this as type.

@youknowriad
Copy link
Contributor

Committed

@youknowriad youknowriad closed this Jun 8, 2021
@oandregal oandregal deleted the update/flag-global-styles branch June 8, 2021 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants