Some small refactorings

This commit is contained in:
Leonardo Murça 2022-12-15 18:27:09 -03:00
parent c4dd609fd5
commit 8ae9a8c241
2 changed files with 29 additions and 19 deletions

View file

@ -10,9 +10,10 @@ String.prototype.endsWith = function (suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1; 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 view = jDataView(evt.target.result, 0, evt.size);
const pattern = new Pattern(); const pattern = new Pattern();
filename = filename.toLowerCase(); filename = filename.toLowerCase();
if (filename.endsWith("pes")) { if (filename.endsWith("pes")) {
pesRead(view, pattern); pesRead(view, pattern);
@ -29,15 +30,16 @@ function displayFileText(filename, evt, canvas) {
pattern.drawShape(canvas); pattern.drawShape(canvas);
} }
function handleFileReadAbort(evt) { function renderAbortMessage(errorMessageRef) {
alert("File read aborted."); errorMessageRef.innerHTML = "Render aborted!";
} }
function handleFileReadError(evt) { function renderErrorMessage(errorName, errorMessageRef) {
let message; let message;
switch (evt.target.error.name) { switch (errorName) {
case "NotFoundError": 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; break;
case "SecurityError": case "SecurityError":
message = "<p>A file security error occured. This can be due to:</p>"; message = "<p>A file security error occured. This can be due to:</p>";
@ -46,29 +48,33 @@ function handleFileReadError(evt) {
message += "<li>Performing too many read calls on file resources.</li>"; message += "<li>Performing too many read calls on file resources.</li>";
message += message +=
"<li>The file has changed on disk since the user selected it.</li></ul>"; "<li>The file has changed on disk since the user selected it.</li></ul>";
alert(message);
break; break;
case "NotReadableError": case "NotReadableError":
alert( message =
"The file cannot be read. This can occur if the file is open in another application." "The file cannot be read. This can occur if the file is open in another application.";
);
break; break;
case "EncodingError": 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; break;
default: default:
alert("File error code " + evt.target.error.name); message = "Something wrong happened!";
} break;
} }
export default function renderFileToCanvas(fileObject, canvas) { errorMessageRef.innerHTML = message;
}
export default function renderFileToCanvas(
fileObject,
canvas,
errorMessageRef
) {
const reader = new FileReader(); const reader = new FileReader();
reader.onloadend = function (x) { reader.onloadend = (evt) => renderToCanvas(fileObject.name, evt, canvas);
displayFileText.apply(null, [fileObject.name, x, canvas]); reader.abort = (/** @type {any} */ _) => renderAbortMessage(errorMessageRef);
}; reader.onerror = (evt) =>
reader.abort = handleFileReadAbort; renderErrorMessage(evt.target.error.name, errorMessageRef);
reader.onerror = handleFileReadError;
if (fileObject) { if (fileObject) {
reader.readAsArrayBuffer(fileObject); reader.readAsArrayBuffer(fileObject);

View file

@ -3,6 +3,7 @@
export let files = []; export let files = [];
let canvasRefs = []; let canvasRefs = [];
let errorMessageRef;
</script> </script>
{#if files.length !== 0} {#if files.length !== 0}
@ -12,8 +13,11 @@
<canvas bind:this={canvasRefs[i]} class="canvas" /> <canvas bind:this={canvasRefs[i]} class="canvas" />
<p>{file.name}</p> <p>{file.name}</p>
</div> </div>
{canvasRefs[i] && renderFileToCanvas(file, canvasRefs[i])} {canvasRefs[i] &&
renderFileToCanvas(file, canvasRefs[i], errorMessageRef)}
{/each} {/each}
<!-- svelte-ignore a11y-missing-content -->
<h1 bind:this={errorMessageRef} />
</div> </div>
{/if} {/if}