Skip to content

Commit 07290d3

Browse files
authored
Merge pull request #105 from Tal500/fix-window-bindings
Safer binding to windows events, and fix issues on SSR.
2 parents 8cb257c + 8c2e1ce commit 07290d3

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/components/Dropzone.svelte

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
isEvtWithFiles,
99
isIeOrEdge,
1010
isPropagationStopped,
11-
onDocumentDragOver,
1211
TOO_MANY_FILES_REJECTION
1312
} from "./../utils/index";
1413
import { onMount, onDestroy, createEventDispatcher } from "svelte";
@@ -257,8 +256,18 @@
257256
}
258257
}
259258
259+
// allow the entire document to be a drag target
260+
function onDocumentDragOver(event) {
261+
if (preventDropOnDocument) {
262+
event.preventDefault();
263+
}
264+
}
265+
260266
let dragTargetsRef = [];
261267
function onDocumentDrop(event) {
268+
if (!preventDropOnDocument) {
269+
return;
270+
}
262271
if (rootRef && rootRef.contains(event.target)) {
263272
// If we intercepted an event for our instance, let it propagate down to the instance's onDrop handler
264273
return;
@@ -284,20 +293,9 @@
284293
}
285294
}
286295
287-
onMount(() => {
288-
window.addEventListener("focus", onWindowFocus, false);
289-
if (preventDropOnDocument) {
290-
document.addEventListener("dragover", onDocumentDragOver, false);
291-
document.addEventListener("drop", onDocumentDrop, false);
292-
}
293-
});
294-
295296
onDestroy(() => {
296-
window.removeEventListener("focus", onWindowFocus, false);
297-
if (preventDropOnDocument) {
298-
document.removeEventListener("dragover", onDocumentDragOver);
299-
document.removeEventListener("drop", onDocumentDrop);
300-
}
297+
// This is critical for canceling the timeout behaviour on `onWindowFocus()`
298+
inputRef = null;
301299
});
302300
303301
function onInputElementClick(event) {
@@ -326,6 +324,8 @@
326324
}
327325
</style>
328326

327+
<svelte:window on:focus={onWindowFocus} on:dragover={onDocumentDragOver} on:drop={onDocumentDrop} />
328+
329329
<div
330330
bind:this={rootRef}
331331
tabindex="0"

src/utils/index.js

-5
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ export function isKindFile(item) {
111111
return typeof item === "object" && item !== null && item.kind === "file";
112112
}
113113

114-
// allow the entire document to be a drag target
115-
export function onDocumentDragOver(event) {
116-
event.preventDefault();
117-
}
118-
119114
function isIe(userAgent) {
120115
return (
121116
userAgent.indexOf("MSIE") !== -1 || userAgent.indexOf("Trident/") !== -1

0 commit comments

Comments
 (0)