drag-drop

WebJar for drag-drop

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

drag-drop
Last Version

Last Version

2.13.3
Release Date

Release Date

Type

Type

jar
Description

Description

drag-drop
WebJar for drag-drop
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/feross/drag-drop

Download drag-drop

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/drag-drop/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>drag-drop</artifactId>
    <version>2.13.3</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/drag-drop/
implementation 'org.webjars.npm:drag-drop:2.13.3'
// https://jarcasting.com/artifacts/org.webjars.npm/drag-drop/
implementation ("org.webjars.npm:drag-drop:2.13.3")
'org.webjars.npm:drag-drop:jar:2.13.3'
<dependency org="org.webjars.npm" name="drag-drop" rev="2.13.3">
  <artifact name="drag-drop" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='drag-drop', version='2.13.3')
)
libraryDependencies += "org.webjars.npm" % "drag-drop" % "2.13.3"
[org.webjars.npm/drag-drop "2.13.3"]

Dependencies

compile (3)

Group / Artifact Type Version
org.webjars.npm : blob-to-buffer jar [1.0.2,2)
org.webjars.npm : flatten jar [1.0.2,2)
org.webjars.npm : run-parallel jar [1.0.0,2)

Project Modules

There are no modules declared in this project.

drag-drop travis npm downloads javascript style guide

HTML5 drag & drop for humans

In case you didn't know, the HTML5 drag and drop API is a total disaster! This is an attempt to make the API usable by mere mortals.

live demo

See https://instant.io.

features

  • simple API
  • supports files and directories
  • supports modern browsers (Chrome, Firefox, Safari, Edge)
  • adds a drag class to the drop target on hover, for easy styling!
  • optionally, get the file(s) as a Buffer (see buffer)

install

npm install drag-drop

This package works in the browser with browserify. If you do not use a bundler, you can use the standalone script directly in a <script> tag.

usage

const dragDrop = require('drag-drop')

dragDrop('#dropTarget', (files, pos, fileList, directories) => {
  console.log('Here are the dropped files', files) // Array of File objects
  console.log('Dropped at coordinates', pos.x, pos.y)
  console.log('Here is the raw FileList object if you need it:', fileList)
  console.log('Here is the list of directories:', directories)
})

Another handy thing this does is add a drag class to the drop target when the user is dragging a file over the drop target. Useful for styling the drop target to make it obvious that this is a drop target!

complete example

const dragDrop = require('drag-drop')

// You can pass in a DOM node or a selector string!
dragDrop('#dropTarget', (files, pos, fileList, directories) => {
  console.log('Here are the dropped files', files)
  console.log('Dropped at coordinates', pos.x, pos.y)
  console.log('Here is the raw FileList object if you need it:', fileList)
  console.log('Here is the list of directories:', directories)

  // `files` is an Array!
  files.forEach(file => {
    console.log(file.name)
    console.log(file.size)
    console.log(file.type)
    console.log(file.lastModifiedDate)
    console.log(file.fullPath) // not real full path due to browser security restrictions
    console.log(file.path) // in Electron, this contains the actual full path

    // convert the file to a Buffer that we can use!
    const reader = new FileReader()
    reader.addEventListener('load', e => {
      // e.target.result is an ArrayBuffer
      const arr = new Uint8Array(e.target.result)
      const buffer = new Buffer(arr)

      // do something with the buffer!
    })
    reader.addEventListener('error', err => {
      console.error('FileReader error' + err)
    })
    reader.readAsArrayBuffer(file)
  })
})

get files as buffers

If you prefer to access file data as Buffers, then just require drag-drop like this:

const dragDrop = require('drag-drop/buffer')

dragDrop('#dropTarget', files => {
  files.forEach(file => {
    // file is actually a buffer!
    console.log(file.readUInt32LE(0))
    console.log(file.toJSON())
    console.log(file.toString('hex')) // etc...

    // but it still has all the normal file properties!
    console.log(file.name)
    console.log(file.size)
    console.log(file.type)
    console.log(file.lastModifiedDate)
  })
})

remove listeners

To stop listening for drag & drop events and remove the event listeners, just use the remove function returned by the dragDrop function.

const dragDrop = require('drag-drop')

const remove = dragDrop('#dropTarget', files => {
  // ...
})

// ... at some point in the future, stop listening for drag & drop events
remove()

detect dragenter, dragover and dragleave events

Instead of passing just an ondrop function as the second argument, instead pass an object with all the events you want to listen for:

const dragDrop = require('drag-drop')

dragDrop('#dropTarget', {
  onDrop: (files, pos, fileList, directories) => {
    console.log('Here are the dropped files', files)
    console.log('Dropped at coordinates', pos.x, pos.y)
    console.log('Here is the raw FileList object if you need it:', fileList)
    console.log('Here is the list of directories:', directories)
  },
  onDragEnter: () => {},
  onDragOver: () => {},
  onDragLeave: () => {}
})

detect drag-and-dropped text

If the user highlights text and drags it, we capture that as a separate event. Listen for it like this:

const dragDrop = require('drag-drop')

dragDrop('#dropTarget', {
  onDropText: (text, pos) => {
    console.log('Here is the dropped text:', text)
    console.log('Dropped at coordinates', pos.x, pos.y)
  }
})

a note about file:// urls

Don't run your app from file://. For security reasons, browsers do not allow you to run your app from file://. In fact, many of the powerful storage APIs throw errors if you run the app locally from file://.

Instead, start a local server and visit your site at http://localhost:port.

license

MIT. Copyright (c) Feross Aboukhadijeh.

Versions

Version
2.13.3
2.13.2
2.11.0