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

Support skipping serialization in the color hook (block supports mechanism) #29142

Merged
merged 5 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions lib/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

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

We can probably return an empty array here, don't you think?

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, yes, it doesn't make sense as it was. Fixed in 29d4578

'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 );
Expand Down
18 changes: 15 additions & 3 deletions packages/block-editor/src/hooks/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 <BlockListBlock { ...props } />;
}

Expand Down