From 3bcf440503c2fca8ac8e3aea5c9a07d447ebb445 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 19 Dec 2023 14:50:43 -0800 Subject: [PATCH 1/3] feat: file drag and drop support --- src/lib/components/chat/MessageInput.svelte | 64 +++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 172485ca..d83a5fcd 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -2,6 +2,7 @@ import { settings } from '$lib/stores'; import toast from 'svelte-french-toast'; import Suggestions from './MessageInput/Suggestions.svelte'; + import { onMount } from 'svelte'; export let submitPrompt: Function; export let stopResponse: Function; @@ -11,6 +12,7 @@ let filesInputElement; let inputFiles; + let dragged = false; export let files = []; @@ -82,8 +84,70 @@ } } }; + + onMount(() => { + const dropZone = document.querySelector('body'); + + dropZone?.addEventListener('dragover', (e) => { + e.preventDefault(); + dragged = true; + }); + + dropZone.addEventListener('drop', (e) => { + e.preventDefault(); + console.log(e); + + if (e.dataTransfer?.files) { + let reader = new FileReader(); + + reader.onload = (event) => { + files = [ + ...files, + { + type: 'image', + url: `${event.target.result}` + } + ]; + }; + + if ( + e.dataTransfer?.files && + e.dataTransfer?.files.length > 0 && + ['image/gif', 'image/jpeg', 'image/png'].includes(e.dataTransfer?.files[0]['type']) + ) { + reader.readAsDataURL(e.dataTransfer?.files[0]); + } else { + toast.error(`Unsupported File Type '${e.dataTransfer?.files[0]['type']}'.`); + } + } + + dragged = false; + }); + }); +{#if dragged} +
+
+
+
+
🏞️
+
Add Images
+ +
+ Drop any images here to add to the conversation +
+
+
+
+
+{/if} +
{#if messages.length == 0 && suggestionPrompts.length !== 0} From 344c91e37a43c46790e1338b134561f3e79ab570 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 19 Dec 2023 14:53:14 -0800 Subject: [PATCH 2/3] fix: dragleave event added --- src/lib/components/chat/MessageInput.svelte | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index d83a5fcd..54291f22 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -123,6 +123,10 @@ dragged = false; }); + + dropZone?.addEventListener('dragleave', () => { + dragged = false; + }); }); From 110498cad68ebcd3ea8eadc30ddbd1610481cec8 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 19 Dec 2023 14:59:19 -0800 Subject: [PATCH 3/3] fix: disable message image drag --- src/lib/components/chat/Messages.svelte | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index c6d53424..9a39089d 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -469,7 +469,12 @@ {#each message.files as file}
{#if file.type === 'image'} - input + input {/if}
{/each}