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

Disable drop state for non-files #2449

Merged
merged 13 commits into from
Oct 20, 2021
Merged

Disable drop state for non-files #2449

merged 13 commits into from
Oct 20, 2021

Conversation

lafe
Copy link
Contributor

@lafe lafe commented Aug 11, 2020

Currently, the dashboard shows the "Drop files here" UI even if the content that is dragged over is not a file. This also happens if the user tries to drag & drop items directly from Outlook onto the drop area using Firefox (see this bug report for details).
This PR checks if the types collection contains the value Files. In this case, at least one of the items will be a file that can be further processed by Uppy.

@arturi
Copy link
Contributor

arturi commented Aug 11, 2020

Hi! Thank you for the PR! Could you add the same check to https://github.com/transloadit/uppy/blob/master/packages/%40uppy/drag-drop/src/index.js#L118, please?

@goto-bus-stop
Copy link
Contributor

Thanks for the PR—I just added a few compatibility tweaks. There is one more issue. We currently support dropping URLs straight onto the Dashboard if you're also using the @uppy/url plugin. This relies on URLs not being filtered out early.

That feature is implemented by having the Dashboard cal the handleRootDrop method of other plugins.

Maybe what we need is another such method, canHandleRootDrop(event), that is called in the dragover phase? If that method returns true, we can do the same thing as when there are files.

@arturi arturi self-requested a review October 4, 2021 13:09
@arturi arturi assigned arturi and unassigned lakesare Oct 4, 2021
@arturi arturi requested a review from Murderlon October 11, 2021 11:20
Comment on lines 606 to 630
// 1. Check if the "type" of the datatransfer object includes files
const { types } = event.dataTransfer
const hasFiles = types.some(type => type === 'Files')

// 2. Check if some plugin can handle the datatransfer without files —
// for instance, the Url plugin can import a url
let somePluginCanHandleRootDrop = true
this.uppy.iteratePlugins((plugin) => {
if (plugin.canHandleRootDrop?.(event)) {
somePluginCanHandleRootDrop = true
}
})

// 3. Deny drop, if no plugins can handle datatransfer, there are no files,
// or when opts.disabled is set, or new uploads are not allowed
if (
(!somePluginCanHandleRootDrop && !hasFiles)
|| this.opts.disabled
// opts.disableLocalFiles should only be taken into account if no plugins
// can handle the datatransfer
|| (this.opts.disableLocalFiles && (hasFiles || !somePluginCanHandleRootDrop))
|| !this.uppy.getState().allowNewUpload
) {
event.dataTransfer.dropEffect = 'none'
clearTimeout(this.removeDragOverClassTimeout)
Copy link
Member

Choose a reason for hiding this comment

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

Can we put this into a function? Doesn't necessarily need to be outside of this function, can be in the same lexical scope.

A pattern I'm starting to dislike in Uppy is class methods growing bigger and bigger, doing multiple things, and it's really hard to follow line by line what's happening. By grouping every computation or check, it makes things much easier to skim if it has an intention revealing name.

For example:

if (!checkUserOptions()) {
  return
}
if (!checkEventForFiles() && !checkPluginSupportForEvent()) {
  return 
}

Looking at this, I don't even need to go into those functions if that's not relevant for what I might be doing there, it already tells me what is happening without thinking too hard.


// 4. Add all dropped files, handle errors
let executedDropErrorOnce = false
Copy link
Member

Choose a reason for hiding this comment

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

won't this be false every time handleDrop is called?

const files = await getDroppedFiles(event.dataTransfer, { logDropError })
this.addFiles(files)
getDroppedFiles(event.dataTransfer, { logDropError })
.then((files) => {
Copy link
Member

Choose a reason for hiding this comment

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

No longer async/await?

if (this.opts.onDrop) this.opts.onDrop(event)

handleDragOver (event) {
this.opts?.onDragOver(event)
Copy link
Member

Choose a reason for hiding this comment

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

should be moved to the bottom.

@arturi arturi merged commit 8985283 into transloadit:main Oct 20, 2021
Murderlon added a commit that referenced this pull request Oct 20, 2021
* main:
  Disable drop state for non-files (#2449)
  meta: use Yarn v3 instead of npm (#3237)
  examples: Update to Parcel 2 (#3275)
  ci: test Companion with Node.js 16.x (#3273)
  e2e tests: Move transloadit2 test into transloadit, remove the former (#3271)
  Release
  ci: fix tranloadit2 e2e tests (#3270)
  Add unique class to discard button (#3269)
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.

5 participants