Skip to content

Commit

Permalink
Style Variations: Filter out the colors and fonts (#97795)
Browse files Browse the repository at this point in the history
* Style Variations: Filter out the colors and fonts

* Fix types
  • Loading branch information
arthur791004 authored Dec 27, 2024
1 parent 28f8bd6 commit 7b4af68
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
GlobalStylesContext,
mergeBaseAndUserConfigs,
withExperimentalBlockEditorProvider,
isColorVariation,
isFontVariation,
} from '../../gutenberg-bridge';
import { useRegisterCoreBlocks } from '../../hooks';
import GlobalStylesVariationPreview from './preview';
Expand Down Expand Up @@ -127,7 +129,10 @@ const GlobalStylesVariations = ( {
const globalStylesVariationsWithoutDefault = useMemo(
() =>
globalStylesVariations.filter(
( globalStylesVariation ) => ! isDefaultGlobalStyleVariationSlug( globalStylesVariation )
( globalStylesVariation ) =>
! isDefaultGlobalStyleVariationSlug( globalStylesVariation ) &&
! isColorVariation( globalStylesVariation ) &&
! isFontVariation( globalStylesVariation )
),
[ globalStylesVariations ]
);
Expand Down
58 changes: 58 additions & 0 deletions packages/global-styles/src/gutenberg-bridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
cleanEmptyObject,
ExperimentalBlockEditorProvider,
GlobalStylesContext: UntypedGSContext,
areGlobalStyleConfigsEqual,
useGlobalStylesOutput,
useGlobalSetting,
useGlobalStyle,
Expand Down Expand Up @@ -64,10 +65,67 @@ const useSafeGlobalStylesOutput = () => {
}
};

/**
* Returns a new object, with properties specified in `properties` array.,
* maintain the original object tree structure.
* The function is recursive, so it will perform a deep search for the given properties.
* E.g., the function will return `{ a: { b: { c: { test: 1 } } } }` if the properties are `[ 'test' ]`.
*
* @param {Object} object The object to filter
* @param {string[]} properties The properties to filter by
* @returns {Object} The merged object.
*/
const filterObjectByProperties = ( object: Record< string, any >, properties: string[] ) => {
if ( ! object || ! properties?.length ) {
return {};
}

const newObject: Record< string, any > = {};
Object.keys( object ).forEach( ( key ) => {
if ( properties.includes( key ) ) {
newObject[ key ] = object[ key ];
} else if ( typeof object[ key ] === 'object' ) {
const newFilter = filterObjectByProperties(
object[ key ] as Record< string, any >,
properties
);
if ( Object.keys( newFilter ).length ) {
newObject[ key ] = newFilter;
}
}
} );
return newObject;
};

/**
* Compares a style variation to the same variation filtered by the specified properties.
* Returns true if the variation contains only the properties specified.
*
* @param {Object} variation The variation to compare.
* @param {string[]} properties The properties to compare.
* @returns {boolean} Whether the variation contains only the specified properties.
*/
const isVariationWithProperties = ( variation: GlobalStylesObject, properties: string[] ) => {
const variationWithProperties = filterObjectByProperties(
window.structuredClone( variation ),
properties
);

return areGlobalStyleConfigsEqual( variationWithProperties, variation );
};

const isColorVariation = ( variation: GlobalStylesObject ) =>
isVariationWithProperties( variation, [ 'color' ] );

const isFontVariation = ( variation: GlobalStylesObject ) =>
isVariationWithProperties( variation, [ 'typography' ] );

export {
cleanEmptyObject,
ExperimentalBlockEditorProvider,
GlobalStylesContext,
isColorVariation,
isFontVariation,
transformStyles,
useSafeGlobalStylesOutput,
useGlobalSetting,
Expand Down

0 comments on commit 7b4af68

Please sign in to comment.