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

Image Block: Keep selected size on changing image #39457

Closed
gaambo opened this issue Mar 15, 2022 · 6 comments · Fixed by #49982
Closed

Image Block: Keep selected size on changing image #39457

gaambo opened this issue Mar 15, 2022 · 6 comments · Fixed by #49982
Assignees
Labels
[Block] Image Affects the Image Block [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.

Comments

@gaambo
Copy link
Contributor

gaambo commented Mar 15, 2022

Description

When changing the image in an image block, the sizeSlug attribute is reset to the default image size or to "full":

// Reset the dimension attributes if changing to a different image.
if ( ! media.id || media.id !== id ) {
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',
};
} else {

I think if an image size was already selected before (any other than the default), this should be used again even when changing images.
This is important if the size of the image is important to the layout.
Example: We're using an image block in a pattern for a hero - we've got a custom image size registered for that. I can select the size, but as soon as I click "replace" on the image block, the size gets reset. I have to tell users/editors to change the size again. The width (100%) of the <img> element is set via CSS, but of course the image looks bad when inseretd in a smaller image size.

I also don't want to change the default image size, since this is only for one pattern and the required/recommended image size may be different for each pattern.

Step-by-step reproduction instructions

  1. Insert an image block in the editor

  2. pick an image

  3. select any other size than the default size

  4. click "replace" and choos another image

  5. the size is reset

  6. Create a pattern which includes an image block with a predefined size

  7. insert the pattern

  8. set/replace the image with any other image

  9. the size is reset

Proposed Solution

Change the code linked above to keep the sizeSlug attribute if one was set before and if the new media is available in this size.

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 5.9, Gutenberg 11.9, custom theme but also TwentyTwentyTwo
  • Firefox, Chrome, Safari

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@github-actions
Copy link

Hi,
This issue has gone 180 days without any activity. This means it is time for a check-in to make sure it is still relevant. If you are still experiencing this issue with the latest versions, you can help the project by responding to confirm the problem and by providing any updated reproduction steps.
Thanks for helping out.

@github-actions github-actions bot added the Needs Testing Needs further testing to be confirmed. label Sep 28, 2022
gaambo added a commit to gaambo/gutenberg that referenced this issue Sep 28, 2022
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Sep 28, 2022
@gaambo
Copy link
Contributor Author

gaambo commented Sep 28, 2022

The issue still exists on trunk. I added a PR to fix the issue.

@Thelmachido
Copy link

Thanks @gaambo I will remove the needs testing label

@Thelmachido Thelmachido removed the Needs Testing Needs further testing to be confirmed. label Sep 29, 2022
@matthisamoto
Copy link

Is there any plan to add this to a milestone? This makes it difficult to maintain consistency in block patterns that rely on a specific image size to be set and selecting the image clears it out.

@gaambo
Copy link
Contributor Author

gaambo commented Apr 21, 2023

I think this issues is a real pain when building patterns or templates for clients. We keep telling the client to always update the image size after changing the image, but since the image size is set in the pattern and we generate the image size for all images, this could easily be done programmatically as is seen in my very small PR.
Unfortunately I messed up trying to update my previous PR to rebase on trunk. Therefore I opened a new PR here. It also includes better & easier testing instructions.

@gaambo
Copy link
Contributor Author

gaambo commented Jun 29, 2023

Just tested with current trunk after #51545 is merged. I thought it may be already fixed, because that PR keeps the aspect ratio when changing images. But the image size is not kept. I can updated my PR after the changes of #51545 but would like to have any feedback if this has a chance to be merged before doing any work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Image Affects the Image Block [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.
Projects
None yet
4 participants