From a3bf1e30644c18e38c0c0bf50f3b09d9346016aa Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Wed, 11 Aug 2021 10:49:03 +1000 Subject: [PATCH 01/20] Add gap block support feature based on #32571 --- lib/block-supports/dimensions.php | 12 ++ lib/class-wp-theme-json-gutenberg.php | 1 + lib/compat.php | 3 + lib/theme.json | 1 + packages/block-editor/src/hooks/dimensions.js | 28 +++- packages/block-editor/src/hooks/gap.js | 153 ++++++++++++++++++ packages/block-editor/src/hooks/test/style.js | 10 ++ packages/blocks/src/api/constants.js | 8 + .../editor/global-styles-renderer.js | 4 +- .../components/sidebar/dimensions-panel.js | 81 +++++++++- 10 files changed, 297 insertions(+), 4 deletions(-) create mode 100644 packages/block-editor/src/hooks/gap.js diff --git a/lib/block-supports/dimensions.php b/lib/block-supports/dimensions.php index 2692b316d4db1..0802d9e77b222 100644 --- a/lib/block-supports/dimensions.php +++ b/lib/block-supports/dimensions.php @@ -54,6 +54,18 @@ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { // Height support to be added in near future. // Width support to be added in near future. + if ( $has_gap_support ) { + $gap_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'gap' ), null ); + + if ( is_array( $gap_value ) ) { + foreach ( $gap_value as $key => $value ) { + $styles[] = sprintf( '--wp--theme--block-%s-gap: %s', $key, $value ); + } + } elseif ( null !== $gap_value ) { + $styles[] = sprintf( '--wp--theme--block-: %s', $gap_value ); + } + } + return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); } diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index cf713529b4f70..80fab8d38a37b 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -99,6 +99,7 @@ class WP_Theme_JSON_Gutenberg { 'wideSize' => null, ), 'spacing' => array( + 'customGap' => null, 'customMargin' => null, 'customPadding' => null, 'units' => null, diff --git a/lib/compat.php b/lib/compat.php index 917d22fe73370..eebd9acd3f0d6 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -186,6 +186,9 @@ function gutenberg_safe_style_attrs( $attrs ) { $attrs[] = 'border-top-right-radius'; $attrs[] = 'border-bottom-right-radius'; $attrs[] = 'border-bottom-left-radius'; + $attrs[] = 'gap'; + $attrs[] = 'column-gap'; + $attrs[] = 'row-gap'; return $attrs; } diff --git a/lib/theme.json b/lib/theme.json index 6b52969009bee..e94f3263ee61f 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -211,6 +211,7 @@ ] }, "spacing": { + "customGap": false, "customMargin": false, "customPadding": false, "units": [ "px", "em", "rem", "vh", "vw", "%" ] diff --git a/packages/block-editor/src/hooks/dimensions.js b/packages/block-editor/src/hooks/dimensions.js index 697955670b497..41450e692ed74 100644 --- a/packages/block-editor/src/hooks/dimensions.js +++ b/packages/block-editor/src/hooks/dimensions.js @@ -13,6 +13,13 @@ import { getBlockSupport } from '@wordpress/blocks'; * Internal dependencies */ import InspectorControls from '../components/inspector-controls'; +import { + GapEdit, + hasGapSupport, + hasGapValue, + resetGap, + useIsGapDisabled, +} from './gap'; import { MarginEdit, hasMarginSupport, @@ -41,6 +48,7 @@ export const AXIAL_SIDES = [ 'vertical', 'horizontal' ]; * @return {WPElement} Inspector controls for spacing support features. */ export function DimensionsPanel( props ) { + const isGapDisabled = useIsGapDisabled( props ); const isPaddingDisabled = useIsPaddingDisabled( props ); const isMarginDisabled = useIsMarginDisabled( props ); const isDisabled = useIsDimensionsDisabled( props ); @@ -64,6 +72,7 @@ export function DimensionsPanel( props ) { ...style, spacing: { ...style?.spacing, + gap: undefined, margin: undefined, padding: undefined, }, @@ -98,6 +107,16 @@ export function DimensionsPanel( props ) { ) } + { ! isGapDisabled && ( + hasGapValue( props ) } + label={ __( 'Gap' ) } + onDeselect={ () => resetGap( props ) } + isShownByDefault={ defaultSpacingControls?.gap } + > + + + ) } ); @@ -115,7 +134,11 @@ export function hasDimensionsSupport( blockName ) { return false; } - return hasPaddingSupport( blockName ) || hasMarginSupport( blockName ); + return ( + hasGapSupport( blockName ) || + hasPaddingSupport( blockName ) || + hasMarginSupport( blockName ) + ); } /** @@ -126,10 +149,11 @@ export function hasDimensionsSupport( blockName ) { * @return {boolean} If spacing support is completely disabled. */ const useIsDimensionsDisabled = ( props = {} ) => { + const gapDisabled = useIsGapDisabled( props ); const paddingDisabled = useIsPaddingDisabled( props ); const marginDisabled = useIsMarginDisabled( props ); - return paddingDisabled && marginDisabled; + return gapDisabled && paddingDisabled && marginDisabled; }; /** diff --git a/packages/block-editor/src/hooks/gap.js b/packages/block-editor/src/hooks/gap.js new file mode 100644 index 0000000000000..6e057f23720a8 --- /dev/null +++ b/packages/block-editor/src/hooks/gap.js @@ -0,0 +1,153 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { Platform } from '@wordpress/element'; +import { getBlockSupport } from '@wordpress/blocks'; +import { + __experimentalUseCustomUnits as useCustomUnits, + __experimentalBoxControl as BoxControl, +} from '@wordpress/components'; + +/** + * Internal dependencies + */ +import useSetting from '../components/use-setting'; +import { SPACING_SUPPORT_KEY, useCustomSides } from './dimensions'; +import { cleanEmptyObject } from './utils'; + +/** + * Determines if there is gap support. + * + * @param {string|Object} blockType Block name or Block Type object. + * @return {boolean} Whether there is support. + */ +export function hasGapSupport( blockType ) { + const support = getBlockSupport( blockType, SPACING_SUPPORT_KEY ); + return !! ( true === support || support?.gap ); +} + +/** + * Checks if there is a current value in the gap block support attributes. + * + * @param {Object} props Block props. + * @return {boolean} Whether or not the block has a gap value set. + */ +export function hasGapValue( props ) { + return props.attributes.style?.spacing?.gap !== undefined; +} + +/** + * Resets the gap block support attribute. This can be used when disabling + * the gap support controls for a block via a progressive discovery panel. + * + * @param {Object} props Block props. + * @param {Object} props.attributes Block's attributes. + * @param {Object} props.setAttributes Function to set block's attributes. + */ +export function resetGap( { attributes = {}, setAttributes } ) { + const { style } = attributes; + + setAttributes( { + style: { + ...style, + spacing: { + ...style?.spacing, + gap: undefined, + }, + }, + } ); +} + +/** + * Custom hook that checks if gap settings have been disabled. + * + * @param {string} name The name of the block. + * @return {boolean} Whether the gap setting is disabled. + */ +export function useIsGapDisabled( { name: blockName } = {} ) { + const isDisabled = ! useSetting( 'spacing.customGap' ); + return ! hasGapSupport( blockName ) || isDisabled; +} + +/** + * Inspector control panel containing the gap related configuration + * + * @param {Object} props + * + * @return {WPElement} Gap edit element. + */ +export function GapEdit( props ) { + const { + name: blockName, + attributes: { style }, + setAttributes, + } = props; + + const units = useCustomUnits( { + availableUnits: useSetting( 'spacing.units' ) || [ + '%', + 'px', + 'em', + 'rem', + 'vw', + ], + } ); + const sides = useCustomSides( blockName, 'gap' ); + + if ( useIsGapDisabled( props ) ) { + return null; + } + + const onChange = ( next ) => { + const newStyle = { + ...style, + spacing: { + ...style?.spacing, + gap: { row: next?.top, column: next?.left }, + }, + }; + + setAttributes( { + style: cleanEmptyObject( newStyle ), + } ); + }; + + const onChangeShowVisualizer = ( next ) => { + const newStyle = { + ...style, + visualizers: { + gap: { row: next?.top, column: next?.left }, + }, + }; + + setAttributes( { + style: cleanEmptyObject( newStyle ), + } ); + }; + + const boxValues = { + top: style?.spacing?.gap?.row, + right: style?.spacing?.gap?.column, + bottom: style?.spacing?.gap?.row, + left: style?.spacing?.gap?.column, + }; + + return Platform.select( { + web: ( + <> + + + ), + native: null, + } ); +} diff --git a/packages/block-editor/src/hooks/test/style.js b/packages/block-editor/src/hooks/test/style.js index 706d9a93ed0ba..9588bc18320ce 100644 --- a/packages/block-editor/src/hooks/test/style.js +++ b/packages/block-editor/src/hooks/test/style.js @@ -24,11 +24,13 @@ describe( 'getInlineStyles', () => { color: '#21759b', }, spacing: { + gap: { row: '1em' }, padding: { top: '10px' }, margin: { bottom: '15px' }, }, } ) ).toEqual( { + '--wp--theme--block-row-gap': '1em', backgroundColor: 'black', borderColor: '#21759b', borderRadius: '10px', @@ -66,6 +68,10 @@ describe( 'getInlineStyles', () => { expect( getInlineStyles( { spacing: { + gap: { + column: '5px', + row: '1em', + }, margin: { top: '10px', right: '0.5rem', @@ -81,6 +87,8 @@ describe( 'getInlineStyles', () => { }, } ) ).toEqual( { + '--wp--theme--block-column-gap': '5px', + '--wp--theme--block-row-gap': '1em', marginTop: '10px', marginRight: '0.5rem', marginBottom: '0.5em', @@ -96,11 +104,13 @@ describe( 'getInlineStyles', () => { expect( getInlineStyles( { spacing: { + gap: '1em', margin: '10px', padding: '20px', }, } ) ).toEqual( { + '--wp--theme--block-gap': '1em', margin: '10px', padding: '20px', } ); diff --git a/packages/blocks/src/api/constants.js b/packages/blocks/src/api/constants.js index 18342aa916256..c1c84157a4658 100644 --- a/packages/blocks/src/api/constants.js +++ b/packages/blocks/src/api/constants.js @@ -72,6 +72,14 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = { value: [ 'typography', 'fontWeight' ], support: [ 'typography', '__experimentalFontWeight' ], }, + '--wp--theme--block-gap': { + value: [ 'spacing', 'gap' ], + support: [ 'spacing', 'gap' ], + properties: { + '--wp--theme--block-column-gap': 'column', + '--wp--theme--block-row-gap': 'row', + }, + }, lineHeight: { value: [ 'typography', 'lineHeight' ], support: [ 'typography', 'lineHeight' ], diff --git a/packages/edit-site/src/components/editor/global-styles-renderer.js b/packages/edit-site/src/components/editor/global-styles-renderer.js index fdec33c3fc023..4c8147af12088 100644 --- a/packages/edit-site/src/components/editor/global-styles-renderer.js +++ b/packages/edit-site/src/components/editor/global-styles-renderer.js @@ -159,7 +159,9 @@ function getStylesDeclarations( blockStyles = {} ) { return; } - const cssProperty = kebabCase( name ); + const cssProperty = name.startsWith( '--' ) + ? name + : kebabCase( name ); declarations.push( `${ cssProperty }: ${ compileStyleValue( get( styleValue, [ prop ] ) diff --git a/packages/edit-site/src/components/sidebar/dimensions-panel.js b/packages/edit-site/src/components/sidebar/dimensions-panel.js index 05b4aadd2d5bf..e4db1c965f188 100644 --- a/packages/edit-site/src/components/sidebar/dimensions-panel.js +++ b/packages/edit-site/src/components/sidebar/dimensions-panel.js @@ -20,8 +20,9 @@ const AXIAL_SIDES = [ 'horizontal', 'vertical' ]; export function useHasDimensionsPanel( context ) { const hasPadding = useHasPadding( context ); const hasMargin = useHasMargin( context ); + const hasGap = useHasGap( context ); - return hasPadding || hasMargin; + return hasPadding || hasMargin || hasGap; } function useHasPadding( { name, supports } ) { @@ -36,6 +37,12 @@ function useHasMargin( { name, supports } ) { return settings && supports.includes( 'margin' ); } +function useHasGap( { name, supports } ) { + const settings = useSetting( 'spacing.customGap', name ); + + return settings && supports.includes( '--wp--theme--block-gap' ); +} + function filterValuesBySides( values, sides ) { if ( ! sides ) { // If no custom side configuration all sides are opted into by default. @@ -59,6 +66,26 @@ function filterValuesBySides( values, sides ) { return filteredValues; } +function filterGapValuesBySides( values, sides ) { + if ( ! sides ) { + return { + row: values?.top, + column: values?.left, + }; + } + + const filteredValues = {}; + + sides.forEach( ( side ) => { + if ( side === 'horizontal' ) { + filteredValues.column = values?.left; + } + if ( side === 'vertical' ) { + filteredValues.row = values?.top; + } + } ); +} + function splitStyleValue( value ) { // Check for shorthand value ( a string value ). if ( value && typeof value === 'string' ) { @@ -74,10 +101,31 @@ function splitStyleValue( value ) { return value; } +function splitGapStyleValue( value ) { + // Check for shorthand value ( a string value ). + if ( value && typeof value === 'string' ) { + return { + top: value, + right: value, + bottom: value, + left: value, + }; + } + + // Convert rows and columns to individual side values. + return { + top: value?.row, + right: value?.column, + bottom: value?.row, + left: value?.column, + }; +} + export default function DimensionsPanel( { context, getStyle, setStyle } ) { const { name } = context; const showPaddingControl = useHasPadding( context ); const showMarginControl = useHasMargin( context ); + const showGapControl = useHasGap( context ); const units = useCustomUnits( { availableUnits: useSetting( 'spacing.units', name ) || [ '%', @@ -116,9 +164,22 @@ export default function DimensionsPanel( { context, getStyle, setStyle } ) { const hasMarginValue = () => marginValues && Object.keys( marginValues ).length; + const gapValues = splitGapStyleValue( + getStyle( name, '--wp--theme--block-gap' ) + ); + const gapSides = useCustomSides( name, '--wp--theme--block-gap' ); + + const setGapValues = ( newGapValues ) => { + const gap = filterGapValuesBySides( newGapValues, gapSides ); + setStyle( name, '--wp--theme--block-gap', gap ); + }; + const resetGapValue = () => setGapValues( {} ); + const hasGapValue = () => gapValues && Object.keys( gapValues ).length; + const resetAll = () => { resetPaddingValue(); resetMarginValue(); + resetGapValue(); }; return ( @@ -163,6 +224,24 @@ export default function DimensionsPanel( { context, getStyle, setStyle } ) { /> ) } + { showGapControl && ( + + + + ) } ); } From 361c744ba820b92b98393e05d57fed3233a1c0e0 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Thu, 12 Aug 2021 12:40:19 +1000 Subject: [PATCH 02/20] Rename CSS variable, refactor to use single variable rather than split values --- lib/block-supports/dimensions.php | 10 +-- lib/class-wp-theme-json-gutenberg.php | 8 +-- lib/compat.php | 3 - lib/theme.json | 2 +- packages/block-editor/src/hooks/dimensions.js | 4 +- packages/block-editor/src/hooks/gap.js | 47 +++---------- packages/block-editor/src/hooks/test/style.js | 14 ++-- packages/blocks/src/api/constants.js | 10 +-- .../editor/global-styles-renderer.js | 4 +- .../components/sidebar/dimensions-panel.js | 69 ++++--------------- 10 files changed, 41 insertions(+), 130 deletions(-) diff --git a/lib/block-supports/dimensions.php b/lib/block-supports/dimensions.php index 0802d9e77b222..03f7878c02034 100644 --- a/lib/block-supports/dimensions.php +++ b/lib/block-supports/dimensions.php @@ -55,14 +55,10 @@ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { // Width support to be added in near future. if ( $has_gap_support ) { - $gap_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'gap' ), null ); + $gap_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'blockGap' ), null ); - if ( is_array( $gap_value ) ) { - foreach ( $gap_value as $key => $value ) { - $styles[] = sprintf( '--wp--theme--block-%s-gap: %s', $key, $value ); - } - } elseif ( null !== $gap_value ) { - $styles[] = sprintf( '--wp--theme--block-: %s', $gap_value ); + if ( is_string( $gap_value ) ) { + $styles[] = sprintf( '--wp--style--block-gap: %s', $gap_value ); } } diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 80fab8d38a37b..c2630797e6039 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -99,10 +99,10 @@ class WP_Theme_JSON_Gutenberg { 'wideSize' => null, ), 'spacing' => array( - 'customGap' => null, - 'customMargin' => null, - 'customPadding' => null, - 'units' => null, + 'customBlockGap' => null, + 'customMargin' => null, + 'customPadding' => null, + 'units' => null, ), 'typography' => array( 'customFontSize' => null, diff --git a/lib/compat.php b/lib/compat.php index eebd9acd3f0d6..917d22fe73370 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -186,9 +186,6 @@ function gutenberg_safe_style_attrs( $attrs ) { $attrs[] = 'border-top-right-radius'; $attrs[] = 'border-bottom-right-radius'; $attrs[] = 'border-bottom-left-radius'; - $attrs[] = 'gap'; - $attrs[] = 'column-gap'; - $attrs[] = 'row-gap'; return $attrs; } diff --git a/lib/theme.json b/lib/theme.json index e94f3263ee61f..e6844cddc1760 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -211,7 +211,7 @@ ] }, "spacing": { - "customGap": false, + "customBlockGap": false, "customMargin": false, "customPadding": false, "units": [ "px", "em", "rem", "vh", "vw", "%" ] diff --git a/packages/block-editor/src/hooks/dimensions.js b/packages/block-editor/src/hooks/dimensions.js index 41450e692ed74..c8c08ca605b3a 100644 --- a/packages/block-editor/src/hooks/dimensions.js +++ b/packages/block-editor/src/hooks/dimensions.js @@ -72,7 +72,7 @@ export function DimensionsPanel( props ) { ...style, spacing: { ...style?.spacing, - gap: undefined, + blockGap: undefined, margin: undefined, padding: undefined, }, @@ -112,7 +112,7 @@ export function DimensionsPanel( props ) { hasValue={ () => hasGapValue( props ) } label={ __( 'Gap' ) } onDeselect={ () => resetGap( props ) } - isShownByDefault={ defaultSpacingControls?.gap } + isShownByDefault={ defaultSpacingControls?.blockGap } > diff --git a/packages/block-editor/src/hooks/gap.js b/packages/block-editor/src/hooks/gap.js index 6e057f23720a8..b573746206d55 100644 --- a/packages/block-editor/src/hooks/gap.js +++ b/packages/block-editor/src/hooks/gap.js @@ -6,14 +6,14 @@ import { Platform } from '@wordpress/element'; import { getBlockSupport } from '@wordpress/blocks'; import { __experimentalUseCustomUnits as useCustomUnits, - __experimentalBoxControl as BoxControl, + __experimentalUnitControl as UnitControl, } from '@wordpress/components'; /** * Internal dependencies */ import useSetting from '../components/use-setting'; -import { SPACING_SUPPORT_KEY, useCustomSides } from './dimensions'; +import { SPACING_SUPPORT_KEY } from './dimensions'; import { cleanEmptyObject } from './utils'; /** @@ -24,7 +24,7 @@ import { cleanEmptyObject } from './utils'; */ export function hasGapSupport( blockType ) { const support = getBlockSupport( blockType, SPACING_SUPPORT_KEY ); - return !! ( true === support || support?.gap ); + return !! ( true === support || support?.blockGap ); } /** @@ -34,7 +34,7 @@ export function hasGapSupport( blockType ) { * @return {boolean} Whether or not the block has a gap value set. */ export function hasGapValue( props ) { - return props.attributes.style?.spacing?.gap !== undefined; + return props.attributes.style?.spacing?.blockGap !== undefined; } /** @@ -53,7 +53,7 @@ export function resetGap( { attributes = {}, setAttributes } ) { ...style, spacing: { ...style?.spacing, - gap: undefined, + blockGap: undefined, }, }, } ); @@ -66,7 +66,7 @@ export function resetGap( { attributes = {}, setAttributes } ) { * @return {boolean} Whether the gap setting is disabled. */ export function useIsGapDisabled( { name: blockName } = {} ) { - const isDisabled = ! useSetting( 'spacing.customGap' ); + const isDisabled = ! useSetting( 'spacing.customBlockGap' ); return ! hasGapSupport( blockName ) || isDisabled; } @@ -79,7 +79,6 @@ export function useIsGapDisabled( { name: blockName } = {} ) { */ export function GapEdit( props ) { const { - name: blockName, attributes: { style }, setAttributes, } = props; @@ -93,7 +92,6 @@ export function GapEdit( props ) { 'vw', ], } ); - const sides = useCustomSides( blockName, 'gap' ); if ( useIsGapDisabled( props ) ) { return null; @@ -104,7 +102,7 @@ export function GapEdit( props ) { ...style, spacing: { ...style?.spacing, - gap: { row: next?.top, column: next?.left }, + blockGap: next, }, }; @@ -113,38 +111,15 @@ export function GapEdit( props ) { } ); }; - const onChangeShowVisualizer = ( next ) => { - const newStyle = { - ...style, - visualizers: { - gap: { row: next?.top, column: next?.left }, - }, - }; - - setAttributes( { - style: cleanEmptyObject( newStyle ), - } ); - }; - - const boxValues = { - top: style?.spacing?.gap?.row, - right: style?.spacing?.gap?.column, - bottom: style?.spacing?.gap?.row, - left: style?.spacing?.gap?.column, - }; - return Platform.select( { web: ( <> - ), diff --git a/packages/block-editor/src/hooks/test/style.js b/packages/block-editor/src/hooks/test/style.js index 9588bc18320ce..3def13ab7ca6c 100644 --- a/packages/block-editor/src/hooks/test/style.js +++ b/packages/block-editor/src/hooks/test/style.js @@ -24,13 +24,13 @@ describe( 'getInlineStyles', () => { color: '#21759b', }, spacing: { - gap: { row: '1em' }, + blockGap: { row: '1em' }, padding: { top: '10px' }, margin: { bottom: '15px' }, }, } ) ).toEqual( { - '--wp--theme--block-row-gap': '1em', + '--wp--style--block-gap': '1em', backgroundColor: 'black', borderColor: '#21759b', borderRadius: '10px', @@ -68,10 +68,6 @@ describe( 'getInlineStyles', () => { expect( getInlineStyles( { spacing: { - gap: { - column: '5px', - row: '1em', - }, margin: { top: '10px', right: '0.5rem', @@ -87,8 +83,6 @@ describe( 'getInlineStyles', () => { }, } ) ).toEqual( { - '--wp--theme--block-column-gap': '5px', - '--wp--theme--block-row-gap': '1em', marginTop: '10px', marginRight: '0.5rem', marginBottom: '0.5em', @@ -104,13 +98,13 @@ describe( 'getInlineStyles', () => { expect( getInlineStyles( { spacing: { - gap: '1em', + blockGap: '1em', margin: '10px', padding: '20px', }, } ) ).toEqual( { - '--wp--theme--block-gap': '1em', + '--wp--style--block-gap': '1em', margin: '10px', padding: '20px', } ); diff --git a/packages/blocks/src/api/constants.js b/packages/blocks/src/api/constants.js index c1c84157a4658..eb34a7a63818d 100644 --- a/packages/blocks/src/api/constants.js +++ b/packages/blocks/src/api/constants.js @@ -72,13 +72,9 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = { value: [ 'typography', 'fontWeight' ], support: [ 'typography', '__experimentalFontWeight' ], }, - '--wp--theme--block-gap': { - value: [ 'spacing', 'gap' ], - support: [ 'spacing', 'gap' ], - properties: { - '--wp--theme--block-column-gap': 'column', - '--wp--theme--block-row-gap': 'row', - }, + '--wp--style--block-gap': { + value: [ 'spacing', 'blockGap' ], + support: [ 'spacing', 'blockGap' ], }, lineHeight: { value: [ 'typography', 'lineHeight' ], diff --git a/packages/edit-site/src/components/editor/global-styles-renderer.js b/packages/edit-site/src/components/editor/global-styles-renderer.js index 4c8147af12088..fdec33c3fc023 100644 --- a/packages/edit-site/src/components/editor/global-styles-renderer.js +++ b/packages/edit-site/src/components/editor/global-styles-renderer.js @@ -159,9 +159,7 @@ function getStylesDeclarations( blockStyles = {} ) { return; } - const cssProperty = name.startsWith( '--' ) - ? name - : kebabCase( name ); + const cssProperty = kebabCase( name ); declarations.push( `${ cssProperty }: ${ compileStyleValue( get( styleValue, [ prop ] ) diff --git a/packages/edit-site/src/components/sidebar/dimensions-panel.js b/packages/edit-site/src/components/sidebar/dimensions-panel.js index e4db1c965f188..f0de8d43442ab 100644 --- a/packages/edit-site/src/components/sidebar/dimensions-panel.js +++ b/packages/edit-site/src/components/sidebar/dimensions-panel.js @@ -6,6 +6,7 @@ import { __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, __experimentalBoxControl as BoxControl, + __experimentalUnitControl as UnitControl, __experimentalUseCustomUnits as useCustomUnits, } from '@wordpress/components'; import { __experimentalUseCustomSides as useCustomSides } from '@wordpress/block-editor'; @@ -38,9 +39,9 @@ function useHasMargin( { name, supports } ) { } function useHasGap( { name, supports } ) { - const settings = useSetting( 'spacing.customGap', name ); + const settings = useSetting( 'spacing.customBlockGap', name ); - return settings && supports.includes( '--wp--theme--block-gap' ); + return settings && supports.includes( '--wp--style--block-gap' ); } function filterValuesBySides( values, sides ) { @@ -66,26 +67,6 @@ function filterValuesBySides( values, sides ) { return filteredValues; } -function filterGapValuesBySides( values, sides ) { - if ( ! sides ) { - return { - row: values?.top, - column: values?.left, - }; - } - - const filteredValues = {}; - - sides.forEach( ( side ) => { - if ( side === 'horizontal' ) { - filteredValues.column = values?.left; - } - if ( side === 'vertical' ) { - filteredValues.row = values?.top; - } - } ); -} - function splitStyleValue( value ) { // Check for shorthand value ( a string value ). if ( value && typeof value === 'string' ) { @@ -101,26 +82,6 @@ function splitStyleValue( value ) { return value; } -function splitGapStyleValue( value ) { - // Check for shorthand value ( a string value ). - if ( value && typeof value === 'string' ) { - return { - top: value, - right: value, - bottom: value, - left: value, - }; - } - - // Convert rows and columns to individual side values. - return { - top: value?.row, - right: value?.column, - bottom: value?.row, - left: value?.column, - }; -} - export default function DimensionsPanel( { context, getStyle, setStyle } ) { const { name } = context; const showPaddingControl = useHasPadding( context ); @@ -164,17 +125,13 @@ export default function DimensionsPanel( { context, getStyle, setStyle } ) { const hasMarginValue = () => marginValues && Object.keys( marginValues ).length; - const gapValues = splitGapStyleValue( - getStyle( name, '--wp--theme--block-gap' ) - ); - const gapSides = useCustomSides( name, '--wp--theme--block-gap' ); + const gapValue = getStyle( name, '--wp--style--block-gap' ); - const setGapValues = ( newGapValues ) => { - const gap = filterGapValuesBySides( newGapValues, gapSides ); - setStyle( name, '--wp--theme--block-gap', gap ); + const setGapValue = ( newGapValue ) => { + setStyle( name, '--wp--style--block-gap', newGapValue ); }; - const resetGapValue = () => setGapValues( {} ); - const hasGapValue = () => gapValues && Object.keys( gapValues ).length; + const resetGapValue = () => setGapValue( undefined ); + const hasGapValue = () => !! gapValue; const resetAll = () => { resetPaddingValue(); @@ -231,14 +188,12 @@ export default function DimensionsPanel( { context, getStyle, setStyle } ) { onDeselect={ resetGapValue } isShownByDefault={ true } > - ) } From b716885c3e07daa390c6e0018eac888034a7a9fa Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Thu, 12 Aug 2021 15:18:38 +1000 Subject: [PATCH 03/20] Switch block gap to be displayed in a single column --- packages/block-editor/src/hooks/dimensions.js | 3 ++- packages/block-editor/src/hooks/gap.js | 8 ++++---- .../src/components/sidebar/dimensions-panel.js | 11 ++++++----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/block-editor/src/hooks/dimensions.js b/packages/block-editor/src/hooks/dimensions.js index c8c08ca605b3a..d85ce67104b39 100644 --- a/packages/block-editor/src/hooks/dimensions.js +++ b/packages/block-editor/src/hooks/dimensions.js @@ -109,8 +109,9 @@ export function DimensionsPanel( props ) { ) } { ! isGapDisabled && ( hasGapValue( props ) } - label={ __( 'Gap' ) } + label={ __( 'Block gap' ) } onDeselect={ () => resetGap( props ) } isShownByDefault={ defaultSpacingControls?.blockGap } > diff --git a/packages/block-editor/src/hooks/gap.js b/packages/block-editor/src/hooks/gap.js index b573746206d55..29d64dcf0239b 100644 --- a/packages/block-editor/src/hooks/gap.js +++ b/packages/block-editor/src/hooks/gap.js @@ -115,11 +115,11 @@ export function GapEdit( props ) { web: ( <> ), diff --git a/packages/edit-site/src/components/sidebar/dimensions-panel.js b/packages/edit-site/src/components/sidebar/dimensions-panel.js index f0de8d43442ab..ab156f7f08896 100644 --- a/packages/edit-site/src/components/sidebar/dimensions-panel.js +++ b/packages/edit-site/src/components/sidebar/dimensions-panel.js @@ -183,17 +183,18 @@ export default function DimensionsPanel( { context, getStyle, setStyle } ) { ) } { showGapControl && ( ) } From 60491e43cc8b8765c0fc40f336ad09307e39c064 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Thu, 12 Aug 2021 15:21:13 +1000 Subject: [PATCH 04/20] Fix failing JS test --- packages/block-editor/src/hooks/test/style.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/hooks/test/style.js b/packages/block-editor/src/hooks/test/style.js index 3def13ab7ca6c..e8c3264eeba6b 100644 --- a/packages/block-editor/src/hooks/test/style.js +++ b/packages/block-editor/src/hooks/test/style.js @@ -24,7 +24,7 @@ describe( 'getInlineStyles', () => { color: '#21759b', }, spacing: { - blockGap: { row: '1em' }, + blockGap: '1em', padding: { top: '10px' }, margin: { bottom: '15px' }, }, From 455068586111b97a9ae8177a87599b3a4bc76dba Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Thu, 12 Aug 2021 17:02:35 +1000 Subject: [PATCH 05/20] Try server-rendering the gap block support, and skip serialization on save in the editor --- lib/block-supports/dimensions.php | 46 ++++++++++++++++++++++++ packages/block-editor/src/hooks/style.js | 24 ++++++++++--- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/lib/block-supports/dimensions.php b/lib/block-supports/dimensions.php index 03f7878c02034..2bf7abbdee208 100644 --- a/lib/block-supports/dimensions.php +++ b/lib/block-supports/dimensions.php @@ -80,6 +80,50 @@ function gutenberg_skip_dimensions_serialization( $block_type ) { $dimensions_support['__experimentalSkipSerialization']; } +/** + * Renders the dimensions support to the block wrapper, for supports that + * require block-level server-side rendering, for example blockGap support + * which uses CSS variables. + * + * @param string $block_content Rendered block content. + * @param array $block Block object. + * @return string Filtered block content. + */ +function gutenberg_render_dimensions_support( $block_content, $block ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); + $has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false ); + if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) { + return $block_content; + } + + $id = uniqid(); + $style = sprintf( + '.wp-container-%s { --wp--style--block-gap: %s; }', + $id, + esc_attr( $block['attrs']['style']['spacing']['blockGap'] ) + ); + + // This assumes the hook only applies to blocks with a single wrapper. + $content = preg_replace( + '/' . preg_quote( 'class="', '/' ) . '/', + 'class="wp-container-' . $id . ' ', + $block_content, + 1 + ); + + // Ideally styles should be loaded in the head, but blocks may be parsed + // after that, so loading in the footer for now. + // See https://core.trac.wordpress.org/ticket/53494. + add_action( + 'wp_footer', + function () use ( $style ) { + echo ''; + } + ); + + return $content; +} + // Register the block support. WP_Block_Supports::get_instance()->register( 'dimensions', @@ -88,3 +132,5 @@ function gutenberg_skip_dimensions_serialization( $block_type ) { 'apply' => 'gutenberg_apply_dimensions_support', ) ); + +add_filter( 'render_block', 'gutenberg_render_dimensions_support', 10, 2 ); diff --git a/packages/block-editor/src/hooks/style.js b/packages/block-editor/src/hooks/style.js index 87bf4685d50d4..cd39b112e796a 100644 --- a/packages/block-editor/src/hooks/style.js +++ b/packages/block-editor/src/hooks/style.js @@ -144,7 +144,7 @@ function addAttribute( settings ) { return settings; } -const skipSerializationPaths = { +const skipSerializationPathsEdit = { [ `${ BORDER_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [ 'border' ], [ `${ COLOR_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [ COLOR_SUPPORT_KEY, @@ -157,23 +157,34 @@ const skipSerializationPaths = { ], }; +const skipSerializationPathsSave = { + ...skipSerializationPathsEdit, + [ `${ SPACING_SUPPORT_KEY }` ]: [ 'spacing', 'blockGap' ], +}; + /** * Override props assigned to save component to inject the CSS variables definition. * * @param {Object} props Additional props applied to save element. * @param {Object} blockType Block type. * @param {Object} attributes Block attributes. + * @param {Object} skipPaths An object of keys and paths to skip serialization. * * @return {Object} Filtered props applied to save element. */ -export function addSaveProps( props, blockType, attributes ) { +export function addSaveProps( + props, + blockType, + attributes, + skipPaths = skipSerializationPathsSave +) { if ( ! hasStyleSupport( blockType ) ) { return props; } let { style } = attributes; - forEach( skipSerializationPaths, ( path, indicator ) => { + forEach( skipPaths, ( path, indicator ) => { if ( getBlockSupport( blockType, indicator ) ) { style = omit( style, path ); } @@ -207,7 +218,12 @@ export function addEditProps( settings ) { props = existingGetEditWrapperProps( attributes ); } - return addSaveProps( props, settings, attributes ); + return addSaveProps( + props, + settings, + attributes, + skipSerializationPathsEdit + ); }; return settings; From a6d8df9a4d2ca4ce8b3fa1da1af9785e7d611edb Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 13 Aug 2021 12:49:28 +1000 Subject: [PATCH 06/20] Fix regression where padding/margin values were skipping serialization --- packages/block-editor/src/hooks/style.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/hooks/style.js b/packages/block-editor/src/hooks/style.js index cd39b112e796a..00148dc2f8982 100644 --- a/packages/block-editor/src/hooks/style.js +++ b/packages/block-editor/src/hooks/style.js @@ -144,6 +144,13 @@ function addAttribute( settings ) { return settings; } +/** + * A dictionary of paths to flag skipping block support serialization as the key, + * with values providing the style paths to be omitted from serialization. + * + * @constant + * @type {Record} + */ const skipSerializationPathsEdit = { [ `${ BORDER_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [ 'border' ], [ `${ COLOR_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [ @@ -157,9 +164,21 @@ const skipSerializationPathsEdit = { ], }; +/** + * A dictionary of paths to flag skipping block support serialization as the key, + * with values providing the style paths to be omitted from serialization. + * + * Extends the Edit skip paths to enable skipping additional paths in just + * the Save component. This allows a block support to be serialized within the + * editor, while using an alternate approach, such as server-side rendering, when + * the support is saved. + * + * @constant + * @type {Record} + */ const skipSerializationPathsSave = { ...skipSerializationPathsEdit, - [ `${ SPACING_SUPPORT_KEY }` ]: [ 'spacing', 'blockGap' ], + [ `${ SPACING_SUPPORT_KEY }` ]: [ 'spacing.blockGap' ], }; /** From b74b73acbc3395f49b7e06f1ae5009f7a4604d0a Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 13 Aug 2021 14:43:01 +1000 Subject: [PATCH 07/20] Update types --- packages/block-editor/src/hooks/style.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/block-editor/src/hooks/style.js b/packages/block-editor/src/hooks/style.js index 00148dc2f8982..9bc7c0e5997c8 100644 --- a/packages/block-editor/src/hooks/style.js +++ b/packages/block-editor/src/hooks/style.js @@ -149,7 +149,7 @@ function addAttribute( settings ) { * with values providing the style paths to be omitted from serialization. * * @constant - * @type {Record} + * @type {Record} */ const skipSerializationPathsEdit = { [ `${ BORDER_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [ 'border' ], @@ -174,7 +174,7 @@ const skipSerializationPathsEdit = { * the support is saved. * * @constant - * @type {Record} + * @type {Record} */ const skipSerializationPathsSave = { ...skipSerializationPathsEdit, @@ -184,10 +184,10 @@ const skipSerializationPathsSave = { /** * Override props assigned to save component to inject the CSS variables definition. * - * @param {Object} props Additional props applied to save element. - * @param {Object} blockType Block type. - * @param {Object} attributes Block attributes. - * @param {Object} skipPaths An object of keys and paths to skip serialization. + * @param {Object} props Additional props applied to save element. + * @param {Object} blockType Block type. + * @param {Object} attributes Block attributes. + * @param {?Record} skipPaths An object of keys and paths to skip serialization. * * @return {Object} Filtered props applied to save element. */ From d53333c5f12934dbb5c23bac11ce2babe1d6f112 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Mon, 16 Aug 2021 12:48:21 +1000 Subject: [PATCH 08/20] Move blockGap support to spacing.php --- lib/block-supports/dimensions.php | 54 ----------------------------- lib/block-supports/spacing.php | 56 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/lib/block-supports/dimensions.php b/lib/block-supports/dimensions.php index 2bf7abbdee208..2692b316d4db1 100644 --- a/lib/block-supports/dimensions.php +++ b/lib/block-supports/dimensions.php @@ -54,14 +54,6 @@ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { // Height support to be added in near future. // Width support to be added in near future. - if ( $has_gap_support ) { - $gap_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'blockGap' ), null ); - - if ( is_string( $gap_value ) ) { - $styles[] = sprintf( '--wp--style--block-gap: %s', $gap_value ); - } - } - return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); } @@ -80,50 +72,6 @@ function gutenberg_skip_dimensions_serialization( $block_type ) { $dimensions_support['__experimentalSkipSerialization']; } -/** - * Renders the dimensions support to the block wrapper, for supports that - * require block-level server-side rendering, for example blockGap support - * which uses CSS variables. - * - * @param string $block_content Rendered block content. - * @param array $block Block object. - * @return string Filtered block content. - */ -function gutenberg_render_dimensions_support( $block_content, $block ) { - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - $has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false ); - if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) { - return $block_content; - } - - $id = uniqid(); - $style = sprintf( - '.wp-container-%s { --wp--style--block-gap: %s; }', - $id, - esc_attr( $block['attrs']['style']['spacing']['blockGap'] ) - ); - - // This assumes the hook only applies to blocks with a single wrapper. - $content = preg_replace( - '/' . preg_quote( 'class="', '/' ) . '/', - 'class="wp-container-' . $id . ' ', - $block_content, - 1 - ); - - // Ideally styles should be loaded in the head, but blocks may be parsed - // after that, so loading in the footer for now. - // See https://core.trac.wordpress.org/ticket/53494. - add_action( - 'wp_footer', - function () use ( $style ) { - echo ''; - } - ); - - return $content; -} - // Register the block support. WP_Block_Supports::get_instance()->register( 'dimensions', @@ -132,5 +80,3 @@ function () use ( $style ) { 'apply' => 'gutenberg_apply_dimensions_support', ) ); - -add_filter( 'render_block', 'gutenberg_render_dimensions_support', 10, 2 ); diff --git a/lib/block-supports/spacing.php b/lib/block-supports/spacing.php index 78f5b59f90fb0..603bf9eea4ca8 100644 --- a/lib/block-supports/spacing.php +++ b/lib/block-supports/spacing.php @@ -45,6 +45,7 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) { $has_padding_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'padding' ), false ); $has_margin_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'margin' ), false ); + $has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false ); $styles = array(); if ( $has_padding_support ) { @@ -71,6 +72,14 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) { } } + if ( $has_gap_support ) { + $gap_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'blockGap' ), null ); + + if ( is_string( $gap_value ) ) { + $styles[] = sprintf( '--wp--style--block-gap: %s', $gap_value ); + } + } + return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); } @@ -90,6 +99,51 @@ function gutenberg_skip_spacing_serialization( $block_type ) { $spacing_support['__experimentalSkipSerialization']; } + +/** + * Renders the spacing support to the block wrapper, for supports that + * require block-level server-side rendering, for example blockGap support + * which uses CSS variables. + * + * @param string $block_content Rendered block content. + * @param array $block Block object. + * @return string Filtered block content. + */ +function gutenberg_render_spacing_support( $block_content, $block ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); + $has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false ); + if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) { + return $block_content; + } + + $id = uniqid(); + $style = sprintf( + '.wp-container-%s { --wp--style--block-gap: %s; }', + $id, + esc_attr( $block['attrs']['style']['spacing']['blockGap'] ) + ); + + // This assumes the hook only applies to blocks with a single wrapper. + $content = preg_replace( + '/' . preg_quote( 'class="', '/' ) . '/', + 'class="wp-container-' . $id . ' ', + $block_content, + 1 + ); + + // Ideally styles should be loaded in the head, but blocks may be parsed + // after that, so loading in the footer for now. + // See https://core.trac.wordpress.org/ticket/53494. + add_action( + 'wp_footer', + function () use ( $style ) { + echo ''; + } + ); + + return $content; +} + // Register the block support. WP_Block_Supports::get_instance()->register( 'spacing', @@ -98,3 +152,5 @@ function gutenberg_skip_spacing_serialization( $block_type ) { 'apply' => 'gutenberg_apply_spacing_support', ) ); + +add_filter( 'render_block', 'gutenberg_render_spacing_support', 10, 2 ); From 7643f7fdbacd33494a184030d3b29776eeb2bba9 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Mon, 16 Aug 2021 16:25:59 +1000 Subject: [PATCH 09/20] Add PHP tests for spacing gap support server-side rendering --- phpunit/block-supports/spacing-test.php | 174 ++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 phpunit/block-supports/spacing-test.php diff --git a/phpunit/block-supports/spacing-test.php b/phpunit/block-supports/spacing-test.php new file mode 100644 index 0000000000000..aaa60ee56584e --- /dev/null +++ b/phpunit/block-supports/spacing-test.php @@ -0,0 +1,174 @@ +Test'; + + function setUp() { + parent::setUp(); + + if ( empty( $GLOBALS['wp_styles'] ) ) { + $GLOBALS['wp_styles'] = null; + } + + $this->old_wp_styles = $GLOBALS['wp_styles']; + + remove_action( 'wp_default_styles', 'wp_default_styles' ); + remove_action( 'wp_print_styles', 'print_emoji_styles' ); + + $GLOBALS['wp_styles'] = new WP_Styles(); + $GLOBALS['wp_styles']->default_version = get_bloginfo( 'version' ); + + $GLOBALS['wp_scripts'] = new WP_Scripts(); + $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' ); + } + + function tearDown() { + unregister_block_type( 'test/test-block' ); + + $GLOBALS['wp_styles'] = $this->old_wp_styles; + + add_action( 'wp_default_styles', 'wp_default_styles' ); + add_action( 'wp_print_styles', 'print_emoji_styles' ); + + parent::tearDown(); + } + + private function get_footer_styles() { + ob_start(); + wp_footer(); + $wp_footer_output = ob_get_contents(); + ob_end_clean(); + + return $wp_footer_output; + } + + function test_spacing_gap_block_support_renders_block_style() { + $block = array( + 'blockName' => 'test/test-block', + 'attrs' => array( + 'style' => array( + 'spacing' => array( + 'blockGap' => '3em', + ), + ), + ), + ); + + $test_block_args = array( + 'api_version' => 2, + 'supports' => array( + 'spacing' => array( + 'blockGap' => true, + ), + ), + ); + + register_block_type( 'test/test-block', $test_block_args ); + $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); + $wp_footer_output = $this->get_footer_styles(); + + $expected_content_pattern = '/^
Test<\/div>$/'; + $matches = array(); + $found_match = preg_match( + $expected_content_pattern, + $render_output, + $matches + ); + + $this->assertSame( + 1, + $found_match, + sprintf( + "Rendered block content did not match pattern: %s\n\nActual block rendered content:\n\n%s", + $expected_content_pattern, + $render_output + ) + ); + + $container_id = $matches[1]; + + $this->assertEquals( + '', + $wp_footer_output + ); + } + + function test_spacing_gap_block_support_does_not_render_style_when_support_is_false() { + $block = array( + 'blockName' => 'test/test-block', + 'attrs' => array( + 'style' => array( + 'spacing' => array( + 'blockGap' => '3em', + ), + ), + ), + ); + + $test_block_args = array( + 'api_version' => 2, + 'supports' => array( + 'spacing' => array( + 'blockGap' => false, + ), + ), + ); + + register_block_type( 'test/test-block', $test_block_args ); + $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); + $wp_footer_output = $this->get_footer_styles(); + + $this->assertEquals( + $this->sample_block_content, + $render_output + ); + + $this->assertEquals( + '', + $wp_footer_output + ); + } + + function test_spacing_gap_block_support_does_not_render_style_when_gap_is_null() { + $block = array( + 'blockName' => 'test/test-block', + 'attrs' => array( + 'style' => array( + 'spacing' => array( + 'blockGap' => null, + ), + ), + ), + ); + + $test_block_args = array( + 'api_version' => 2, + 'supports' => array( + 'spacing' => array( + 'blockGap' => true, + ), + ), + ); + + register_block_type( 'test/test-block', $test_block_args ); + $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); + $wp_footer_output = $this->get_footer_styles(); + + $this->assertEquals( + $this->sample_block_content, + $render_output + ); + + $this->assertEquals( + '', + $wp_footer_output + ); + } +} From c9d87a5de408611962d2749126f47104e732f807 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Mon, 16 Aug 2021 17:05:41 +1000 Subject: [PATCH 10/20] Fix whitespace alignment in spacing block support tests --- phpunit/block-supports/spacing-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/block-supports/spacing-test.php b/phpunit/block-supports/spacing-test.php index aaa60ee56584e..fbc0363aefc87 100644 --- a/phpunit/block-supports/spacing-test.php +++ b/phpunit/block-supports/spacing-test.php @@ -32,7 +32,7 @@ function setUp() { function tearDown() { unregister_block_type( 'test/test-block' ); - $GLOBALS['wp_styles'] = $this->old_wp_styles; + $GLOBALS['wp_styles'] = $this->old_wp_styles; add_action( 'wp_default_styles', 'wp_default_styles' ); add_action( 'wp_print_styles', 'print_emoji_styles' ); @@ -122,7 +122,7 @@ function test_spacing_gap_block_support_does_not_render_style_when_support_is_fa ); register_block_type( 'test/test-block', $test_block_args ); - $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); + $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); $wp_footer_output = $this->get_footer_styles(); $this->assertEquals( @@ -158,7 +158,7 @@ function test_spacing_gap_block_support_does_not_render_style_when_gap_is_null() ); register_block_type( 'test/test-block', $test_block_args ); - $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); + $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); $wp_footer_output = $this->get_footer_styles(); $this->assertEquals( From edb506e6c931ccc62483aa14ace372a3883deaaa Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Tue, 17 Aug 2021 17:30:25 +1000 Subject: [PATCH 11/20] Refactor server-rendering to use an inline style, update tests --- lib/block-supports/spacing.php | 32 ++++---- phpunit/block-supports/spacing-test.php | 100 +++++++++--------------- 2 files changed, 50 insertions(+), 82 deletions(-) diff --git a/lib/block-supports/spacing.php b/lib/block-supports/spacing.php index 603bf9eea4ca8..7ae2773ebf08c 100644 --- a/lib/block-supports/spacing.php +++ b/lib/block-supports/spacing.php @@ -116,32 +116,30 @@ function gutenberg_render_spacing_support( $block_content, $block ) { return $block_content; } - $id = uniqid(); $style = sprintf( - '.wp-container-%s { --wp--style--block-gap: %s; }', - $id, + '--wp--style--block-gap: %s;', esc_attr( $block['attrs']['style']['spacing']['blockGap'] ) ); - // This assumes the hook only applies to blocks with a single wrapper. - $content = preg_replace( - '/' . preg_quote( 'class="', '/' ) . '/', - 'class="wp-container-' . $id . ' ', + // Attempt to update an existing style attribute on the wrapper element. + $injected_style = preg_replace( + '/^(.+?)(' . preg_quote( 'style="', '/' ) . ')(?=.+?>)/', + '$1$2' . $style . ' ', $block_content, 1 ); - // Ideally styles should be loaded in the head, but blocks may be parsed - // after that, so loading in the footer for now. - // See https://core.trac.wordpress.org/ticket/53494. - add_action( - 'wp_footer', - function () use ( $style ) { - echo ''; - } - ); + // If there is no existing style attribute, add one to the wrapper element. + if ( $injected_style === $block_content ) { + $injected_style = preg_replace( + '/<([a-zA-Z]+)/', + '<$1 style="' . $style . '"', + $block_content, + 1 + ); + }; - return $content; + return $injected_style; } // Register the block support. diff --git a/phpunit/block-supports/spacing-test.php b/phpunit/block-supports/spacing-test.php index fbc0363aefc87..b415aabf65fb2 100644 --- a/phpunit/block-supports/spacing-test.php +++ b/phpunit/block-supports/spacing-test.php @@ -7,49 +7,49 @@ */ class WP_Block_Supports_Spacing_Test extends WP_UnitTestCase { - private $old_wp_styles; private $sample_block_content = '
Test
'; function setUp() { parent::setUp(); - - if ( empty( $GLOBALS['wp_styles'] ) ) { - $GLOBALS['wp_styles'] = null; - } - - $this->old_wp_styles = $GLOBALS['wp_styles']; - - remove_action( 'wp_default_styles', 'wp_default_styles' ); - remove_action( 'wp_print_styles', 'print_emoji_styles' ); - - $GLOBALS['wp_styles'] = new WP_Styles(); - $GLOBALS['wp_styles']->default_version = get_bloginfo( 'version' ); - - $GLOBALS['wp_scripts'] = new WP_Scripts(); - $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' ); } function tearDown() { unregister_block_type( 'test/test-block' ); - $GLOBALS['wp_styles'] = $this->old_wp_styles; - - add_action( 'wp_default_styles', 'wp_default_styles' ); - add_action( 'wp_print_styles', 'print_emoji_styles' ); - parent::tearDown(); } - private function get_footer_styles() { - ob_start(); - wp_footer(); - $wp_footer_output = ob_get_contents(); - ob_end_clean(); + function test_spacing_gap_block_support_renders_block_inline_style() { + $block = array( + 'blockName' => 'test/test-block', + 'attrs' => array( + 'style' => array( + 'spacing' => array( + 'blockGap' => '3em', + ), + ), + ), + ); + + $test_block_args = array( + 'api_version' => 2, + 'supports' => array( + 'spacing' => array( + 'blockGap' => true, + ), + ), + ); + + register_block_type( 'test/test-block', $test_block_args ); + $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); - return $wp_footer_output; + $this->assertSame( + '
Test
', + $render_output + ); } - function test_spacing_gap_block_support_renders_block_style() { + function test_spacing_gap_block_support_renders_appended_block_inline_style() { $block = array( 'blockName' => 'test/test-block', 'attrs' => array( @@ -71,32 +71,14 @@ function test_spacing_gap_block_support_renders_block_style() { ); register_block_type( 'test/test-block', $test_block_args ); - $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); - $wp_footer_output = $this->get_footer_styles(); - - $expected_content_pattern = '/^
Test<\/div>$/'; - $matches = array(); - $found_match = preg_match( - $expected_content_pattern, - $render_output, - $matches + $render_output = gutenberg_render_spacing_support( + '

Test

', + $block ); $this->assertSame( - 1, - $found_match, - sprintf( - "Rendered block content did not match pattern: %s\n\nActual block rendered content:\n\n%s", - $expected_content_pattern, - $render_output - ) - ); - - $container_id = $matches[1]; - - $this->assertEquals( - '', - $wp_footer_output + '

Test

', + $render_output ); } @@ -122,18 +104,12 @@ function test_spacing_gap_block_support_does_not_render_style_when_support_is_fa ); register_block_type( 'test/test-block', $test_block_args ); - $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); - $wp_footer_output = $this->get_footer_styles(); + $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); $this->assertEquals( $this->sample_block_content, $render_output ); - - $this->assertEquals( - '', - $wp_footer_output - ); } function test_spacing_gap_block_support_does_not_render_style_when_gap_is_null() { @@ -158,17 +134,11 @@ function test_spacing_gap_block_support_does_not_render_style_when_gap_is_null() ); register_block_type( 'test/test-block', $test_block_args ); - $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); - $wp_footer_output = $this->get_footer_styles(); + $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); $this->assertEquals( $this->sample_block_content, $render_output ); - - $this->assertEquals( - '', - $wp_footer_output - ); } } From 016f4a7e4d6294b5a737518bbe3216433ddfb993 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Wed, 18 Aug 2021 16:57:16 +1000 Subject: [PATCH 12/20] Tweak regex to fix issue where wrapper has no style attribute, but child node does --- lib/block-supports/spacing.php | 26 ++++-- phpunit/block-supports/spacing-test.php | 116 +++++++++--------------- 2 files changed, 60 insertions(+), 82 deletions(-) diff --git a/lib/block-supports/spacing.php b/lib/block-supports/spacing.php index 7ae2773ebf08c..35aa827477fed 100644 --- a/lib/block-supports/spacing.php +++ b/lib/block-supports/spacing.php @@ -101,30 +101,38 @@ function gutenberg_skip_spacing_serialization( $block_type ) { /** - * Renders the spacing support to the block wrapper, for supports that - * require block-level server-side rendering, for example blockGap support - * which uses CSS variables. + * Renders the spacing gap support to the block wrapper, to ensure + * that the CSS variable is rendered in all environments. * * @param string $block_content Rendered block content. * @param array $block Block object. * @return string Filtered block content. */ -function gutenberg_render_spacing_support( $block_content, $block ) { +function gutenberg_render_spacing_gap_support( $block_content, $block ) { $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); $has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false ); if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) { return $block_content; } + $gap_value = $block['attrs']['style']['spacing']['blockGap']; + + // Skip if gap value contains unsupported characters. + // Regex for CSS value borrowed from `safecss_filter_attr`, and used here + // because we only want to match against the value, not the CSS attribute. + if ( preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ) { + return $block_content; + } + $style = sprintf( - '--wp--style--block-gap: %s;', - esc_attr( $block['attrs']['style']['spacing']['blockGap'] ) + '--wp--style--block-gap: %s', + esc_attr( $gap_value ) ); // Attempt to update an existing style attribute on the wrapper element. $injected_style = preg_replace( - '/^(.+?)(' . preg_quote( 'style="', '/' ) . ')(?=.+?>)/', - '$1$2' . $style . ' ', + '/^([^>.]+?)(' . preg_quote( 'style="', '/' ) . ')(?=.+?>)/', + '$1$2' . $style . '; ', $block_content, 1 ); @@ -151,4 +159,4 @@ function gutenberg_render_spacing_support( $block_content, $block ) { ) ); -add_filter( 'render_block', 'gutenberg_render_spacing_support', 10, 2 ); +add_filter( 'render_block', 'gutenberg_render_spacing_gap_support', 10, 2 ); diff --git a/phpunit/block-supports/spacing-test.php b/phpunit/block-supports/spacing-test.php index b415aabf65fb2..0b9dd0f22d96d 100644 --- a/phpunit/block-supports/spacing-test.php +++ b/phpunit/block-supports/spacing-test.php @@ -9,18 +9,13 @@ class WP_Block_Supports_Spacing_Test extends WP_UnitTestCase { private $sample_block_content = '
Test
'; + private $test_gap_block_value = array(); + private $test_gap_block_args = array(); + function setUp() { parent::setUp(); - } - - function tearDown() { - unregister_block_type( 'test/test-block' ); - parent::tearDown(); - } - - function test_spacing_gap_block_support_renders_block_inline_style() { - $block = array( + $this->test_gap_block_value = array( 'blockName' => 'test/test-block', 'attrs' => array( 'style' => array( @@ -31,7 +26,7 @@ function test_spacing_gap_block_support_renders_block_inline_style() { ), ); - $test_block_args = array( + $this->test_gap_block_args = array( 'api_version' => 2, 'supports' => array( 'spacing' => array( @@ -39,41 +34,45 @@ function test_spacing_gap_block_support_renders_block_inline_style() { ), ), ); + } - register_block_type( 'test/test-block', $test_block_args ); - $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); + function tearDown() { + unregister_block_type( 'test/test-block' ); + + parent::tearDown(); + } + + function test_spacing_gap_block_support_renders_block_inline_style() { + register_block_type( 'test/test-block', $this->test_gap_block_args ); + $render_output = gutenberg_render_spacing_gap_support( + $this->sample_block_content, + $this->test_gap_block_value + ); $this->assertSame( - '
Test
', + '
Test
', $render_output ); } - function test_spacing_gap_block_support_renders_appended_block_inline_style() { - $block = array( - 'blockName' => 'test/test-block', - 'attrs' => array( - 'style' => array( - 'spacing' => array( - 'blockGap' => '3em', - ), - ), - ), + function test_spacing_gap_block_support_renders_block_inline_style_with_inner_tag() { + register_block_type( 'test/test-block', $this->test_gap_block_args ); + $render_output = gutenberg_render_spacing_gap_support( + '

Test

', + $this->test_gap_block_value ); - $test_block_args = array( - 'api_version' => 2, - 'supports' => array( - 'spacing' => array( - 'blockGap' => true, - ), - ), + $this->assertSame( + '

Test

', + $render_output ); + } - register_block_type( 'test/test-block', $test_block_args ); - $render_output = gutenberg_render_spacing_support( + function test_spacing_gap_block_support_renders_appended_block_inline_style() { + register_block_type( 'test/test-block', $this->test_gap_block_args ); + $render_output = gutenberg_render_spacing_gap_support( '

Test

', - $block + $this->test_gap_block_value ); $this->assertSame( @@ -83,29 +82,14 @@ function test_spacing_gap_block_support_renders_appended_block_inline_style() { } function test_spacing_gap_block_support_does_not_render_style_when_support_is_false() { - $block = array( - 'blockName' => 'test/test-block', - 'attrs' => array( - 'style' => array( - 'spacing' => array( - 'blockGap' => '3em', - ), - ), - ), - ); + $this->test_gap_block_args['supports']['spacing']['blockGap'] = false; - $test_block_args = array( - 'api_version' => 2, - 'supports' => array( - 'spacing' => array( - 'blockGap' => false, - ), - ), + register_block_type( 'test/test-block', $test_gap_block_args ); + $render_output = gutenberg_render_spacing_gap_support( + $this->sample_block_content, + $this->test_gap_block_value ); - register_block_type( 'test/test-block', $test_block_args ); - $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); - $this->assertEquals( $this->sample_block_content, $render_output @@ -113,28 +97,14 @@ function test_spacing_gap_block_support_does_not_render_style_when_support_is_fa } function test_spacing_gap_block_support_does_not_render_style_when_gap_is_null() { - $block = array( - 'blockName' => 'test/test-block', - 'attrs' => array( - 'style' => array( - 'spacing' => array( - 'blockGap' => null, - ), - ), - ), - ); - - $test_block_args = array( - 'api_version' => 2, - 'supports' => array( - 'spacing' => array( - 'blockGap' => true, - ), - ), - ); + $this->test_gap_block_value['attrs']['style']['spacing']['blockGap'] = null; + $this->test_gap_block_args['supports']['spacing']['blockGap'] = true; register_block_type( 'test/test-block', $test_block_args ); - $render_output = gutenberg_render_spacing_support( $this->sample_block_content, $block ); + $render_output = gutenberg_render_spacing_gap_support( + $this->sample_block_content, + $this->test_gap_block_value + ); $this->assertEquals( $this->sample_block_content, From 1a22b15ef23019ac35daee0567d4f54005b402c1 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Wed, 18 Aug 2021 17:06:31 +1000 Subject: [PATCH 13/20] Add additional tests --- phpunit/block-supports/spacing-test.php | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/phpunit/block-supports/spacing-test.php b/phpunit/block-supports/spacing-test.php index 0b9dd0f22d96d..d0caa13a0dff5 100644 --- a/phpunit/block-supports/spacing-test.php +++ b/phpunit/block-supports/spacing-test.php @@ -68,6 +68,19 @@ function test_spacing_gap_block_support_renders_block_inline_style_with_inner_ta ); } + function test_spacing_gap_block_support_renders_block_inline_style_with_no_other_attributes() { + register_block_type( 'test/test-block', $this->test_gap_block_args ); + $render_output = gutenberg_render_spacing_gap_support( + '

Test

', + $this->test_gap_block_value + ); + + $this->assertSame( + '

Test

', + $render_output + ); + } + function test_spacing_gap_block_support_renders_appended_block_inline_style() { register_block_type( 'test/test-block', $this->test_gap_block_args ); $render_output = gutenberg_render_spacing_gap_support( @@ -111,4 +124,20 @@ function test_spacing_gap_block_support_does_not_render_style_when_gap_is_null() $render_output ); } + + function test_spacing_gap_block_support_does_not_render_style_when_gap_is_illegal_value() { + $this->test_gap_block_value['attrs']['style']['spacing']['blockGap'] = '" javascript="alert("hello");'; + $this->test_gap_block_args['supports']['spacing']['blockGap'] = true; + + register_block_type( 'test/test-block', $test_block_args ); + $render_output = gutenberg_render_spacing_gap_support( + $this->sample_block_content, + $this->test_gap_block_value + ); + + $this->assertEquals( + $this->sample_block_content, + $render_output + ); + } } From 0d52de9435e06b2a747eff1ab6704933a0dcf83d Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Wed, 18 Aug 2021 17:07:51 +1000 Subject: [PATCH 14/20] Align equals signs correctly --- phpunit/block-supports/spacing-test.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/phpunit/block-supports/spacing-test.php b/phpunit/block-supports/spacing-test.php index d0caa13a0dff5..6c33c86a39980 100644 --- a/phpunit/block-supports/spacing-test.php +++ b/phpunit/block-supports/spacing-test.php @@ -8,9 +8,8 @@ class WP_Block_Supports_Spacing_Test extends WP_UnitTestCase { private $sample_block_content = '
Test
'; - private $test_gap_block_value = array(); - private $test_gap_block_args = array(); + private $test_gap_block_args = array(); function setUp() { parent::setUp(); @@ -111,7 +110,7 @@ function test_spacing_gap_block_support_does_not_render_style_when_support_is_fa function test_spacing_gap_block_support_does_not_render_style_when_gap_is_null() { $this->test_gap_block_value['attrs']['style']['spacing']['blockGap'] = null; - $this->test_gap_block_args['supports']['spacing']['blockGap'] = true; + $this->test_gap_block_args['supports']['spacing']['blockGap'] = true; register_block_type( 'test/test-block', $test_block_args ); $render_output = gutenberg_render_spacing_gap_support( @@ -127,7 +126,7 @@ function test_spacing_gap_block_support_does_not_render_style_when_gap_is_null() function test_spacing_gap_block_support_does_not_render_style_when_gap_is_illegal_value() { $this->test_gap_block_value['attrs']['style']['spacing']['blockGap'] = '" javascript="alert("hello");'; - $this->test_gap_block_args['supports']['spacing']['blockGap'] = true; + $this->test_gap_block_args['supports']['spacing']['blockGap'] = true; register_block_type( 'test/test-block', $test_block_args ); $render_output = gutenberg_render_spacing_gap_support( From f8a09329ef60b849dfdc40f0afdd1dd3a8806a3d Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Wed, 18 Aug 2021 17:19:57 +1000 Subject: [PATCH 15/20] Fix typo / phpcs warning --- phpunit/block-supports/spacing-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/block-supports/spacing-test.php b/phpunit/block-supports/spacing-test.php index 6c33c86a39980..1038cfcb5ca97 100644 --- a/phpunit/block-supports/spacing-test.php +++ b/phpunit/block-supports/spacing-test.php @@ -96,7 +96,7 @@ function test_spacing_gap_block_support_renders_appended_block_inline_style() { function test_spacing_gap_block_support_does_not_render_style_when_support_is_false() { $this->test_gap_block_args['supports']['spacing']['blockGap'] = false; - register_block_type( 'test/test-block', $test_gap_block_args ); + register_block_type( 'test/test-block', $this->test_gap_block_args ); $render_output = gutenberg_render_spacing_gap_support( $this->sample_block_content, $this->test_gap_block_value @@ -112,7 +112,7 @@ function test_spacing_gap_block_support_does_not_render_style_when_gap_is_null() $this->test_gap_block_value['attrs']['style']['spacing']['blockGap'] = null; $this->test_gap_block_args['supports']['spacing']['blockGap'] = true; - register_block_type( 'test/test-block', $test_block_args ); + register_block_type( 'test/test-block', $this->test_gap_block_args ); $render_output = gutenberg_render_spacing_gap_support( $this->sample_block_content, $this->test_gap_block_value @@ -128,7 +128,7 @@ function test_spacing_gap_block_support_does_not_render_style_when_gap_is_illega $this->test_gap_block_value['attrs']['style']['spacing']['blockGap'] = '" javascript="alert("hello");'; $this->test_gap_block_args['supports']['spacing']['blockGap'] = true; - register_block_type( 'test/test-block', $test_block_args ); + register_block_type( 'test/test-block', $this->test_gap_block_args ); $render_output = gutenberg_render_spacing_gap_support( $this->sample_block_content, $this->test_gap_block_value From a974db1cf86d1f27c8153981bf9797f870e17b25 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Wed, 18 Aug 2021 17:28:04 +1000 Subject: [PATCH 16/20] Remove gap from apply_spacing_support so it isn't applied twice for dynamic blocks --- lib/block-supports/spacing.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lib/block-supports/spacing.php b/lib/block-supports/spacing.php index 35aa827477fed..4ad5f643e527e 100644 --- a/lib/block-supports/spacing.php +++ b/lib/block-supports/spacing.php @@ -45,7 +45,6 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) { $has_padding_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'padding' ), false ); $has_margin_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'margin' ), false ); - $has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false ); $styles = array(); if ( $has_padding_support ) { @@ -72,14 +71,6 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) { } } - if ( $has_gap_support ) { - $gap_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'blockGap' ), null ); - - if ( is_string( $gap_value ) ) { - $styles[] = sprintf( '--wp--style--block-gap: %s', $gap_value ); - } - } - return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); } From 276837e39f0dba2c18f979c3c7708c2c145e7510 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 27 Aug 2021 11:58:59 +1000 Subject: [PATCH 17/20] Remove duplicate block gap style attribute from constants --- packages/blocks/src/api/constants.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/blocks/src/api/constants.js b/packages/blocks/src/api/constants.js index eb34a7a63818d..8971264238089 100644 --- a/packages/blocks/src/api/constants.js +++ b/packages/blocks/src/api/constants.js @@ -72,10 +72,6 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = { value: [ 'typography', 'fontWeight' ], support: [ 'typography', '__experimentalFontWeight' ], }, - '--wp--style--block-gap': { - value: [ 'spacing', 'blockGap' ], - support: [ 'spacing', 'blockGap' ], - }, lineHeight: { value: [ 'typography', 'lineHeight' ], support: [ 'typography', 'lineHeight' ], @@ -114,6 +110,7 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = { }, '--wp--style--block-gap': { value: [ 'spacing', 'blockGap' ], + support: [ 'spacing', 'blockGap' ], }, }; From 530d8b5a86856419e0f4d5bf79c33cf32276cfaa Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 27 Aug 2021 12:31:41 +1000 Subject: [PATCH 18/20] Tweak regex --- lib/block-supports/spacing.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/block-supports/spacing.php b/lib/block-supports/spacing.php index 4ad5f643e527e..ca7b77f43864b 100644 --- a/lib/block-supports/spacing.php +++ b/lib/block-supports/spacing.php @@ -131,8 +131,8 @@ function gutenberg_render_spacing_gap_support( $block_content, $block ) { // If there is no existing style attribute, add one to the wrapper element. if ( $injected_style === $block_content ) { $injected_style = preg_replace( - '/<([a-zA-Z]+)/', - '<$1 style="' . $style . '"', + '/<([a-zA-Z0-9]+)([ >])/', + '<$1 style="' . $style . '"$2', $block_content, 1 ); From 9a421ca97332dee2ee0ecd09271ec49142d3d66f Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Mon, 30 Aug 2021 15:31:53 +1000 Subject: [PATCH 19/20] Rename support from customBlockGap to blockGap --- lib/class-wp-theme-json-gutenberg.php | 2 +- lib/theme.json | 2 +- packages/block-editor/src/hooks/gap.js | 2 +- packages/edit-site/src/components/sidebar/dimensions-panel.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index c2630797e6039..fc93fb9fe549d 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -99,7 +99,7 @@ class WP_Theme_JSON_Gutenberg { 'wideSize' => null, ), 'spacing' => array( - 'customBlockGap' => null, + 'blockGap' => null, 'customMargin' => null, 'customPadding' => null, 'units' => null, diff --git a/lib/theme.json b/lib/theme.json index e6844cddc1760..5d952b381ef93 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -211,7 +211,7 @@ ] }, "spacing": { - "customBlockGap": false, + "blockGap": false, "customMargin": false, "customPadding": false, "units": [ "px", "em", "rem", "vh", "vw", "%" ] diff --git a/packages/block-editor/src/hooks/gap.js b/packages/block-editor/src/hooks/gap.js index 29d64dcf0239b..d20b94c2b72cd 100644 --- a/packages/block-editor/src/hooks/gap.js +++ b/packages/block-editor/src/hooks/gap.js @@ -66,7 +66,7 @@ export function resetGap( { attributes = {}, setAttributes } ) { * @return {boolean} Whether the gap setting is disabled. */ export function useIsGapDisabled( { name: blockName } = {} ) { - const isDisabled = ! useSetting( 'spacing.customBlockGap' ); + const isDisabled = ! useSetting( 'spacing.blockGap' ); return ! hasGapSupport( blockName ) || isDisabled; } diff --git a/packages/edit-site/src/components/sidebar/dimensions-panel.js b/packages/edit-site/src/components/sidebar/dimensions-panel.js index ab156f7f08896..72059768b3018 100644 --- a/packages/edit-site/src/components/sidebar/dimensions-panel.js +++ b/packages/edit-site/src/components/sidebar/dimensions-panel.js @@ -39,7 +39,7 @@ function useHasMargin( { name, supports } ) { } function useHasGap( { name, supports } ) { - const settings = useSetting( 'spacing.customBlockGap', name ); + const settings = useSetting( 'spacing.blockGap', name ); return settings && supports.includes( '--wp--style--block-gap' ); } From 779afe586341bdd8c78e627988dd00a97cfaf4b2 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Mon, 30 Aug 2021 15:37:39 +1000 Subject: [PATCH 20/20] Align whitespace --- lib/class-wp-theme-json-gutenberg.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index fc93fb9fe549d..f63f3f40bbb42 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -99,10 +99,10 @@ class WP_Theme_JSON_Gutenberg { 'wideSize' => null, ), 'spacing' => array( - 'blockGap' => null, - 'customMargin' => null, - 'customPadding' => null, - 'units' => null, + 'blockGap' => null, + 'customMargin' => null, + 'customPadding' => null, + 'units' => null, ), 'typography' => array( 'customFontSize' => null,