Skip to content

Commit

Permalink
Update: Make global styles shape consistent with local styles shape.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jun 2, 2020
1 parent 9c4d9a5 commit d35b123
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 37 deletions.
5 changes: 5 additions & 0 deletions lib/experimental-default-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
"typography": {
"dropCap": false
}
},
"styles": {
"color": {
"link": "#00e"
}
}
}
}
27 changes: 12 additions & 15 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,12 @@ function gutenberg_experimental_global_styles_get_block_data() {
*/
function gutenberg_experimental_global_styles_flatten_styles_tree( $styles ) {
$mappings = array(
'line-height' => array( 'typography', 'lineHeight' ),
'font-size' => array( 'typography', 'fontSize' ),
'background' => array( 'color', 'gradient' ),
'background-color' => array( 'color', 'background' ),
'color' => array( 'color', 'text' ),
'line-height' => array( 'typography', 'lineHeight' ),
'font-size' => array( 'typography', 'fontSize' ),
'background' => array( 'color', 'gradient' ),
'background-color' => array( 'color', 'background' ),
'color' => array( 'color', 'text' ),
'--wp--color--link' => array( 'color', 'link' ),
);

$result = array();
Expand Down Expand Up @@ -502,10 +503,6 @@ function gutenberg_experimental_global_styles_get_stylesheet() {
* and enqueues the resulting stylesheet.
*/
function gutenberg_experimental_global_styles_enqueue_assets() {
if ( ! gutenberg_experimental_global_styles_has_theme_json_support() ) {
return;
}

$stylesheet = gutenberg_experimental_global_styles_get_stylesheet();

wp_register_style( 'global-styles', false, array(), true, true );
Expand All @@ -520,18 +517,18 @@ function gutenberg_experimental_global_styles_enqueue_assets() {
* @return array New block editor settings
*/
function gutenberg_experimental_global_styles_settings( $settings ) {
if ( ! gutenberg_experimental_global_styles_has_theme_json_support() ) {
return $settings;
}

$settings['__experimentalGlobalStylesUserEntityId'] = gutenberg_experimental_global_styles_get_user_cpt_id();

$global_styles = gutenberg_experimental_global_styles_merge_trees(
$global_styles_base = gutenberg_experimental_global_styles_merge_trees(
gutenberg_experimental_global_styles_get_core(),
gutenberg_experimental_global_styles_get_theme()
);

$settings['__experimentalGlobalStylesBase'] = $global_styles;
$settings['__experimentalGlobalStylesBase'] = $global_styles_base;
$settings['__experimentalGlobalStyles'] = gutenberg_experimental_global_styles_merge_trees(
$global_styles_base,
gutenberg_experimental_global_styles_get_user()
);

// Add the styles for the editor via the settings
// so they get processed as if they were added via add_editor_styles:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function ColorGradientControlInner( {
onGradientChange,
colorValue,
gradientValue,
clearable,
} ) {
const canChooseAColor =
onColorChange && ( ! isEmpty( colors ) || ! disableCustomColors );
Expand Down Expand Up @@ -152,7 +153,7 @@ function ColorGradientControlInner( {
}
: onColorChange
}
{ ...{ colors, disableCustomColors } }
{ ...{ colors, disableCustomColors, clearable } }
/>
) }
{ ( currentTab === 'gradient' || ! canChooseAColor ) && (
Expand All @@ -166,7 +167,7 @@ function ColorGradientControlInner( {
}
: onGradientChange
}
{ ...{ gradients, disableCustomGradients } }
{ ...{ gradients, disableCustomGradients, clearable } }
/>
) }
</fieldset>
Expand Down
48 changes: 48 additions & 0 deletions packages/block-editor/src/hooks/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export const COLOR_SUPPORT_KEY = '__experimentalColor';
const hasColorSupport = ( blockType ) =>
Platform.OS === 'web' && hasBlockSupport( blockType, COLOR_SUPPORT_KEY );

const hasLinkColorSupport = ( blockType ) => {
if ( Platform.OS !== 'web' ) {
return false;
}

const colorSupport = getBlockSupport( blockType, COLOR_SUPPORT_KEY );

return isObject( colorSupport ) && !! colorSupport.linkColor;
};

const hasGradientSupport = ( blockType ) => {
if ( Platform.OS !== 'web' ) {
return false;
Expand Down Expand Up @@ -151,6 +161,15 @@ export function addEditProps( settings ) {
return settings;
}

const getLinkColorFromAttributeValue = ( colors, value ) => {
const attributeParsed = /var:preset\|color\|(.+)/.exec( value );
if ( attributeParsed && attributeParsed[ 1 ] ) {
return getColorObjectByAttributeValues( colors, attributeParsed[ 1 ] )
.color;
}
return value;
};

/**
* Inspector control panel containing the color related configuration
*
Expand Down Expand Up @@ -245,6 +264,21 @@ export function ColorEdit( props ) {
};
};

const onChangeLinkColor = ( value ) => {
const colorObject = getColorObjectByColorValue( colors, value );
props.setAttributes( {
style: {
...props.attributes.style,
color: {
...props.attributes.style?.color,
link: colorObject?.slug
? `var:preset|color|${ colorObject.slug }`
: value,
},
},
} );
};

return (
<ColorPanel
enableContrastChecking={
Expand Down Expand Up @@ -275,6 +309,20 @@ export function ColorEdit( props ) {
? onChangeGradient
: undefined,
},
...( hasLinkColorSupport( blockName )
? [
{
label: __( 'Link Color' ),
onColorChange: onChangeLinkColor,
colorValue: getLinkColorFromAttributeValue(
colors,
props.globalStyles?.color?.link
),
clearable: !! props.attributes.style?.color
?.link,
},
]
: [] ),
] }
/>
);
Expand Down
105 changes: 90 additions & 15 deletions packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { has, get } from 'lodash';
import { has, get, startsWith, reduce, merge } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -11,7 +11,13 @@ import { hasBlockSupport } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { Platform } from '@wordpress/element';
import {
Platform,
createContext,
useContext,
useMemo,
} from '@wordpress/element';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -35,6 +41,20 @@ const typographySupportKeys = [
const hasStyleSupport = ( blockType ) =>
styleSupportKeys.some( ( key ) => hasBlockSupport( blockType, key ) );

const VARIABLE_REFERENCE_PREFIX = 'var:';
const VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE = '|';
const VARIABLE_PATH_SEPARATOR_TOKEN_STYLE = '--';
function compileStyleValue( uncompiledValue ) {
if ( startsWith( uncompiledValue, VARIABLE_REFERENCE_PREFIX ) ) {
const variable = uncompiledValue
.slice( VARIABLE_REFERENCE_PREFIX.length )
.split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE )
.join( VARIABLE_PATH_SEPARATOR_TOKEN_STYLE );
return `var(--wp--${ variable })`;
}
return uncompiledValue;
}

/**
* Returns the inline styles to add depending on the style object
*
Expand All @@ -48,12 +68,13 @@ export function getInlineStyles( styles = {} ) {
background: [ 'color', 'gradient' ],
backgroundColor: [ 'color', 'background' ],
color: [ 'color', 'text' ],
'--wp--color--link': [ 'color', 'link' ],
};

const output = {};
Object.entries( mappings ).forEach( ( [ styleKey, objectKey ] ) => {
if ( has( styles, objectKey ) ) {
output[ styleKey ] = get( styles, objectKey );
output[ styleKey ] = compileStyleValue( get( styles, objectKey ) );
}
} );

Expand Down Expand Up @@ -130,6 +151,18 @@ export function addEditProps( settings ) {
return settings;
}

const GlobalStylesContext = createContext( {} );

// Todo: Move this specification to the block.json
const selectorMapping = {
'core/heading/h1': ( { level } ) => level === 1,
'core/heading/h2': ( { level } ) => level === 2,
'core/heading/h3': ( { level } ) => level === 3,
'core/heading/h4': ( { level } ) => level === 4,
'core/heading/h5': ( { level } ) => level === 5,
'core/heading/h6': ( { level } ) => level === 6,
};

/**
* Override the default edit UI to include new inspector controls for
* all the custom styles configs.
Expand All @@ -144,18 +177,60 @@ export const withBlockControls = createHigherOrderComponent(
hasBlockSupport( blockName, key )
);

return [
Platform.OS === 'web' && hasTypographySupport && (
<InspectorControls key="typography">
<PanelBody title={ __( 'Typography' ) }>
<FontSizeEdit { ...props } />
<LineHeightEdit { ...props } />
</PanelBody>
</InspectorControls>
),
<ColorEdit key="colors" { ...props } />,
<BlockEdit key="edit" { ...props } />,
];
const { __experimentalGlobalStyles } = useSelect( ( select ) => {
return select( 'core/block-editor' ).getSettings();
}, [] );
const globalStylesContext = useContext( GlobalStylesContext );

const globalStylesBlock = useMemo( () => {
const globalBlockSpecificStyles = reduce(
__experimentalGlobalStyles,
( accumulator, value, selector ) => {
if (
selector === blockName ||
( selector.startsWith( blockName + '/' ) &&
selectorMapping[ selector ] &&
selectorMapping[ selector ]( props.attributes ) )
) {
return merge( accumulator, value.styles );
}
return accumulator;
},
{}
);

// The styles are merged according the following priority:
// First: Local block styles.
// Second: Styles targeting the block specifically.
// Third: Styles inherited from the parent blocks.
// Forth: The global styles applied to the root.
return merge(
{},
__experimentalGlobalStyles?.global?.styles || {},
globalStylesContext || {},
globalBlockSpecificStyles,
props.attributes.style || {}
);
} );

return (
<GlobalStylesContext.Provider value={ globalStylesBlock }>
{ Platform.OS === 'web' && hasTypographySupport && (
<InspectorControls key="typography">
<PanelBody title={ __( 'Typography' ) }>
<FontSizeEdit { ...props } />
<LineHeightEdit { ...props } />
</PanelBody>
</InspectorControls>
) }
<ColorEdit
key="colors"
globalStyles={ globalStylesBlock }
{ ...props }
/>
<BlockEdit key="edit" { ...props } />
</GlobalStylesContext.Provider>
);
},
'withToolbarControls'
);
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/columns/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"html": false,
"lightBlockWrapper": true,
"__experimentalColor": {
"gradients": true
"gradients": true,
"linkColor": true
}
}
}
3 changes: 2 additions & 1 deletion packages/block-library/src/group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"html": false,
"lightBlockWrapper": true,
"__experimentalColor": {
"gradients": true
"gradients": true,
"linkColor": true
}
}
}
4 changes: 3 additions & 1 deletion packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"anchor": true,
"__unstablePasteTextInline": true,
"lightBlockWrapper": true,
"__experimentalColor": true,
"__experimentalColor": {
"linkColor": true
},
"__experimentalLineHeight": true,
"__experimentalFontSize": true
}
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/media-text/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
],
"html": false,
"__experimentalColor": {
"gradients": true
"gradients": true,
"linkColor": true
}
}
}
5 changes: 4 additions & 1 deletion packages/block-library/src/paragraph/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
"className": false,
"__unstablePasteTextInline": true,
"lightBlockWrapper": true,
"__experimentalColor": true,
"__experimentalColor": {
"linkColor": true
},
"__experimentalLinkColor": true,
"__experimentalLineHeight": true,
"__experimentalFontSize": true,
"__experimentalFeatures": {
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@
background: linear-gradient(135deg,rgb(2,3,129) 0%,rgb(40,116,252) 100%);
}
/* stylelint-enable function-comma-space-after */
a {
color: var(--wp--color--link);
}
}


Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class EditorProvider extends Component {
'__experimentalEnableFullSiteEditingDemo',
'__experimentalFeatures',
'__experimentalGlobalStylesUserEntityId',
'__experimentalGlobalStyles',
'__experimentalGlobalStylesBase',
'__experimentalPreferredStyleVariations',
'alignWide',
Expand Down

0 comments on commit d35b123

Please sign in to comment.