Skip to content

Commit

Permalink
Lodash: Remove some _.get() from Image block (#48489)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Feb 27, 2023
1 parent b4e9e0a commit 0dde423
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
8 changes: 4 additions & 4 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { get, isEmpty } from 'lodash';
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -65,8 +65,8 @@ export const pickRelevantMediaFiles = ( image, size ) => {
);

imageProps.url =
get( image, [ 'sizes', size, 'url' ] ) ||
get( image, [ 'media_details', 'sizes', size, 'source_url' ] ) ||
image?.sizes?.[ size ]?.url ||
image?.media_details?.sizes?.[ size ]?.source_url ||
image.url;
return imageProps;
};
Expand Down Expand Up @@ -192,7 +192,7 @@ export function ImageEdit( {

// If a caption text was meanwhile written by the user,
// make sure the text is not overwritten by empty captions.
if ( captionRef.current && ! get( mediaAttributes, [ 'caption' ] ) ) {
if ( captionRef.current && ! mediaAttributes.caption ) {
const { caption: omittedCaption, ...restMediaAttributes } =
mediaAttributes;
mediaAttributes = restMediaAttributes;
Expand Down
13 changes: 4 additions & 9 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, isEmpty } from 'lodash';
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -166,8 +166,8 @@ export default function Image( {
! isContentLocked &&
! ( isWideAligned && isLargeViewport );
const imageSizeOptions = imageSizes
.filter( ( { slug } ) =>
get( image, [ 'media_details', 'sizes', slug, 'source_url' ] )
.filter(
( { slug } ) => image?.media_details?.sizes?.[ slug ]?.source_url
)
.map( ( { name, slug } ) => ( { value: slug, label: name } ) );

Expand Down Expand Up @@ -259,12 +259,7 @@ export default function Image( {
}

function updateImage( newSizeSlug ) {
const newUrl = get( image, [
'media_details',
'sizes',
newSizeSlug,
'source_url',
] );
const newUrl = image?.media_details?.sizes?.[ newSizeSlug ]?.source_url;
if ( ! newUrl ) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/image/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isEmpty, get } from 'lodash';
import { isEmpty } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -64,7 +64,7 @@ export function getUpdatedLinkTargetSettings( value, { rel } ) {
* @param {string} size Selected size slug to apply.
*/
export function getImageSizeAttributes( image, size ) {
const url = get( image, [ 'media_details', 'sizes', size, 'source_url' ] );
const url = image?.media_details?.sizes?.[ size ]?.source_url;

if ( url ) {
return { url, width: undefined, height: undefined, sizeSlug: size };
Expand Down

1 comment on commit 0dde423

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 0dde423.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4284254922
📝 Reported issues:

Please sign in to comment.