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

Lightbox bugfixes #14598

Merged
merged 4 commits into from Apr 13, 2018
Merged

Lightbox bugfixes #14598

merged 4 commits into from Apr 13, 2018

Conversation

cathyxz
Copy link
Contributor

@cathyxz cathyxz commented Apr 12, 2018

  1. Bail out of transition when image height and width is 0, 0.
  2. Wait for carousel to be visible in close gallery to avoid race when the image shows as 0, 0.

* @private
*/
closeGallery_() {
this.vsync_.mutate(() => {
return this.vsync_.mutatePromise(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be this.mutateElement? /cc @jridgewell

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do need to refactor this entire component now...

@@ -1417,11 +1424,13 @@ export class AmpLightboxGallery extends AMP.BaseElement {
}

const closeGalleryAndShowTargetSlide = event => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Using class methods for listener callbacks with more than a couple lines of logic can help keep things organized

this.closeGallery_().then(() => {
this.currentElemId_ = thumbnailObj.element.lightboxItemId;
dev().assert(this.carousel_).getImpl()
.then(carousel => carousel.showSlideWhenReady(this.currentElemId_));
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: More than one level of Promise chaining

.implementation_.getImageBoxWithOffset();

// If the image isn't initialized, bail out the transition.
if (imageBox.width == 0 && imageBox.height == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm this seems like a proxy for something else like a lifecycle method. Is there a class method or hook you can use to know if the image viewer is initialized?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So for this, I refactored <amp-image-viewer> to return null in the uninitialized case, so that we do a null check instead. I think doing a fade-in is better than waiting for the load, since the image could still be loading while we're animating the fade.

element.addEventListener('click', closeGalleryAndShowTargetSlide);
const handleThumbnailClick = this.generateThumbnailClickHandler_(
thumbnailObj.element.lightboxItemId);
element.addEventListener('click', handleThumbnailClick.bind(this));
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like you're creating a new function to save the id parameter, but that is a lot of indirection. Instead of returning a function from generateThumbnailClickHandler, use the class method to do work, then call it from the fat-arrow function and pass the id.

handleThumbnailClick_(event, id) {
  event.stopPropagation();
  Promise.all([
    this.closeGallery_(),
    dev().assert(this.carousel_).getImpl(),
  ]).then(values => {
    this.currentElemId_ = id;
    const carouselImpl = values[1];
    carouselImpl.showSlideWhenReady(this.currentElemId_);
        this.updateDescriptionBox_();
  });
}
 
/* ... */

element.addEventListener('click', 
    event => this.handleThumbnailClick_(event, thumbnailObj.element.lightboxItemId));

@@ -76,11 +76,11 @@ export class AmpImageViewer extends AMP.BaseElement {
/** @private {number} */
this.sourceHeight_ = 0;

/** @private {!../../../src/layout-rect.LayoutRectDef} */
this.elementBox_ = layoutRectLtwh(0, 0, 0, 0);
/** @private {?../../../src/layout-rect.LayoutRectDef} */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm now a bigger fan of being explicit about this value not being initialized as opposed to giving it a valid but incorrect (0,0,0,0) value.

@cathyxz
Copy link
Contributor Author

cathyxz commented Apr 13, 2018

@cvializ PTAL? =)

Copy link
Contributor

@cvializ cvializ left a comment

Choose a reason for hiding this comment

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

LGTM

@cathyxz cathyxz merged commit 1d303f0 into ampproject:master Apr 13, 2018
@cathyxz cathyxz deleted the bugfix/images-0-0 branch April 13, 2018 18:54
This was referenced Apr 16, 2018
glevitzky pushed a commit that referenced this pull request Apr 27, 2018
* Add bail out when image is unitialized, add wait for promise to avoid race

* Refactor vsync and const function

* Refactor to use getImpl and measureMutate

* Refactor to use measureMutate instead of vsync, change image-viewer api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants