From c9ad8bac607696eb1d8b5948fa61949e45db18f9 Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Fri, 21 Apr 2023 09:39:19 +0200 Subject: [PATCH 1/2] image block: keep selected size on image change --- packages/block-library/src/image/edit.js | 25 +++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/image/edit.js b/packages/block-library/src/image/edit.js index 2c1e0ee3a41767..6f305280102298 100644 --- a/packages/block-library/src/image/edit.js +++ b/packages/block-library/src/image/edit.js @@ -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 ] ?? {} ) ); } @@ -168,7 +168,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. @@ -184,9 +193,7 @@ export function ImageEdit( { additionalAttributes = { // 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" From e2ed98c80be9759663a1692a5306fbd9d3a6b66d Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Fri, 21 Apr 2023 09:40:09 +0200 Subject: [PATCH 2/2] remove wrong/old comment --- packages/block-library/src/image/edit.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/block-library/src/image/edit.js b/packages/block-library/src/image/edit.js index 6f305280102298..c58a96e5949112 100644 --- a/packages/block-library/src/image/edit.js +++ b/packages/block-library/src/image/edit.js @@ -191,8 +191,6 @@ export function ImageEdit( { // Reset the dimension attributes if changing to a different image. if ( ! media.id || media.id !== id ) { additionalAttributes = { - // 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: newSize, }; } else {