-
-
Notifications
You must be signed in to change notification settings - Fork 119
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
Conversation
addon/components/file-dropzone.ts
Outdated
// @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; | ||
// } |
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 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
I will try to do a full review later today. But thought that leaving some thoughts regarding naming early may make sense.
Did you considered to keep the Prefixing event handlers with 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. |
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.
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.
7ec3b29
to
0a5717c
Compare
refactor(data transfer): convert to typescript, add namespace
…dle (deprecated) multiple argument internally test(file queue): reduce mirage handler timing
…ch (deprecated) onFileAdd
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
0a5717c
to
5d81caa
Compare
They reappeared in the rebase.
Adds new arguments
queue
andfilter
to FileDropzone.Includes basic test coverage of these new APIs while deprecated API tests still pass.
Adds typing to
FileDropzone
component andDataTransferWrapper
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
anddrop
but there's anember-template-lint
rule specifically discouraging this.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.