-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathfile-drop.js
More file actions
213 lines (174 loc) · 5.67 KB
/
file-drop.js
File metadata and controls
213 lines (174 loc) · 5.67 KB
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
/**
* oat - File Drop Component
* Drag files onto an area or click it to open the native file picker.
*
* Usage:
* <ot-file-drop accept="image/*" multiple>
* <strong>Drop images here</strong>
* <small>or click to browse</small>
* </ot-file-drop>
*
* Events:
* - ot-file-drop: detail = { files, accepted, rejected }
*/
import { OtBase } from './base.js';
class OtFileDrop extends OtBase {
#input;
#dragDepth = 0;
static get observedAttributes() {
return ['accept', 'multiple', 'disabled', 'name'];
}
init() {
this.#input = this.querySelector(':scope > input[type="file"]') || document.createElement('input');
const hadContent = [...this.childNodes].some(node => {
return node !== this.#input && (node.nodeType === Node.ELEMENT_NODE || node.textContent.trim());
});
if (!this.#input.parentElement) {
this.#input.type = 'file';
this.prepend(this.#input);
}
this.#input.tabIndex = -1;
this.#input.setAttribute('aria-hidden', 'true');
if (!this.hasAttribute('role')) this.setAttribute('role', 'button');
if (!this.hasAttribute('tabindex')) this.tabIndex = 0;
if (!this.hasAttribute('aria-label') && !this.textContent.trim()) {
this.setAttribute('aria-label', 'Choose files');
}
if (!hadContent) {
const content = document.createElement('span');
content.className = 'content';
content.innerHTML = '<strong>Drop files here</strong><small>or click to browse</small>';
this.append(content);
}
this.#syncInput();
this.addEventListener('click', this);
this.addEventListener('keydown', this);
this.addEventListener('dragenter', this);
this.addEventListener('dragover', this);
this.addEventListener('dragleave', this);
this.addEventListener('drop', this);
this.#input.addEventListener('change', this);
}
cleanup() {
this.removeEventListener('click', this);
this.removeEventListener('keydown', this);
this.removeEventListener('dragenter', this);
this.removeEventListener('dragover', this);
this.removeEventListener('dragleave', this);
this.removeEventListener('drop', this);
this.#input?.removeEventListener('change', this);
}
attributeChangedCallback() {
this.#syncInput();
}
onclick(e) {
if (this.disabled || e.target === this.#input) return;
this.#input.click();
}
onkeydown(e) {
if (this.disabled) return;
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
this.#input.click();
}
}
ondragenter(e) {
if (!this.#hasFiles(e) || this.disabled) return;
e.preventDefault();
this.#dragDepth += 1;
this.dataset.dragging = '';
}
ondragover(e) {
if (!this.#hasFiles(e) || this.disabled) return;
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}
ondragleave(e) {
if (!this.#hasFiles(e) || this.disabled) return;
this.#dragDepth -= 1;
if (this.#dragDepth <= 0) this.#clearDragState();
}
ondrop(e) {
if (!this.#hasFiles(e) || this.disabled) return;
e.preventDefault();
this.#clearDragState();
this.#setFiles(e.dataTransfer.files);
}
onchange() {
this.#setFiles(this.#input.files);
}
#setFiles(fileList) {
const files = [...fileList];
const { accepted, rejected } = this.#partitionFiles(files);
const selected = this.multiple ? accepted : accepted.slice(0, 1);
const overflow = this.multiple ? [] : accepted.slice(1);
const allRejected = [...rejected, ...overflow];
try {
const dt = new DataTransfer();
selected.forEach(file => dt.items.add(file));
this.#input.files = dt.files;
} catch {
// Some older browsers do not allow assigning input.files. The emitted
// event still exposes the selected files.
}
this.#emitFiles(selected, allRejected);
}
#emitFiles(files, rejected = []) {
this.toggleAttribute('data-invalid', rejected.length > 0);
this.emit('ot-file-drop', { files, accepted: files, rejected });
}
#partitionFiles(files) {
const tests = this.accept.split(',').map(t => t.trim().toLowerCase()).filter(Boolean);
if (tests.length === 0) return { accepted: files, rejected: [] };
return files.reduce((acc, file) => {
(this.#matchesAccept(file, tests) ? acc.accepted : acc.rejected).push(file);
return acc;
}, { accepted: [], rejected: [] });
}
#matchesAccept(file, tests) {
const name = file.name.toLowerCase();
const type = file.type.toLowerCase();
return tests.some(test => {
if (test.startsWith('.')) return name.endsWith(test);
if (test.endsWith('/*')) return type.startsWith(test.slice(0, -1));
return type === test;
});
}
#hasFiles(e) {
return [...(e.dataTransfer?.types || [])].includes('Files');
}
#clearDragState() {
this.#dragDepth = 0;
delete this.dataset.dragging;
}
#syncInput() {
if (!this.#input) return;
this.#input.accept = this.accept;
this.#input.multiple = this.multiple;
this.#input.disabled = this.disabled;
this.#input.name = this.getAttribute('name') || '';
this.setAttribute('aria-disabled', String(this.disabled));
}
get files() {
return this.#input ? [...this.#input.files] : [];
}
get accept() {
return this.getAttribute('accept') || '';
}
set accept(value) {
value ? this.setAttribute('accept', value) : this.removeAttribute('accept');
}
get multiple() {
return this.hasAttribute('multiple');
}
set multiple(value) {
this.toggleAttribute('multiple', Boolean(value));
}
get disabled() {
return this.hasAttribute('disabled');
}
set disabled(value) {
this.toggleAttribute('disabled', Boolean(value));
}
}
customElements.define('ot-file-drop', OtFileDrop);