Skip to content

Commit

Permalink
Tweaks to simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed May 18, 2021
1 parent c4c1f48 commit bed5ad8
Showing 1 changed file with 27 additions and 49 deletions.
76 changes: 27 additions & 49 deletions packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ function addAttribute( settings ) {
return settings;
}

const skipSerializationPaths = [
{
indicator: `${ BORDER_SUPPORT_KEY }.__experimentalSkipSerialization'`,
path: [ 'border' ],
},
{
indicator: `${ COLOR_SUPPORT_KEY }.__experimentalSkipSerialization`,
path: [ COLOR_SUPPORT_KEY ],
},
{
indicator: `__experimentalSkipFontSizeSerialization`,
path: [ 'typography', 'fontSize' ],
},
{
indicator: `__experimentalSkipTypographySerialization`,
path: without( TYPOGRAPHY_SUPPORT_KEYS, FONT_SIZE_SUPPORT_KEY ).map(
( feature ) =>
find( STYLE_PROPERTY, ( property ) =>
isEqual( property.support, [ feature ] )
)?.value
),
},
];

/**
* Override props assigned to save component to inject the CSS variables definition.
*
Expand All @@ -122,56 +146,10 @@ export function addSaveProps( props, blockType, attributes ) {

let { style } = attributes;

const skipSerializationPaths = [
{
indicator: [
BORDER_SUPPORT_KEY,
'__experimentalSkipSerialization',
],
value: [ 'border' ],
},
{
indicator: [ COLOR_SUPPORT_KEY, '__experimentalSkipSerialization' ],
value: [ COLOR_SUPPORT_KEY ],
},
{
indicator: '__experimentalSkipFontSizeSerialization',
skipvalueSerialization: [ 'typography', 'fontSize' ],
},
{
indicator: '__experimentalSkipTypographySerialization',
value: without(
TYPOGRAPHY_SUPPORT_KEYS,
FONT_SIZE_SUPPORT_KEY
).map(
( feature ) =>
find( STYLE_PROPERTY, ( property ) =>
isEqual( property.support, [ feature ] )
)?.value
),
},
];

skipSerializationPaths.forEach( ( { indicator, value } ) => {
if ( Array.isArray( indicator ) && indicator.length ) {
if (
indicator.length === 1 &&
! getBlockSupport( blockType, indicator[ 0 ] )
) {
return;
}
if (
! getBlockSupport( blockType, indicator[ 0 ] )?.[
indicator[ 1 ]
]
) {
return;
}
} else if ( ! getBlockSupport( blockType, indicator ) ) {
return;
skipSerializationPaths.forEach( ( { indicator, path } ) => {
if ( getBlockSupport( blockType, indicator ) ) {
style = omit( style, path );
}

style = omit( style, value );
} );

props.style = {
Expand Down

0 comments on commit bed5ad8

Please sign in to comment.