Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const GlobalStylesContext = createContext( {
/* eslint-disable no-unused-vars */
getSetting: ( context, path ) => {},
setSetting: ( context, path, newValue ) => {},
getStyleProperty: ( context, propertyName ) => {},
getStyleProperty: ( context, propertyName, origin ) => {},
setStyleProperty: ( context, propertyName, newValue ) => {},
globalContext: {},
/* eslint-enable no-unused-vars */
Expand Down Expand Up @@ -82,11 +82,14 @@ export default function GlobalStylesProvider( {
set( contextSettings, path, newValue );
setContent( JSON.stringify( newContent ) );
},
getStyleProperty: ( context, propertyName ) =>
get(
userStyles?.[ context ]?.styles,
getStyleProperty: ( context, propertyName, origin = 'merged' ) => {
const styles = 'user' === origin ? userStyles : mergedStyles;

return get(
styles?.[ context ]?.styles,
STYLE_PROPERTY[ propertyName ]
),
);
},
setStyleProperty: ( context, propertyName, newValue ) => {
const newContent = { ...userStyles };
let contextStyles = newContent?.[ context ]?.styles;
Expand Down
65 changes: 56 additions & 9 deletions packages/edit-site/src/components/editor/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, find } from 'lodash';
import { get, find, camelCase, isString } from 'lodash';
/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -52,21 +52,68 @@ export function getPresetVariable( presetCategory, presets, value ) {
);
}

export function getPresetValueFromVariable(
presetCategory,
presets,
variable
function getValueFromPresetVariable(
features,
blockName,
[ presetType, slug ]
) {
if ( ! variable ) {
presetType = camelCase( presetType );
const presetData = PRESET_CATEGORIES[ presetType ];
if ( ! presetData ) {
return;
}
const presets =
get( features, [ blockName, ...presetData.path ] ) ??
get( features, [ GLOBAL_CONTEXT, ...presetData.path ] );
if ( ! presets ) {
return;
}
const slug = variable.slice( variable.lastIndexOf( '|' ) + 1 );
const presetObject = find( presets, ( preset ) => {
return preset.slug === slug;
} );
if ( presetObject ) {
const presetData = PRESET_CATEGORIES[ presetCategory ];
const { key } = presetData;
return presetObject[ key ];
const result = presetObject[ key ];
return getValueFromVariable( features, blockName, result ) || result;
}
}

function getValueFromCustomVariable( features, blockName, path ) {
const result =
get( features, [ blockName, 'custom', ...path ] ) ??
get( features, [ GLOBAL_CONTEXT, 'custom', ...path ] );
// A variable may reference another variable so we need recursion until we find the value.
return getValueFromVariable( features, blockName, result ) || result;
}

export function getValueFromVariable( features, blockName, variable ) {
if ( ! variable || ! isString( variable ) ) {
return;
}
let parsedVar;
const INTERNAL_REFERENCE_PREFIX = 'var:';
const CSS_REFERENCE_PREFIX = 'var(--wp--';
const CSS_REFERENCE_SUFFIX = ')';
if ( variable.startsWith( INTERNAL_REFERENCE_PREFIX ) ) {
parsedVar = variable
.slice( INTERNAL_REFERENCE_PREFIX.length )
.split( '|' );
} else if (
variable.startsWith( CSS_REFERENCE_PREFIX ) &&
variable.endsWith( CSS_REFERENCE_SUFFIX )
) {
parsedVar = variable
.slice( CSS_REFERENCE_PREFIX.length, -CSS_REFERENCE_SUFFIX.length )
.split( '--' );
} else {
return;
}

const [ type, ...path ] = parsedVar;
if ( type === 'preset' ) {
return getValueFromPresetVariable( features, blockName, path );
}
if ( type === 'custom' ) {
return getValueFromCustomVariable( features, blockName, path );
}
}
39 changes: 27 additions & 12 deletions packages/edit-site/src/components/sidebar/color-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
*/
import { __experimentalPanelColorGradientSettings as PanelColorGradientSettings } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import {
LINK_COLOR,
useEditorFeature,
getPresetValueFromVariable,
getValueFromVariable,
getPresetVariable,
} from '../editor/utils';
import ColorPalettePanel from './color-palette-panel';
Expand All @@ -31,6 +32,9 @@ export default function ColorPanel( {
getSetting,
setSetting,
} ) {
const features = useSelect( ( select ) => {
return select( 'core/edit-site' ).getSettings().__experimentalFeatures;
} );
const colors = useEditorFeature( 'color.palette', name );
const disableCustomColors = ! useEditorFeature( 'color.custom', name );
const gradients = useEditorFeature( 'color.gradients', name );
Expand All @@ -43,52 +47,62 @@ export default function ColorPanel( {

if ( supports.includes( 'color' ) ) {
const color = getStyleProperty( name, 'color' );
const userColor = getStyleProperty( name, 'color', 'user' );
settings.push( {
colorValue:
getPresetValueFromVariable( 'color', colors, color ) || color,
colorValue: getValueFromVariable( features, name, color ) || color,
onColorChange: ( value ) =>
setStyleProperty(
name,
'color',
getPresetVariable( 'color', colors, value ) || value
),
label: __( 'Text color' ),
clearable: color === userColor,
} );
}

let backgroundSettings = {};
if ( supports.includes( 'backgroundColor' ) ) {
const backgroundColor = getStyleProperty( name, 'backgroundColor' );
const userBackgroundColor = getStyleProperty(
name,
'backgroundColor',
'user'
);
backgroundSettings = {
colorValue:
getPresetValueFromVariable(
'color',
colors,
backgroundColor
) || backgroundColor,
getValueFromVariable( features, name, backgroundColor ) ||
backgroundColor,
onColorChange: ( value ) =>
setStyleProperty(
name,
'backgroundColor',
getPresetVariable( 'color', colors, value ) || value
),
};
if ( backgroundColor ) {
backgroundSettings.clearable =
backgroundColor === userBackgroundColor;
}
}

let gradientSettings = {};
if ( supports.includes( 'background' ) ) {
const gradient = getStyleProperty( name, 'background' );
const userGradient = getStyleProperty( name, 'background', 'user' );
gradientSettings = {
gradientValue:
getPresetValueFromVariable( 'gradient', gradients, gradient ) ||
gradient,
getValueFromVariable( features, name, gradient ) || gradient,
onGradientChange: ( value ) =>
setStyleProperty(
name,
'background',
getPresetVariable( 'gradient', gradients, value ) || value
),
};
if ( gradient ) {
gradientSettings.clearable = gradient === userGradient;
}
}

if (
Expand All @@ -104,16 +118,17 @@ export default function ColorPanel( {

if ( supports.includes( LINK_COLOR ) ) {
const color = getStyleProperty( name, LINK_COLOR );
const userColor = getStyleProperty( name, LINK_COLOR, 'user' );
settings.push( {
colorValue:
getPresetValueFromVariable( 'color', colors, color ) || color,
colorValue: getValueFromVariable( features, name, color ) || color,
onColorChange: ( value ) =>
setStyleProperty(
name,
LINK_COLOR,
getPresetVariable( 'color', colors, value ) || value
),
label: __( 'Link color' ),
clearable: color === userColor,
} );
}
return (
Expand Down
24 changes: 20 additions & 4 deletions packages/edit-site/src/components/sidebar/typography-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
} from '@wordpress/block-editor';
import { PanelBody, FontSizePicker } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { useEditorFeature } from '../editor/utils';
import { useEditorFeature, getValueFromVariable } from '../editor/utils';

export function useHasTypographyPanel( { supports, name } ) {
return (
Expand All @@ -32,6 +33,9 @@ export default function TypographyPanel( {
getStyleProperty,
setStyleProperty,
} ) {
const features = useSelect( ( select ) => {
return select( 'core/edit-site' ).getSettings().__experimentalFeatures;
} );
const fontSizes = useEditorFeature( 'typography.fontSizes', name );
const disableCustomFontSizes = ! useEditorFeature(
'typography.customFontSize',
Expand All @@ -40,20 +44,29 @@ export default function TypographyPanel( {
const fontFamilies = useEditorFeature( 'typography.fontFamilies', name );
const hasLineHeightEnabled = useHasLineHeightControl( { supports, name } );

const fontFamily = getStyleProperty( name, 'fontFamily' );
const fontSize = getStyleProperty( name, 'fontSize' );
const lineHeight = getStyleProperty( name, 'lineHeight' );
return (
<PanelBody title={ __( 'Typography' ) } initialOpen={ true }>
{ supports.includes( 'fontFamily' ) && (
<FontFamilyControl
fontFamilies={ fontFamilies }
value={ getStyleProperty( name, 'fontFamily' ) }
value={
getValueFromVariable( features, name, fontFamily ) ||
fontFamily
}
onChange={ ( value ) =>
setStyleProperty( name, 'fontFamily', value )
}
/>
) }
{ supports.includes( 'fontSize' ) && (
<FontSizePicker
value={ getStyleProperty( name, 'fontSize' ) }
value={
getValueFromVariable( features, name, fontSize ) ||
fontSize
}
onChange={ ( value ) =>
setStyleProperty( name, 'fontSize', value )
}
Expand All @@ -63,7 +76,10 @@ export default function TypographyPanel( {
) }
{ hasLineHeightEnabled && (
<LineHeightControl
value={ getStyleProperty( name, 'lineHeight' ) }
value={
getValueFromVariable( features, name, lineHeight ) ||
lineHeight
}
onChange={ ( value ) =>
setStyleProperty( name, 'lineHeight', value )
}
Expand Down