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

add onDropText event to support text dropping #24

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ function dragDrop (elem, listeners) {
e.stopPropagation()
e.preventDefault()
if (e.dataTransfer.items) {
var items = toArray(e.dataTransfer.items)

// Only add "drag" class when `items` contains a file
var items = toArray(e.dataTransfer.items).filter(function (item) {
var fileItems = items.filter(function (item) {
return item.kind === 'file'
})
if (items.length === 0) return

// Otherwise add "drag" class if `onDropText` event is present and `items` contains a string
if (fileItems.length === 0 && !listeners.onDropText) return

var stringItem = items.find(function (item) {
return item.kind === 'string' && item.type === 'text/plain'
})

if (!stringItem) return
}

elem.classList.add('drag')
Expand Down Expand Up @@ -81,25 +91,36 @@ function dragDrop (elem, listeners) {
}

if (e.dataTransfer.items) {
var items = toArray(e.dataTransfer.items)

// Handle directories in Chrome using the proprietary FileSystem API
var items = toArray(e.dataTransfer.items).filter(function (item) {
var fileItems = items.filter(function (item) {
return item.kind === 'file'
})

if (items.length === 0) return

parallel(items.map(function (item) {
return function (cb) {
processEntry(item.webkitGetAsEntry(), cb)
}
}), function (err, results) {
// This catches permission errors with file:// in Chrome. This should never
// throw in production code, so the user does not need to use try-catch.
if (err) throw err
if (listeners.onDrop) {
listeners.onDrop(flatten(results), pos)
if (fileItems.length !== 0) {
parallel(fileItems.map(function (item) {
return function (cb) {
processEntry(item.webkitGetAsEntry(), cb)
}
}), function (err, results) {
// This catches permission errors with file:// in Chrome. This should never
// throw in production code, so the user does not need to use try-catch.
if (err) throw err
if (listeners.onDrop) {
listeners.onDrop(flatten(results), pos)
}
})
} else if (listeners.onDropText) {
// Handle text drop if `onDropText` event is present
var stringItem = items.find(function (item) {
return item.kind === 'string' && item.type === 'text/plain'
})

if (stringItem) {
stringItem.getAsString(function (text) { listeners.onDropText(text, pos) })
}
})
}
} else {
var files = toArray(e.dataTransfer.files)

Expand Down