-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Centralize style & support mappings for blocks #25185
Changes from all commits
d51a5de
e78fd5a
b4b50c8
7c9560a
cc79a34
6821e59
4b882e4
68f8e6c
9d1b159
391336a
6b635ad
7a44eaa
b6229f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,26 +262,50 @@ function gutenberg_experimental_global_styles_get_theme() { | |
} | ||
|
||
/** | ||
* Returns the style features a particular block supports. | ||
* Return how the style property is structured. | ||
* | ||
* @param array $supports The block supports array. | ||
* @return array Style property structure. | ||
*/ | ||
function gutenberg_experimental_global_styles_get_style_property() { | ||
return array( | ||
'line-height' => array( 'typography', 'lineHeight' ), | ||
'font-size' => array( 'typography', 'fontSize' ), | ||
'background' => array( 'color', 'gradient' ), | ||
'background-color' => array( 'color', 'background' ), | ||
'color' => array( 'color', 'text' ), | ||
'--wp--style--color--link' => array( 'color', 'link' ), | ||
); | ||
} | ||
|
||
/** | ||
* Return how the support keys are structured. | ||
* | ||
* @return array Style features supported by the block. | ||
* @return array Support keys structure. | ||
*/ | ||
function gutenberg_experimental_global_styles_get_supported_styles( $supports ) { | ||
$style_features = array( | ||
function gutenberg_experimental_global_styles_get_support_keys() { | ||
return array( | ||
'--wp--style--color--link' => array( '__experimentalColor', 'linkColor' ), | ||
'background-color' => array( '__experimentalColor' ), | ||
'backgroundColor' => array( '__experimentalColor' ), | ||
'background' => array( '__experimentalColor', 'gradients' ), | ||
'color' => array( '__experimentalColor' ), | ||
'font-size' => array( '__experimentalFontSize' ), | ||
'line-height' => array( '__experimentalLineHeight' ), | ||
'fontSize' => array( '__experimentalFontSize' ), | ||
'lineHeight' => array( '__experimentalLineHeight' ), | ||
); | ||
} | ||
|
||
/** | ||
* Returns the style features a particular block supports. | ||
* | ||
* @param array $supports The block supports array. | ||
* | ||
* @return array Style features supported by the block. | ||
*/ | ||
function gutenberg_experimental_global_styles_get_supported_styles( $supports ) { | ||
$support_keys = gutenberg_experimental_global_styles_get_support_keys(); | ||
$supported_features = array(); | ||
foreach ( $style_features as $style_feature => $path ) { | ||
foreach ( $support_keys as $key => $path ) { | ||
if ( gutenberg_experimental_get( $supports, $path ) ) { | ||
$supported_features[] = $style_feature; | ||
$supported_features[] = $key; | ||
} | ||
} | ||
|
||
|
@@ -385,17 +409,9 @@ function gutenberg_experimental_global_styles_get_block_data() { | |
* @return array Containing a set of css rules. | ||
*/ | ||
function gutenberg_experimental_global_styles_flatten_styles_tree( $styles ) { | ||
$mappings = array( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No changes here, only extracted to a separate function. |
||
'line-height' => array( 'typography', 'lineHeight' ), | ||
'font-size' => array( 'typography', 'fontSize' ), | ||
'background' => array( 'color', 'gradient' ), | ||
'background-color' => array( 'color', 'background' ), | ||
'color' => array( 'color', 'text' ), | ||
'--wp--style--color--link' => array( 'color', 'link' ), | ||
); | ||
$mappings = gutenberg_experimental_global_styles_get_style_property(); | ||
|
||
$result = array(); | ||
|
||
foreach ( $mappings as $key => $path ) { | ||
$value = gutenberg_experimental_get( $styles, $path, null ); | ||
if ( null !== $value ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import { | |
useMemo, | ||
} from '@wordpress/element'; | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
import { __EXPERIMENTAL_STYLE_PROPERTY as STYLE_PROPERTY } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -21,8 +22,8 @@ import getGlobalStyles from './global-styles-renderer'; | |
|
||
const GlobalStylesContext = createContext( { | ||
/* eslint-disable no-unused-vars */ | ||
getProperty: ( context, path ) => {}, | ||
setProperty: ( context, path, newValue ) => {}, | ||
getStyleProperty: ( context, propertyName ) => {}, | ||
setStyleProperty: ( context, propertyName, newValue ) => {}, | ||
globalContext: {}, | ||
/* eslint-enable no-unused-vars */ | ||
} ); | ||
|
@@ -44,16 +45,19 @@ export default ( { children, baseStyles, contexts } ) => { | |
const nextValue = useMemo( | ||
() => ( { | ||
contexts, | ||
getProperty: ( context, path ) => | ||
get( userStyles?.[ context ]?.styles, path ), | ||
setProperty: ( context, path, newValue ) => { | ||
getStyleProperty: ( context, propertyName ) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we probably also need the old setters, otherwise for example to configure presets using the UI how are we going to do it? We also need setters and getters in global styles that don't map to a CSS property. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So far, I don't believe we need anything else. Note that with the old format you could only change properties within My understanding is that we'll need to create new presets for users, but I'm not sure how that's going to work because we're going to need access to both theme and user presets at the same time (unlike the current styles or features where we merge both). Perhaps in that case we could have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes true we could only change style paths anyway. So I guess the change is positive :) |
||
get( | ||
userStyles?.[ context ]?.styles, | ||
STYLE_PROPERTY[ propertyName ] | ||
), | ||
setStyleProperty: ( context, propertyName, newValue ) => { | ||
const newContent = { ...userStyles }; | ||
let contextStyles = newContent?.[ context ]?.styles; | ||
if ( ! contextStyles ) { | ||
contextStyles = {}; | ||
set( newContent, [ context, 'styles' ], contextStyles ); | ||
} | ||
set( contextStyles, path, newValue ); | ||
set( contextStyles, STYLE_PROPERTY[ propertyName ], newValue ); | ||
setContent( JSON.stringify( newContent ) ); | ||
}, | ||
} ), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,59 +7,54 @@ import { __ } from '@wordpress/i18n'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
BACKGROUND_COLOR, | ||
GRADIENT_COLOR, | ||
LINK_COLOR, | ||
TEXT_COLOR, | ||
} from '../editor/utils'; | ||
import { LINK_COLOR } from '../editor/utils'; | ||
|
||
export default ( { | ||
context: { supports, name }, | ||
getProperty, | ||
setProperty, | ||
getStyleProperty, | ||
setStyleProperty, | ||
} ) => { | ||
if ( | ||
! supports.includes( TEXT_COLOR ) && | ||
! supports.includes( BACKGROUND_COLOR ) && | ||
! supports.includes( GRADIENT_COLOR ) && | ||
! supports.includes( 'color' ) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These keys are the ones that were updated in the server ( |
||
! supports.includes( 'backgrounColor' ) && | ||
! supports.includes( 'background' ) && | ||
! supports.includes( LINK_COLOR ) | ||
) { | ||
return null; | ||
} | ||
|
||
const settings = []; | ||
|
||
if ( supports.includes( TEXT_COLOR ) ) { | ||
if ( supports.includes( 'color' ) ) { | ||
settings.push( { | ||
colorValue: getProperty( name, [ 'color', 'text' ] ), | ||
colorValue: getStyleProperty( name, 'color' ), | ||
onColorChange: ( value ) => | ||
setProperty( name, [ 'color', 'text' ], value ), | ||
setStyleProperty( name, 'color', value ), | ||
label: __( 'Text color' ), | ||
} ); | ||
} | ||
|
||
let backgroundSettings = {}; | ||
if ( supports.includes( BACKGROUND_COLOR ) ) { | ||
if ( supports.includes( 'backgroundColor' ) ) { | ||
backgroundSettings = { | ||
colorValue: getProperty( name, [ 'color', 'background' ] ), | ||
colorValue: getStyleProperty( name, 'backgroundColor' ), | ||
onColorChange: ( value ) => | ||
setProperty( name, [ 'color', 'background' ], value ), | ||
setStyleProperty( name, 'backgroundColor', value ), | ||
}; | ||
} | ||
|
||
let gradientSettings = {}; | ||
if ( supports.includes( GRADIENT_COLOR ) ) { | ||
if ( supports.includes( 'background' ) ) { | ||
gradientSettings = { | ||
gradientValue: getProperty( name, [ 'color', 'gradient' ] ), | ||
gradientValue: getStyleProperty( name, 'background' ), | ||
onGradientChange: ( value ) => | ||
setProperty( name, [ 'color', 'gradient' ], value ), | ||
setStyleProperty( name, 'background', value ), | ||
}; | ||
} | ||
|
||
if ( | ||
supports.includes( GRADIENT_COLOR ) || | ||
supports.includes( BACKGROUND_COLOR ) | ||
supports.includes( 'background' ) || | ||
supports.includes( 'backgroundColor' ) | ||
) { | ||
settings.push( { | ||
...backgroundSettings, | ||
|
@@ -70,9 +65,9 @@ export default ( { | |
|
||
if ( supports.includes( LINK_COLOR ) ) { | ||
settings.push( { | ||
colorValue: getProperty( name, [ 'color', 'link' ] ), | ||
colorValue: getStyleProperty( name, LINK_COLOR ), | ||
onColorChange: ( value ) => | ||
setProperty( name, [ 'color', 'link' ], value ), | ||
setStyleProperty( name, LINK_COLOR, value ), | ||
label: __( 'Link color' ), | ||
} ); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These keys were renamed to use the same conventions as client-side (camelCase over kebab-case), making everything more coherent and less computation-demanding.