From 100d37e552be2c17257fdeeca6d988ab0df0c947 Mon Sep 17 00:00:00 2001 From: waltem01 Date: Thu, 14 Mar 2024 15:25:46 +0100 Subject: [PATCH] refactor: auto format --- Webserver/src/lib/client/color.ts | 12 +- Webserver/src/lib/client/httpRequests.ts | 45 +- Webserver/src/lib/client/matrix.ts | 5 +- Webserver/src/lib/server/buildAPIStr.ts | 2 +- Webserver/src/routes/+page.svelte | 20 +- Webserver/src/routes/admin/+page.svelte | 4 +- Webserver/src/routes/api/reddit/+server.ts | 56 +- Webserver/src/routes/clock/+page.svelte | 482 ++++++++-------- Webserver/src/routes/image/+page.svelte | 638 ++++++++++----------- Webserver/tsconfig.json | 12 +- Webserver/vite.config.ts | 2 +- 11 files changed, 638 insertions(+), 640 deletions(-) diff --git a/Webserver/src/lib/client/color.ts b/Webserver/src/lib/client/color.ts index b5cf9d9..441c2ae 100644 --- a/Webserver/src/lib/client/color.ts +++ b/Webserver/src/lib/client/color.ts @@ -18,10 +18,10 @@ export function hexToRgb(hex: string): Color | null { const result = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); return result ? { - r: parseInt(result[1], 16), - g: parseInt(result[2], 16), - b: parseInt(result[3], 16) - } + r: parseInt(result[1], 16), + g: parseInt(result[2], 16), + b: parseInt(result[3], 16) + } : null; } @@ -36,7 +36,5 @@ export function getLuminance(rgb: Color): number { // Convert an rgb color object to a hex color string export function rgbToHex(color: Color): string { - return [color.r, color.g, color.b] - .map((e) => e.toString(16).padStart(2, '0')) - .join(''); + return [color.r, color.g, color.b].map((e) => e.toString(16).padStart(2, '0')).join(''); } diff --git a/Webserver/src/lib/client/httpRequests.ts b/Webserver/src/lib/client/httpRequests.ts index e7a8ea5..e59d0ad 100644 --- a/Webserver/src/lib/client/httpRequests.ts +++ b/Webserver/src/lib/client/httpRequests.ts @@ -1,23 +1,22 @@ -import type { APIResponse } from '$lib/interfaces'; - -export async function redirectAPI({ form, fdata }: { form?: HTMLFormElement; fdata?: FormData }) { - if (!fdata && form) fdata = new FormData(form); - else if (!fdata) throw new Error('No formdata provided!'); - - // Get endpoint, prefer form, then formdata or empty - const endpoint = (form?.dataset.endpoint ?? fdata.get('endpoint')) ?? ''; - // Append endpoint to formdata - if (!fdata?.has('endpoint')) - fdata.append('endpoint', endpoint); - - // Send request to be redirected to given endpoint - const response = await fetch('/api/redirect', { - method: 'POST', - body: fdata - }); - - // Await respose from webserver - const mdata = (await response.json()) as APIResponse; - // Basic error handling - if (!mdata.success) alert(`Error while processing '${endpoint}'!`); -} +import type { APIResponse } from '$lib/interfaces'; + +export async function redirectAPI({ form, fdata }: { form?: HTMLFormElement; fdata?: FormData }) { + if (!fdata && form) fdata = new FormData(form); + else if (!fdata) throw new Error('No formdata provided!'); + + // Get endpoint, prefer form, then formdata or empty + const endpoint = form?.dataset.endpoint ?? fdata.get('endpoint') ?? ''; + // Append endpoint to formdata + if (!fdata?.has('endpoint')) fdata.append('endpoint', endpoint); + + // Send request to be redirected to given endpoint + const response = await fetch('/api/redirect', { + method: 'POST', + body: fdata + }); + + // Await respose from webserver + const mdata = (await response.json()) as APIResponse; + // Basic error handling + if (!mdata.success) alert(`Error while processing '${endpoint}'!`); +} diff --git a/Webserver/src/lib/client/matrix.ts b/Webserver/src/lib/client/matrix.ts index 36aef55..66fbb72 100644 --- a/Webserver/src/lib/client/matrix.ts +++ b/Webserver/src/lib/client/matrix.ts @@ -46,9 +46,6 @@ export function createGridArray(matrix: Matrix): MatrixCell[][] { const { factor } = scale; return Array.from({ length: height / factor }, (_, y) => - Array.from( - { length: width / factor }, - (_, x) => ({ x, y, color: '000000' }) as MatrixCell - ) + Array.from({ length: width / factor }, (_, x) => ({ x, y, color: '000000' }) as MatrixCell) ) as MatrixCell[][]; } diff --git a/Webserver/src/lib/server/buildAPIStr.ts b/Webserver/src/lib/server/buildAPIStr.ts index 603c400..5460fc2 100644 --- a/Webserver/src/lib/server/buildAPIStr.ts +++ b/Webserver/src/lib/server/buildAPIStr.ts @@ -2,4 +2,4 @@ import { API_SERVER_IP, API_SERVER_PORT } from '$env/static/private'; export function buildAPIStr(endpoint: string) { return `http://${API_SERVER_IP ?? 'localhost'}:${API_SERVER_PORT ?? '8080'}/${endpoint}`; -} \ No newline at end of file +} diff --git a/Webserver/src/routes/+page.svelte b/Webserver/src/routes/+page.svelte index 87cebf6..a0c61e2 100644 --- a/Webserver/src/routes/+page.svelte +++ b/Webserver/src/routes/+page.svelte @@ -1,10 +1,10 @@ -

Did you mean:

-

- Admin/Control Panel -

-

- Image upload -

-

- Clock -

+

Did you mean:

+

+ Admin/Control Panel +

+

+ Image upload +

+

+ Clock +

diff --git a/Webserver/src/routes/admin/+page.svelte b/Webserver/src/routes/admin/+page.svelte index d613807..aba19a0 100644 --- a/Webserver/src/routes/admin/+page.svelte +++ b/Webserver/src/routes/admin/+page.svelte @@ -227,7 +227,9 @@ matrix = initializeMatrix(); matrix.scale = { factor: 4, - // 0.1875 (made up padding) * 192 (height when it was made up) * 4 (scaling when it was made up) + // 0.1875 (made up padding) * + // 192 (height when it was made up) * + // 4 (scaling when it was made up) padding: 144 / matrix.width }; matrix.grid = createGridArray(matrix); diff --git a/Webserver/src/routes/api/reddit/+server.ts b/Webserver/src/routes/api/reddit/+server.ts index b3729e7..ce6d780 100644 --- a/Webserver/src/routes/api/reddit/+server.ts +++ b/Webserver/src/routes/api/reddit/+server.ts @@ -1,28 +1,28 @@ -import type { APIResponse } from "$lib/interfaces"; -import { json, type RequestHandler } from "@sveltejs/kit"; - -export const GET: RequestHandler = async ({ url }) => { - // Get endpoint on main API from searchparams - const params = url.searchParams; - const subreddit = params.get('subreddit'); - // Return if param not found - if (subreddit === null) return json({ success: false } as APIResponse); - - // Get subreddit page data - const response = await fetch(`https://www.reddit.com/r/${subreddit}/`, { - headers: new Headers({ - "User-Agent": "MatrixRedditMemes/0.0.1" - }) - }); - // Read data as string - const text = await response.text(); - - // Get all sources of posts - const regex = /]+role="presentation"[^>]+src="(.*?)"[^>]*>/g; - let results = [], match; - while ((match = regex.exec(text)) !== null) - results.push(match[1]); - - // Return source data - return json({ success: true, results } as APIResponse); -}; +import type { APIResponse } from '$lib/interfaces'; +import { json, type RequestHandler } from '@sveltejs/kit'; + +export const GET: RequestHandler = async ({ url }) => { + // Get endpoint on main API from searchparams + const params = url.searchParams; + const subreddit = params.get('subreddit'); + // Return if param not found + if (subreddit === null) return json({ success: false } as APIResponse); + + // Get subreddit page data + const response = await fetch(`https://www.reddit.com/r/${subreddit}/`, { + headers: new Headers({ + 'User-Agent': 'MatrixRedditMemes/0.0.1' + }) + }); + // Read data as string + const text = await response.text(); + + // Get all sources of posts + const regex = /]+role="presentation"[^>]+src="(.*?)"[^>]*>/g; + let results = [], + match; + while ((match = regex.exec(text)) !== null) results.push(match[1]); + + // Return source data + return json({ success: true, results } as APIResponse); +}; diff --git a/Webserver/src/routes/clock/+page.svelte b/Webserver/src/routes/clock/+page.svelte index d2dcafe..f2491d0 100644 --- a/Webserver/src/routes/clock/+page.svelte +++ b/Webserver/src/routes/clock/+page.svelte @@ -1,241 +1,241 @@ - - - -

Matrix Clock Display

- -
(clockEnabled = !clockEnabled)}> - - -
- -
- - - - - - - - - - -
+ + + +

Matrix Clock Display

+ +
(clockEnabled = !clockEnabled)}> + + +
+ +
+ + + + + + + + + + +
diff --git a/Webserver/src/routes/image/+page.svelte b/Webserver/src/routes/image/+page.svelte index 82db20b..d5c587e 100644 --- a/Webserver/src/routes/image/+page.svelte +++ b/Webserver/src/routes/image/+page.svelte @@ -1,319 +1,319 @@ - - -

Matrix Image Upload

-
-
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- -
- -
-
- - - -
- - -
- - - {#if imageURL} - User uploaded or reddit - {/if} -
- -{#if uploadStarted} - {#if submitData.subreddit} - {#await fetchReddit(submitData)} -

Fetching subreddit data . . .

-

{uploadData.elapsed} seconds elapsed.

- {/await} - {:else} - {#await fileAsDataURL(submitData)} -

Loading image data . . .

-

{uploadData.elapsed} seconds elapsed.

- {:then dataUrl} - {#await sendImage(dataUrl)} -

Sending image . . .

-

{uploadData.elapsed} seconds elapsed.

- {:then} - {#await placeImage(submitData)} -

Placing image . . .

-

{uploadData.elapsed} seconds elapsed.

- {:then} - {#await updateMatrix(submitData)} -

Updating matrix . . .

-

{uploadData.elapsed} seconds elapsed.

- {:then} -

Done!

-

{uploadData.elapsed} seconds elapsed.

- {/await} - {/await} - {/await} - {/await} - {/if} -{/if} + + +

Matrix Image Upload

+
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+ +
+
+ + + +
+ + +
+ + + {#if imageURL} + User uploaded or reddit + {/if} +
+ +{#if uploadStarted} + {#if submitData.subreddit} + {#await fetchReddit(submitData)} +

Fetching subreddit data . . .

+

{uploadData.elapsed} seconds elapsed.

+ {/await} + {:else} + {#await fileAsDataURL(submitData)} +

Loading image data . . .

+

{uploadData.elapsed} seconds elapsed.

+ {:then dataUrl} + {#await sendImage(dataUrl)} +

Sending image . . .

+

{uploadData.elapsed} seconds elapsed.

+ {:then} + {#await placeImage(submitData)} +

Placing image . . .

+

{uploadData.elapsed} seconds elapsed.

+ {:then} + {#await updateMatrix(submitData)} +

Updating matrix . . .

+

{uploadData.elapsed} seconds elapsed.

+ {:then} +

Done!

+

{uploadData.elapsed} seconds elapsed.

+ {/await} + {/await} + {/await} + {/await} + {/if} +{/if} diff --git a/Webserver/tsconfig.json b/Webserver/tsconfig.json index 1797076..a588d37 100644 --- a/Webserver/tsconfig.json +++ b/Webserver/tsconfig.json @@ -10,11 +10,13 @@ "sourceMap": true, "strict": true, "moduleResolution": "bundler", - "plugins": [{ - "name": "typescript-svelte-plugin", - "assumeIsSvelteProject": false, - "enabled": true - }] + "plugins": [ + { + "name": "typescript-svelte-plugin", + "assumeIsSvelteProject": false, + "enabled": true + } + ] } // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias // diff --git a/Webserver/vite.config.ts b/Webserver/vite.config.ts index f78a266..fea8786 100644 --- a/Webserver/vite.config.ts +++ b/Webserver/vite.config.ts @@ -6,5 +6,5 @@ export default defineConfig({ test: { include: ['src/**/*.{test,spec}.{js,ts}'], exclude: ['src/playwright'] - }, + } });