diff --git a/src/data.ts b/src/data.ts index e93e958..a072b70 100644 --- a/src/data.ts +++ b/src/data.ts @@ -1,14 +1,14 @@ import words from "~words" -export { words } export const wordLookup = new Map( Object.entries(words).map(([key, value]) => [key.toLowerCase(), value]) ) -export function isWord(word: string) { - return wordLookup.has(word.toLowerCase()) +for (const word of Object.values(words)) { + if (word.ucsur) + wordLookup.set( + // "U+F196C" -> 0xF196C -> "\uDB86\uDD6C" + String.fromCodePoint(parseInt(word.ucsur.slice(2), 16)), + word + ) } - -// for (const [word, data] of wordLookup.entries()) { -// data.ucsur -// } diff --git a/src/dom.ts b/src/dom.ts index ab3cccc..32ac4ff 100644 --- a/src/dom.ts +++ b/src/dom.ts @@ -12,6 +12,12 @@ export function rectContains(rect: Range | Element, x: number, y: number) { } export function getWord(text: string, index: number): string | null { + // UCSUR + const codePoint = text.codePointAt(index) || 0 + if (codePoint >= 0xf1900 && codePoint <= 0xf19ff) + return String.fromCodePoint(codePoint) + + // Alpha word const preIndex = text.slice(0, index).match(/[a-z]*$/i)![0] const postIndex = text.slice(index).match(/^[a-z]*/i)![0] return preIndex + postIndex || null diff --git a/src/index.ts b/src/index.ts index 06178f9..3bdf76d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { isWord, words } from "data" +import { wordLookup } from "data" import { clearWord, container, setWord, stylesheet } from "display" import { getWordAtPoint } from "dom" @@ -6,16 +6,18 @@ let prevWord: string | null = null window.addEventListener("dblclick", (evt) => { if (!(evt.target instanceof Node)) return - const word = getWordAtPoint(evt.target, evt.x, evt.y) + const wordText = getWordAtPoint(evt.target, evt.x, evt.y) + + if (!wordText || wordText === prevWord) return - if (!word || word === prevWord) return - if (!isWord(word)) return + const word = wordLookup.get(wordText) + if (!word) return document.getSelection()?.removeAllRanges() - if (word) setWord(words[word]) + if (wordText) setWord(word) else clearWord() - prevWord = word + prevWord = wordText }) window.addEventListener("click", (evt) => {