|
| 1 | +import './style.css' |
| 2 | +import { renderHeader } from './components/header.ts' |
| 3 | + |
| 4 | +document.querySelector<HTMLDivElement>('#app')!.innerHTML = ` |
| 5 | +<main class="min-h-screen bg-base-200" data-theme="light"> |
| 6 | + <section class="mx-auto flex max-w-5xl flex-col gap-8 px-4 py-8 md:px-8 md:py-12"> |
| 7 | + ${renderHeader()} |
| 8 | +
|
| 9 | + <section class="rounded-box border border-base-300 bg-base-100 p-8 shadow-sm"> |
| 10 | + <h1 class="text-3xl font-bold">Asciify</h1> |
| 11 | + <p class="mt-3 text-base-content/70">Type text below to convert it to ASCII in your browser.</p> |
| 12 | +
|
| 13 | + <form class="mt-6 flex flex-col gap-4" action="#" method="post" onsubmit="return false;"> |
| 14 | + <label class="form-control w-full"> |
| 15 | + <div class="label"> |
| 16 | + <span class="label-text">Input text</span> |
| 17 | + </div> |
| 18 | + <textarea id="input-text" class="textarea textarea-bordered min-h-40 w-full" placeholder="Paste or type text here"></textarea> |
| 19 | + </label> |
| 20 | +
|
| 21 | + <label class="form-control w-full"> |
| 22 | + <div class="label"> |
| 23 | + <span class="label-text">ASCII output</span> |
| 24 | + </div> |
| 25 | + <textarea id="output-text" class="textarea textarea-bordered min-h-40 w-full" readonly></textarea> |
| 26 | + </label> |
| 27 | + </form> |
| 28 | + </section> |
| 29 | + </section> |
| 30 | +</main> |
| 31 | +` |
| 32 | + |
| 33 | +const inputText = document.querySelector<HTMLTextAreaElement>('#input-text') |
| 34 | +const outputText = document.querySelector<HTMLTextAreaElement>('#output-text') |
| 35 | + |
| 36 | +type AnyAsciiFn = (value: string) => string |
| 37 | +let anyAsciiPromise: Promise<AnyAsciiFn> | undefined |
| 38 | + |
| 39 | +const loadAnyAscii = (): Promise<AnyAsciiFn> => { |
| 40 | + if (!anyAsciiPromise) { |
| 41 | + anyAsciiPromise = import('any-ascii').then((module) => module.default) |
| 42 | + } |
| 43 | + |
| 44 | + return anyAsciiPromise |
| 45 | +} |
| 46 | + |
| 47 | +const updateOutput = async () => { |
| 48 | + if (!inputText || !outputText) { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + const anyAscii = await loadAnyAscii() |
| 53 | + outputText.value = anyAscii(inputText.value) |
| 54 | +} |
| 55 | + |
| 56 | +inputText?.addEventListener('input', () => { |
| 57 | + void updateOutput() |
| 58 | +}) |
| 59 | +void updateOutput() |
0 commit comments