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

fix: preloader bug #185

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions src/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ export default class Uploader {
* @param {Function} onPreview - callback fired when preview is ready
*/
uploadSelectedFile({ onPreview }) {
const preparePreview = function (file) {
const reader = new FileReader();

reader.readAsDataURL(file);
reader.onload = (e) => {
onPreview(e.target.result);
};
};

/**
* Custom uploading
* or default uploading
Expand All @@ -44,8 +35,8 @@ export default class Uploader {

// custom uploading
if (this.config.uploader && typeof this.config.uploader.uploadByFile === 'function') {
upload = ajax.selectFiles({ accept: this.config.types }).then((files) => {
preparePreview(files[0]);
upload = ajax.selectFiles({ accept: this.config.types }).then(async (files) => {
onPreview(await preparePreview(files[0]));

const customUpload = this.config.uploader.uploadByFile(files[0]);

Expand All @@ -63,8 +54,8 @@ export default class Uploader {
data: this.config.additionalRequestData,
accept: this.config.types,
headers: this.config.additionalRequestHeaders,
beforeSend: (files) => {
preparePreview(files[0]);
beforeSend: async (files) => {
onPreview(await preparePreview(files[0]));
},
fieldName: this.config.field,
}).then((response) => response.body);
Expand Down Expand Up @@ -123,18 +114,11 @@ export default class Uploader {
* @param {File} file - file pasted by drag-n-drop
* @param {Function} onPreview - file pasted by drag-n-drop
*/
uploadByFile(file, { onPreview }) {
async uploadByFile(file, { onPreview }) {
/**
* Load file for preview
*
* @type {FileReader}
* Load preview
*/
const reader = new FileReader();

reader.readAsDataURL(file);
reader.onload = (e) => {
onPreview(e.target.result);
};
onPreview(await preparePreview(file));

let upload;

Expand Down Expand Up @@ -177,3 +161,35 @@ export default class Uploader {
}
}

/**
* Check if passed object is a Promise
*
* @param {*} object - object to check
* @returns {boolean}
*/
function isPromise(object) {
return object && typeof object.then === "function";
}

/**
* Prepare preview
*
* @param {File} file - file to preview
* @returns {Promise<string>}
*/
const preparePreview = (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();

reader.readAsDataURL(file);
reader.onload = () => {
resolve(reader.result);
};
reader.onabort = () => {
reject(new Error('Aborted on file reading'));
};
reader.onerror = () => {
reject(new Error('Error on file reading'));
};
});
};