-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellcheck-custom-highlight-sample-with-highlightsFromPoint.html
95 lines (90 loc) · 4.73 KB
/
spellcheck-custom-highlight-sample-with-highlightsFromPoint.html
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html>
<head>
<style>
div::highlight(spellcheck-highlight) {
/* background-color: rgba(255, 48, 0, 25); */
text-decoration-color: red;
text-decoration-style: wavy;
text-decoration-line: underline;
text-decoration-thickness: 0.5px;
}
div::highlight(active-spellcheck-highlight) {
background-color: rgba(255, 48, 0, 0.25);
}
div::highlight(grammar-highlight) {
text-decoration-color: blue;
text-decoration-style: double;
text-decoration-line: underline;
text-decoration-thickness: 0.3px;
}
div::highlight(active-grammar-highlight) {
background-color: rgba(0, 85, 204, 0.25);
}
</style>
</head>
<body>
<div contenteditable="true" spellcheck="false">ths is a spellchecker tst. this will produced a bnh of errrs.</div>
<script>
let listenForMouseMove = false;
let div = document.querySelector('div');
let textNode = div.firstChild;
let spellingRanges = [
new StaticRange({startContainer: textNode, startOffset: 0, endContainer: textNode, endOffset: 3}),
new StaticRange({startContainer: textNode, startOffset: 22, endContainer: textNode, endOffset: 25}),
new StaticRange({startContainer: textNode, startOffset: 48, endContainer: textNode, endOffset: 51}),
new StaticRange({startContainer: textNode, startOffset: 55, endContainer: textNode, endOffset: 60})
];
let spellingHighlight = new Highlight(...spellingRanges);
CSS.highlights.set('spellcheck-highlight', spellingHighlight);
let grammarRanges = [
new StaticRange({startContainer: textNode, startOffset: 37, endContainer: textNode, endOffset: 45})
];
let grammarHighlight = new Highlight(...grammarRanges);
CSS.highlights.set('grammar-highlight', grammarHighlight);
function createActiveHighlights(highlights) {
for (highlight of highlights) {
if (highlight.name === 'spellcheck-highlight') {
CSS.highlights.delete('active-spellcheck-highlight');
CSS.highlights.delete('active-grammar-highlight');
for (spellingRange of highlight.entries()) {
if (spellingRange.startOffset <= highlight.range.startOffset && highlight.range.endOffset <= spellingRange.endOffset) {
let activeSpellingHighlight = new Highlight(highlight.range);
CSS.highlights.set('active-spellcheck-highlight', activeSpellingHighlight);
break;
}
}
CSS.highlights.set('active-spellcheck-highlight', activeSpellingHighlight);
} else if (highlight.name === 'grammar-highlight') {
CSS.highlights.delete('active-spellcheck-highlight');
CSS.highlights.delete('active-grammar-highlight');
for (grammarRange of highlight.entries()) {
if (grammarRange.startOffset <= highlight.range.startOffset && highlight.range.endOffset <= grammarRange.endOffset) {
let activeGrammarHighlight = new Highlight(highlight.range);
CSS.highights.set('active-grammar-highlight', activeGrammarHighlight);
break;
}
}
CSS.highlights.set('active-grammar-highlight', activeGrammarHighlight);
}
}
}
div.addEventListener('mouseover', (event) => {
listenForMouseMove = true;
let highlights = CSS.highlights.highlightsFromPoint(event.clientX, event.clientY);
createActiveHighlights(highlights);
});
div.addEventListener('mousemove', (event) => {
if (listenForMouseMove) {
let highlights = CSS.highlights.highlightsFromPoint(event.clientX, event.clientY);
createActiveHighlights(highlights);
}
});
div.addEventListener('mouseout', (event) => {
listenForMouseMove = false;
CSS.highlights.delete('active-spellcheck-highlight');
CSS.highlights.delete('active-grammar-highlight');
});
</script>
</body>
</html>