Skip to content

Commit

Permalink
ucsur support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vap0r1ze committed Jan 9, 2023
1 parent 86aad68 commit a710417
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/data.ts
Original file line number Diff line number Diff line change
@@ -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
// }
6 changes: 6 additions & 0 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { isWord, words } from "data"
import { wordLookup } from "data"
import { clearWord, container, setWord, stylesheet } from "display"
import { getWordAtPoint } from "dom"

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) => {
Expand Down

0 comments on commit a710417

Please sign in to comment.