Skip to content

Commit

Permalink
Block Supports: Allow skipping serialization of font size (#30879)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
  • Loading branch information
ockham and aaronrobertshaw authored Apr 28, 2021
1 parent 6fbb4ab commit a4003a0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
4 changes: 3 additions & 1 deletion lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$has_text_decoration_support = _wp_array_get( $block_type->supports, array( '__experimentalTextDecoration' ), false );
$has_text_transform_support = _wp_array_get( $block_type->supports, array( '__experimentalTextTransform' ), false );

$skip_font_size_support_serialization = _wp_array_get( $block_type->supports, array( '__experimentalSkipFontSizeSerialization' ), false );

// Font Size.
if ( $has_font_size_support ) {
if ( $has_font_size_support && ! $skip_font_size_support_serialization ) {
$has_named_font_size = array_key_exists( 'fontSize', $block_attributes );
$has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );

Expand Down
46 changes: 30 additions & 16 deletions packages/block-editor/src/hooks/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ function addSaveProps( props, blockType, attributes ) {
return props;
}

if (
hasBlockSupport( blockType, '__experimentalSkipFontSizeSerialization' )
) {
return props;
}

// Use TokenList to dedupe classes.
const classes = new TokenList( props.className );
classes.add( getFontSizeClass( attributes.fontSize ) );
Expand Down Expand Up @@ -168,30 +174,38 @@ const withFontSizeInlineStyles = createHigherOrderComponent(
wrapperProps,
} = props;

const newProps = { ...props };

// Only add inline styles if the block supports font sizes, doesn't
// already have an inline font size, and does have a class to extract
// the font size from.
// Only add inline styles if the block supports font sizes,
// doesn't skip serialization of font sizes,
// doesn't already have an inline font size,
// and does have a class to extract the font size from.
if (
hasBlockSupport( blockName, FONT_SIZE_SUPPORT_KEY ) &&
fontSize &&
! style?.typography?.fontSize
! hasBlockSupport( blockName, FONT_SIZE_SUPPORT_KEY ) ||
hasBlockSupport(
blockName,
'__experimentalSkipFontSizeSerialization'
) ||
! fontSize ||
style?.typography?.fontSize
) {
const fontSizeValue = getFontSize(
fontSizes,
fontSize,
style?.typography?.fontSize
).size;
return <BlockListBlock { ...props } />;
}

const fontSizeValue = getFontSize(
fontSizes,
fontSize,
style?.typography?.fontSize
).size;

newProps.wrapperProps = {
const newProps = {
...props,
wrapperProps: {
...wrapperProps,
style: {
fontSize: fontSizeValue,
...wrapperProps?.style,
},
};
}
},
};

return <BlockListBlock { ...newProps } />;
},
Expand Down
14 changes: 12 additions & 2 deletions packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { capitalize, get, has, omitBy, startsWith } from 'lodash';
import { capitalize, get, has, omit, omitBy, startsWith } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -19,6 +19,7 @@ import { createHigherOrderComponent } from '@wordpress/compose';
*/
import { BORDER_SUPPORT_KEY, BorderPanel } from './border';
import { COLOR_SUPPORT_KEY, ColorEdit } from './color';
import { FONT_SIZE_SUPPORT_KEY } from './font-size';
import { TypographyPanel, TYPOGRAPHY_SUPPORT_KEYS } from './typography';
import { SPACING_SUPPORT_KEY, PaddingEdit } from './padding';
import SpacingPanelControl from '../components/spacing-panel-control';
Expand Down Expand Up @@ -127,10 +128,19 @@ export function addSaveProps( props, blockType, attributes ) {
}

const { style } = attributes;
const filteredStyle = omitKeysNotToSerialize( style, {
let filteredStyle = omitKeysNotToSerialize( style, {
border: getBlockSupport( blockType, BORDER_SUPPORT_KEY ),
[ COLOR_SUPPORT_KEY ]: getBlockSupport( blockType, COLOR_SUPPORT_KEY ),
} );

if (
getBlockSupport( blockType, '__experimentalSkipFontSizeSerialization' )
) {
filteredStyle = omit( filteredStyle, [
[ 'typography', FONT_SIZE_SUPPORT_KEY ],
] );
}

props.style = {
...getInlineStyles( filteredStyle ),
...props.style,
Expand Down

0 comments on commit a4003a0

Please sign in to comment.