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

Block Library - Gallery: Set 'addToGallery' prop to false when images don't have IDs #30122

Merged
merged 1 commit into from
Apr 6, 2021

Conversation

Mamaduka
Copy link
Member

@Mamaduka Mamaduka commented Mar 23, 2021

Description

In the "add to gallery" state, passing an array of undefined ID values to the MediaUpload component causes adding all images to the gallery.

I'm trying to resolve this by setting the addToGallery prop to false and the MediaPlaceholder value to an empty object when images don't have IDs. With both props set to "falsy" values, the gallery media frame is initialized in the "Create Gallery" state. This lets users replace placeholder images from patterns.

Fixes #29834.

How has this been tested?

  1. Have multiple items already added to the media library.
  2. Create a new post
  3. Drag the Gallery pattern (Two images side by side)
  4. Click media library at the bottom
  5. Select an image
  6. Click on "Create Gallery" and then "Insert Gallery"
  7. Only the selected image should replace one image from the pattern

Types of changes

Bugfix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • [N/A] My code follows the accessibility standards.
  • [N/A] I've tested my changes with keyboard and screen readers.
  • [N/A] My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • [N/A] I've updated all React Native files affected by any refactorings/renamings in this PR.

This also sets MediaPlaceholder value to an empty object. With both props set to "falsy" values, the gallery media frame is initialized in the "Create Gallery" state. This lets users replace placeholder images from patterns.
@Mamaduka Mamaduka requested a review from mkevins as a code owner March 23, 2021 04:47
@Mamaduka Mamaduka added [Block] Gallery Affects the Gallery Block - used to display groups of images [Feature] Patterns A collection of blocks that can be synced (previously reusable blocks) or unsynced [Type] Bug An existing feature does not function as intended labels Mar 23, 2021
@Mamaduka Mamaduka self-assigned this Mar 23, 2021
@gziolo gziolo added the Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release label Mar 23, 2021
@@ -324,10 +324,11 @@ function GalleryEdit( props ) {
}, [ linkTo ] );

const hasImages = !! images.length;
Copy link
Contributor

@gwwar gwwar Apr 2, 2021

Choose a reason for hiding this comment

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

Thanks for taking this one! We can also filter out any unexpected data in the array. This can help guard against malformed data (potentially from bugs/accidental user-edits).

const imageIds = images.filter( ( id ) => ( Number.isInteger( id ) && id > 0 ) ); // for example [null,1,null,-3,44] will return [1, 44]
const hasImages = !! images.length;

//...
addToGallery={ hasImages }
value={ imageIds }

@glendaviesnz since you've been 👀 the Gallery block refactor, is it expected that the pattern in two images side-by-side has serialized null ids? If not, do you have a recommendation for a better spot to validate values here?

Twenty Twenty-One, Gallery Two Images Side by Side
Screen Shot 2021-04-02 at 8 41 25 AM
<!-- wp:gallery {"ids":[null,null],"linkTo":"none","align":"wide"} -->
<figure class="wp-block-gallery alignwide columns-2 is-cropped"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><img src="https://s.w.org/images/core/5.5/don-quixote-05.jpg" alt="An old pencil drawing of Don Quixote and Sancho Panza sitting on their horses, by Wilhelm Marstrand."/></figure></li><li class="blocks-gallery-item"><figure><img src="https://s.w.org/images/core/5.5/don-quixote-01.jpg" alt="An old pencil drawing of Don Quixote and Sancho Panza sitting on their horses, by Wilhelm Marstrand."/></figure></li></ul></figure>
<!-- /wp:gallery -->

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, @gwwar, for the review.

We can also filter out any unexpected data in the array.

It makes sense.

I think we should keep image IDs check separated. While patterns might not have IDs, technically, images are there, and we want to have an edit component in that state.

Copy link
Contributor

@glendaviesnz glendaviesnz Apr 6, 2021

Choose a reason for hiding this comment

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

is it expected that the pattern in two images side-by-side has serialized null ids?

