From 0c28bc20addc78353941cb8a2623a22935f2af3c Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Tue, 11 Jul 2023 10:02:47 +0300 Subject: [PATCH] Lodash: Remove completely from site editor (#52480) --- package-lock.json | 1 - packages/edit-site/package.json | 1 - .../src/components/add-new-template/utils.js | 15 +++++++++------ .../hooks/push-changes-to-global-styles/index.js | 15 +++++++++------ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 37d26804e35fb..f552635bec7cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17823,7 +17823,6 @@ "downloadjs": "^1.4.7", "fast-deep-equal": "^3.1.3", "is-plain-object": "^5.0.0", - "lodash": "^4.17.21", "memize": "^2.1.0", "react-autosize-textarea": "^7.1.0", "rememo": "^4.0.2", diff --git a/packages/edit-site/package.json b/packages/edit-site/package.json index 230faa8857f1c..06e52ed9fd53e 100644 --- a/packages/edit-site/package.json +++ b/packages/edit-site/package.json @@ -71,7 +71,6 @@ "downloadjs": "^1.4.7", "fast-deep-equal": "^3.1.3", "is-plain-object": "^5.0.0", - "lodash": "^4.17.21", "memize": "^2.1.0", "react-autosize-textarea": "^7.1.0", "rememo": "^4.0.2", diff --git a/packages/edit-site/src/components/add-new-template/utils.js b/packages/edit-site/src/components/add-new-template/utils.js index 8de37c1f64632..1869cdb72f592 100644 --- a/packages/edit-site/src/components/add-new-template/utils.js +++ b/packages/edit-site/src/components/add-new-template/utils.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -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 @@ -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 ) ), } ) ); }; diff --git a/packages/edit-site/src/hooks/push-changes-to-global-styles/index.js b/packages/edit-site/src/hooks/push-changes-to-global-styles/index.js index 4e7b671a0c260..6e55b89d22b2c 100644 --- a/packages/edit-site/src/hooks/push-changes-to-global-styles/index.js +++ b/packages/edit-site/src/hooks/push-changes-to-global-styles/index.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -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 ); @@ -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 ); return value ? [ { path, value } ] : []; } ), [ supports, name, attributes ]