-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.js
270 lines (216 loc) · 6.53 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*! drag-drop. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
module.exports = dragDrop
module.exports.processItems = processItems
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 }
}
elem.addEventListener('dragenter', onDragEnter, false)
elem.addEventListener('dragover', onDragOver, false)
elem.addEventListener('dragleave', onDragLeave, false)
elem.addEventListener('drop', onDrop, false)
let isEntered = false
let numIgnoredEnters = 0
// Function to remove drag-drop listeners
return function cleanup () {
removeDragClass()
elem.removeEventListener('dragenter', onDragEnter, false)
elem.removeEventListener('dragover', onDragOver, false)
elem.removeEventListener('dragleave', onDragLeave, false)
elem.removeEventListener('drop', onDrop, false)
}
function isEventHandleable (event) {
if (event.dataTransfer.items || event.dataTransfer.types) {
// 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(event.dataTransfer.items)
const types = Array.from(event.dataTransfer.types)
let fileItems
let textItems
if (items.length) {
fileItems = items.filter(item => { return item.kind === 'file' })
textItems = items.filter(item => { return item.kind === 'string' })
} else if (types.length) {
// event.dataTransfer.items is empty during 'dragover' in Safari, so use
// event.dataTransfer.types as a fallback
fileItems = types.filter(item => item === 'Files')
textItems = types.filter(item => item.startsWith('text/'))
} else {
return false
}
if (fileItems.length === 0 && !listeners.onDropText) return false
if (textItems.length === 0 && !listeners.onDrop) return false
if (fileItems.length === 0 && textItems.length === 0) return false
return true
}
return false
}
function onDragEnter (event) {
event.stopPropagation()
event.preventDefault()
if (!isEventHandleable(event)) return
if (isEntered) {
numIgnoredEnters += 1
return false // early return
}
isEntered = true
if (listeners.onDragEnter) {
listeners.onDragEnter(event)
}
addDragClass()
return false
}
function onDragOver (event) {
event.stopPropagation()
event.preventDefault()
if (!isEventHandleable(event)) return
if (listeners.onDragOver) {
listeners.onDragOver(event)
}
event.dataTransfer.dropEffect = 'copy'
return false
}
function onDragLeave (event) {
event.stopPropagation()
event.preventDefault()
if (!isEventHandleable(event)) return
if (numIgnoredEnters > 0) {
numIgnoredEnters -= 1
return false
}
isEntered = false
if (listeners.onDragLeave) {
listeners.onDragLeave(event)
}
removeDragClass()
return false
}
function onDrop (event) {
event.stopPropagation()
event.preventDefault()
if (listeners.onDragLeave) {
listeners.onDragLeave(event)
}
removeDragClass()
isEntered = false
numIgnoredEnters = 0
const pos = {
x: event.clientX,
y: event.clientY
}
// text drop support
const text = event.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 && event.dataTransfer.items) {
processItems(event.dataTransfer.items, (err, files, directories) => {
if (err) {
// TODO: A future version of this library might expose this somehow
console.error(err)
return
}
if (files.length === 0) return
const fileList = event.dataTransfer.files
// TODO: This callback has too many arguments, and the order is too
// arbitrary. In next major version, it should be cleaned up.
listeners.onDrop(files, pos, fileList, directories)
})
}
return false
}
function addDragClass () {
elem.classList.add('drag')
}
function removeDragClass () {
elem.classList.remove('drag')
}
}
function processItems (items, cb) {
// Handle directories in Chrome using the proprietary FileSystem API
items = Array.from(items).filter(item => {
return item.kind === 'file'
})
if (items.length === 0) {
cb(null, [], [])
}
parallel(items.map(item => {
return cb => {
processEntry(item.webkitGetAsEntry(), cb)
}
}), (err, results) => {
// This catches permission errors with file:// in Chrome
if (err) {
cb(err)
return
}
const entries = results.flat(Infinity)
const files = entries.filter(item => {
return item.isFile
})
const directories = entries.filter(item => {
return item.isDirectory
})
cb(null, files, directories)
})
}
function processEntry (entry, cb) {
let entries = []
if (entry.isFile) {
entry.file(file => {
file.fullPath = entry.fullPath // preserve path 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(currentEntries => {
if (currentEntries.length > 0) {
entries = entries.concat(Array.from(currentEntries))
readEntries(reader) // 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)
}
})
}
}