|
| 1 | +<script setup lang="ts"> |
| 2 | +/** |
| 3 | + * Quick transliteration widget — compact inline island for blog posts, |
| 4 | + * doc pages, etc. Picks a system from a small curated list and shows |
| 5 | + * the result below the input. |
| 6 | + * |
| 7 | + * Loading interscript-ts dynamically keeps the page bundle small. |
| 8 | + */ |
| 9 | +import { ref, computed, onMounted } from "vue" |
| 10 | +
|
| 11 | +interface Props { |
| 12 | + /** Optional initial system code. */ |
| 13 | + defaultSystem?: string |
| 14 | + /** Optional initial input text. */ |
| 15 | + defaultInput?: string |
| 16 | + /** Compact layout (no header). */ |
| 17 | + compact?: boolean |
| 18 | +} |
| 19 | +
|
| 20 | +const props = withDefaults(defineProps<Props>(), { |
| 21 | + defaultSystem: "bgnpcgn-ukr-Cyrl-Latn-2019", |
| 22 | + defaultInput: "Антон", |
| 23 | + compact: false, |
| 24 | +}) |
| 25 | +
|
| 26 | +const systems = [ |
| 27 | + { code: "bgnpcgn-ukr-Cyrl-Latn-2019", label: "BGN/PCGN Ukrainian (2019)" }, |
| 28 | + { code: "bgnpcgn-deu-Latn-Latn-2000", label: "BGN/PCGN German (2000)" }, |
| 29 | + { code: "odni-rus-Cyrl-Latn-2015", label: "ODNI Russian (2015)" }, |
| 30 | + { code: "alalc-amh-Ethi-Latn-2011", label: "ALA-LC Amharic (2011)" }, |
| 31 | + { code: "un-tam-Taml-Latn-1972", label: "UN Tamil (1972)" }, |
| 32 | +] |
| 33 | +
|
| 34 | +const selected = ref(props.defaultSystem) |
| 35 | +const input = ref(props.defaultInput) |
| 36 | +const ready = ref(false) |
| 37 | +let transliterateFn: ((code: string, input: string) => string) | null = null |
| 38 | +
|
| 39 | +async function ensureEngine() { |
| 40 | + if (transliterateFn) return |
| 41 | + try { |
| 42 | + const mod = await import("interscript-ts") |
| 43 | + const modules = import.meta.glob("/maps/*.json", { eager: true, as: "raw" }) |
| 44 | + const maps: Record<string, unknown> = {} |
| 45 | + for (const [path, raw] of Object.entries(modules)) { |
| 46 | + const code = path.match(/\/maps\/(.+)\.json$/)?.[1] |
| 47 | + if (code) maps[code] = JSON.parse(raw as string) |
| 48 | + } |
| 49 | + mod.reset() |
| 50 | + mod.configure({ strategies: [mod.bundledStrategy(maps)] }) |
| 51 | + transliterateFn = mod.transliterate |
| 52 | + ready.value = true |
| 53 | + } catch (e) { |
| 54 | + console.error("interscript-ts load failed:", e) |
| 55 | + } |
| 56 | +} |
| 57 | +
|
| 58 | +const output = computed(() => { |
| 59 | + if (!transliterateFn) return "" |
| 60 | + try { |
| 61 | + return transliterateFn(selected.value, input.value) |
| 62 | + } catch { |
| 63 | + return "" |
| 64 | + } |
| 65 | +}) |
| 66 | +
|
| 67 | +onMounted(ensureEngine) |
| 68 | +</script> |
| 69 | + |
| 70 | +<template> |
| 71 | + <div class="quick" :class="{ compact }"> |
| 72 | + <div v-if="!compact" class="header"> |
| 73 | + <label for="qs">Quick transliteration</label> |
| 74 | + </div> |
| 75 | + <select id="qs" v-model="selected" class="select"> |
| 76 | + <option v-for="s in systems" :key="s.code" :value="s.code">{{ s.label }}</option> |
| 77 | + </select> |
| 78 | + <input |
| 79 | + v-model="input" |
| 80 | + type="text" |
| 81 | + class="input" |
| 82 | + placeholder="Type text…" |
| 83 | + :disabled="!ready" |
| 84 | + /> |
| 85 | + <div class="output" :class="{ placeholder: !output }"> |
| 86 | + {{ output || (ready ? '—' : 'Loading…') }} |
| 87 | + </div> |
| 88 | + </div> |
| 89 | +</template> |
| 90 | + |
| 91 | +<style scoped> |
| 92 | +.quick { |
| 93 | + border: 1px solid var(--border, #ddd); |
| 94 | + border-radius: 6px; |
| 95 | + padding: 0.75rem; |
| 96 | + background: var(--surface, #f8f8f8); |
| 97 | + display: grid; |
| 98 | + grid-template-columns: 1fr; |
| 99 | + gap: 0.4rem; |
| 100 | +} |
| 101 | +.quick.compact { |
| 102 | + padding: 0.5rem; |
| 103 | +} |
| 104 | +.header { |
| 105 | + font-size: 0.85rem; |
| 106 | + color: var(--muted, #666); |
| 107 | + text-transform: uppercase; |
| 108 | + letter-spacing: 0.05em; |
| 109 | +} |
| 110 | +.select, |
| 111 | +.input { |
| 112 | + padding: 0.4rem 0.5rem; |
| 113 | + border: 1px solid var(--border, #ddd); |
| 114 | + border-radius: 4px; |
| 115 | + background: white; |
| 116 | + font-family: inherit; |
| 117 | + font-size: 0.95rem; |
| 118 | +} |
| 119 | +.output { |
| 120 | + padding: 0.4rem 0.5rem; |
| 121 | + font-family: monospace; |
| 122 | + background: white; |
| 123 | + border: 1px solid var(--border, #ddd); |
| 124 | + border-radius: 4px; |
| 125 | + min-height: 1.8rem; |
| 126 | +} |
| 127 | +.output.placeholder { |
| 128 | + color: var(--muted, #999); |
| 129 | +} |
| 130 | +</style> |
0 commit comments