refactor: general code clean up

This commit is contained in:
waltem01 2024-02-22 10:47:26 +01:00
parent f2cb916782
commit 2d0c93cf3b

View File

@ -9,12 +9,14 @@
import type { APIResponse } from '$lib/interfaces'; import type { APIResponse } from '$lib/interfaces';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
interface UploadData { // Data structure to keep track of upload statistics
interface UploadStat {
start: number; start: number;
elapsed: string; elapsed: string;
interval: NodeJS.Timeout; interval: NodeJS.Timeout;
} }
// Data structure to represent user data
interface SubmitData { interface SubmitData {
x: number; x: number;
y: number; y: number;
@ -23,16 +25,20 @@
update: boolean; update: boolean;
} }
// System variables
let imageURL: string | null; let imageURL: string | null;
let uploadData: UploadData, matrix: Matrix; let uploadData: UploadStat, matrix: Matrix;
let uploadStarted = false, let uploadStarted = false,
submitData: SubmitData; submitData: SubmitData;
// Update matrix over endpoint
async function updateMatrix(data: SubmitData) { async function updateMatrix(data: SubmitData) {
if (data.update) await fetch('/api/redirect?endpoint=update'); if (data.update) await fetch('/api/redirect?endpoint=update');
clearInterval(uploadData.interval); clearInterval(uploadData.interval);
} }
// Placing image over endpoint (by specifying position)
// Image is always max resolution! See resizing in `fileAsDataURL`.
async function placeImage(data: SubmitData) { async function placeImage(data: SubmitData) {
const fdata = new FormData(); const fdata = new FormData();
fdata.append('x', data.x.toString()); fdata.append('x', data.x.toString());
@ -40,12 +46,14 @@
await post(fdata, 'image'); await post(fdata, 'image');
} }
// Send image data to endpoint
async function sendImage(url: string) { async function sendImage(url: string) {
const fdata = new FormData(); const fdata = new FormData();
fdata.append('url', url); fdata.append('url', url);
await post(fdata, 'upload'); await post(fdata, 'upload');
} }
// Handle sending HTTP POST request with specified data
async function post(fdata: FormData, endpoint: string) { async function post(fdata: FormData, endpoint: string) {
// Append endpoint to formdata for redirection // Append endpoint to formdata for redirection
fdata.append('endpoint', endpoint); fdata.append('endpoint', endpoint);
@ -60,6 +68,7 @@
if (!mdata.success) alert(`Error while processing '${endpoint}'!`); if (!mdata.success) alert(`Error while processing '${endpoint}'!`);
} }
// Load and read file, encode as base64 data url
async function fileAsDataURL(data: SubmitData): Promise<string> { async function fileAsDataURL(data: SubmitData): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const img = new Image(); const img = new Image();
@ -75,8 +84,7 @@
context?.drawImage(img, 0, 0, canvas.width, canvas.height); context?.drawImage(img, 0, 0, canvas.width, canvas.height);
// Convert the canvas image to a Data URL // Convert the canvas image to a Data URL
const dataURL = canvas.toDataURL(); resolve(canvas.toDataURL());
resolve(dataURL);
}; };
img.onerror = reject; img.onerror = reject;
@ -85,6 +93,7 @@
}); });
} }
// Getting an image from input
function getImage(event: Event) { function getImage(event: Event) {
uploadStarted = false; uploadStarted = false;
// Revoke previous image url, if any // Revoke previous image url, if any
@ -102,6 +111,7 @@
imageURL = URL.createObjectURL(file); imageURL = URL.createObjectURL(file);
} }
// Handling form submit event
function handleSubmit(event: SubmitEvent) { function handleSubmit(event: SubmitEvent) {
// Get image data from submit // Get image data from submit
const form = event.target as HTMLFormElement; const form = event.target as HTMLFormElement;
@ -131,6 +141,7 @@
uploadStarted = true; uploadStarted = true;
} }
// On client loaded
onMount(() => { onMount(() => {
matrix = { matrix = {
width: +PUBLIC_LED_WIDTH * +PUBLIC_LED_CHAIN, width: +PUBLIC_LED_WIDTH * +PUBLIC_LED_CHAIN,
@ -208,7 +219,6 @@
name="image" name="image"
id="image" id="image"
accept="image/*" accept="image/*"
required
on:change={getImage} on:change={getImage}
/> />
</div> </div>