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

Gallery block refactor: check for new images by clientId instead of id to stop link settings being lost when images edited #30550

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
8 changes: 4 additions & 4 deletions packages/block-library/src/gallery/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 { isEmpty, concat, find } from 'lodash';
import { concat, find } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -440,7 +440,6 @@ function GalleryEdit( props ) {
return <View { ...blockProps }>{ mediaPlaceholder }</View>;
}

const shouldShowSizeOptions = ! isEmpty( imageSizeOptions );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It turns out that some images have no size details, particularly those that have been cropped to max, so need to account for this so loader doesn't show indefinitely

const hasLinkTo = linkTo && linkTo !== 'none';

return (
Expand Down Expand Up @@ -478,15 +477,16 @@ function GalleryEdit( props ) {
onChange={ toggleOpenInNewTab }
/>
) }
{ shouldShowSizeOptions ? (
{ imageSizeOptions?.length > 0 && (
<SelectControl
label={ __( 'Image size' ) }
value={ sizeSlug }
options={ imageSizeOptions }
onChange={ updateImagesSize }
hideCancelButton={ true }
/>
) : (
) }
{ ! imageSizeOptions && (
<BaseControl className={ 'gallery-image-sizes' }>
<BaseControl.VisualLabel>
{ __( 'Image size' ) }
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/gallery/use-get-new-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export default function useGetNewImages( images, imageData ) {
const newImages = images.filter(
( image ) =>
! newCurrentImages.find(
( currentImage ) => image.id && currentImage.id === image.id
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cropped images have a different id but same clientId

( currentImage ) =>
image.clientId &&
currentImage.clientId === image.clientId
) &&
imageData?.find( ( img ) => img.id === image.id ) &&
! image.fromSavedConent
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/gallery/use-image-sizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function useImageSizes( images, isSelected, getSettings ) {

function getImageSizing() {
if ( ! images || images.length === 0 ) {
return [];
return;
}
const { imageSizes } = getSettings();
let resizedImages = {};
Expand Down