From 8ae9a8c2412e447d7bf59b1a7772b78ad79338e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Mur=C3=A7a?= Date: Thu, 15 Dec 2022 18:27:09 -0300 Subject: [PATCH] Some small refactorings --- src/file-renderer/index.js | 42 ++++++++++++++++++++++---------------- src/lib/CardList.svelte | 6 +++++- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/file-renderer/index.js b/src/file-renderer/index.js index fa77679..5ae2d6c 100644 --- a/src/file-renderer/index.js +++ b/src/file-renderer/index.js @@ -10,9 +10,10 @@ String.prototype.endsWith = function (suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; -function displayFileText(filename, evt, canvas) { +function renderToCanvas(filename, evt, canvas) { const view = jDataView(evt.target.result, 0, evt.size); const pattern = new Pattern(); + filename = filename.toLowerCase(); if (filename.endsWith("pes")) { pesRead(view, pattern); @@ -29,15 +30,16 @@ function displayFileText(filename, evt, canvas) { pattern.drawShape(canvas); } -function handleFileReadAbort(evt) { - alert("File read aborted."); +function renderAbortMessage(errorMessageRef) { + errorMessageRef.innerHTML = "Render aborted!"; } -function handleFileReadError(evt) { +function renderErrorMessage(errorName, errorMessageRef) { let message; - switch (evt.target.error.name) { + switch (errorName) { case "NotFoundError": - alert("The file could not be found at the time the read was processed."); + message = + "The file could not be found at the time the read was processed."; break; case "SecurityError": message = "

A file security error occured. This can be due to:

"; @@ -46,29 +48,33 @@ function handleFileReadError(evt) { message += "
  • Performing too many read calls on file resources.
  • "; message += "
  • The file has changed on disk since the user selected it.
  • "; - alert(message); break; case "NotReadableError": - alert( - "The file cannot be read. This can occur if the file is open in another application." - ); + message = + "The file cannot be read. This can occur if the file is open in another application."; break; case "EncodingError": - alert("The length of the data URL for the file is too long."); + message = "The length of the data URL for the file is too long."; break; default: - alert("File error code " + evt.target.error.name); + message = "Something wrong happened!"; + break; } + + errorMessageRef.innerHTML = message; } -export default function renderFileToCanvas(fileObject, canvas) { +export default function renderFileToCanvas( + fileObject, + canvas, + errorMessageRef +) { const reader = new FileReader(); - reader.onloadend = function (x) { - displayFileText.apply(null, [fileObject.name, x, canvas]); - }; - reader.abort = handleFileReadAbort; - reader.onerror = handleFileReadError; + reader.onloadend = (evt) => renderToCanvas(fileObject.name, evt, canvas); + reader.abort = (/** @type {any} */ _) => renderAbortMessage(errorMessageRef); + reader.onerror = (evt) => + renderErrorMessage(evt.target.error.name, errorMessageRef); if (fileObject) { reader.readAsArrayBuffer(fileObject); diff --git a/src/lib/CardList.svelte b/src/lib/CardList.svelte index fe93d26..a2b7fc1 100644 --- a/src/lib/CardList.svelte +++ b/src/lib/CardList.svelte @@ -3,6 +3,7 @@ export let files = []; let canvasRefs = []; + let errorMessageRef; {#if files.length !== 0} @@ -12,8 +13,11 @@

    {file.name}

    - {canvasRefs[i] && renderFileToCanvas(file, canvasRefs[i])} + {canvasRefs[i] && + renderFileToCanvas(file, canvasRefs[i], errorMessageRef)} {/each} + +

    {/if}