|
9 | 9 | isIeOrEdge,
|
10 | 10 | isPropagationStopped,
|
11 | 11 | onDocumentDragOver,
|
12 |
| - TOO_MANY_FILES_REJECTION |
| 12 | + TOO_MANY_FILES_REJECTION, |
13 | 13 | } from "./../utils/index";
|
14 | 14 | import { onMount, onDestroy, createEventDispatcher } from "svelte";
|
15 | 15 |
|
|
45 | 45 | isDragReject: false,
|
46 | 46 | draggedFiles: [],
|
47 | 47 | acceptedFiles: [],
|
48 |
| - fileRejections: [] |
| 48 | + fileRejections: [], |
49 | 49 | };
|
50 | 50 |
|
51 | 51 | let rootRef;
|
|
112 | 112 | dragTargetsRef = [...dragTargetsRef, event.target];
|
113 | 113 |
|
114 | 114 | if (isEvtWithFiles(event)) {
|
115 |
| - Promise.resolve(getFilesFromEvent(event)).then(draggedFiles => { |
| 115 | + Promise.resolve(getFilesFromEvent(event)).then((draggedFiles) => { |
116 | 116 | if (isPropagationStopped(event) && !noDragEventsBubbling) {
|
117 | 117 | return;
|
118 | 118 | }
|
|
121 | 121 | state.isDragActive = true;
|
122 | 122 |
|
123 | 123 | dispatch("dragenter", {
|
124 |
| - dragEvent: event |
| 124 | + dragEvent: event, |
125 | 125 | });
|
126 | 126 | });
|
127 | 127 | }
|
|
139 | 139 |
|
140 | 140 | if (isEvtWithFiles(event)) {
|
141 | 141 | dispatch("dragover", {
|
142 |
| - dragEvent: event |
| 142 | + dragEvent: event, |
143 | 143 | });
|
144 | 144 | }
|
145 | 145 |
|
|
152 | 152 |
|
153 | 153 | // Only deactivate once the dropzone and all children have been left
|
154 | 154 | const targets = dragTargetsRef.filter(
|
155 |
| - target => rootRef && rootRef.contains(target) |
| 155 | + (target) => rootRef && rootRef.contains(target) |
156 | 156 | );
|
157 | 157 | // Make sure to remove a target present multiple times only once
|
158 | 158 | // (Firefox may fire dragenter/dragleave multiple times on the same element)
|
|
170 | 170 |
|
171 | 171 | if (isEvtWithFiles(event)) {
|
172 | 172 | dispatch("dragleave", {
|
173 |
| - dragEvent: event |
| 173 | + dragEvent: event, |
174 | 174 | });
|
175 | 175 | }
|
176 | 176 | }
|
|
182 | 182 | dragTargetsRef = [];
|
183 | 183 |
|
184 | 184 | if (isEvtWithFiles(event)) {
|
185 |
| - Promise.resolve(getFilesFromEvent(event)).then(files => { |
| 185 | + Promise.resolve(getFilesFromEvent(event)).then((files) => { |
186 | 186 | if (isPropagationStopped(event) && !noDragEventsBubbling) {
|
187 | 187 | return;
|
188 | 188 | }
|
189 | 189 |
|
190 | 190 | const acceptedFiles = [];
|
191 | 191 | const fileRejections = [];
|
192 | 192 |
|
193 |
| - files.forEach(file => { |
| 193 | + files.forEach((file) => { |
194 | 194 | const [accepted, acceptError] = fileAccepted(file, accept);
|
195 | 195 | const [sizeMatch, sizeError] = fileMatchSize(file, minSize, maxSize);
|
196 | 196 | if (accepted && sizeMatch) {
|
197 | 197 | acceptedFiles.push(file);
|
198 | 198 | } else {
|
199 |
| - const errors = [acceptError, sizeError].filter(e => e); |
| 199 | + const errors = [acceptError, sizeError].filter((e) => e); |
200 | 200 | fileRejections.push({ file, errors });
|
201 | 201 | }
|
202 | 202 | });
|
203 | 203 |
|
204 | 204 | if (!multiple && acceptedFiles.length > 1) {
|
205 | 205 | // Reject everything and empty accepted files
|
206 |
| - acceptedFiles.forEach(file => { |
| 206 | + acceptedFiles.forEach((file) => { |
207 | 207 | fileRejections.push({ file, errors: [TOO_MANY_FILES_REJECTION] });
|
208 | 208 | });
|
209 | 209 | acceptedFiles.splice(0);
|
|
215 | 215 | dispatch("drop", {
|
216 | 216 | acceptedFiles,
|
217 | 217 | fileRejections,
|
218 |
| - event |
| 218 | + event, |
219 | 219 | });
|
220 | 220 |
|
221 | 221 | if (fileRejections.length > 0) {
|
222 | 222 | dispatch("droprejected", {
|
223 | 223 | fileRejections,
|
224 |
| - event |
| 224 | + event, |
225 | 225 | });
|
226 | 226 | }
|
227 | 227 |
|
228 | 228 | if (acceptedFiles.length > 0) {
|
229 | 229 | dispatch("dropaccepted", {
|
230 | 230 | acceptedFiles,
|
231 |
| - event |
| 231 | + event, |
232 | 232 | });
|
233 | 233 | }
|
234 | 234 | });
|
|
290 | 290 | });
|
291 | 291 |
|
292 | 292 | onDestroy(() => {
|
293 |
| - window.removeEventListener("focus", onWindowFocus, false); |
| 293 | + window && window.removeEventListener("focus", onWindowFocus, false); |
294 | 294 | if (preventDropOnDocument) {
|
295 |
| - document.removeEventListener("dragover", onDocumentDragOver); |
296 |
| - document.removeEventListener("drop", onDocumentDrop); |
| 295 | + document && document.removeEventListener("dragover", onDocumentDragOver); |
| 296 | + document && document.removeEventListener("drop", onDocumentDrop); |
297 | 297 | }
|
298 | 298 | });
|
299 | 299 |
|
|
302 | 302 | }
|
303 | 303 | </script>
|
304 | 304 |
|
305 |
| -<style> |
306 |
| - .dropzone { |
307 |
| - flex: 1; |
308 |
| - display: flex; |
309 |
| - flex-direction: column; |
310 |
| - align-items: center; |
311 |
| - padding: 20px; |
312 |
| - border-width: 2px; |
313 |
| - border-radius: 2px; |
314 |
| - border-color: #eeeeee; |
315 |
| - border-style: dashed; |
316 |
| - background-color: #fafafa; |
317 |
| - color: #bdbdbd; |
318 |
| - outline: none; |
319 |
| - transition: border 0.24s ease-in-out; |
320 |
| - } |
321 |
| - .dropzone:focus { |
322 |
| - border-color: #2196f3; |
323 |
| - } |
324 |
| -</style> |
325 |
| - |
326 | 305 | <div
|
327 | 306 | bind:this={rootRef}
|
328 | 307 | tabindex="0"
|
|
336 | 315 | on:dragenter={composeDragHandler(onDragEnterCb)}
|
337 | 316 | on:dragover={composeDragHandler(onDragOverCb)}
|
338 | 317 | on:dragleave={composeDragHandler(onDragLeaveCb)}
|
339 |
| - on:drop={composeDragHandler(onDropCb)}> |
| 318 | + on:drop={composeDragHandler(onDropCb)} |
| 319 | +> |
340 | 320 | <input
|
341 | 321 | {accept}
|
342 | 322 | {multiple}
|
|
346 | 326 | on:change={onDropCb}
|
347 | 327 | on:click={onInputElementClick}
|
348 | 328 | bind:this={inputRef}
|
349 |
| - style="display: none;" /> |
| 329 | + style="display: none;" |
| 330 | + /> |
350 | 331 | <slot>
|
351 | 332 | <p>Drag 'n' drop some files here, or click to select files</p>
|
352 | 333 | </slot>
|
353 | 334 | </div>
|
| 335 | + |
| 336 | +<style> |
| 337 | + .dropzone { |
| 338 | + flex: 1; |
| 339 | + display: flex; |
| 340 | + flex-direction: column; |
| 341 | + align-items: center; |
| 342 | + padding: 20px; |
| 343 | + border-width: 2px; |
| 344 | + border-radius: 2px; |
| 345 | + border-color: #eeeeee; |
| 346 | + border-style: dashed; |
| 347 | + background-color: #fafafa; |
| 348 | + color: #bdbdbd; |
| 349 | + outline: none; |
| 350 | + transition: border 0.24s ease-in-out; |
| 351 | + } |
| 352 | + .dropzone:focus { |
| 353 | + border-color: #2196f3; |
| 354 | + } |
| 355 | +</style> |
0 commit comments