Skip to content

Commit

Permalink
Lodash: Refactor away from _.set() in global styles (#52279)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Jul 10, 2023
1 parent 42d5624 commit b0a3ef9
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions packages/block-editor/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import fastDeepEqual from 'fast-deep-equal/es6';
import { get, set } from 'lodash';
import { get } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -16,6 +16,7 @@ import { _x } from '@wordpress/i18n';
* Internal dependencies
*/
import { getValueFromVariable, getPresetVariableFromValue } from './utils';
import { setImmutably } from '../../utils/object';
import { GlobalStylesContext } from './context';
import { unlock } from '../../lock-unlock';

Expand Down Expand Up @@ -108,15 +109,15 @@ export function useGlobalSetting( propertyPath, blockName, source = 'all' ) {
);
}

const result = {};
let result = {};
VALID_SETTINGS.forEach( ( setting ) => {
const value =
get(
configToUse,
`settings${ appendedBlockPath }.${ setting }`
) ?? get( configToUse, `settings.${ setting }` );
if ( value ) {
set( result, setting, value );
result = setImmutably( result, setting.split( '.' ), value );
}
} );
return result;
Expand All @@ -130,13 +131,9 @@ export function useGlobalSetting( propertyPath, blockName, source = 'all' ) {
] );

const setSetting = ( newValue ) => {
setUserConfig( ( currentConfig ) => {
// Deep clone `currentConfig` to avoid mutating it later.
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) );
set( newUserConfig, contextualPath, newValue );

return newUserConfig;
} );
setUserConfig( ( currentConfig ) =>
setImmutably( currentConfig, contextualPath.split( '.' ), newValue )
);
};

return [ settingValue, setSetting ];
Expand All @@ -160,12 +157,10 @@ export function useGlobalStyle(
: `styles.blocks.${ blockName }${ appendedPath }`;

const setStyle = ( newValue ) => {
setUserConfig( ( currentConfig ) => {
// Deep clone `currentConfig` to avoid mutating it later.
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) );
set(
newUserConfig,
finalPath,
setUserConfig( ( currentConfig ) =>
setImmutably(
currentConfig,
finalPath.split( '.' ),
shouldDecodeEncode
? getPresetVariableFromValue(
mergedConfig.settings,
Expand All @@ -174,9 +169,8 @@ export function useGlobalStyle(
newValue
)
: newValue
);
return newUserConfig;
} );
)
);
};

let rawResult, result;
Expand Down

0 comments on commit b0a3ef9

Please sign in to comment.