|
1 | 1 | <script setup lang="ts">
|
2 |
| -import { convertTextToUnicode, convertUnicodeToText } from './text-to-unicode.service'; |
| 2 | +import { type ConverterId, SKIP_PRINTABLE_ASCII_RE, converters } from './text-to-unicode.service'; |
3 | 3 | import { useCopy } from '@/composable/copy';
|
4 | 4 |
|
| 5 | +const converterId = ref<ConverterId>('fullUnicode'); |
| 6 | +const skipAscii = ref(true); |
| 7 | +
|
5 | 8 | const inputText = ref('');
|
6 |
| -const unicodeFromText = computed(() => inputText.value.trim() === '' ? '' : convertTextToUnicode(inputText.value)); |
| 9 | +const unicodeFromText = computed(() => |
| 10 | + inputText.value.trim() === '' |
| 11 | + ? '' |
| 12 | + : converters[converterId.value].escape(inputText.value, skipAscii.value ? SKIP_PRINTABLE_ASCII_RE : undefined), |
| 13 | +); |
7 | 14 | const { copy: copyUnicode } = useCopy({ source: unicodeFromText });
|
8 | 15 |
|
9 | 16 | const inputUnicode = ref('');
|
10 |
| -const textFromUnicode = computed(() => inputUnicode.value.trim() === '' ? '' : convertUnicodeToText(inputUnicode.value)); |
| 17 | +const textFromUnicode = computed(() => |
| 18 | + inputUnicode.value.trim() === '' ? '' : converters[converterId.value].unescape(inputUnicode.value), |
| 19 | +); |
11 | 20 | const { copy: copyText } = useCopy({ source: textFromUnicode });
|
12 | 21 | </script>
|
13 | 22 |
|
14 | 23 | <template>
|
15 |
| - <c-card title="Text to Unicode"> |
16 |
| - <c-input-text v-model:value="inputText" multiline placeholder="e.g. 'Hello Avengers'" label="Enter text to convert to unicode" autosize autofocus raw-text test-id="text-to-unicode-input" /> |
17 |
| - <c-input-text v-model:value="unicodeFromText" label="Unicode from your text" multiline raw-text readonly mt-2 placeholder="The unicode representation of your text will be here" test-id="text-to-unicode-output" /> |
18 |
| - <div mt-2 flex justify-center> |
19 |
| - <c-button :disabled="!unicodeFromText" @click="copyUnicode()"> |
20 |
| - Copy unicode to clipboard |
21 |
| - </c-button> |
22 |
| - </div> |
23 |
| - </c-card> |
24 |
| - |
25 |
| - <c-card title="Unicode to Text"> |
26 |
| - <c-input-text v-model:value="inputUnicode" multiline placeholder="Input Unicode" label="Enter unicode to convert to text" autosize raw-text test-id="unicode-to-text-input" /> |
27 |
| - <c-input-text v-model:value="textFromUnicode" label="Text from your Unicode" multiline raw-text readonly mt-2 placeholder="The text representation of your unicode will be here" test-id="unicode-to-text-output" /> |
28 |
| - <div mt-2 flex justify-center> |
29 |
| - <c-button :disabled="!textFromUnicode" @click="copyText()"> |
30 |
| - Copy text to clipboard |
31 |
| - </c-button> |
| 24 | + <div class="outer" flex flex-col gap-6> |
| 25 | + <div class="controls"> |
| 26 | + <c-select |
| 27 | + v-model:value="converterId" |
| 28 | + searchable |
| 29 | + label="Conversion type:" |
| 30 | + :options="Object.entries(converters).map(([key, val]) => ({ label: val.name, value: key }))" |
| 31 | + /> |
32 | 32 | </div>
|
33 |
| - </c-card> |
| 33 | + <c-card class="card" title="Text to Unicode"> |
| 34 | + <c-input-text |
| 35 | + v-model:value="inputText" |
| 36 | + multiline |
| 37 | + placeholder="e.g. 'Hello Avengers'" |
| 38 | + label="Enter text to convert to Unicode" |
| 39 | + autosize |
| 40 | + autofocus |
| 41 | + raw-text |
| 42 | + test-id="text-to-unicode-input" |
| 43 | + /> |
| 44 | + <c-input-text |
| 45 | + v-model:value="unicodeFromText" |
| 46 | + label="Unicode from your text" |
| 47 | + multiline |
| 48 | + raw-text |
| 49 | + readonly |
| 50 | + mt-2 |
| 51 | + placeholder="The unicode representation of your text will be here" |
| 52 | + test-id="text-to-unicode-output" |
| 53 | + /> |
| 54 | + <div mt-2 flex justify-start> |
| 55 | + <n-form-item label="Skip ASCII?" :show-feedback="false" label-placement="left"> |
| 56 | + <n-switch v-model:value="skipAscii" /> |
| 57 | + </n-form-item> |
| 58 | + </div> |
| 59 | + <div mt-2 flex justify-center> |
| 60 | + <c-button :disabled="!unicodeFromText" @click="copyUnicode()"> Copy unicode to clipboard </c-button> |
| 61 | + </div> |
| 62 | + </c-card> |
| 63 | + <c-card class="card" title="Unicode to Text"> |
| 64 | + <c-input-text |
| 65 | + v-model:value="inputUnicode" |
| 66 | + multiline |
| 67 | + placeholder="Input Unicode" |
| 68 | + label="Enter unicode to convert to text" |
| 69 | + autosize |
| 70 | + raw-text |
| 71 | + test-id="unicode-to-text-input" |
| 72 | + /> |
| 73 | + <c-input-text |
| 74 | + v-model:value="textFromUnicode" |
| 75 | + label="Text from your Unicode" |
| 76 | + multiline |
| 77 | + raw-text |
| 78 | + readonly |
| 79 | + mt-2 |
| 80 | + placeholder="The text representation of your unicode will be here" |
| 81 | + test-id="unicode-to-text-output" |
| 82 | + /> |
| 83 | + <div mt-2 flex justify-center> |
| 84 | + <c-button :disabled="!textFromUnicode" @click="copyText()"> Copy text to clipboard </c-button> |
| 85 | + </div> |
| 86 | + </c-card> |
| 87 | + </div> |
34 | 88 | </template>
|
| 89 | + |
| 90 | +<style lang="less" scoped> |
| 91 | +.outer { |
| 92 | + flex: 0 1 1200px; |
| 93 | + margin-inline: 50px; |
| 94 | + display: flex; |
| 95 | + flex-direction: row; |
| 96 | + flex-wrap: wrap; |
| 97 | +} |
| 98 | +
|
| 99 | +.controls { |
| 100 | + flex: 0 1 100%; |
| 101 | +} |
| 102 | +
|
| 103 | +.card { |
| 104 | + flex: 1 0 max(40%, 500px); |
| 105 | +} |
| 106 | +</style> |
0 commit comments