Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Optimised gallery image upload previews (#1079)
Browse files Browse the repository at this point in the history
no issue
- use `URL.createObjectURL(file)` to get a blob url rather than using `FileReader.readAsDataURL` which generates a very large data attribute
- speeds up the display of previews and associated browser hangs when an upload finishes, especially noticeable with large images and fast connections where multiple uploads finish around the same time
  • Loading branch information
kevinansfield authored Dec 5, 2018
1 parent 55a3500 commit 7c70617
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions lib/koenig-editor/addon/components/koenig-card-gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,30 +228,22 @@ export default Component.extend({
},

_readDataFromImageFile(file) {
let reader = new FileReader();
let url = URL.createObjectURL(file);
let image = EmberObject.create({
fileName: file.name
fileName: file.name,
previewSrc: url
});

reader.onload = (e) => {
let imgElement = new Image();
let previewSrc = htmlSafe(e.target.result);
let imageElem = new Image();
imageElem.onload = () => {
// update current display images
image.set('width', imageElem.naturalWidth);
image.set('height', imageElem.naturalHeight);

image.set('previewSrc', previewSrc);

imgElement.onload = () => {
// update current display images
image.set('width', imgElement.width);
image.set('height', imgElement.height);

// ensure width/height makes it into the payload images
this._buildAndSaveImagesPayload();
};

imgElement.src = previewSrc;
// ensure width/height makes it into the payload images
this._buildAndSaveImagesPayload();
};

reader.readAsDataURL(file);
imageElem.src = url;

return image;
},
Expand Down

0 comments on commit 7c70617

Please sign in to comment.