-
Notifications
You must be signed in to change notification settings - Fork 105
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
Fix: Inconsistent natural image dimensions in IE #324
Changes from 1 commit
4094262
991f919
d26ab3f
3a5e7ea
d9f7261
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ class ImageBaseViewer extends BaseViewer { | |
return; | ||
} | ||
|
||
ImageBaseViewer.setOriginalImageSize(this.imageEl); | ||
this.loadUI(); | ||
this.zoom(); | ||
|
||
|
@@ -167,6 +168,33 @@ class ImageBaseViewer extends BaseViewer { | |
this.bindControlListeners(); | ||
} | ||
|
||
/** | ||
* Sets the original image width and height on the img element. Can be removed when | ||
* naturalHeight and naturalWidth attributes work correctly in IE 11. | ||
* | ||
* @param {Image} imageEl - The image to set the original size attributes on | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. {HTMLElement} |
||
* @return {void} | ||
*/ | ||
static setOriginalImageSize(imageEl) { | ||
const image = imageEl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @JustinHoldstock mentioned at some point he prefers just disabling the eslint rule that warns against this when you mean to explicitly modify the passed in param. We also generally tend to keep the 'El' portion of a name to indicate an HTML Element. Would look like:
|
||
|
||
// Do not bother loading a new image when the natural size attributes exist | ||
if (imageEl.naturalWidth > 0 && imageEl.naturalHeight > 0) { | ||
image.originalWidth = imageEl.naturalWidth; | ||
image.originalHeight = imageEl.naturalHeight; | ||
} else { | ||
const originalImg = new Image(); | ||
image.originalWidth = 1; | ||
image.originalHeight = 1; | ||
|
||
originalImg.onload = () => { | ||
image.originalWidth = originalImg.width || 1; | ||
image.originalHeight = originalImg.height || 1; | ||
}; | ||
originalImg.src = imageEl.src; | ||
} | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Event Listeners | ||
//-------------------------------------------------------------------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.setOriginalImageSize(this.imageEl);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setOriginalImageSize is implemented as a static method right now. Is there any preference for avoiding static methods? I know that only one Viewer will actually be instantiated so there will not be much of a difference in our use case but I am advocating for the use of static methods to avoid the creation of monolithic utility files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have a policy one way or another, but I'd lean towards making this a normal private function in the base viewer until otherwise needed since that's how we've organized other similar helper functions. The moment this is required in a different viewer outside of the ImageBaseViewer inheritance chain, we can easily update this to a static method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to be non-static