|
1 | 1 | <script lang="ts">
|
2 |
| - import Combobox from "$lib/components/ui/combobox/combobox.svelte"; |
| 2 | + import {Textarea} from "$lib/components/ui/textarea" |
| 3 | + import {cn} from "$lib/utils"; |
| 4 | +
|
| 5 | + let template: string |
| 6 | + let parsedTemplate: string = "" |
| 7 | + let errors: string[] = [] |
| 8 | +
|
| 9 | + const api = "https://api.gttp.dev/api/v1/parse" |
| 10 | +
|
| 11 | + function keyDown(e: KeyboardEvent) { |
| 12 | + if (e.key === "Tab") { |
| 13 | + e.preventDefault() |
| 14 | + const start = this.selectionStart |
| 15 | + const end = this.selectionEnd |
| 16 | + template = template.substring(0, start) + " " + template.substring(end) |
| 17 | + this.selectionStart = this.selectionEnd = start + 2 |
| 18 | + } |
| 19 | +
|
| 20 | + fetch(api, { |
| 21 | + method: "POST", |
| 22 | + headers: { |
| 23 | + "Content-Type": "application/json" |
| 24 | + }, |
| 25 | + body: JSON.stringify({template}) |
| 26 | + }).then(res => res.json()).then(data => { |
| 27 | + parsedTemplate = data.rendered |
| 28 | + errors = [] |
| 29 | + errors = data.errors |
| 30 | + }) |
| 31 | + } |
3 | 32 | </script>
|
4 | 33 | <div class="flex justify-center">
|
5 | 34 | <div class="max-w-screen-lg w-full px-8">
|
6 | 35 | <div>
|
7 |
| - <Combobox options={[ |
8 |
| - {value: "a", label: "A"}, |
9 |
| - {value: "b", label: "B"}, |
10 |
| - {value: "c", label: "C"}, |
11 |
| - ]} multi={true}/> |
| 36 | + <Textarea |
| 37 | + on:keyup={keyDown} |
| 38 | + class={cn("h-96")} |
| 39 | + bind:value={template} |
| 40 | + /> |
| 41 | + </div> |
| 42 | + <div class="mt-16"> |
| 43 | + {#if errors && errors.length > 0} |
| 44 | + <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert"> |
| 45 | + <strong class="font-bold">Error</strong> |
| 46 | + <ul class="list-disc list-inside"> |
| 47 | + {#each errors as error} |
| 48 | + <li>{error}</li> |
| 49 | + {/each} |
| 50 | + </ul> |
| 51 | + </div> |
| 52 | + {/if} |
| 53 | + <pre> |
| 54 | + {parsedTemplate} |
| 55 | + </pre> |
12 | 56 | </div>
|
13 | 57 | </div>
|
14 | 58 | </div>
|
0 commit comments