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

Set meta based on drop target event #2973

Closed
wants to merge 5 commits into from

Conversation

tobiasps
Copy link

This pull request extends the DropTarget plugin to allow for manipulating the meta fields of the dropped file based on the DragEvent. As an example, this allows using DropTarget to enable uploads to specific folders in a UI by using the DragEvent to determine the parents of the event.

The pull request contains the following changes:

  • Added new option activeClass for customizing the class added to target on dragover.
  • Added new option setMeta to enable setting specific meta fields depending on the drop event. This enables using one DropTarget with body as target with the meta fields being set depending on which DOM element the files were dropped.
  • Added droptarget:dragover, droptarget:dragleave, and droptarget:drop events to allow for maniulating classes on the current element.

…t on dragover.

Added new option setMeta to enable setting specific meta fields depending on the drop event. This enables using one DropTarget with body as target with the meta fields being set depending on which DOM element the files was dropped.
Copy link
Member

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

Thanks for sending this. Would you be able to write tests for these options?

packages/@uppy/drop-target/src/index.js Outdated Show resolved Hide resolved
tobiasps and others added 2 commits August 28, 2021 22:38
Simplify adding meta fields from setMeta in addFiles function.

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
packages/@uppy/drop-target/types/index.d.ts Outdated Show resolved Hide resolved
packages/@uppy/drop-target/src/index.js Outdated Show resolved Hide resolved
tobiasps and others added 2 commits August 30, 2021 13:29
Allow setMeta to be optional.
Using more descriptive type for the setMeta option.

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
Comment on lines +31 to +32
const descriptors = files.map((file) => {
return {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason not to use the shorter form as before?

Suggested change
const descriptors = files.map((file) => {
return {
const descriptors = files.map((file) => ({

Copy link
Author

Choose a reason for hiding this comment

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

No, I'll fix this.

Copy link
Member

@Murderlon Murderlon left a comment

Choose a reason for hiding this comment

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

I have a few questions/concerns about these changes.

  • Why isn't uppy.setFileMeta enough for your use case?
  • Do we need activeClass? If you want custom styles, can't you target the existing class and override it?

@tobiasps
Copy link
Author

I have a few questions/concerns about these changes.

What I needed was a way to set the file meta depending on which dom node the file was dropped. To do this I needed to get access to the drag event object which is why I implemented the setMeta hook, which includes the drag event.
My use case is uploading to a file tree where each folder is a possible upload point. With this change to DropTarget, I can have one global DropTarget instance and use the drag event to determine where the file was dropped.

We have a small screencast of the functionality: https://share.iogates.com/show/511557/343338-34ys4uatjh0xhvhc

  • Do we need activeClass? If you want custom styles, can't you target the existing class and override it?

Our web app is old and we are using uppy in the traditional way, as a global library. It was easier for us to change the CSS changes by effectively disabling the default behaviour by changing the active class name.

@aduh95
Copy link
Member

aduh95 commented Sep 15, 2021

I think you could indeed achieve what you are trying to do by subclassing the DropTarget class plugin:

class MyDropTarget extends DropTarget {
  install(...args) {
    const { addFiles, handleDrop } = this;
    this.addFiles = (files) => {
      addFiles(files);
      return files;
    };
    this.handleDrop = (event) =>
      handleDrop(event).then((files) => {
        /* here you have access to `files`, `event`, and `uppy.setFileMeta` */
      });
    return super.install(...args);
  }
}

uppy.use(MyDropTarget, {
  target: document.body,
});

For the activeClass hack, I don't have an opinion, it's also possible to hack you way around using a similar trick.

@Murderlon
Copy link
Member

Murderlon commented Sep 15, 2021

@tobiasps thanks for clarifying. I'm not happy with adding setMeta in this plugin while we already have setMeta (and setFileMeta) in @uppy/core.

Could we instead only expose the events, like #2914, but pass the file ID as an additional parameter? Then you should be able to use uppy.setFileMeta in your custom event handler.

@Murderlon
Copy link
Member

Superseded by #3238

@Murderlon Murderlon closed this Oct 5, 2021
@tobiasps
Copy link
Author

tobiasps commented Oct 6, 2021

Superseded by #3238

Great! Thanks for your work @Murderlon

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.

None yet

3 participants