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
Contributor

@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.
@@ -324,10 +324,11 @@ function GalleryEdit( props ) {
}, [ linkTo ] );

const hasImages = !! images.length;

This comment has been minimized.

@gwwar

gwwar Apr 2, 2021
Contributor

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 -->

This comment has been minimized.

@Mamaduka

Mamaduka Apr 2, 2021
Author Contributor

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.

This comment has been minimized.

@glendaviesnz

glendaviesnz Apr 6, 2021
Contributor

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.

This comment has been minimized.

@glendaviesnz

glendaviesnz Apr 6, 2021
Contributor

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.

@gziolo
gziolo approved these changes Apr 6, 2021
Copy link
Member

@gziolo gziolo left a comment

Let's get it into the WP 5.7.1 release.

@gziolo gziolo merged commit 07abb2b into WordPress:trunk Apr 6, 2021
20 checks passed
20 checks passed
Bump version
Details
Cancel Previous Runs
Details
Check
Details
Checks (12)
Details
Admin - 1 Admin - 1
Details
Run performance tests
Details
pull-request-automation
Details
test (gutenberg-editor-gallery)
Details
test (12.2, gutenberg-editor-gallery)
Details
All
Details
JavaScript (12)
Details
Checks (14)
Details
Admin - 2 Admin - 2
Details
JavaScript (14)
Details
Admin - 3 Admin - 3
Details
Admin - 4 Admin - 4
Details
Build Release Artifact
Details
PHP
Details
Create Release Draft and Attach Asset
Details
Mobile
Details
WordPress 5.7.x Must Haves automation moved this from Needs Review to Done Apr 6, 2021
@github-actions github-actions bot added this to the Gutenberg 10.4 milestone Apr 6, 2021
gziolo added 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 Mamaduka:fix/add-to-gallery-check branch Apr 6, 2021
@gwwar
Copy link
Contributor

@gwwar gwwar commented Apr 6, 2021

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

ntsekouras added 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment