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

Lodash: Remove completely from site editor #52480

Merged
merged 1 commit into from
Jul 11, 2023
Merged
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
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"downloadjs": "^1.4.7",
"fast-deep-equal": "^3.1.3",
"is-plain-object": "^5.0.0",
"lodash": "^4.17.21",
Copy link
Member Author

Choose a reason for hiding this comment

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

One of the last packages to remove the dependency from 🥳

"memize": "^2.1.0",
"react-autosize-textarea": "^7.1.0",
"rememo": "^4.0.2",
Expand Down
15 changes: 9 additions & 6 deletions packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -20,6 +15,14 @@ import { blockMeta, post, archive } from '@wordpress/icons';
* @property {string} name The entity's name.
*/

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

/**
* Helper util to map records to add a `name` prop from a
* provided path, in order to handle all entities in the same
Expand All @@ -32,7 +35,7 @@ import { blockMeta, post, archive } from '@wordpress/icons';
export const mapToIHasNameAndId = ( entities, path ) => {
return ( entities || [] ).map( ( entity ) => ( {
...entity,
name: decodeEntities( get( entity, path ) ),
name: decodeEntities( getValueFromObjectPath( entity, path ) ),
Copy link
Member Author

Choose a reason for hiding this comment

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

path is expected to be a string, and for entity we're handling nullish values just in case.

} ) );
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -96,6 +91,14 @@ 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;
};

function useChangesToPush( name, attributes ) {
const supports = useSupportedStyles( name );

Expand All @@ -115,7 +118,7 @@ function useChangesToPush( name, attributes ) {
];
const value = presetAttributeValue
? `var:preset|${ STYLE_PATH_TO_CSS_VAR_INFIX[ presetAttributeKey ] }|${ presetAttributeValue }`
: get( attributes.style, path );
: getValueFromObjectPath( attributes.style, path );
Copy link
Member Author

Choose a reason for hiding this comment

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

path is expected to be an array, and for attributes.style we're handling nullish values just in case:

const presetAttributeKey = path.join( '.' );

return value ? [ { path, value } ] : [];
} ),
[ supports, name, attributes ]
Expand Down