From 148763a42fb7ac22e741e6f81d990428efa81787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Thu, 18 Feb 2021 19:29:24 +0100 Subject: [PATCH 1/5] Skip serialization client-side --- packages/block-editor/src/hooks/color.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/block-editor/src/hooks/color.js b/packages/block-editor/src/hooks/color.js index 5490e045fbc056..da1573ca7aaad6 100644 --- a/packages/block-editor/src/hooks/color.js +++ b/packages/block-editor/src/hooks/color.js @@ -44,6 +44,12 @@ const hasColorSupport = ( blockType ) => { ); }; +const shouldSkipSerialization = ( blockType ) => { + const colorSupport = getBlockSupport( blockType, COLOR_SUPPORT_KEY ); + + return colorSupport?.skipSerialization; +}; + const hasLinkColorSupport = ( blockType ) => { if ( Platform.OS !== 'web' ) { return false; @@ -124,7 +130,10 @@ function addAttributes( settings ) { * @return {Object} Filtered props applied to save element */ export function addSaveProps( props, blockType, attributes ) { - if ( ! hasColorSupport( blockType ) ) { + if ( + ! hasColorSupport( blockType ) || + shouldSkipSerialization( blockType ) + ) { return props; } @@ -169,7 +178,10 @@ export function addSaveProps( props, blockType, attributes ) { * @return {Object} Filtered block settings */ export function addEditProps( settings ) { - if ( ! hasColorSupport( settings ) ) { + if ( + ! hasColorSupport( settings ) || + shouldSkipSerialization( settings ) + ) { return settings; } const existingGetEditWrapperProps = settings.getEditWrapperProps; @@ -375,7 +387,7 @@ export const withColorPaletteStyles = createHigherOrderComponent( const { name, attributes } = props; const { backgroundColor, textColor } = attributes; const colors = useEditorFeature( 'color.palette' ) || EMPTY_ARRAY; - if ( ! hasColorSupport( name ) ) { + if ( ! hasColorSupport( name ) || shouldSkipSerialization( name ) ) { return ; } From c4b904cce1c67bda44aa2cdce8a8a06a134b6859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Thu, 18 Feb 2021 20:03:50 +0100 Subject: [PATCH 2/5] Skip serialization server side. Dynamic blocks that do not want serialization could avoid it by not calling get_block_wrapper_attributes. However, there may be cases in which some attributes should be serialized and others should not. --- lib/block-supports/colors.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/block-supports/colors.php b/lib/block-supports/colors.php index e652f578393877..0fa1d48f365624 100644 --- a/lib/block-supports/colors.php +++ b/lib/block-supports/colors.php @@ -60,6 +60,14 @@ function gutenberg_register_colors_support( $block_type ) { */ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { $color_support = gutenberg_experimental_get( $block_type->supports, array( 'color' ), false ); + + if ( array_key_exists( 'skipSerialization', $color_support ) && $color_support['skipSerialization'] ) { + return array( + 'class' => '', + 'styles' => '', + ); + } + $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'text' ), true ) ); $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'background' ), true ) ); $has_link_colors_support = gutenberg_experimental_get( $color_support, array( 'link' ), false ); From 29d4578a19da67adebd26be74aa70230daabe030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Fri, 19 Feb 2021 14:30:57 +0100 Subject: [PATCH 3/5] Return empty attributes if serialization is skipped --- lib/block-supports/colors.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/block-supports/colors.php b/lib/block-supports/colors.php index 0fa1d48f365624..5326a10718ad4a 100644 --- a/lib/block-supports/colors.php +++ b/lib/block-supports/colors.php @@ -62,10 +62,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { $color_support = gutenberg_experimental_get( $block_type->supports, array( 'color' ), false ); if ( array_key_exists( 'skipSerialization', $color_support ) && $color_support['skipSerialization'] ) { - return array( - 'class' => '', - 'styles' => '', - ); + return array(); } $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'text' ), true ) ); From 420ec337aa22de7558ef6834ad7bcce1f7c29506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Fri, 19 Feb 2021 14:38:26 +0100 Subject: [PATCH 4/5] Signal this is experimental --- lib/block-supports/colors.php | 2 +- packages/block-editor/src/hooks/color.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/block-supports/colors.php b/lib/block-supports/colors.php index 5326a10718ad4a..758f6a8ce75527 100644 --- a/lib/block-supports/colors.php +++ b/lib/block-supports/colors.php @@ -61,7 +61,7 @@ function gutenberg_register_colors_support( $block_type ) { function gutenberg_apply_colors_support( $block_type, $block_attributes ) { $color_support = gutenberg_experimental_get( $block_type->supports, array( 'color' ), false ); - if ( array_key_exists( 'skipSerialization', $color_support ) && $color_support['skipSerialization'] ) { + if ( array_key_exists( '__experimentalSkipSerialization', $color_support ) && $color_support['__experimentalSkipSerialization'] ) { return array(); } diff --git a/packages/block-editor/src/hooks/color.js b/packages/block-editor/src/hooks/color.js index da1573ca7aaad6..d8821b16713f39 100644 --- a/packages/block-editor/src/hooks/color.js +++ b/packages/block-editor/src/hooks/color.js @@ -47,7 +47,7 @@ const hasColorSupport = ( blockType ) => { const shouldSkipSerialization = ( blockType ) => { const colorSupport = getBlockSupport( blockType, COLOR_SUPPORT_KEY ); - return colorSupport?.skipSerialization; + return colorSupport?.__experimentalSkipSerialization; }; const hasLinkColorSupport = ( blockType ) => { From 47adda834323f5941ebde1289c46e7a764c68ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Fri, 19 Feb 2021 15:54:11 +0100 Subject: [PATCH 5/5] Cover cases in which color is not an array --- lib/block-supports/colors.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/block-supports/colors.php b/lib/block-supports/colors.php index 758f6a8ce75527..d1d0b4655fdfdf 100644 --- a/lib/block-supports/colors.php +++ b/lib/block-supports/colors.php @@ -61,7 +61,11 @@ function gutenberg_register_colors_support( $block_type ) { function gutenberg_apply_colors_support( $block_type, $block_attributes ) { $color_support = gutenberg_experimental_get( $block_type->supports, array( 'color' ), false ); - if ( array_key_exists( '__experimentalSkipSerialization', $color_support ) && $color_support['__experimentalSkipSerialization'] ) { + if ( + is_array( $color_support ) && + array_key_exists( '__experimentalSkipSerialization', $color_support ) && + $color_support['__experimentalSkipSerialization'] + ) { return array(); }