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

Provide some editor defaults for themes that do not provide any editor styles #35737

Closed
wants to merge 2 commits into from
Closed
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
41 changes: 0 additions & 41 deletions lib/compat/wordpress-5.9/default-editor-styles.php

This file was deleted.

39 changes: 25 additions & 14 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,41 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
}

if ( 'other' === $context ) {
$block_styles = array( 'css' => gutenberg_experimental_global_styles_get_stylesheet( $consolidated, 'block_styles' ) );
$css_variables = array(
'css' => gutenberg_experimental_global_styles_get_stylesheet( $consolidated, 'css_variables' ),
'__experimentalNoWrapper' => true,
);
$new_styles = array();

// Make sure the styles array exists.
// Make sure the styles array exists, first.
// In some contexts, like the navigation editor, it doesn't.
if ( ! isset( $settings['styles'] ) ) {
$settings['styles'] = array();
}

// Reset existing global styles.
$styles_without_existing_global_styles = array();
// Add some editor defaults for themes
// that haven't provided any editor styles.
if ( empty( $settings['styles'] ) ) {
$new_styles[] = array(
'css' => file_get_contents( gutenberg_dir_path() . 'build/block-editor/default-editor-styles.css' ),
);
}

// Take all provided styles but the ones provided by core (font & global styles).
foreach ( $settings['styles'] as $style ) {
if ( ! isset( $style['__unstableType'] ) || 'globalStyles' !== $style['__unstableType'] ) {
$styles_without_existing_global_styles[] = $style;
if (
! isset( $style['__unstableType'] ) ||
'globalStyles' !== $style['__unstableType'] || // Remove the global styles from core.
'core' !== $style['__unstableType'] // Remove the default font addition from core.
) {
$new_styles[] = $style;
}
}

// Add the new ones.
$styles_without_existing_global_styles[] = $css_variables;
$styles_without_existing_global_styles[] = $block_styles;
$settings['styles'] = $styles_without_existing_global_styles;
// Add the new styles for global styles back.
$new_styles[] = array( 'css' => gutenberg_experimental_global_styles_get_stylesheet( $consolidated, 'block_styles' ) );
$new_styles[] = array(
'css' => gutenberg_experimental_global_styles_get_stylesheet( $consolidated, 'css_variables' ),
'__experimentalNoWrapper' => true,
);

$settings['styles'] = $new_styles;
}

// Copied from get_block_editor_settings() at wordpress-develop/block-editor.php.
Expand Down
1 change: 0 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat.php';
require __DIR__ . '/compat/wordpress-5.8/index.php';
require __DIR__ . '/compat/wordpress-5.8.1/index.php';
require __DIR__ . '/compat/wordpress-5.9/default-editor-styles.php';
require __DIR__ . '/utils.php';
require __DIR__ . '/editor-settings.php';

Expand Down
4 changes: 1 addition & 3 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ function Editor( {
] );

const styles = useMemo( () => {
return hasThemeStyles && settings.styles?.length
? settings.styles
: settings.defaultEditorStyles;
return hasThemeStyles && settings.styles?.length ? settings.styles : [];
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to remove the "default theme styles" from themes when they uncheck "use theme styles", right?

Copy link
Member Author

@oandregal oandregal Oct 20, 2021

Choose a reason for hiding this comment

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

I'm not sure I understand this code very well :) Specifically, the role of hasThemeStyles and where that comes from.

This is what I understand:

I have an alternative PR to this one that still adds the default styles in the client but passes a flag to ignore the ones we add for core presets. See #35736 However, I thought this one was a better approach. I certainly don't have the full picture yet, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

When you go the "preferences" of the block editor, you can disable "theme styles", when disabled (for any theme), the default editor styles should be used.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, so there are a couple of cases here: the theme does not provide any style or the user deactivates theme styles. 🤔

If the theme styles depend on a user's action we need to be able to load different styles in the client. This PR doesn't make much sense then. I'll close this and focus on #35736

}, [ settings, hasThemeStyles ] );

if ( ! post ) {
Expand Down