From ce5601bde25ed431f3fe9ea75cc089e1796382e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Mur=C3=A7a?= Date: Fri, 11 Nov 2022 12:59:27 -0300 Subject: [PATCH] Refactor to handle in svelte way --- src/lib/FileViewer.svelte | 85 +++++++++++++++++---------------------- src/utils/main.js | 2 + 2 files changed, 40 insertions(+), 47 deletions(-) diff --git a/src/lib/FileViewer.svelte b/src/lib/FileViewer.svelte index 586b32d..4f3b768 100644 --- a/src/lib/FileViewer.svelte +++ b/src/lib/FileViewer.svelte @@ -2,48 +2,24 @@ import { startFileRead } from "../utils/main"; let files; + let filesRendered = false; + let filesRefs = []; const acceptedFiles = [".pes"]; const maxFileSize = 700000; - function onSubmitHandler() { - for (var i = 0, file; (file = files[i]); i++) { - const canvasContainer = document.getElementById("canvas-container"); - const canvasCard = document.createElement(`div`); - const canvasEl = document.createElement(`canvas`); - const fileNameEl = document.createElement(`p`); - - canvasCard.id = `canvas-card-${i}`; - canvasCard.style["display"] = "flex"; - canvasCard.style["flex-direction"] = "column"; - canvasCard.style["justify-content"] = "center"; - canvasCard.style["align-items"] = "center"; - canvasCard.style["width"] = "550px"; - canvasCard.style["height"] = "550px"; - canvasCard.style["margin-bottom"] = "15px"; - canvasCard.style["border"] = "2px solid black"; - - canvasEl.id = `mycanvas-${i}`; - canvasEl.style["height"] = "80%"; - canvasEl.style["width"] = "fit-content"; - - fileNameEl.textContent = file.name; - - canvasCard.appendChild(canvasEl); - canvasCard.appendChild(fileNameEl); - canvasContainer.appendChild(canvasCard); - - if (file) { - startFileRead(file, canvasEl); - } - } - document.getElementById("selected-files-container").remove(); + function onSubmit() { + filesRendered = true; } - function onDropHandler(evt) { - onChangeFileHandler(evt); + function onDrop(evt) { + onChange(evt); } - function onChangeFileHandler(evt) { + function onChange(evt) { + files = null; + filesRefs = []; + filesRendered = false; + const changedFiles = evt.dataTransfer ? evt.dataTransfer.files : evt.target.files; @@ -61,11 +37,11 @@ files = filesToUpload; } - function handleOnClick() { + function onClick() { document.getElementById("file-input").click(); } - function handleOnKeydown(evt) { + function onKeydown(evt) { if (evt.key === "Enter") { document.getElementById("file-input").click(); } @@ -73,7 +49,7 @@
@@ -87,10 +63,10 @@
-
- {#if files} +{#if files && files.length !== 0 && !filesRendered} +

Selected files:

{#each Array.from(files) as file}

{file.name} ({file.size / 1000} kb)

{/each} - {/if} -
+
+{/if} -
+{#if files && files.length !== 0 && filesRendered} +
+ {#each Array.from(files) as file, i} +
+ +

{file.name}

+
+ {filesRefs[i] && startFileRead(file, filesRefs[i])} + {/each} +
+{/if}