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

[custom-handler] Allow specifying a custom file reader handler #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kensnyder
Copy link
Owner

No description provided.

@kensnyder kensnyder mentioned this pull request Mar 19, 2019
@stargazing-dino
Copy link

stargazing-dino commented Mar 20, 2019

File reading is not my thing but would this line

const blob = file.getAsFile ? file.getAsFile() : file;

be better served before the fileReader callback is checked for and called? That way we can be semi sure a File is returned in the callback instead of an Item?

@kensnyder
Copy link
Owner Author

@Nolence Let me update the PR.

@kensnyder
Copy link
Owner Author

@Nolence have a look now.

Copy link

@stargazing-dino stargazing-dino left a comment

Choose a reason for hiding this comment

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

Hi Ken, first off, thanks for working on this. I used to go to school at the U and was always running about Salt Lake, but it's rare that I meet another Utahn online.

It seems like this line is disregarding a lot of valid cases:

if (!file.getAsFile) {
  // dragged object is not a file
  return;
}

As files themselves don't have that method, only vague DataTransferItems. Here's what I had in mind:

const blob = file.getAsFile ? file.getAsFile() : file;

if (!(blob instanceof Blob)) {
  // dragged object is not a file
  return;
}

// defer to custom file reader handler if specified
if (typeof this.options.fileReader === "function") {
  this.options.fileReader(blob, callback);
  return;
}
// otherwise set up file reader
const reader = new FileReader();
reader.onload = evt => {
  callback(evt.target.result);
};
// read the clipboard item or file
reader.readAsDataURL(blob);

I tested this on FireFox, Chrome and Edge. Edge didn't seem to support drag and drop but that seems like a wholly different PR.

@zachzoup
Copy link

Is this going to be merged? Doesn't seem to be any issues but it's been almost three months.

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.

3 participants