Skip to content

Commit

Permalink
WordPress#39457 image block: keep image size on changing image
Browse files Browse the repository at this point in the history
  • Loading branch information
gaambo committed Apr 21, 2023
1 parent 13a093a commit 200935a
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ const isTemporaryImage = ( id, url ) => ! id && isBlobURL( url );
export const isExternalImage = ( id, url ) => url && ! id && ! isBlobURL( url );

/**
* Checks if WP generated default image size. Size generation is skipped
* Checks if WP generated the specified image size. Size generation is skipped
* when the image is smaller than the said size.
*
* @param {Object} image
* @param {string} defaultSize
* @param {string} size
*
* @return {boolean} Whether or not it has default image size.
*/
function hasDefaultSize( image, defaultSize ) {
function hasSize( image, size ) {
return (
'url' in ( image?.sizes?.[ defaultSize ] ?? {} ) ||
'source_url' in ( image?.media_details?.sizes?.[ defaultSize ] ?? {} )
'url' in ( image?.sizes?.[ size ] ?? {} ) ||
'source_url' in ( image?.media_details?.sizes?.[ size ] ?? {} )
);
}

Expand Down Expand Up @@ -171,7 +171,16 @@ export function ImageEdit( {

setTemporaryURL();

let mediaAttributes = pickRelevantMediaFiles( media, imageDefaultSize );
// Try to use the previous selected image size if its available
// otherwise try the default image size or fallback to "full"
let newSize = 'full';
if ( sizeSlug && hasSize( media, sizeSlug ) ) {
newSize = sizeSlug;
} else if ( hasSize( media, imageDefaultSize ) ) {
newSize = imageDefaultSize;
}

let mediaAttributes = pickRelevantMediaFiles( media, newSize );

// If a caption text was meanwhile written by the user,
// make sure the text is not overwritten by empty captions.
Expand All @@ -187,11 +196,7 @@ export function ImageEdit( {
additionalAttributes = {
width: undefined,
height: undefined,
// Fallback to size "full" if there's no default image size.
// It means the image is smaller, and the block will use a full-size URL.
sizeSlug: hasDefaultSize( media, imageDefaultSize )
? imageDefaultSize
: 'full',
sizeSlug: newSize,
};
} else {
// Keep the same url when selecting the same file, so "Resolution"
Expand Down

0 comments on commit 200935a

Please sign in to comment.