-
Notifications
You must be signed in to change notification settings - Fork 2
Implement drag & drop for local JSON files #3
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
Open
DoctorNefario
wants to merge
2
commits into
tasgon:master
Choose a base branch
from
DoctorNefario:local_file_improvements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,99 @@ | ||
| <script lang="ts"> | ||
| import '../app.css'; | ||
| import { notification } from '$lib/stores'; | ||
| import { goto, invalidateAll } from '$app/navigation'; | ||
| import { localData, localFile, notification } from '$lib/stores'; | ||
| import { page } from '$app/stores'; | ||
|
|
||
| $: if ($notification) { | ||
| setTimeout(() => ($notification = ''), 2000); | ||
| } | ||
|
|
||
| let dragTarget: EventTarget | null = null; | ||
| let validDragFile = false; | ||
|
|
||
| const onDragEnter = (e: DragEvent) => { | ||
| dragTarget = e.target; | ||
|
|
||
| if (!e.dataTransfer) return; | ||
|
|
||
| validDragFile = [...e.dataTransfer.items].some( | ||
| (item) => item.kind === 'file' && item.type === 'application/json' | ||
| ); | ||
| }; | ||
|
|
||
| const onDragLeave = (e: DragEvent) => { | ||
| if (e.target === dragTarget || !dragTarget || !e.target) { | ||
| dragTarget = null; | ||
| validDragFile = false; | ||
| } | ||
| }; | ||
|
|
||
| const onDragOver = (e: DragEvent) => { | ||
| if (!e.dataTransfer) return; | ||
|
|
||
| const fileItems = [...e.dataTransfer.items].filter((item) => item.kind === 'file'); | ||
|
|
||
| if (fileItems.length === 0) return; | ||
| e.preventDefault(); | ||
|
|
||
| if (fileItems.some((item) => item.type === 'application/json')) { | ||
| e.dataTransfer.dropEffect = 'copy'; | ||
| } else { | ||
| e.dataTransfer.dropEffect = 'none'; | ||
| } | ||
| }; | ||
|
|
||
| const onDrop = (e: DragEvent) => { | ||
| dragTarget = null; | ||
| validDragFile = false; | ||
| if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) { | ||
| e.preventDefault(); | ||
| $localFile = e.dataTransfer.files[0]; | ||
| } | ||
| }; | ||
|
|
||
| $: if ($localFile) { | ||
| const file = $localFile; | ||
| $localFile = null; | ||
|
|
||
| file.arrayBuffer().then((data) => { | ||
| try { | ||
| let string_data = new TextDecoder().decode(data); | ||
| $localData = JSON.parse(string_data); | ||
| const pathname = $page.url.pathname; | ||
| if (pathname === '/p' || pathname.startsWith('/p/')) { | ||
| invalidateAll(); | ||
| } else { | ||
| goto('/p'); | ||
| } | ||
| } catch (e) { | ||
| $notification = `Couldn't open ${file.name}: ${e}`; | ||
| } | ||
| }); | ||
| } | ||
| </script> | ||
|
|
||
| <svelte:document | ||
| on:dragenter={onDragEnter} | ||
| on:dragleave={onDragLeave} | ||
| on:dragover={onDragOver} | ||
| on:drop={onDrop} | ||
| /> | ||
|
|
||
| <slot /> | ||
|
|
||
| {#if validDragFile} | ||
| <div class="fixed left-0 top-0 h-dvh w-dvw bg-gray-900/60 p-10"> | ||
| <div | ||
| class="pointer-events-none flex h-full w-full items-center justify-center rounded-3xl border-4 border-dashed border-green-500 bg-gray-900/80" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| > | ||
| <h1 class="text-3xl">Drop profile JSON here</h1> | ||
| </div> | ||
| </div> | ||
| {/if} | ||
|
|
||
| {#if $notification} | ||
| <div class="fixed right-10 bottom-10 bg-gray-700/50 w-96 p-2 rounded-lg backdrop-blur-md"> | ||
| <div class="fixed bottom-10 right-10 w-96 rounded-lg bg-gray-700/50 p-2 backdrop-blur-md"> | ||
| {@html $notification} | ||
| </div> | ||
| {/if} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we care about the dragover event? Wouldn't dragenter have already captured the file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately
dataTransfer.dropEffect(required for theondropevent to fire) can only be written to inside theondragoverevent. You could try mergingonDragEnterintoonDragOver, and removing theondragenterevent, but I recall that it didn't work quite right when I did that. (Which was before I had it fully working, to be fair)