-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lodash: Refactor away from _.set()
in global styles
#52279
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* External dependencies | ||
*/ | ||
import fastDeepEqual from 'fast-deep-equal/es6'; | ||
import { get, set } from 'lodash'; | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -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'; | ||
|
||
|
@@ -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; | ||
|
@@ -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 ) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplifying because we don't need to deep clone - |
||
setImmutably( currentConfig, contextualPath.split( '.' ), newValue ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We just have to split |
||
); | ||
}; | ||
|
||
return [ settingValue, setSetting ]; | ||
|
@@ -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 ) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplifying because we don't need to deep clone - |
||
setImmutably( | ||
currentConfig, | ||
finalPath.split( '.' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We just have to split |
||
shouldDecodeEncode | ||
? getPresetVariableFromValue( | ||
mergedConfig.settings, | ||
|
@@ -174,9 +169,8 @@ export function useGlobalStyle( | |
newValue | ||
) | ||
: newValue | ||
); | ||
return newUserConfig; | ||
} ); | ||
) | ||
); | ||
}; | ||
|
||
let rawResult, result; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty straightforward to trace -
result
is already declared to be an object, andsetting
is already a string, which is handled just fine bysetImmutably
once we split it to an array path.