-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.js
205 lines (166 loc) · 5.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
module.exports = dragDrop
const parallel = require('run-parallel')
function dragDrop (elem, listeners) {
if (typeof elem === 'string') {
const selector = elem
elem = window.document.querySelector(elem)
if (!elem) {
throw new Error(`"${selector}" does not match any HTML elements`)
}
}
if (!elem) {
throw new Error(`"${elem}" is not a valid HTML element`)
}
if (typeof listeners === 'function') {
listeners = { onDrop: listeners }
}
let timeout
elem.addEventListener('dragenter', onDragEnter, false)
elem.addEventListener('dragover', onDragOver, false)
elem.addEventListener('dragleave', onDragLeave, false)
elem.addEventListener('drop', onDrop, false)
// Function to remove drag-drop listeners
return function remove () {
removeDragClass()
elem.removeEventListener('dragenter', onDragEnter, false)
elem.removeEventListener('dragover', onDragOver, false)
elem.removeEventListener('dragleave', onDragLeave, false)
elem.removeEventListener('drop', onDrop, false)
}
function onDragEnter (e) {
if (listeners.onDragEnter) {
listeners.onDragEnter(e)
}
// Prevent event
e.stopPropagation()
e.preventDefault()
return false
}
function onDragOver (e) {
e.stopPropagation()
e.preventDefault()
if (listeners.onDragOver) {
listeners.onDragOver(e)
}
if (e.dataTransfer.items) {
// Only add "drag" class when `items` contains items that are able to be
// handled by the registered listeners (files vs. text)
const items = Array.from(e.dataTransfer.items)
const fileItems = items.filter(item => { return item.kind === 'file' })
const textItems = items.filter(item => { return item.kind === 'string' })
if (fileItems.length === 0 && !listeners.onDropText) return
if (textItems.length === 0 && !listeners.onDrop) return
if (fileItems.length === 0 && textItems.length === 0) return
}
elem.classList.add('drag')
clearTimeout(timeout)
e.dataTransfer.dropEffect = 'copy'
return false
}
function onDragLeave (e) {
e.stopPropagation()
e.preventDefault()
if (listeners.onDragLeave) {
listeners.onDragLeave(e)
}
clearTimeout(timeout)
timeout = setTimeout(removeDragClass, 50)
return false
}
function onDrop (e) {
e.stopPropagation()
e.preventDefault()
if (listeners.onDragLeave) {
listeners.onDragLeave(e)
}
clearTimeout(timeout)
removeDragClass()
const pos = {
x: e.clientX,
y: e.clientY
}
// text drop support
const text = e.dataTransfer.getData('text')
if (text && listeners.onDropText) {
listeners.onDropText(text, pos)
}
// File drop support. The `dataTransfer.items` API supports directories, so we
// use it instead of `dataTransfer.files`, even though it's much more
// complicated to use.
// See: https://github.com/feross/drag-drop/issues/39
if (listeners.onDrop && e.dataTransfer.items) {
const fileList = e.dataTransfer.files
// Handle directories in Chrome using the proprietary FileSystem API
const items = Array.from(e.dataTransfer.items).filter(item => {
return item.kind === 'file'
})
if (items.length === 0) return
parallel(items.map(item => {
return cb => {
processEntry(item.webkitGetAsEntry(), cb)
}
}), (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
const entries = results.flat()
const files = entries.filter(item => {
return item.isFile
})
const directories = entries.filter(item => {
return item.isDirectory
})
listeners.onDrop(files, pos, fileList, directories)
})
}
return false
}
function removeDragClass () {
elem.classList.remove('drag')
}
}
function processEntry (entry, cb) {
let entries = []
if (entry.isFile) {
entry.file(file => {
file.fullPath = entry.fullPath // preserve pathing for consumer
file.isFile = true
file.isDirectory = false
cb(null, file)
}, err => {
cb(err)
})
} else if (entry.isDirectory) {
const reader = entry.createReader()
readEntries(reader)
}
function readEntries (reader) {
reader.readEntries(entries_ => {
if (entries_.length > 0) {
entries = entries.concat(Array.from(entries_))
readEntries() // continue reading entries until `readEntries` returns no more
} else {
doneEntries()
}
})
}
function doneEntries () {
parallel(entries.map(entry => {
return cb => {
processEntry(entry, cb)
}
}), (err, results) => {
if (err) {
cb(err)
} else {
results.push({
fullPath: entry.fullPath,
name: entry.name,
isFile: false,
isDirectory: true
})
cb(null, results)
}
})
}
}