-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
68 lines (57 loc) · 1.67 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// deno-lint-ignore no-explicit-any
declare const CSS: { highlights: any }
const $p = document.querySelector("p")!
function* walk(walker: TreeWalker) {
while (walker.nextNode()) {
yield walker.currentNode
}
}
const parse = () => {
const walker = document.createTreeWalker(
$p,
NodeFilter.SHOW_TEXT,
)
const ranges = walk(walker).flatMap(el => el.textContent!
.matchAll(/[AEIOUaeiou]+/g)
.map(({ index, [0]: str }) => {
const range = new Range()
range.setStart(el, index)
range.setEnd(el, index + str.length)
return range
})
)
const highlight = new Highlight(...ranges)
CSS.highlights.set("hi1", highlight)
}
parse()
$p.addEventListener("input", parse)
let prevRange: Range | null = null
$p.addEventListener("input", _e => {
const currRange = getSelection()!.getRangeAt(0)
if (prevRange?.startContainer == currRange.startContainer) {
const node = currRange.startContainer
const range = new Range()
range.setStart(node, prevRange.startOffset)
range.setEnd(node, currRange.endOffset)
if (CSS.highlights.has("hi2")) {
const highlight = CSS.highlights.get("hi2")
highlight.add(range)
} else {
const highlight = new Highlight(range)
CSS.highlights.set("hi2", highlight)
}
}
})
document.addEventListener("selectionchange", _e => {
prevRange = getSelection()!.getRangeAt(0)
})
document.styleSheets[0].insertRule(`
::highlight(hi1) {
color: #a55;
}
`)
document.styleSheets[0].insertRule(`
::highlight(hi2) {
background: #dfd;
}
`)