Skip to content

Commit

Permalink
Block Editor: Rename immutableSet to setImmutably (#50040)
Browse files Browse the repository at this point in the history
* Block Editor: Rename immutableSet to setImmutably

Addresses #49365 (comment)

* Add breaking change to CHANGELOG.md
  • Loading branch information
mcsf authored Apr 26, 2023
1 parent 9f7aef3 commit 754d20e
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 64 deletions.
4 changes: 4 additions & 0 deletions packages/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Breaking Changes

- Renamed utility function `immutableSet` to `setImmutably` ([#50040](https://github.com/WordPress/gutenberg/pull/50040)).

## 11.8.0 (2023-04-12)

## 11.7.0 (2023-03-29)
Expand Down
26 changes: 13 additions & 13 deletions packages/block-editor/src/components/global-styles/color-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { __ } from '@wordpress/i18n';
import ColorGradientControl from '../colors-gradients/control';
import { useColorsPerOrigin, useGradientsPerOrigin } from './hooks';
import { getValueFromVariable } from './utils';
import { immutableSet } from '../../utils/object';
import { setImmutably } from '../../utils/object';

export function useHasColorPanel( settings ) {
const hasTextPanel = useHasTextPanel( settings );
Expand Down Expand Up @@ -330,7 +330,7 @@ export default function ColorPanel( {
const hasTextColor = () => !! userTextColor;
const setTextColor = ( newColor ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'color', 'text' ],
encodeColorValue( newColor )
Expand All @@ -347,7 +347,7 @@ export default function ColorPanel( {
const userGradient = decodeValue( value?.color?.gradient );
const hasBackground = () => !! userBackgroundColor || !! userGradient;
const setBackgroundColor = ( newColor ) => {
const newValue = immutableSet(
const newValue = setImmutably(
value,
[ 'color', 'background' ],
encodeColorValue( newColor )
Expand All @@ -356,7 +356,7 @@ export default function ColorPanel( {
onChange( newValue );
};
const setGradient = ( newGradient ) => {
const newValue = immutableSet(
const newValue = setImmutably(
value,
[ 'color', 'gradient' ],
encodeGradientValue( newGradient )
Expand All @@ -365,7 +365,7 @@ export default function ColorPanel( {
onChange( newValue );
};
const resetBackground = () => {
const newValue = immutableSet(
const newValue = setImmutably(
value,
[ 'color', 'background' ],
undefined
Expand All @@ -382,7 +382,7 @@ export default function ColorPanel( {
const userLinkColor = decodeValue( value?.elements?.link?.color?.text );
const setLinkColor = ( newColor ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'elements', 'link', 'color', 'text' ],
encodeColorValue( newColor )
Expand All @@ -397,7 +397,7 @@ export default function ColorPanel( {
);
const setHoverLinkColor = ( newColor ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'elements', 'link', ':hover', 'color', 'text' ],
encodeColorValue( newColor )
Expand All @@ -406,12 +406,12 @@ export default function ColorPanel( {
};
const hasLink = () => !! userLinkColor || !! userHoverLinkColor;
const resetLink = () => {
let newValue = immutableSet(
let newValue = setImmutably(
value,
[ 'elements', 'link', ':hover', 'color', 'text' ],
undefined
);
newValue = immutableSet(
newValue = setImmutably(
newValue,
[ 'elements', 'link', 'color', 'text' ],
undefined
Expand Down Expand Up @@ -591,7 +591,7 @@ export default function ColorPanel( {
elementGradientUserColor
);
const resetElement = () => {
const newValue = immutableSet(
const newValue = setImmutably(
value,
[ 'elements', name, 'color', 'background' ],
undefined
Expand All @@ -603,15 +603,15 @@ export default function ColorPanel( {

const setElementTextColor = ( newTextColor ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'elements', name, 'color', 'text' ],
encodeColorValue( newTextColor )
)
);
};
const setElementBackgroundColor = ( newBackgroundColor ) => {
const newValue = immutableSet(
const newValue = setImmutably(
value,
[ 'elements', name, 'color', 'background' ],
encodeColorValue( newBackgroundColor )
Expand All @@ -620,7 +620,7 @@ export default function ColorPanel( {
onChange( newValue );
};
const setElementGradient = ( newGradient ) => {
const newValue = immutableSet(
const newValue = setImmutably(
value,
[ 'elements', name, 'color', 'gradient' ],
encodeGradientValue( newGradient )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { store as blockEditorStore } from '@wordpress/block-editor';
import PanelColorGradientSettings from '../colors-gradients/panel-color-gradient-settings';
import { useColorsPerOrigin, useGradientsPerOrigin } from './hooks';
import { getValueFromVariable } from './utils';
import { immutableSet } from '../../utils/object';
import { setImmutably } from '../../utils/object';
import ContrastChecker from '../contrast-checker';
import InspectorControls from '../inspector-controls';
import {
Expand Down Expand Up @@ -80,7 +80,7 @@ const ColorPanel = ( {
const setTextColor = useCallback(
( newColor ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'color', 'text' ],
encodeColorValue( newColor )
Expand All @@ -100,7 +100,7 @@ const ColorPanel = ( {
const gradient = decodeValue( inheritedValue?.color?.gradient );
const setBackgroundColor = useCallback(
( newColor ) => {
const newValue = immutableSet(
const newValue = setImmutably(
value,
[ 'color', 'background' ],
encodeColorValue( newColor )
Expand All @@ -112,7 +112,7 @@ const ColorPanel = ( {
);
const setGradient = useCallback(
( newGradient ) => {
const newValue = immutableSet(
const newValue = setImmutably(
value,
[ 'color', 'gradient' ],
encodeGradientValue( newGradient )
Expand All @@ -123,7 +123,7 @@ const ColorPanel = ( {
[ encodeGradientValue, onChange, value ]
);
const resetBackground = useCallback( () => {
const newValue = immutableSet(
const newValue = setImmutably(
value,
[ 'color', 'background' ],
undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import SpacingSizesControl from '../spacing-sizes-control';
import HeightControl from '../height-control';
import ChildLayoutControl from '../child-layout-control';
import { cleanEmptyObject } from '../../hooks/utils';
import { immutableSet } from '../../utils/object';
import { setImmutably } from '../../utils/object';

const AXIAL_SIDES = [ 'horizontal', 'vertical' ];

Expand Down Expand Up @@ -226,7 +226,7 @@ export default function DimensionsPanel( {
const contentSizeValue = decodeValue( inheritedValue?.layout?.contentSize );
const setContentSizeValue = ( newValue ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'layout', 'contentSize' ],
newValue || undefined
Expand All @@ -242,7 +242,7 @@ export default function DimensionsPanel( {
const wideSizeValue = decodeValue( inheritedValue?.layout?.wideSize );
const setWideSizeValue = ( newValue ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'layout', 'wideSize' ],
newValue || undefined
Expand All @@ -264,7 +264,7 @@ export default function DimensionsPanel( {
paddingSides.some( ( side ) => AXIAL_SIDES.includes( side ) );
const setPaddingValues = ( newPaddingValues ) => {
const padding = filterValuesBySides( newPaddingValues, paddingSides );
onChange( immutableSet( value, [ 'spacing', 'padding' ], padding ) );
onChange( setImmutably( value, [ 'spacing', 'padding' ], padding ) );
};
const hasPaddingValue = () =>
!! value?.spacing?.padding &&
Expand All @@ -284,7 +284,7 @@ export default function DimensionsPanel( {
marginSides.some( ( side ) => AXIAL_SIDES.includes( side ) );
const setMarginValues = ( newMarginValues ) => {
const margin = filterValuesBySides( newMarginValues, marginSides );
onChange( immutableSet( value, [ 'spacing', 'margin' ], margin ) );
onChange( setImmutably( value, [ 'spacing', 'margin' ], margin ) );
};
const hasMarginValue = () =>
!! value?.spacing?.margin &&
Expand All @@ -303,7 +303,7 @@ export default function DimensionsPanel( {
gapSides && gapSides.some( ( side ) => AXIAL_SIDES.includes( side ) );
const setGapValue = ( newGapValue ) => {
onChange(
immutableSet( value, [ 'spacing', 'blockGap' ], newGapValue )
setImmutably( value, [ 'spacing', 'blockGap' ], newGapValue )
);
};
const setGapValues = ( nextBoxGapValue ) => {
Expand All @@ -328,7 +328,7 @@ export default function DimensionsPanel( {
const minHeightValue = decodeValue( inheritedValue?.dimensions?.minHeight );
const setMinHeightValue = ( newValue ) => {
onChange(
immutableSet( value, [ 'dimensions', 'minHeight' ], newValue )
setImmutably( value, [ 'dimensions', 'minHeight' ], newValue )
);
};
const resetMinHeightValue = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { shadow as shadowIcon, Icon, check } from '@wordpress/icons';
* Internal dependencies
*/
import { getValueFromVariable } from './utils';
import { immutableSet } from '../../utils/object';
import { setImmutably } from '../../utils/object';

export function useHasEffectsPanel( settings ) {
const hasShadowControl = useHasShadowControl( settings );
Expand Down Expand Up @@ -81,7 +81,7 @@ export default function EffectsPanel( {
const hasShadowEnabled = useHasShadowControl( settings );
const shadow = decodeValue( inheritedValue?.shadow );
const setShadow = ( newValue ) => {
onChange( immutableSet( value, [ 'shadow' ], newValue ) );
onChange( setImmutably( value, [ 'shadow' ], newValue ) );
};
const hasShadow = () => !! value?.shadow;
const resetShadow = () => setShadow( undefined );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { useCallback, useMemo } from '@wordpress/element';
* Internal dependencies
*/
import { getValueFromVariable } from './utils';
import { immutableSet } from '../../utils/object';
import { setImmutably } from '../../utils/object';

const EMPTY_ARRAY = [];
function useMultiOriginColorPresets(
Expand Down Expand Up @@ -143,7 +143,7 @@ export default function FiltersPanel( {
const settedValue = duotonePreset
? `var:preset|duotone|${ duotonePreset.slug }`
: newValue;
onChange( immutableSet( value, [ 'filter', 'duotone' ], settedValue ) );
onChange( setImmutably( value, [ 'filter', 'duotone' ], settedValue ) );
};
const hasDuotone = () => !! value?.filter?.duotone;
const resetDuotone = () => setDuotone( undefined );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import LetterSpacingControl from '../letter-spacing-control';
import TextTransformControl from '../text-transform-control';
import TextDecorationControl from '../text-decoration-control';
import { getValueFromVariable } from './utils';
import { immutableSet } from '../../utils/object';
import { setImmutably } from '../../utils/object';

const MIN_TEXT_COLUMNS = 1;
const MAX_TEXT_COLUMNS = 6;
Expand Down Expand Up @@ -163,7 +163,7 @@ export default function TypographyPanel( {
( { fontFamily: f } ) => f === newValue
)?.slug;
onChange(
immutableSet(
setImmutably(
value,
[ 'typography', 'fontFamily' ],
slug
Expand All @@ -190,7 +190,7 @@ export default function TypographyPanel( {
: newValue;

onChange(
immutableSet(
setImmutably(
value,
[ 'typography', 'fontSize' ],
actualValue || undefined
Expand Down Expand Up @@ -231,7 +231,7 @@ export default function TypographyPanel( {
const lineHeight = decodeValue( inheritedValue?.typography?.lineHeight );
const setLineHeight = ( newValue ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'typography', 'lineHeight' ],
newValue || undefined
Expand All @@ -248,7 +248,7 @@ export default function TypographyPanel( {
);
const setLetterSpacing = ( newValue ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'typography', 'letterSpacing' ],
newValue || undefined
Expand All @@ -263,7 +263,7 @@ export default function TypographyPanel( {
const textColumns = decodeValue( inheritedValue?.typography?.textColumns );
const setTextColumns = ( newValue ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'typography', 'textColumns' ],
newValue || undefined
Expand All @@ -280,7 +280,7 @@ export default function TypographyPanel( {
);
const setTextTransform = ( newValue ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'typography', 'textTransform' ],
newValue || undefined
Expand All @@ -297,7 +297,7 @@ export default function TypographyPanel( {
);
const setTextDecoration = ( newValue ) => {
onChange(
immutableSet(
setImmutably(
value,
[ 'typography', 'textDecoration' ],
newValue || undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useMemo } from '@wordpress/element';
*/
import { useSetting } from '../components';
import { useSettingsForBlockElement } from '../components/global-styles/hooks';
import { immutableSet } from '../utils/object';
import { setImmutably } from '../utils/object';

/**
* Removed falsy values from nested object.
Expand Down Expand Up @@ -82,7 +82,7 @@ export function transformStyles(
if ( styleValue ) {
returnBlock = {
...returnBlock,
attributes: immutableSet(
attributes: setImmutably(
returnBlock.attributes,
path,
styleValue
Expand Down
8 changes: 4 additions & 4 deletions packages/block-editor/src/utils/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ function cloneObject( object ) {
}

/**
* Perform an immutable set.
* Handles nullish initial values.
* Clones all nested objects in the specified object.
* Immutably sets a value inside an object. Like `lodash#set`, but returning a
* new object. Treats nullish initial values as empty objects. Clones any
* nested objects.
*
* @param {Object} object Object to set a value in.
* @param {number|string|Array} path Path in the object to modify.
* @param {*} value New value to set.
* @return {Object} Cloned object with the new value set.
*/
export function immutableSet( object, path, value ) {
export function setImmutably( object, path, value ) {
const normalizedPath = normalizePath( path );
const newObject = object ? cloneObject( object ) : {};

Expand Down
Loading

0 comments on commit 754d20e

Please sign in to comment.