-
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 8 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 |
---|---|---|
|
@@ -10,3 +10,18 @@ export const DEPRECATED_ENTRY_KEYS = [ | |
'migrate', | ||
'isEligible', | ||
]; | ||
|
||
export const LINK_COLOR = '--wp--style--color--link'; | ||
|
||
export const STYLE_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. I think this is a good place for this kind of info, as it speaks about how the block attributes are structured. However, I don't feel super strongly about this. If this is a blocker I'm fine moving it to any other place. 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 guess we should prefix the constant with experimental. |
||
[ LINK_COLOR ]: [ 'color', 'link' ], | ||
background: [ 'color', 'gradient' ], | ||
backgroundColor: [ 'color', 'background' ], | ||
color: [ 'color', 'text' ], | ||
fontSize: [ 'typography', 'fontSize' ], | ||
lineHeight: [ 'typography', 'lineHeight' ], | ||
paddingBottom: [ 'spacing', 'padding', 'bottom' ], | ||
paddingLeft: [ 'spacing', 'padding', 'left' ], | ||
paddingRight: [ 'spacing', 'padding', 'right' ], | ||
paddingTop: [ 'spacing', 'padding', 'top' ], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,63 +3,54 @@ | |
*/ | ||
import { __experimentalPanelColorGradientSettings as PanelColorGradientSettings } from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
BACKGROUND_COLOR, | ||
GRADIENT_COLOR, | ||
LINK_COLOR, | ||
TEXT_COLOR, | ||
} from '../editor/utils'; | ||
import { STYLE_PROPERTY, LINK_COLOR } from '@wordpress/blocks'; | ||
|
||
export default ( { | ||
context: { supports, name }, | ||
getProperty, | ||
setProperty, | ||
} ) => { | ||
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: getProperty( name, STYLE_PROPERTY.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. Personally, this is the major advantage of centralization, and what pushed me to create this PR. Controls don't need to know what's the path of the property they want to access. 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. If the idea is to let control not need to know the shape of the property they want to access, I guess Of course, we may need to change something that does not contain a property e.g: a preset. So I guess in the future we may have a get and setter that receives the property directly and one that receives the paths. But for now, what we have is ok. 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. |
||
onColorChange: ( value ) => | ||
setProperty( name, [ 'color', 'text' ], value ), | ||
setProperty( name, STYLE_PROPERTY.color, value ), | ||
label: __( 'Text color' ), | ||
} ); | ||
} | ||
|
||
let backgroundSettings = {}; | ||
if ( supports.includes( BACKGROUND_COLOR ) ) { | ||
if ( supports.includes( 'backgroundColor' ) ) { | ||
backgroundSettings = { | ||
colorValue: getProperty( name, [ 'color', 'background' ] ), | ||
colorValue: getProperty( name, STYLE_PROPERTY.backgroundColor ), | ||
onColorChange: ( value ) => | ||
setProperty( name, [ 'color', 'background' ], value ), | ||
setProperty( name, STYLE_PROPERTY.backgroundColor, value ), | ||
}; | ||
} | ||
|
||
let gradientSettings = {}; | ||
if ( supports.includes( GRADIENT_COLOR ) ) { | ||
if ( supports.includes( 'background' ) ) { | ||
gradientSettings = { | ||
gradientValue: getProperty( name, [ 'color', 'gradient' ] ), | ||
gradientValue: getProperty( name, STYLE_PROPERTY.background ), | ||
onGradientChange: ( value ) => | ||
setProperty( name, [ 'color', 'gradient' ], value ), | ||
setProperty( name, STYLE_PROPERTY.background, value ), | ||
}; | ||
} | ||
|
||
if ( | ||
supports.includes( GRADIENT_COLOR ) || | ||
supports.includes( BACKGROUND_COLOR ) | ||
supports.includes( 'background' ) || | ||
supports.includes( 'backgroundColor' ) | ||
) { | ||
settings.push( { | ||
...backgroundSettings, | ||
|
@@ -70,9 +61,9 @@ export default ( { | |
|
||
if ( supports.includes( LINK_COLOR ) ) { | ||
settings.push( { | ||
colorValue: getProperty( name, [ 'color', 'link' ] ), | ||
colorValue: getProperty( name, STYLE_PROPERTY[ LINK_COLOR ] ), | ||
onColorChange: ( value ) => | ||
setProperty( name, [ 'color', 'link' ], value ), | ||
setProperty( name, STYLE_PROPERTY[ 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.