/
index.js
174 lines (143 loc) · 4.74 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
import BasePlugin from '@uppy/core/lib/BasePlugin'
import getDroppedFiles from '@uppy/utils/lib/getDroppedFiles'
import toArray from '@uppy/utils/lib/toArray'
import packageJson from '../package.json'
function isFileTransfer (event) {
return event.dataTransfer.types?.some((type) => type === 'Files') ?? false
}
/**
* Drop Target plugin
*
*/
export default class DropTarget extends BasePlugin {
static VERSION = packageJson.version
constructor (uppy, opts) {
super(uppy, opts)
this.type = 'acquirer'
this.id = this.opts.id || 'DropTarget'
this.title = 'Drop Target'
// Default options
const defaultOpts = {
target: null,
}
// Merge default options with the ones set by user
this.opts = { ...defaultOpts, ...opts }
this.removeDragOverClassTimeout = null
}
addFiles = (files) => {
const descriptors = files.map((file) => ({
source: this.id,
name: file.name,
type: file.type,
data: file,
meta: {
// path of the file relative to the ancestor directory the user selected.
// e.g. 'docs/Old Prague/airbnb.pdf'
relativePath: file.relativePath || null,
},
}))
try {
this.uppy.addFiles(descriptors)
} catch (err) {
this.uppy.log(err)
}
}
handleDrop = async (event) => {
if (!isFileTransfer(event)) {
return
}
event.preventDefault()
event.stopPropagation()
clearTimeout(this.removeDragOverClassTimeout)
// Remove dragover class
event.currentTarget.classList.remove('uppy-is-drag-over')
this.setPluginState({ isDraggingOver: false })
// Let any acquirer plugin (Url/Webcam/etc.) handle drops to the root
this.uppy.iteratePlugins((plugin) => {
if (plugin.type === 'acquirer') {
// Every Plugin with .type acquirer can define handleRootDrop(event)
plugin.handleRootDrop?.(event)
}
})
// Add all dropped files, handle errors
let executedDropErrorOnce = false
const logDropError = (error) => {
this.uppy.log(error, 'error')
// In practice all drop errors are most likely the same,
// so let's just show one to avoid overwhelming the user
if (!executedDropErrorOnce) {
this.uppy.info(error.message, 'error')
executedDropErrorOnce = true
}
}
const files = await getDroppedFiles(event.dataTransfer, { logDropError })
if (files.length > 0) {
this.uppy.log('[DropTarget] Files were dropped')
this.addFiles(files)
}
this.opts.onDrop?.(event)
}
handleDragOver = (event) => {
if (!isFileTransfer(event)) {
return
}
event.preventDefault()
event.stopPropagation()
// Add a small (+) icon on drop
// (and prevent browsers from interpreting this as files being _moved_ into the browser,
// https://github.com/transloadit/uppy/issues/1978)
event.dataTransfer.dropEffect = 'copy' // eslint-disable-line no-param-reassign
clearTimeout(this.removeDragOverClassTimeout)
event.currentTarget.classList.add('uppy-is-drag-over')
this.setPluginState({ isDraggingOver: true })
this.opts.onDragOver?.(event)
}
handleDragLeave = (event) => {
if (!isFileTransfer(event)) {
return
}
event.preventDefault()
event.stopPropagation()
const { currentTarget } = event
clearTimeout(this.removeDragOverClassTimeout)
// Timeout against flickering, this solution is taken from drag-drop library.
// Solution with 'pointer-events: none' didn't work across browsers.
this.removeDragOverClassTimeout = setTimeout(() => {
currentTarget.classList.remove('uppy-is-drag-over')
this.setPluginState({ isDraggingOver: false })
}, 50)
this.opts.onDragLeave?.(event)
}
addListeners = () => {
const { target } = this.opts
if (target instanceof Element) {
this.nodes = [target]
} else if (typeof target === 'string') {
this.nodes = toArray(document.querySelectorAll(target))
}
if (!this.nodes && !this.nodes.length > 0) {
throw new Error(`"${target}" does not match any HTML elements`)
}
this.nodes.forEach((node) => {
node.addEventListener('dragover', this.handleDragOver, false)
node.addEventListener('dragleave', this.handleDragLeave, false)
node.addEventListener('drop', this.handleDrop, false)
})
}
removeListeners = () => {
if (this.nodes) {
this.nodes.forEach((node) => {
node.removeEventListener('dragover', this.handleDragOver, false)
node.removeEventListener('dragleave', this.handleDragLeave, false)
node.removeEventListener('drop', this.handleDrop, false)
})
}
}
install () {
this.setPluginState({ isDraggingOver: false })
this.addListeners()
}
uninstall () {
this.removeListeners()
}
}