-
Notifications
You must be signed in to change notification settings - Fork 7
/
texthighlighter.js
157 lines (129 loc) · 5.33 KB
/
texthighlighter.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* *******************************************************
By Walter Staeblein - 2023
MIT Licence - https://choosealicense.com/licenses/mit/
https://github.com/wstaeblein/highlightTextarea
*******************************************************/
class textHighlight {
constructor(ele) {
if (ele.tagName != 'TEXTAREA') { throw 'The element must be a textarea'; }
let styles = window.getComputedStyle(ele);
this.origBkgColor = styles.backgroundColor;
this.ele = ele;
this.searchArg = '';
this.sensitive = false;
this.sel = -1;
this.handlers = {
input: this.#inputHandler.bind(this),
scroll: this.#scrollHandler.bind(this)
}
this.ele.classList.add('hlta-textarea');
this.ele.addEventListener('input', this.handlers.input);
this.ele.addEventListener('scroll', this.handlers.scroll);
let nodeCont = document.createElement('div');
nodeCont.classList.add('hlta-container');
this.container = nodeCont;
let nodeBack = document.createElement('div');
nodeBack.classList.add('hlta-backdrop');
this.backdrop = nodeBack;
let nodeHilite = document.createElement('div');
nodeHilite.classList.add('hlta-highlight');
this.hilite = nodeHilite;
this.ele.parentNode.insertBefore(nodeCont, this.ele.nextSibling);
this.backdrop.append(this.hilite);
this.container.append(this.backdrop);
this.container.appendChild(this.ele);
let observer = new ResizeObserver(this.#resizeObs.bind(this));
observer.observe(this.ele);
this.#inputHandler();
}
search(arg, sensitive, word) {
this.searchArg = arg;
this.sensitive = !!sensitive;
this.word = !!word;
this.#inputHandler();
this.sel = -1;
this.next();
}
next() {
this.sel +=1;
this.#setSelection(true);
}
prev() {
this.sel -=1;
this.#setSelection(true);
}
count() {
let eles = this.hilite.querySelectorAll('mark');
return eles.length;
}
clear() {
this.searchArg = '';
this.hilite.innerHTML = this.hilite.textContent;
this.sel = -1;
}
destroy() {
this.ele.removeEventListener('input', this.handlers.input);
this.ele.removeEventListener('scroll', this.handlers.scroll);
observer.disconnect();
this.container.parentNode.insertBefore(this.ele, this.container);
while (this.container.firstChild) {
this.container.removeChild(this.container.lastChild);
}
this.container.remove();
}
#resizeObs() {
let styles = window.getComputedStyle(this.ele);
let width = this.ele.scrollWidth;
let height = this.ele.offsetHeight;
let css = `width: ${width}px; height: ${height}px; margin: ${styles.marginTop} ${styles.marginRight} ${styles.marginBottom} ${styles.marginLeft};
background-color: ${this.origBkgColor}`;
this.backdrop.style.cssText = css;
this.#copyStyles(this.ele, this.hilite, ['width', 'paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom', 'borderTop', 'letterSpacing',
'borderLeft', 'borderRight', 'borderBottom', 'fontFamily', 'fontSize', 'fontWeight', 'lineHeight']);
this.hilite.style.minHeight = styles.height;
this.hilite.style.whiteSpace = 'pre-wrap';
}
#inputHandler() {
this.hilite.innerHTML = this.#markText();
this.#scrollHandler();
if (this.sel > -1) { this.#setSelection(); }
}
#copyStyles(src, dest, styles2Copy) {
let styles = window.getComputedStyle(src);
styles2Copy.forEach((stl) => dest.style[stl] = styles[stl])
}
#markText() {
let txt = this.ele.value.replace(/</g, '<').replace(/>/g, '>');
if (this.searchArg) { console.log(this.ele.value)
let searchArg = this.searchArg.replace(/</g, '<').replace(/>/g, '>');
let boundary = this.word ? '\\b' : '';
let re = new RegExp(boundary + '(' + this.#escapeString(searchArg) + ')' + boundary, 'g' + (this.sensitive ? '' : 'i'));
return txt.replace(re, '<mark>$&</mark>');
} else {
return txt;
}
}
#escapeString(txt) {
let specials = ['-', '[', ']', '/', '{', '}', '(', ')', '*', '+', '?', '.', '\\', '^', '$', '|'];
return txt.replace(RegExp('[' + specials.join('\\') + ']', 'g'), '\\$&');
}
#scrollHandler() {
this.backdrop.scrollTop = this.ele.scrollTop || 0;
let sclLeft = this.ele.scrollLeft;
this.backdrop.style.transform = (sclLeft > 0) ? 'translateX(' + -sclLeft + 'px)' : '';
}
#setSelection(scroll) {
let eles = this.hilite.querySelectorAll('mark');
let len = eles.length;
if (this.sel >= len) { this.sel = 0; }
if (this.sel < 0) { this.sel = len - 1; }
for (let i = 0; i < len; ++i) {
if (i == this.sel) {
eles[i].classList.add('sel');
if (scroll) { this.ele.scrollTop = eles[i].offsetTop > 10 ? eles[i].offsetTop - 10 : eles[i].offsetTop; }
} else {
eles[i].classList.remove('sel');
}
}
}
}