Skip to content

Safer binding to windows events, and fix issues on SSR. #105

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

Merged
merged 1 commit into from
Aug 7, 2022
Merged
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
28 changes: 14 additions & 14 deletions src/components/Dropzone.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
isEvtWithFiles,
isIeOrEdge,
isPropagationStopped,
onDocumentDragOver,
TOO_MANY_FILES_REJECTION
} from "./../utils/index";
import { onMount, onDestroy, createEventDispatcher } from "svelte";
Expand Down Expand Up @@ -257,8 +256,18 @@
}
}

// allow the entire document to be a drag target
function onDocumentDragOver(event) {
if (preventDropOnDocument) {
event.preventDefault();
}
}

let dragTargetsRef = [];
function onDocumentDrop(event) {
if (!preventDropOnDocument) {
return;
}
if (rootRef && rootRef.contains(event.target)) {
// If we intercepted an event for our instance, let it propagate down to the instance's onDrop handler
return;
Expand All @@ -284,20 +293,9 @@
}
}

onMount(() => {
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);
}
// This is critical for canceling the timeout behaviour on `onWindowFocus()`
inputRef = null;
});

function onInputElementClick(event) {
Expand Down Expand Up @@ -326,6 +324,8 @@
}
</style>

<svelte:window on:focus={onWindowFocus} on:dragover={onDocumentDragOver} on:drop={onDocumentDrop} />

<div
bind:this={rootRef}
tabindex="0"
Expand Down
5 changes: 0 additions & 5 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ export function isKindFile(item) {
return typeof item === "object" && item !== null && item.kind === "file";
}

// allow the entire document to be a drag target
export function onDocumentDragOver(event) {
event.preventDefault();
}

function isIe(userAgent) {
return (
userAgent.indexOf("MSIE") !== -1 || userAgent.indexOf("Trident/") !== -1
Expand Down