|
| 1 | +<script setup lang="ts"> |
| 2 | +import unicodeNames from '@unicode/unicode-15.1.0/Names/index.js'; |
| 3 | +import unicodeCategories from '@unicode/unicode-15.1.0/General_Category'; |
| 4 | +import utf8 from 'utf8'; |
| 5 | +
|
| 6 | +import { useFuzzySearch } from '@/composable/fuzzySearch'; |
| 7 | +import useDebouncedRef from '@/composable/debouncedref'; |
| 8 | +
|
| 9 | +function toPaddedHex(num: number) { |
| 10 | + return num.toString(16).padStart(4, '0').toUpperCase(); |
| 11 | +} |
| 12 | +
|
| 13 | +function toUTF8(codePoint: number) { |
| 14 | + const utf8String = utf8.encode(String.fromCodePoint(codePoint)); |
| 15 | + return [...utf8String].map(c => `\\x${c.codePointAt(0)?.toString(16).toUpperCase()}`); |
| 16 | +} |
| 17 | +
|
| 18 | +const searchQuery = useDebouncedRef('', 500); |
| 19 | +const parsedSearchQuery = computed(() => { |
| 20 | + const parsedRegex = /^\s*(?:\&#x(?<hex1>[\da-f]+);|\&#(?<dec>\d+);|(?:U\+|\\u)?\s*(?<hex2>[\da-f]+))\s*$/gi; // NOSONAR |
| 21 | + const parsedQuery = parsedRegex.exec(searchQuery.value); |
| 22 | + if (parsedQuery) { |
| 23 | + if (parsedQuery.groups?.hex1 || parsedQuery.groups?.hex2) { |
| 24 | + return `=${toPaddedHex(Number.parseInt(parsedQuery.groups.hex1 || parsedQuery.groups.hex2, 16))}`; |
| 25 | + } |
| 26 | + if (parsedQuery.groups?.dec) { |
| 27 | + return `=${toPaddedHex(Number.parseInt(parsedQuery.groups.dec, 10))}`; |
| 28 | + } |
| 29 | + } |
| 30 | + return searchQuery.value; |
| 31 | +}); |
| 32 | +
|
| 33 | +const unicodeSearchData = [...unicodeNames].map(([codePoint, characterName]) => { |
| 34 | + const hex = toPaddedHex(codePoint); |
| 35 | + return { |
| 36 | + codePoint, |
| 37 | + characterName: `${characterName} (U+${hex})`, |
| 38 | + hex, |
| 39 | + }; |
| 40 | +}); |
| 41 | +
|
| 42 | +const { searchResult } = useFuzzySearch({ |
| 43 | + search: parsedSearchQuery, |
| 44 | + data: unicodeSearchData, |
| 45 | + options: { |
| 46 | + keys: ['characterName', 'hex'], |
| 47 | + threshold: 0.2, |
| 48 | + isCaseSensitive: false, |
| 49 | + minMatchCharLength: 3, |
| 50 | + useExtendedSearch: true, |
| 51 | + }, |
| 52 | +}); |
| 53 | +</script> |
| 54 | + |
| 55 | +<template> |
| 56 | + <div mx-auto max-w-2400px important:flex-1> |
| 57 | + <div flex items-center gap-3> |
| 58 | + <c-input-text |
| 59 | + v-model:value="searchQuery" |
| 60 | + placeholder="Search Unicode by name (e.g. 'zero width') or code point..." |
| 61 | + mx-auto max-w-600px |
| 62 | + > |
| 63 | + <template #prefix> |
| 64 | + <icon-mdi-search mr-6px color-black op-70 dark:color-white /> |
| 65 | + </template> |
| 66 | + </c-input-text> |
| 67 | + </div> |
| 68 | + |
| 69 | + <div v-if="searchQuery.trim().length > 0"> |
| 70 | + <div |
| 71 | + v-if="searchResult.length === 0" |
| 72 | + mt-4 |
| 73 | + text-20px |
| 74 | + font-bold |
| 75 | + > |
| 76 | + No results |
| 77 | + </div> |
| 78 | + |
| 79 | + <div v-else> |
| 80 | + <div mt-4 text-20px font-bold> |
| 81 | + Search result |
| 82 | + </div> |
| 83 | + |
| 84 | + <n-table> |
| 85 | + <thead> |
| 86 | + <th>UCOD</th> |
| 87 | + <th>Display/UTF8</th> |
| 88 | + <th style="width:30%"> |
| 89 | + Category |
| 90 | + </th> |
| 91 | + <th>Html</th> |
| 92 | + <th style="width:30%"> |
| 93 | + Name |
| 94 | + </th> |
| 95 | + </thead> |
| 96 | + <tbody> |
| 97 | + <tr v-for="(result, ix) in searchResult" :key="ix"> |
| 98 | + <td> |
| 99 | + <input-copyable :value="`U+${toPaddedHex(result.codePoint)}`" mb-1 /> |
| 100 | + <!-- //NOSONAR --><n-a :href="`https://unicodeplus.com/U+${toPaddedHex(result.codePoint)}`" target="_blank"> |
| 101 | + > More info |
| 102 | + </n-a> |
| 103 | + </td> |
| 104 | + <td> |
| 105 | + <input-copyable :value="String.fromCodePoint(result.codePoint)" mb-1 /> |
| 106 | + <input-copyable :value="toUTF8(result.codePoint)" /> |
| 107 | + </td> |
| 108 | + <td> |
| 109 | + <input-copyable :value="unicodeCategories.get(result.codePoint)" /> |
| 110 | + </td> |
| 111 | + <td> |
| 112 | + <input-copyable :value="`\&\#x${toPaddedHex(result.codePoint)};`" mb-1 /> |
| 113 | + <input-copyable :value="`\&\#${result.codePoint};`" /> |
| 114 | + </td> |
| 115 | + <td><input-copyable :value="result.characterName" /></td> |
| 116 | + </tr> |
| 117 | + </tbody> |
| 118 | + </n-table> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | +</template> |
0 commit comments