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

Gallery: Update media frame state management #2488

Merged
merged 2 commits into from
Apr 24, 2018

Conversation

joemcgill
Copy link
Member

Following #2294, this updates the Media component so that existing galleries instantiate a media frame in the gallery-edit state with the correct attachment models passed in on creation, rather than overriding the state after the frame has been created.

This fixes several small bugs, like having text from the gallery state still showing in the toolbar, the cancel menu item not working as expected in the menu, etc.

This also reworks the way attachments are fetched so that we can get the entire collection of attachments in a single HTTP request, rather than separate requests for each attachment.

@joemcgill joemcgill requested a review from mkaz August 21, 2017 13:03
@codecov
Copy link

codecov bot commented Aug 21, 2017

Codecov Report

Merging #2488 into master will decrease coverage by 16.89%.
The diff coverage is 14.28%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2488      +/-   ##
==========================================
- Coverage      44%   27.11%   -16.9%     
==========================================
  Files         398      159     -239     
  Lines        9015     4902    -4113     
  Branches     1596      817     -779     
==========================================
- Hits         3967     1329    -2638     
+ Misses       4322     3030    -1292     
+ Partials      726      543     -183
Impacted Files Coverage Δ
blocks/media-upload-button/index.js 7.14% <14.28%> (ø)
blocks/url-input/button.js 0% <0%> (-100%) ⬇️
components/panel/row.js 0% <0%> (-100%) ⬇️
components/keyboard-shortcuts/index.js 0% <0%> (-92.86%) ⬇️
blocks/color-palette/index.js 0% <0%> (-90%) ⬇️
blocks/alignment-toolbar/index.js 16.66% <0%> (-83.34%) ⬇️
blocks/block-controls/index.js 0% <0%> (-80%) ⬇️
components/drop-zone/index.js 0% <0%> (-72.73%) ⬇️
blocks/block-alignment-toolbar/index.js 33.33% <0%> (-55.56%) ⬇️
blocks/library/more/index.js 30% <0%> (-51.49%) ⬇️
... and 464 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d96d79b...4586353. Read the comment docs.

@@ -121,25 +142,6 @@ class MediaUploadButton extends Component {
onSelect( multiple ? attachment : attachment[ 0 ] );
}

onOpen() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this means we should also set a selection object when we're not in a gallery (Editing an image block is broken with this change).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah right. We do need to handle this the same way for images as well.

Copy link
Member

Choose a reason for hiding this comment

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

The problem was fixed with the new commit I added :)

@@ -78,11 +92,19 @@ class MediaUploadButton extends Component {
}

