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

Update FileDropzone API #678

Merged
merged 13 commits into from
Feb 21, 2022
Merged

Update FileDropzone API #678

merged 13 commits into from
Feb 21, 2022

Conversation

gilest
Copy link
Collaborator

@gilest gilest commented Jan 28, 2022

Adds new arguments queue and filter to FileDropzone.

Includes basic test coverage of these new APIs while deprecated API tests still pass.

Adds typing to FileDropzone component and DataTransferWrapper internal utility. I could have taken this further but I think it's good to do this process incrementally. Honestly there's a lot to untangle in some of these internal utilities (regarding typing support).

Registers/unregisters the FileDropzone as an event listener on the queue in the method from #654.

A note about naming

Following the work of @gossi in #620 had planned to name the callback args dragEnter, dragLeave and drop but there's an ember-template-lint rule specifically discouraging this.

Screen Shot 2022-01-29 at 12 21 55 PM

I don't like the names I have chosen – will welcome other suggestions of how we name the callback arguments but I would very much like it to be consistent across the whole addon.

I've decided to revert to using existing event names with the on prefix.

Comment on lines 255 to 300
// @TODO - add tests for these or remove them
// // Testing support for dragging and dropping images
// // from other browser windows
// let url;

// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// const html = this.dataTransferWrapper.getData('text/html');
// if (html) {
// const parsedHtml = parseHTML(html);
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// const img = parsedHtml.getElementsByTagName('img')[0];
// if (img) {
// url = img.src;
// }
// }

// if (url == null) {
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// url = this.dataTransferWrapper.getData('text/uri-list');
// }

// if (url) {
// const image = new Image();
// const [filename] = url.split('/').slice(-1);
// image.crossOrigin = 'anonymous';
// image.onload = () => {
// const canvas = document.createElement('canvas');
// canvas.width = image.width;
// canvas.height = image.height;

// const ctx = canvas.getContext('2d');
// ctx?.drawImage(image, 0, 0);

// if (canvas.toBlob) {
// canvas.toBlob((blob) => {
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// const [file] = this.addFiles([blob], FileSource.web);
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// file.name = filename;
// });
// } else {
// const binStr = atob(canvas.toDataURL().split(',')[1]);
// const len = binStr.length;
// const arr = new Uint8Array(len);

// for (let i = 0; i < len; i++) {
// arr[i] = binStr.charCodeAt(i);
// }
// const blob = new Blob([arr], { type: 'image/png' });
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// blob.name = filename;
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// const [file] = this.addFiles([blob], FileSource.web);
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// file.name = filename;
// }
// };
// /* eslint-disable no-console */
// image.onerror = function (e) {
// console.log(e);
// };
// /* eslint-enable no-console */
// image.src = url;
// }
Copy link
Collaborator Author

@gilest gilest Jan 28, 2022

Choose a reason for hiding this comment

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

This code concerns me as I have found there is no test coverage of it and I'm not sure how to test it manually.

I'm worried we may have broken this functionality without our knowledge in recent updates. However I did try to drag an image from another browser window in v4.0.3 and it didn't work so 🤷🏻‍♂️ .

Options:

  • Explicitly remove this as it was undocumented (although I think allowUploadsFromWebsites is related) and untested. Can always be added back as an non-breaking enhancement
  • Try and figure out how it works and support it now

addon/queue.ts Outdated Show resolved Hide resolved
@gilest gilest requested review from gossi and jelhan January 28, 2022 23:32
@gilest gilest mentioned this pull request Jan 28, 2022
22 tasks
@jelhan
Copy link
Collaborator

jelhan commented Feb 1, 2022

I will try to do a full review later today. But thought that leaving some thoughts regarding naming early may make sense.

A note about naming

Following the work of @gossi in #620 had planned to name the callback args dragEnter, dragLeave and drop but there's an ember-template-lint rule specifically discouraging this.

Did you considered to keep the on* naming convention? For example onDragEnter and onDrop.

Prefixing event handlers with on helps with readability in my opinion. It communicates to the reader that this is an event handler, which executes a passed in function when a specific situation happens. It is also seems to be kind of a standard among favorite Ember addons.

It also helps to discover the available event handlers - especially when looking at autocompletion in templates. I hope that we will get to that in the foreseeable future. First-class component templates and Glimmer component signature type RFCs are very promising in that regard.

Copy link
Collaborator

@gossi gossi left a comment

Choose a reason for hiding this comment

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

I love to see all this classic code go and see it replaced with new code.

I didn't look into the details of drag'n'drop code (as I don't know the details) but more on a "highlevel" view. Left some comments about the naming.

Apart from that, I think, it is looking really good.

addon/components/file-dropzone.ts Outdated Show resolved Hide resolved
addon/components/file-dropzone.ts Outdated Show resolved Hide resolved
addon/components/file-dropzone.ts Outdated Show resolved Hide resolved
addon/components/file-dropzone.ts Outdated Show resolved Hide resolved
tests/integration/components/file-dropzone-test.js Outdated Show resolved Hide resolved
@gilest gilest marked this pull request as ready for review February 18, 2022 21:50
@gilest
Copy link
Collaborator Author

gilest commented Feb 18, 2022

@gossi @jelhan Thanks for your comments. I've opted to use the original callback names and the on prefix.

I think it's important that the callback names are consistent so I've created #701 to track.

Believe this is ready to merge and will do so in a day or two if I don't get further reviews.

@gilest gilest requested a review from gossi February 18, 2022 23:03
refactor(data transfer): convert to typescript, add namespace
…dle (deprecated) multiple argument internally

test(file queue): reduce mirage handler timing
Comment out experimental cross-window support. It's not covered by any tests. Not sure it works or is in use.

Use FileSource enums rather than literals.

Ensure that dropping multiple files is allowed by default.
docs(file upload): improve deprecated method recommendation
…opzoneWrapper - hopefully makes it more clear that this is an internal wrapper

Simplify logic and clarify the function of FileDropzoneWrapper by creating a filesOrItems property.
Improve typing of file property callback arguments.
feat(both components): remove unpublished filtering
docs(validation): update validation doc to describe the `@filter` arg
They reappeared in the rebase.
@gilest gilest merged commit 840f8d9 into master Feb 21, 2022
@gilest gilest deleted the update-dropzone-api branch February 21, 2022 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants