From 879e2e979b76b853aed7de5d2f0f705498c4ff19 Mon Sep 17 00:00:00 2001 From: nwrenger Date: Wed, 31 Jan 2024 21:07:14 +0100 Subject: [PATCH] :sparkles: Type Changes --- src/lib/stores.ts | 2 +- src/lib/types.ts | 81 +++++++++------ src/lib/utils.ts | 6 +- src/routes/builder/+page.svelte | 4 +- src/routes/builder/EditModal.svelte | 90 +++++++---------- src/routes/builder/Grid.svelte | 150 +++++++++++++--------------- src/routes/builder/Menu.svelte | 130 ++++++++++++------------ 7 files changed, 226 insertions(+), 237 deletions(-) diff --git a/src/lib/stores.ts b/src/lib/stores.ts index 41cad6d..6979d66 100644 --- a/src/lib/stores.ts +++ b/src/lib/stores.ts @@ -4,4 +4,4 @@ import type { Views } from './types'; import { localStorageStore } from '@skeletonlabs/skeleton'; export let gridController = writable(); -export let views = localStorageStore('views', { views: [], current: 0 }); +export let views = localStorageStore('views', { pages: [], current: 0 }); diff --git a/src/lib/types.ts b/src/lib/types.ts index 6827591..21ead61 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,56 +1,75 @@ +export type Option = T | null; + export enum GuiType { - Header = 'header', - BodyText = 'body-text', - Buttons = 'buttons', - Alert = 'alert' + Header = 0, + BodyText, + Buttons, + Alert } -export enum GuiAlign { - Left = 'left', - Right = 'right', - Top = 'top', - Bottom = 'bottom', - Center = 'center' +export enum Align { + Left = 0, + Right, + Top, + Bottom, + Center } -export enum EventType { - View = 'view', - Function = 'function' +export function alignToString(align: Align | string): string { + switch (align) { + case Align.Left: + return 'Left'; + case Align.Right: + return 'Right'; + case Align.Top: + return 'Top'; + case Align.Bottom: + return 'Bottom'; + case Align.Center: + return 'Center'; + default: + throw new Error('Invalid Align value'); + } } -export type Option = T | null; -export type IdArray = { rows: T[]; id: string }[]; -export type EventData = T extends EventType.View ? number : string; +export type Event = + | { + View: number; + } + | { + Function: string; + }; export interface Views { - views: IdArray; + pages: Page[]; current: number; } -export interface Item { +export interface Page { + id: string; + page: View[]; +} + +export interface View { id: string; - type: GuiType; name: string; + type: GuiType; moveable: boolean; x: number; y: number; w: number; h: number; - data: { textValue: Option; actions: Option[]> }; + data: Data; } -export interface AlignedText { - horizontal: GuiAlign; - vertical: GuiAlign; - text: string; +export interface Data { + text_value: Option; + horizontal: Option; + vertical: Option; + actions: Option[]>; } -export interface Actions { - textValue: string | undefined; +export interface Action { + text_value: String; event: Event; } - -export interface Event { - type: EventType; - data: EventData | undefined; -} diff --git a/src/lib/utils.ts b/src/lib/utils.ts index b078530..afc2ea1 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,4 +1,4 @@ -import type { Item } from './types'; +import type { View } from './types'; export function keys(obj: T) { return Object.keys(obj) as Array; @@ -39,7 +39,7 @@ export function areObjectsEqual(obj1: any, obj2: any): boolean { return true; } -export function isItemColliding(item: Item, otherItem: Item) { +export function isItemColliding(item: View, otherItem: View) { return ( item.id !== otherItem.id && item.x <= otherItem.x + otherItem.w - 1 && @@ -49,7 +49,7 @@ export function isItemColliding(item: Item, otherItem: Item) { ); } -export function areItemsColliding(item: Item, items: Item[]) { +export function areItemsColliding(item: View, items: View[]) { for (const item2 of items) { if (isItemColliding(item, item2)) return true; } diff --git a/src/routes/builder/+page.svelte b/src/routes/builder/+page.svelte index 482f8f4..55d061e 100644 --- a/src/routes/builder/+page.svelte +++ b/src/routes/builder/+page.svelte @@ -8,9 +8,9 @@ -

{$views.views.length != 0 ? `View ${$views.current + 1}` : 'No Views!'}

+

{$views.pages.length != 0 ? `View ${$views.current + 1}` : 'No Views!'}

diff --git a/src/routes/builder/EditModal.svelte b/src/routes/builder/EditModal.svelte index 2720453..c9462da 100644 --- a/src/routes/builder/EditModal.svelte +++ b/src/routes/builder/EditModal.svelte @@ -3,7 +3,7 @@ // Stores import { getModalStore, type ModalSettings } from '@skeletonlabs/skeleton'; import { views } from '$lib/stores'; - import { EventType, GuiAlign, GuiType, type Item } from '$lib/types'; + import { Align, alignToString, GuiType, type View, type Action } from '$lib/types'; // Props /** Exposes parent props to this component. */ @@ -14,12 +14,15 @@ const cBase = 'card p-4 w-modal shadow-xl space-y-4'; const cHeader = 'text-2xl font-bold'; - let item: Item = $modalStore[0]?.meta.toChange; + let view: View = $modalStore[0]?.meta.toChange; // apply reactivity on close modalStore.subscribe((modals: ModalSettings[]) => { if (!modals.length) $views = $views; }); + + // select shenanigans + let ev = new Array(view.data.actions?.length).fill('view'); @@ -28,101 +31,86 @@
Edit
- {#if item.data.textValue != null} - {#if typeof item.data.textValue == 'string'} + {#if view.data.text_value != null} + {#if typeof view.data.text_value == 'string'} - {:else if typeof item.data.textValue == 'object'} - + {/if} + {#if view.data.horizontal != null && view.data.vertical != null} - {:else} - {/if} {/if} - {#if item.data.actions != null} - {#each item.data.actions as action, i} + {#if view.data.actions != null} + {#each view.data.actions as action, i} {#if action != null} @@ -131,14 +119,12 @@
Action {i + 1}
{ + action = { + text_value: '', + event: { View: 1 } + }; + }}> Add Action {/if} diff --git a/src/routes/builder/Grid.svelte b/src/routes/builder/Grid.svelte index 63c2f41..7c0bbec 100644 --- a/src/routes/builder/Grid.svelte +++ b/src/routes/builder/Grid.svelte @@ -5,18 +5,18 @@ import { keys } from '$lib/utils'; import { Grid, GridItem } from 'svelte-grid-extended'; import { gridController, views } from '$lib/stores'; - import { GuiAlign, GuiType, type Item } from '$lib/types'; + import { Align, GuiType, type View } from '$lib/types'; const modalStore = getModalStore(); function deleteItem(id: string) { - $views.views[$views.current].rows = $views.views[$views.current].rows.filter( - (item: { id: string }) => item.id !== id + $views.pages[$views.current].page = $views.pages[$views.current].page.filter( + (view: { id: string }) => view.id !== id ); $views = $views; } - function editItem(toChange: Item) { + function editItem(toChange: View) { const modal: ModalSettings = { type: 'component', component: 'editModal', @@ -28,31 +28,31 @@ modalStore.trigger(modal); } - $: verticalAlign = (align: GuiAlign) => { + $: verticalAlign = (align: Align) => { switch (align) { - case GuiAlign.Left: + case Align.Left: return ''; - case GuiAlign.Right: + case Align.Right: return ''; - case GuiAlign.Top: + case Align.Top: return 'items-start'; - case GuiAlign.Bottom: + case Align.Bottom: return 'items-end'; - case GuiAlign.Center: + case Align.Center: return 'items-center'; } }; - $: horizontalAlign = (align: GuiAlign) => { + $: horizontalAlign = (align: Align) => { switch (align) { - case GuiAlign.Left: + case Align.Left: return 'justify-start'; - case GuiAlign.Right: + case Align.Right: return 'justify-end'; - case GuiAlign.Top: + case Align.Top: return ''; - case GuiAlign.Bottom: + case Align.Bottom: return ''; - case GuiAlign.Center: + case Align.Center: return 'justify-center'; } }; @@ -67,74 +67,62 @@ bind:controller={$gridController} class="z-[1]" > - {#if $views.views[$views.current]} - {#each $views.views[$views.current].rows as item (item.id)} + {#if $views.pages[$views.current]} + {#each $views.pages[$views.current].page as view (view.id)}
- {#if item.type == GuiType.Header} - {#if typeof item.data.textValue == 'object'} -
-

- {#if item.data.textValue?.text} - {item.data.textValue.text} - {:else} - Unset - {/if} -

-
- {:else} -

- TypeError: Mismatched Type. Please delete this Element and put in a new one! -

- {/if} - {:else if item.type == GuiType.BodyText} - {#if typeof item.data.textValue == 'object'} -
-

- {#if item.data.textValue?.text} - {item.data.textValue.text} - {:else} - Unset - {/if} -

-
- {:else} -

- TypeError: Mismatched Type. Please delete this Element and put in a new one! + {#if view.type == GuiType.Header} +

+

+ {#if view.data.text_value} + {view.data.text_value} + {:else} + Unset + {/if} +

+
+ {:else if view.type == GuiType.BodyText} +
+

+ {#if view.data.text_value} + {view.data.text_value} + {:else} + Unset + {/if}

- {/if} - {:else if item.type == GuiType.Buttons} - {#if item.data.actions} +
+ {:else if view.type == GuiType.Buttons} + {#if view.data.actions}
- {#each item.data.actions as action} + {#each view.data.actions as action} {#if action} - {action.textValue} + {action.text_value} {#if action.event} {action.event.type}{Object.keys(action.event)[0]} {/if} @@ -147,22 +135,22 @@ {/each}
{/if} - {:else if item.type == GuiType.Alert} - {#if item.data.textValue} -

{item.data.textValue}

+ {:else if view.type == GuiType.Alert} + {#if view.data.text_value} +

{view.data.text_value}

{:else}

Unset

{/if}
- {#if item.data.actions && item.data.actions[0]} + {#if view.data.actions && view.data.actions[0]} - {item.data.actions[0].textValue} - {#if item.data.actions[0].event} + {view.data.actions[0].text_value} + {#if view.data.actions[0].event} {item.data.actions[0].event.type}{Object.keys(view.data.actions[0].event)[0]} {/if} @@ -174,22 +162,22 @@ {/if}
{:else} - {item.name}, - {#each keys(item.data) as key (key)} - {key}: {item.data[key]}, + {view.name}, + {#each keys(view.data) as key (key)} + {key}: {view.data[key]}, {/each} {/if} - {item.name} + {view.name} diff --git a/src/routes/builder/Menu.svelte b/src/routes/builder/Menu.svelte index c679ee9..0ab4353 100644 --- a/src/routes/builder/Menu.svelte +++ b/src/routes/builder/Menu.svelte @@ -1,106 +1,102 @@