Yes, this is the way the gallery handles external images, eg. if you add a stand alone image block and choose the option to add it from url, and then transform the image block to a gallery block it will add a null entry to the ids array.

do you have a recommendation for a better spot to validate values here?

I had a quick look at it appears like the ideal fix for this will need to go in the core media browser, as it needs to know that if it is passed an addToGallery=true, but an empty value, to assume that a gallery with external images is being updated, and in which case it would need to display those images in the gallery edit screen, even though they are not saved in the media library.

Currently @Mamaduka your fix prevents all of the images being added by default, but the media library defaults to 'Create gallery' which means that the existing images are overwritten:

gallery

This is ok with the patterns use case, as we can probably assume the user wants to replace the placeholder images, but as mentioned above it is possible for a user to create their own gallery with null image ids, in which case their images will be lost if they try to add additional images from the media library.

I had a quick look and couldn't see a way around this from the block side of things. I will have a look at the core side and see what options there might be, but not familiar with that code so not sure how far I will get.

Copy link
Contributor

@glendaviesnz glendaviesnz Apr 6, 2021

Choose a reason for hiding this comment

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

in which case their images will be lost if they try to add additional images from the media library.

this is the current behaviour anyway I just realised, ie. if a user edits a gallery with null id images their existing images are lost, so we wouldn't make things any worse by at least not adding all images by default.

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

Let's get it into the WP 5.7.1 release.

@gziolo gziolo merged commit 07abb2b into WordPress:trunk Apr 6, 2021
@github-actions github-actions bot added this to the Gutenberg 10.4 milestone Apr 6, 2021
gziolo pushed a commit that referenced this pull request Apr 6, 2021
…#30122)

This also sets MediaPlaceholder value to an empty object. With both props set to "falsy" values, the gallery media frame is initialized in the "Create Gallery" state. This lets users replace placeholder images from patterns.
@gziolo gziolo changed the title Gallery: Set 'addToGallery' prop to false when images don't have IDs. Block Library - Gallery: Set 'addToGallery' prop to false when images don't have IDs Apr 6, 2021
gziolo added a commit that referenced this pull request Apr 6, 2021
* revert #27717 (#30524)

Co-authored-by: grzim <[email protected]>

* Use getAuthors for 'showCombobox' check (#30218)

* Use getAuthors for 'showCombobox' check

* Add inline comment

* Gallery: Set 'addToGallery' prop to false when images don't have IDs. (#30122)

This also sets MediaPlaceholder value to an empty object. With both props set to "falsy" values, the gallery media frame is initialized in the "Create Gallery" state. This lets users replace placeholder images from patterns.

Co-authored-by: Grzegorz <[email protected]>
Co-authored-by: grzim <[email protected]>
Co-authored-by: George Mamadashvili <[email protected]>
@Mamaduka Mamaduka deleted the fix/add-to-gallery-check branch April 6, 2021 12:52
@gwwar
Copy link
Contributor

gwwar commented Apr 6, 2021

Excellent, thanks @Mamaduka for the contribution! 💖 to @glendaviesnz and @gziolo for reviewing too!

ntsekouras pushed a commit that referenced this pull request Apr 7, 2021
* revert #27717 (#30524)

Co-authored-by: grzim <[email protected]>

* Use getAuthors for 'showCombobox' check (#30218)

* Use getAuthors for 'showCombobox' check

* Add inline comment

* Gallery: Set 'addToGallery' prop to false when images don't have IDs. (#30122)

This also sets MediaPlaceholder value to an empty object. With both props set to "falsy" values, the gallery media frame is initialized in the "Create Gallery" state. This lets users replace placeholder images from patterns.

Co-authored-by: Grzegorz <[email protected]>
Co-authored-by: grzim <[email protected]>
Co-authored-by: George Mamadashvili <[email protected]>
@desrosj desrosj removed the Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release label Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Gallery Affects the Gallery Block - used to display groups of images [Feature] Patterns A collection of blocks that can be synced (previously reusable blocks) or unsynced [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gallery block adds all media items when adding an image to a default gallery block pattern
5 participants