From 54fb34dd286a5c3de36d819bac904e41502be833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Mon, 18 Oct 2021 17:38:45 +0200 Subject: [PATCH 1/2] Provide editor defaults for themes that do not provide any editor styles --- .../wordpress-5.9/default-editor-styles.php | 41 ------------------- lib/global-styles.php | 41 ++++++++++++------- lib/load.php | 1 - packages/edit-post/src/editor.js | 4 +- 4 files changed, 28 insertions(+), 59 deletions(-) delete mode 100644 lib/compat/wordpress-5.9/default-editor-styles.php diff --git a/lib/compat/wordpress-5.9/default-editor-styles.php b/lib/compat/wordpress-5.9/default-editor-styles.php deleted file mode 100644 index ff796b53c02a7..0000000000000 --- a/lib/compat/wordpress-5.9/default-editor-styles.php +++ /dev/null @@ -1,41 +0,0 @@ - file_get_contents( $default_editor_styles_file ), - ), - ); - - // Remove the default font addition from Core Code. - $styles_without_core_styles = array(); - if ( isset( $settings['styles'] ) ) { - foreach ( $settings['styles'] as $style ) { - if ( - ! isset( $style['__unstableType'] ) || - 'core' !== $style['__unstableType'] - ) { - $styles_without_core_styles[] = $style; - } - } - } - $settings['styles'] = $styles_without_core_styles; - - return $settings; -} -add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_settings_with_default_editor_styles' ); diff --git a/lib/global-styles.php b/lib/global-styles.php index e33f79bb758b8..c6fc1c89cff8c 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -131,30 +131,43 @@ 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. + $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[] = $css_variables; + $new_styles[] = $block_styles; + + $settings['styles'] = $new_styles; } // Copied from get_block_editor_settings() at wordpress-develop/block-editor.php. diff --git a/lib/load.php b/lib/load.php index 14dac73e96e6f..579304ee78610 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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'; diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index c35519b761731..33d3b3ec96dd1 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -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 : []; }, [ settings, hasThemeStyles ] ); if ( ! post ) { From 4820c8035f0e7fba21121b4635ce9cd52a651ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Mon, 18 Oct 2021 17:48:55 +0200 Subject: [PATCH 2/2] Fix linting issues --- lib/global-styles.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index c6fc1c89cff8c..44debd3b441bd 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -143,7 +143,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) && // 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' ) + 'css' => file_get_contents( gutenberg_dir_path() . 'build/block-editor/default-editor-styles.css' ), ); } @@ -159,13 +159,11 @@ function_exists( 'gutenberg_is_edit_site_page' ) && } // Add the new styles for global styles back. - $block_styles = array( 'css' => gutenberg_experimental_global_styles_get_stylesheet( $consolidated, 'block_styles' ) ); - $css_variables = array( + $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, ); - $new_styles[] = $css_variables; - $new_styles[] = $block_styles; $settings['styles'] = $new_styles; }