Skip to content

fix for #81 #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-file-dropzone",
"version": "0.0.15",
"version": "1.0.0-dev.1",
"description": "Svelte component for fileupload and file dropzone",
"svelte": "src/components/Dropzone.svelte",
"module": "dist/index.mjs",
Expand Down
97 changes: 52 additions & 45 deletions src/components/Dropzone.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script>
import { browser } from "$app/env";
import { fromEvent } from "file-selector";
import {
allFilesAccepted,
Expand All @@ -9,7 +10,7 @@
isIeOrEdge,
isPropagationStopped,
onDocumentDragOver,
TOO_MANY_FILES_REJECTION
TOO_MANY_FILES_REJECTION,
} from "./../utils/index";
import { onMount, onDestroy, createEventDispatcher } from "svelte";

Expand Down Expand Up @@ -45,7 +46,7 @@
isDragReject: false,
draggedFiles: [],
acceptedFiles: [],
fileRejections: []
fileRejections: [],
};

let rootRef;
Expand Down Expand Up @@ -112,7 +113,7 @@
dragTargetsRef = [...dragTargetsRef, event.target];

if (isEvtWithFiles(event)) {
Promise.resolve(getFilesFromEvent(event)).then(draggedFiles => {
Promise.resolve(getFilesFromEvent(event)).then((draggedFiles) => {
if (isPropagationStopped(event) && !noDragEventsBubbling) {
return;
}
Expand All @@ -121,7 +122,7 @@
state.isDragActive = true;

dispatch("dragenter", {
dragEvent: event
dragEvent: event,
});
});
}
Expand All @@ -139,7 +140,7 @@

if (isEvtWithFiles(event)) {
dispatch("dragover", {
dragEvent: event
dragEvent: event,
});
}

Expand All @@ -152,7 +153,7 @@

// Only deactivate once the dropzone and all children have been left
const targets = dragTargetsRef.filter(
target => rootRef && rootRef.contains(target)
(target) => rootRef && rootRef.contains(target)
);
// Make sure to remove a target present multiple times only once
// (Firefox may fire dragenter/dragleave multiple times on the same element)
Expand All @@ -170,7 +171,7 @@

if (isEvtWithFiles(event)) {
dispatch("dragleave", {
dragEvent: event
dragEvent: event,
});
}
}
Expand All @@ -182,28 +183,28 @@
dragTargetsRef = [];

if (isEvtWithFiles(event)) {
Promise.resolve(getFilesFromEvent(event)).then(files => {
Promise.resolve(getFilesFromEvent(event)).then((files) => {
if (isPropagationStopped(event) && !noDragEventsBubbling) {
return;
}

const acceptedFiles = [];
const fileRejections = [];

files.forEach(file => {
files.forEach((file) => {
const [accepted, acceptError] = fileAccepted(file, accept);
const [sizeMatch, sizeError] = fileMatchSize(file, minSize, maxSize);
if (accepted && sizeMatch) {
acceptedFiles.push(file);
} else {
const errors = [acceptError, sizeError].filter(e => e);
const errors = [acceptError, sizeError].filter((e) => e);
fileRejections.push({ file, errors });
}
});

if (!multiple && acceptedFiles.length > 1) {
// Reject everything and empty accepted files
acceptedFiles.forEach(file => {
acceptedFiles.forEach((file) => {
fileRejections.push({ file, errors: [TOO_MANY_FILES_REJECTION] });
});
acceptedFiles.splice(0);
Expand All @@ -215,20 +216,20 @@
dispatch("drop", {
acceptedFiles,
fileRejections,
event
event,
});

if (fileRejections.length > 0) {
dispatch("droprejected", {
fileRejections,
event
event,
});
}

if (acceptedFiles.length > 0) {
dispatch("dropaccepted", {
acceptedFiles,
event
event,
});
}
});
Expand Down Expand Up @@ -282,18 +283,22 @@
}

onMount(() => {
window.addEventListener("focus", onWindowFocus, false);
if (preventDropOnDocument) {
document.addEventListener("dragover", onDocumentDragOver, false);
document.addEventListener("drop", onDocumentDrop, false);
if (browser) {
window.addEventListener("focus", onWindowFocus, false);
if (preventDropOnDocument) {
document.addEventListener("dragover", onDocumentDragOver, false);
document.addEventListener("drop", onDocumentDrop, false);
}
}
});

onDestroy(() => {
window.removeEventListener("focus", onWindowFocus, false);
if (preventDropOnDocument) {
document.removeEventListener("dragover", onDocumentDragOver);
document.removeEventListener("drop", onDocumentDrop);
if (browser) {
window.removeEventListener("focus", onWindowFocus, false);
if (preventDropOnDocument) {
document.removeEventListener("dragover", onDocumentDragOver);
document.removeEventListener("drop", onDocumentDrop);
}
}
});

Expand All @@ -302,27 +307,6 @@
}
</script>

<style>
.dropzone {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
border-width: 2px;
border-radius: 2px;
border-color: #eeeeee;
border-style: dashed;
background-color: #fafafa;
color: #bdbdbd;
outline: none;
transition: border 0.24s ease-in-out;
}
.dropzone:focus {
border-color: #2196f3;
}
</style>

<div
bind:this={rootRef}
tabindex="0"
Expand All @@ -336,7 +320,8 @@
on:dragenter={composeDragHandler(onDragEnterCb)}
on:dragover={composeDragHandler(onDragOverCb)}
on:dragleave={composeDragHandler(onDragLeaveCb)}
on:drop={composeDragHandler(onDropCb)}>
on:drop={composeDragHandler(onDropCb)}
>
<input
{accept}
{multiple}
Expand All @@ -346,8 +331,30 @@
on:change={onDropCb}
on:click={onInputElementClick}
bind:this={inputRef}
style="display: none;" />
style="display: none;"
/>
<slot>
<p>Drag 'n' drop some files here, or click to select files</p>
</slot>
</div>

<style>
.dropzone {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
border-width: 2px;
border-radius: 2px;
border-color: #eeeeee;
border-style: dashed;
background-color: #fafafa;
color: #bdbdbd;
outline: none;
transition: border 0.24s ease-in-out;
}
.dropzone:focus {
border-color: #2196f3;
}
</style>