if ( gallery ) {
const ids = this.props.value || [];
const attachments = getAttachmentsCollection( ids );
const currentState = ( this.props.value ) ? 'gallery-edit' : 'gallery';
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we do the same for a single media? I mean do we have something like 'edit' and 'select' states for single media selector?

Copy link
Member

Choose a reason for hiding this comment

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

Hi, @youknowriad we have edit states for single media like audio and video, but this states open with a modal to change details like sizes, captions etc of the item that we can now change in the Gutenberg block. So for single media I don't think we should use this states.

@@ -78,11 +92,19 @@ class MediaUploadButton extends Component {
}

if ( gallery ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't the gallery prop a duplicate of multiple prop?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not exactly, because you could use a media upload button to insert multiple images that are not in a gallery. I would like to see us handle props differently for galleries than passing a gallery prop (see: #1979) but was going to leave that as a separate issue.

Copy link
Contributor

@youknowriad youknowriad Aug 21, 2017

Choose a reason for hiding this comment

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

In this case, should we expect the selection object to be dependent on the multiple prop instead of the gallery prop. I'm guessing the selection shape depends on whether the selection is multiple or not, and not whether it's a gallery view or not?

Copy link
Member Author

Choose a reason for hiding this comment

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

I see what you're getting at. Yeah, we could probably use a similar method in both cases.

@youknowriad
Copy link
Contributor

Any plans to update this. I'm thinking addressing at least this is important.

This also reworks the way attachments are fetched so that we can get the entire collection of attachments in a single HTTP request, rather than separate requests for each attachment.

--
Anything we can help with?

@youknowriad youknowriad added the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Jan 30, 2018
@joemcgill
Copy link
Member Author

Hi @youknowriad, thanks for checking in. I clearly haven't had the time to circle back to this issue, but I agree that it would be nice to update/finish this off. I should have time in a few weeks to dig back into this issue if you're ok with leaving it open.

@youknowriad
Copy link
Contributor

@joemcgill Sure, not urgent at all, just wanted to check in. Let's leave it open.

joemcgill and others added 2 commits April 23, 2018 11:16
…ding of media frame when editing a gallery; Editing a gallery opens media frame in gallery-edit state

This sets the editing option based on the values property of our component.

Previously, we were replacing the media frame state right before opening
the modal, but after views like the menu were created. This caused a few
bugs, like the cancel button moving back to the initial "create gallery"
state and some labels being incorrect. This creates a selection from any
existing IDs and passes it to the frame before it is created to avoid
these issues.

Additionally, this updates the method for getting a collection of
attachments using `wp.media.query` so we can fetch all of the attachment
models in one request, rather than relying on seperate requests for each
attachment in the gallery.

When opening the `wp.media` modal to modify an existing gallery, the
frame should be opened in the `gallery-edit` state, rather than the
`gallery` state used when selecting images for a new gallery.
…d; Corrected bug where if gallery was empty all media content was loaded.
@jorgefilipecosta jorgefilipecosta force-pushed the update/gallery-block-media-frame-updates branch from 271fa19 to 4586353 Compare April 23, 2018 14:30
@jorgefilipecosta
Copy link
Member

jorgefilipecosta commented Apr 23, 2018

Hi @joemcgill, @youknowriad,
I think the changes in this PR are a must as they increase the performance, and make the gallery modal use the correct frame when editing instead of the create one.
I rebased this PR keeping exactly the same changes proposed by @joemcgill.
And after I added a new commit which addresses some problems I found:
Before all images of the MediaUpload were loaded even if we did not open the media modal, now they are loaded only when the Media modal opens.
Before if we added an empty gallery block, as we did not filter by id all the media attachments were loaded.
Problems using MediaUplod for a single item e.g: image block were solved. The logic was kept the same as before, but an improvement to not fetch the attachment if we already have it was added.

@jorgefilipecosta jorgefilipecosta removed the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Apr 23, 2018
};
if ( !! type ) {
frameConfig.library = { type };
}

if ( gallery ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Still having trouble to understand the difference between gallery and multiple props. I wonder if we should consolidate these two or if they are useful separately.

Copy link
Member

Choose a reason for hiding this comment

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

Hi @youknowriad, the gallery flag indicates that we want to use the gallery media frame, the multiple still uses the normal select frame but allows us to select multiple files. Imagine a document block that wants to allow to select multiple documents it may use multiple flag but not use gallery flag.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@jorgefilipecosta jorgefilipecosta merged commit d13a862 into master Apr 24, 2018
@jorgefilipecosta jorgefilipecosta deleted the update/gallery-block-media-frame-updates branch April 24, 2018 07:22
@gziolo gziolo added this to the 2.8 milestone Apr 24, 2018
@joemcgill
Copy link
Member Author

Nice changes. Thanks for picking this up, @jorgefilipecosta!

nuzzio pushed a commit to nuzzio/gutenberg that referenced this pull request Apr 25, 2018
* Gallery: Properly set editing option for the media frame; Improve loading of media frame when editing a gallery; Editing a gallery opens media frame in gallery-edit state

This sets the editing option based on the values property of our component.

Previously, we were replacing the media frame state right before opening
the modal, but after views like the menu were created. This caused a few
bugs, like the cancel button moving back to the initial "create gallery"
state and some labels being incorrect. This creates a selection from any
existing IDs and passes it to the frame before it is created to avoid
these issues.

Additionally, this updates the method for getting a collection of
attachments using `wp.media.query` so we can fetch all of the attachment
models in one request, rather than relying on seperate requests for each
attachment in the gallery.

When opening the `wp.media` modal to modify an existing gallery, the
frame should be opened in the `gallery-edit` state, rather than the
`gallery` state used when selecting images for a new gallery.

* Corrected bug where single media selection was not working as expected; Corrected bug where if gallery was empty all media content was loaded.
@mtias
Copy link
Member

mtias commented May 3, 2018

Thanks for finishing this one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants