diff --git a/src/ChartEdit.svelte b/src/ChartEdit.svelte index 86380da..600cc4e 100644 --- a/src/ChartEdit.svelte +++ b/src/ChartEdit.svelte @@ -3,10 +3,11 @@ import IoMdLink from 'svelte-icons/io/IoMdLink.svelte' import IoMdAlert from 'svelte-icons/io/IoMdAlert.svelte' import { writeText } from 'clipboard-polyfill'; - import { Button, TextField, Loading } from 'attractions'; + import { Button, TextField, Loading, Popover, PopoverButton, H2, FormField, FileInput, FileTile, Slider } from 'attractions'; import Chart from './Chart.svelte'; import { onMount } from 'svelte'; import { currentChartID } from './stores'; + import { writable } from 'svelte/store'; let isLoaded = false; let error = null; @@ -17,12 +18,24 @@ let right = ''; let bot = ''; - let images = []; + let images = writable([]); + let currentImage = { + file: null, + name: '', + x: 0.5, + y: 0.5 + }; + + let upload = null; + let s; + + let submitError = null; onMount(() => { - let s = new WebSocket('ws://localhost:5252/connect?id=' + $currentChartID); + s = new WebSocket('ws://localhost:5252/connect?id=' + $currentChartID); s.addEventListener('message', (e) => { let data = JSON.parse(e.data); + console.log(data); if (!isLoaded) { name = data.name || ''; top = data.topText || ''; @@ -31,13 +44,50 @@ bot = data.bottomText || ''; isLoaded = true; } else { - images.push(...data); + for (const img of data) { + img.file = 'http://localhost:5252/img/' + img.file; + } + images.update(imgs => [...imgs, ...data]); } }); s.addEventListener('error', (e) => { error = e; }); }); + + let submitting = false; + + async function submitImage() { + submitting = true; + let formData = new FormData(); + formData.append('id', $currentChartID); + formData.append('name', currentImage.name); + formData.append('x', currentImage.x); + formData.append('y', currentImage.y); + formData.append('icon', upload); + + try { + const response = await fetch('http://localhost:5252/add', { + method: 'POST', + mode: 'cors', + body: formData, + }); + + if (response.ok) { + submitting = false; + currentImage = {file: null, name: '', x: 0.5, y: 0.5} + } else { + submitting = false; + if (response.body) { + submitError = response.statusText + ': ' + await response.text(); + } else { + submitError = response.statusText; + } + } + } catch(err) { + submitting = false; + } + } + +
@@ -84,9 +172,92 @@


{:else if isLoaded} - {#key name + top + left + right + bot} - {name} - - {/key} +
+
+ {#key name} + {name} + {/key} +
+
+ {#key top + left + right + bot} +
+ {/key} + {#each [...$images, currentImage] as img} + {#if img && img.file} +
+ +
+ {img.name} +
+
+ {img.name} +
+
+
+ {/if} + {/each} +
+
+

add an image

+ + + { + const reader = new FileReader(); + reader.onload = (e) => {currentImage.file = e.target.result} + reader.readAsDataURL(upload); + }}/> + {#if upload != null} + upload = null}/> + {/if} +
+ + + + + + + + + + +
+ +
+ + {#if submitError} +
+
+
+ {submitError} +
+
+ {/if} +
+
+
+
{/if}
\ No newline at end of file