From 6a92e7304b1aa4ea9992ab000e523cdaaa6e004e Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 18 May 2021 18:47:39 +0100 Subject: [PATCH] Allow dropzone to preview images before being uploaded --- packages/bbui/src/Form/Core/Dropzone.svelte | 47 +++++++++++++++------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/packages/bbui/src/Form/Core/Dropzone.svelte b/packages/bbui/src/Form/Core/Dropzone.svelte index 5b5c72b809..3315c923f8 100644 --- a/packages/bbui/src/Form/Core/Dropzone.svelte +++ b/packages/bbui/src/Form/Core/Dropzone.svelte @@ -37,9 +37,28 @@ const fieldId = id || generateID() let selectedImageIdx = 0 let fileDragged = false + let selectedUrl $: selectedImage = value?.[selectedImageIdx] ?? null $: fileCount = value?.length ?? 0 - $: isImage = imageExtensions.includes(selectedImage?.extension?.toLowerCase()) + $: isImage = + imageExtensions.includes(selectedImage?.extension?.toLowerCase()) || + selectedImage?.type?.startsWith("image") + + $: { + if (selectedImage?.url) { + selectedUrl = selectedImage?.url + } else if (selectedImage) { + try { + let reader = new FileReader() + reader.readAsDataURL(selectedImage) + reader.onload = e => { + selectedUrl = e.target.result + } + } catch (error) { + selectedUrl = null + } + } + } async function processFileList(fileList) { if ( @@ -102,11 +121,13 @@