forked from lonekorean/highlight-within-textarea
-
Notifications
You must be signed in to change notification settings - Fork 4
/
highlight-within-textarea.js
331 lines (286 loc) · 9.83 KB
/
highlight-within-textarea.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* highlight-within-textarea
*
* @author Will Boyd
* @github https://github.com/lonekorean/highlight-within-textarea
*/
const HighlightWithinTextarea = (function() {
let ID = 'hwt';
let HighlightWithinTextarea = function(el, config) {
this.init(el, config);
};
HighlightWithinTextarea.prototype = {
init: function(el, config) {
this.el = el;
// backwards compatibility with v1 (deprecated)
if (this.getType(config) === 'function') {
config = { highlight: config };
}
if (this.getType(config) === 'custom') {
this.highlight = config;
this.generate();
} else {
console.error('valid config object not provided');
}
},
// returns identifier strings that aren't necessarily "real" JavaScript types
getType: function(instance) {
let type = typeof instance;
if (!instance) {
return 'falsey';
} else if (Array.isArray(instance)) {
if (instance.length === 2 && typeof instance[0] === 'number' && typeof instance[1] === 'number') {
return 'range';
} else {
return 'array';
}
} else if (type === 'object') {
if (instance instanceof RegExp) {
return 'regexp';
} else if (instance.hasOwnProperty('highlight')) {
return 'custom';
}
} else if (type === 'function' || type === 'string') {
return type;
}
return 'other';
},
generate: function() {
this.el.classList.add(ID + '-input', ID + '-content');
this.el.addEventListener('input', this.handleInput.bind(this));
this.el.addEventListener('scroll', this.handleScroll.bind(this));
this.highlights = document.createElement('div');
this.highlights.classList.add(ID + '-highlights', ID + '-content');
this.backdrop = document.createElement('div');
this.backdrop.classList.add(ID + '-backdrop');
this.backdrop.appendChild(this.highlights);
this.container = document.createElement('div');
this.container.classList.add(ID + '-container');
this.el.insertAdjacentElement('afterend', this.container);
this.container.appendChild(this.backdrop);
this.container.appendChild(this.el);
this.container.addEventListener('scroll', this.blockContainerScroll.bind(this));
this.browser = this.detectBrowser();
switch (this.browser) {
case 'firefox':
this.fixFirefox();
break;
case 'ios':
this.fixIOS();
break;
}
// plugin function checks this for success
this.isGenerated = true;
// trigger input event to highlight any existing input
this.handleInput();
},
// browser sniffing sucks, but there are browser-specific quirks to handle
// that are not a matter of feature detection
detectBrowser: function() {
let ua = window.navigator.userAgent.toLowerCase();
if (ua.indexOf('firefox') !== -1) {
return 'firefox';
} else if (!!ua.match(/msie|trident\/7|edge/)) {
return 'ie';
} else if (!!ua.match(/ipad|iphone|ipod/) && ua.indexOf('windows phone') === -1) {
// Windows Phone flags itself as "like iPhone", thus the extra check
return 'ios';
} else {
return 'other';
}
},
// Firefox doesn't show text that scrolls into the padding of a textarea, so
// rearrange a couple box models to make highlights behave the same way
fixFirefox: function() {
// take padding and border pixels from highlights div
const highlightsStyle = getComputedStyle(this.highlights);
let padding = ['padding-top', 'padding-right', 'padding-bottom', 'padding-left']
.map(p => highlightsStyle[p]);
let border = ['border-top-width', 'border-right-width', 'border-bottom-width', 'border-left-width']
.map(p => highlightsStyle[p]);
let marginProperties = ['marginTop', 'marginRight', 'marginBottom', 'marginLeft'];
this.highlights.style.padding = 0;
this.highlights.style.borderWidth = 0;
marginProperties.forEach((p, i) => {
this.backdrop.style[p] =
"calc(" + getComputedStyle(this.backdrop)[p] + " + " + padding[i] + " + " + border[i] + ")";
});
},
// iOS adds 3px of (unremovable) padding to the left and right of a textarea,
// so adjust highlights div to match
fixIOS: function() {
this.highlights.style['padding-left'] = "calc(" + getComputedStyle(this.highlights)['padding-left'] + " + 3px)";
this.highlights.style['padding-right'] = "calc(" + getComputedStyle(this.highlights)['padding-right'] + " + 3px)";
},
handleInput: function() {
let input = this.el.value;
let ranges = this.getRanges(input, this.highlight);
let unstaggeredRanges = this.removeStaggeredRanges(ranges);
let boundaries = this.getBoundaries(unstaggeredRanges);
this.renderMarks(boundaries);
},
getRanges: function(input, highlight) {
let type = this.getType(highlight);
switch (type) {
case 'array':
return this.getArrayRanges(input, highlight);
case 'function':
return this.getFunctionRanges(input, highlight);
case 'regexp':
return this.getRegExpRanges(input, highlight);
case 'string':
return this.getStringRanges(input, highlight);
case 'range':
return this.getRangeRanges(input, highlight);
case 'custom':
return this.getCustomRanges(input, highlight);
default:
if (!highlight) {
// do nothing for falsey values
return [];
} else {
console.error('unrecognized highlight type');
}
}
},
getArrayRanges: function(input, arr) {
let ranges = arr.map(this.getRanges.bind(this, input));
return Array.prototype.concat.apply([], ranges);
},
getFunctionRanges: function(input, func) {
return this.getRanges(input, func(input));
},
getRegExpRanges: function(input, regex) {
let ranges = [];
let match;
while (match = regex.exec(input), match !== null) {
ranges.push([match.index, match.index + match[0].length]);
if (!regex.global) {
// non-global regexes do not increase lastIndex, causing an infinite loop,
// but we can just break manually after the first match
break;
}
}
return ranges;
},
getStringRanges: function(input, str) {
let ranges = [];
let inputLower = input.toLowerCase();
let strLower = str.toLowerCase();
let index = 0;
while (index = inputLower.indexOf(strLower, index), index !== -1) {
ranges.push([index, index + strLower.length]);
index += strLower.length;
}
return ranges;
},
getRangeRanges: function(input, range) {
return [range];
},
getCustomRanges: function(input, custom) {
let ranges = this.getRanges(input, custom.highlight);
if (custom.className) {
ranges.forEach(function(range) {
// persist class name as a property of the array
if (range.className) {
range.className = custom.className + ' ' + range.className;
} else {
range.className = custom.className;
}
});
}
return ranges;
},
// prevent staggered overlaps (clean nesting is fine)
removeStaggeredRanges: function(ranges) {
let unstaggeredRanges = [];
ranges.forEach(function(range) {
let isStaggered = unstaggeredRanges.some(function(unstaggeredRange) {
let isStartInside = range[0] > unstaggeredRange[0] && range[0] < unstaggeredRange[1];
let isStopInside = range[1] > unstaggeredRange[0] && range[1] < unstaggeredRange[1];
return isStartInside !== isStopInside; // xor
});
if (!isStaggered) {
unstaggeredRanges.push(range);
}
});
return unstaggeredRanges;
},
getBoundaries: function(ranges) {
let boundaries = [];
ranges.forEach(function(range) {
boundaries.push({
type: 'start',
index: range[0],
className: range.className
});
boundaries.push({
type: 'stop',
index: range[1]
});
});
this.sortBoundaries(boundaries);
return boundaries;
},
sortBoundaries: function(boundaries) {
// backwards sort (since marks are inserted right to left)
boundaries.sort(function(a, b) {
if (a.index !== b.index) {
return b.index - a.index;
} else if (a.type === 'stop' && b.type === 'start') {
return 1;
} else if (a.type === 'start' && b.type === 'stop') {
return -1;
} else {
return 0;
}
});
},
renderMarks: function(boundaries) {
let input = this.el.value;
boundaries.forEach(function(boundary, index) {
let markup;
if (boundary.type === 'start') {
markup = '{{hwt-mark-start|' + index + '}}';
} else {
markup = '{{hwt-mark-stop}}';
}
input = input.slice(0, boundary.index) + markup + input.slice(boundary.index);
});
// this keeps scrolling aligned when input ends with a newline
input = input.replace(/\n({{hwt-mark-stop}})?$/, '\n\n$1');
// encode HTML entities
input = input.replace(/</g, '<').replace(/>/g, '>');
if (this.browser === 'ie') {
// IE/Edge wraps whitespace differently in a div vs textarea, this fixes it
input = input.replace(/ /g, ' <wbr>');
}
// replace start tokens with opening <mark> tags with class name
input = input.replace(/{{hwt-mark-start\|(\d+)}}/g, function(match, submatch) {
let className = boundaries[+submatch].className;
if (className) {
return '<mark class="' + className + '">';
} else {
return '<mark>';
}
});
// replace stop tokens with closing </mark> tags
input = input.replace(/{{hwt-mark-stop}}/g, '</mark>');
this.highlights.innerHTML = input;
},
handleScroll: function() {
this.backdrop.scrollTop = this.el.scrollTop;
// Chrome and Safari won't break long strings of spaces, which can cause
// horizontal scrolling, this compensates by shifting highlights by the
// horizontally scrolled amount to keep things aligned
let scrollLeft = this.el.scrollLeft;
this.backdrop.style.transform = (scrollLeft > 0) ? 'translateX(' + -scrollLeft + 'px)' : '';
},
// in Chrome, page up/down in the textarea will shift stuff within the
// container (despite the CSS), this immediately reverts the shift
blockContainerScroll: function() {
this.container.scrollLeft = 0;
},
};
return HighlightWithinTextarea;
})();