|
| 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">URL Encode/Decode</h1> |
| 11 | + <p class="mt-3 text-base-content/70">Edit either side and the other updates automatically.</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">Plain text</span> |
| 17 | + </div> |
| 18 | + <textarea id="plain-text" class="textarea textarea-bordered min-h-40 w-full" placeholder="Type plain text"></textarea> |
| 19 | + </label> |
| 20 | +
|
| 21 | + <label class="form-control w-full"> |
| 22 | + <div class="label"> |
| 23 | + <span class="label-text">URL-encoded text</span> |
| 24 | + </div> |
| 25 | + <textarea id="encoded-text" class="textarea textarea-bordered min-h-40 w-full" placeholder="Type URL-encoded text"></textarea> |
| 26 | + </label> |
| 27 | + </form> |
| 28 | + </section> |
| 29 | + </section> |
| 30 | +</main> |
| 31 | +` |
| 32 | + |
| 33 | +const plainText = document.querySelector<HTMLTextAreaElement>('#plain-text') |
| 34 | +const encodedText = document.querySelector<HTMLTextAreaElement>('#encoded-text') |
| 35 | +let isSyncing = false |
| 36 | + |
| 37 | +const syncFromPlain = () => { |
| 38 | + if (!plainText || !encodedText || isSyncing) { |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + isSyncing = true |
| 43 | + encodedText.value = encodeURIComponent(plainText.value) |
| 44 | + isSyncing = false |
| 45 | +} |
| 46 | + |
| 47 | +const syncFromEncoded = () => { |
| 48 | + if (!plainText || !encodedText || isSyncing) { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + isSyncing = true |
| 53 | + |
| 54 | + try { |
| 55 | + plainText.value = decodeURIComponent(encodedText.value) |
| 56 | + } catch { |
| 57 | + plainText.value = encodedText.value |
| 58 | + } |
| 59 | + |
| 60 | + isSyncing = false |
| 61 | +} |
| 62 | + |
| 63 | +plainText?.addEventListener('input', syncFromPlain) |
| 64 | +encodedText?.addEventListener('input', syncFromEncoded) |
0 commit comments