Skip to content
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

Avoid override certain paths on theme variation switch #59921

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
* Internal dependencies
*/
import { mergeBaseAndUserConfigs } from '../global-styles-provider';
import getValueFromObjectPath from '../../../utils/get-value-from-object-path';
import setNestedValue from '../../../utils/set-nested-value';
import { unlock } from '../../../lock-unlock';

const { GlobalStylesContext, areGlobalStyleConfigsEqual } = unlock(
blockEditorPrivateApis
);

const PATHS_TO_AVOID_OVERWRITING = [
scruffian marked this conversation as resolved.
Show resolved Hide resolved
[ 'settings', 'typography', 'fontFamilies', 'custom' ],
[ 'settings', 'color', 'palette', 'custom' ],
];

export default function Variation( { variation, children } ) {
const [ isFocused, setIsFocused ] = useState( false );
const { base, user, setUserConfig } = useContext( GlobalStylesContext );
Expand All @@ -38,10 +45,20 @@ export default function Variation( { variation, children } ) {
);

const selectVariation = () => {
setUserConfig( () => ( {
settings: variation.settings,
styles: variation.styles,
} ) );
setUserConfig( ( currentConfig ) => {
// Avoids overwriting certain paths when applying a theme variation.
for ( const path of PATHS_TO_AVOID_OVERWRITING ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this needs to be a loop. Isn't it only really one path we want to avoid overwriting?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is currently but that could change in the future. If we wanted to simplify this PR we could avoid the loop and make the "path" a single string and not an array of strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if that would be better, if we don't have other cases that this need to happen for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Dave said, the loop makes the code simpler when more paths are added. In fact, I added a new path for custom color palettes in this commit: 651b30d

// Gets the value from the current config.
const value = getValueFromObjectPath( currentConfig, path );
// Sets the value in the applied variation.
setNestedValue( variation, path, value );
}

return {
settings: variation.settings,
styles: variation.styles,
};
} );
};

const selectOnEnter = ( event ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { store as coreStore } from '@wordpress/core-data';
import { useSupportedStyles } from '../../components/global-styles/hooks';
import { unlock } from '../../lock-unlock';
import cloneDeep from '../../utils/clone-deep';
import getValueFromObjectPath from '../../utils/get-value-from-object-path';
import setNestedValue from '../../utils/set-nested-value';

const { cleanEmptyObject, GlobalStylesContext } = unlock(
blockEditorPrivateApis
Expand Down Expand Up @@ -105,14 +107,6 @@ const STYLE_PATH_TO_PRESET_BLOCK_ATTRIBUTE = {

const SUPPORTED_STYLES = [ 'border', 'color', 'spacing', 'typography' ];

const getValueFromObjectPath = ( object, path ) => {
let value = object;
path.forEach( ( fieldName ) => {
value = value?.[ fieldName ];
} );
return value;
};

const flatBorderProperties = [ 'borderColor', 'borderWidth', 'borderStyle' ];
const sides = [ 'top', 'right', 'bottom', 'left' ];

Expand Down Expand Up @@ -236,46 +230,6 @@ function useChangesToPush( name, attributes, userConfig ) {
}, [ supports, attributes, blockUserConfig ] );
}

/**
* Sets the value at path of object.
* If a portion of path doesn’t exist, it’s created.
* Arrays are created for missing index properties while objects are created
* for all other missing properties.
*
* This function intentionally mutates the input object.
*
* Inspired by _.set().
*
* @see https://lodash.com/docs/4.17.15#set
*
* @todo Needs to be deduplicated with its copy in `@wordpress/core-data`.
*
* @param {Object} object Object to modify
* @param {Array} path Path of the property to set.
* @param {*} value Value to set.
*/
function setNestedValue( object, path, value ) {
if ( ! object || typeof object !== 'object' ) {
return object;
}

path.reduce( ( acc, key, idx ) => {
if ( acc[ key ] === undefined ) {
if ( Number.isInteger( path[ idx + 1 ] ) ) {
acc[ key ] = [];
} else {
acc[ key ] = {};
}
}
if ( idx === path.length - 1 ) {
acc[ key ] = value;
}
return acc[ key ];
}, object );

return object;
}

function PushChangesToGlobalStylesControl( {
name,
attributes,
Expand Down
7 changes: 7 additions & 0 deletions packages/edit-site/src/utils/get-value-from-object-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function getValueFromObjectPath( object, path ) {
let value = object;
path.forEach( ( fieldName ) => {
value = value?.[ fieldName ];
} );
return value;
}
39 changes: 39 additions & 0 deletions packages/edit-site/src/utils/set-nested-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Sets the value at path of object.
* If a portion of path doesn’t exist, it’s created.
* Arrays are created for missing index properties while objects are created
* for all other missing properties.
*
* This function intentionally mutates the input object.
*
* Inspired by _.set().
*
* @see https://lodash.com/docs/4.17.15#set
*
* @todo Needs to be deduplicated with its copy in `@wordpress/core-data`.
*
* @param {Object} object Object to modify
* @param {Array} path Path of the property to set.
* @param {*} value Value to set.
*/
export default function setNestedValue( object, path, value ) {
if ( ! object || typeof object !== 'object' ) {
return object;
}

path.reduce( ( acc, key, idx ) => {
if ( acc[ key ] === undefined ) {
if ( Number.isInteger( path[ idx + 1 ] ) ) {
acc[ key ] = [];
} else {
acc[ key ] = {};
}
}
if ( idx === path.length - 1 ) {
acc[ key ] = value;
}
return acc[ key ];
}, object );

return object;
}
Loading