|
| 1 | +<script lang="ts"> |
| 2 | + import { render as renderProfile } from '$lib/renderer/index'; |
| 3 | + import TemplateEditor from '$lib/components/editors/TemplateEditor.svelte'; |
| 4 | + import type { PageData } from './$types'; |
| 5 | + import { page } from '$app/stores'; |
| 6 | + import minimalTheme from './minimalTheme.html.j2?raw'; |
| 7 | + import weirdTheme from '$lib/themes/weird.html.j2?raw'; |
| 8 | + import { onMount } from 'svelte'; |
| 9 | +
|
| 10 | + let { data }: { data: PageData } = $props(); |
| 11 | +
|
| 12 | + const encoder = new TextEncoder(); |
| 13 | +
|
| 14 | + let template = $state(weirdTheme); |
| 15 | +
|
| 16 | + let renderMount: HTMLIFrameElement; |
| 17 | +
|
| 18 | + onMount(() => { |
| 19 | + if (data.theme) { |
| 20 | + template = new TextDecoder().decode(data.theme.data); |
| 21 | + } |
| 22 | + }); |
| 23 | +
|
| 24 | + $effect(() => { |
| 25 | + const rendered = renderProfile( |
| 26 | + { handle: $page.params.username, ...data.profile, pages: data.pages }, |
| 27 | + encoder.encode(template) |
| 28 | + ); |
| 29 | + renderMount.contentWindow?.document.open(); |
| 30 | + renderMount.contentWindow?.document.write(rendered); |
| 31 | + renderMount.contentWindow?.document.close(); |
| 32 | + }); |
| 33 | +
|
| 34 | + function loadMinimalTheme() { |
| 35 | + template = minimalTheme; |
| 36 | + } |
| 37 | +
|
| 38 | + function resetToDefault() { |
| 39 | + template = weirdTheme; |
| 40 | + } |
| 41 | +
|
| 42 | + async function setUserTheme(template?: string) { |
| 43 | + await fetch(`/${$page.params.username}/settings/setTheme`, { |
| 44 | + method: 'post', |
| 45 | + body: JSON.stringify({ |
| 46 | + template |
| 47 | + }), |
| 48 | + headers: [['content-type', 'application/json']] |
| 49 | + }); |
| 50 | +
|
| 51 | + window.location.reload(); |
| 52 | + } |
| 53 | +
|
| 54 | + async function save() { |
| 55 | + await setUserTheme(template === weirdTheme ? undefined : template); |
| 56 | + } |
| 57 | +</script> |
| 58 | + |
| 59 | +<main class="flex w-full flex-col"> |
| 60 | + <div class="mx-8 flex flex-row items-center justify-between gap-3 pt-4"> |
| 61 | + <span class="flex-grow"></span> |
| 62 | + <h1 class="text-center text-2xl font-bold">Experimental Theme Editor</h1> |
| 63 | + <span class="flex-grow"></span> |
| 64 | + <span class="flex gap-2"> |
| 65 | + <button class="variant-ghost btn" onclick={save}>Save</button> |
| 66 | + <button class="variant-ghost btn" onclick={resetToDefault}>Reset to Default</button> |
| 67 | + <button class="variant-ghost btn" onclick={loadMinimalTheme} |
| 68 | + >Load Minimal Example Theme</button |
| 69 | + > |
| 70 | + </span> |
| 71 | + </div> |
| 72 | + <div class="flex h-[80vh] w-full flex-grow flex-row items-stretch justify-stretch"> |
| 73 | + <div class="h-full w-full pr-2 pt-2"> |
| 74 | + <h2 class="mb-2 text-center text-xl font-bold">HTML Template</h2> |
| 75 | + <div class="max-h-full overflow-y-scroll"> |
| 76 | + <TemplateEditor bind:content={template} class="max-h-full" /> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + <div class="h-full w-full px-2 pt-8"> |
| 80 | + <iframe |
| 81 | + title="Page Preview" |
| 82 | + bind:this={renderMount} |
| 83 | + height="100%" |
| 84 | + width="100%" |
| 85 | + style="border: 3px solid white; border-radius: 1em;" |
| 86 | + class="bg-white shadow-md" |
| 87 | + ></iframe> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | +</main> |
0 commit comments