Skip to content

Commit

Permalink
Client side
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Nov 18, 2020
1 parent 0ef9982 commit a763bd4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { set, get, mapValues, mergeWith } from 'lodash';
import { set, get, mapValues, mergeWith, isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -21,6 +21,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
* Internal dependencies
*/
import { default as getGlobalStyles } from './global-styles-renderer';
import { PRESET_CATEGORIES } from './utils';

const EMPTY_CONTENT = '{}';

Expand Down Expand Up @@ -74,6 +75,30 @@ export default function GlobalStylesProvider( {
mergeTreesCustomizer
);

for ( const context in newMergedStyles ) {
const userSettings = get( newUserStyles, [ context, 'settings' ] );
const baseSettings = get( baseStyles, [ context, 'settings' ] );
if ( ! isEmpty( userSettings ) && ! isEmpty( baseSettings ) ) {
for ( const presetCategory in PRESET_CATEGORIES ) {
const { path } = PRESET_CATEGORIES[ presetCategory ];
const userPreset = get( userSettings, path );
const basePreset = get( baseSettings, path );
if ( ! isEmpty( userPreset ) && ! isEmpty( basePreset ) ) {
const inactivePreset = get(
newMergedStyles,
[ context, 'deactivatedSettings', ...path ],
[]
);
set(
newMergedStyles,
[ context, 'deactivatedSettings', ...path ],
[ ...inactivePreset, ...basePreset ]
);
}
}
}
}

return {
userStyles: newUserStyles,
mergedStyles: newMergedStyles,
Expand Down
31 changes: 25 additions & 6 deletions packages/edit-site/src/components/editor/global-styles-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,21 @@ export default ( blockData, tree ) => {
*
* @param {string} blockSelector
* @param {Object} blockPresets
* @param {Object} deactivatedPresets
* @return {string} CSS declarations for the preset classes.
*/
const getBlockPresetClasses = ( blockSelector, blockPresets = {} ) => {
const getBlockPresetClasses = (
blockSelector,
blockPresets = {},
deactivatedPresets = {}
) => {
return reduce(
PRESET_CLASSES,
( declarations, { path, key, property }, classSuffix ) => {
const presets = get( blockPresets, path, [] );
const presets = [
...get( deactivatedPresets, path, [] ),
...get( blockPresets, path, [] ),
];
presets.forEach( ( preset ) => {
const slug = preset.slug;
const value = preset[ key ];
Expand All @@ -93,14 +101,21 @@ export default ( blockData, tree ) => {
* Transform given preset tree into a set of style declarations.
*
* @param {Object} blockPresets
* @param {Object} deactivatedPresets
*
* @return {Array} An array of style declarations.
*/
const getBlockPresetsDeclarations = ( blockPresets = {} ) => {
const getBlockPresetsDeclarations = (
blockPresets = {},
deactivatedPresets = {}
) => {
return reduce(
PRESET_CATEGORIES,
( declarations, { path, key }, category ) => {
const preset = get( blockPresets, path, [] );
const preset = [
...get( deactivatedPresets, path, [] ),
...get( blockPresets, path, [] ),
];
preset.forEach( ( value ) => {
declarations.push(
`--wp--preset--${ kebabCase( category ) }--${
Expand Down Expand Up @@ -159,7 +174,10 @@ export default ( blockData, tree ) => {
blockData[ context ].supports,
tree?.[ context ]?.styles
),
...getBlockPresetsDeclarations( tree?.[ context ]?.settings ),
...getBlockPresetsDeclarations(
tree?.[ context ]?.settings,
tree?.[ context ]?.deactivatedSettings
),
...getCustomDeclarations( tree?.[ context ]?.settings?.custom ),
];
if ( blockDeclarations.length > 0 ) {
Expand All @@ -170,7 +188,8 @@ export default ( blockData, tree ) => {

const presetClasses = getBlockPresetClasses(
blockSelector,
tree?.[ context ]?.settings
tree?.[ context ]?.settings,
tree?.[ context ]?.deactivatedSettings
);
if ( presetClasses ) {
styles.push( presetClasses );
Expand Down

0 comments on commit a763bd4

Please sign in to comment.