-
Notifications
You must be signed in to change notification settings - Fork 0
/
eplvr.js
245 lines (206 loc) · 6.51 KB
/
eplvr.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
const isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
const isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);
const copy = document.querySelector("#copy");
const clear = document.querySelector("#clear");
const spellcheck = document.querySelector("#spellcheck");
const textarea = document.querySelector("#myBtaan");
const highlights = document.querySelector("#highlights");
const length = document.querySelector("#length");
const output = document.querySelector("output");
const list = document.querySelector("#list");
const template = document.querySelector("template");
let hiddens = [];
let duplicates = {};
// Text analysis
const count = (text, minLength) => {
// https://mathiasbynens.be/notes/es-unicode-property-escapes#word
const wordRegex = isChrome || isSafari
? new RegExp(`([\\p{Alphabetic}\\p{Mark}\\p{Decimal_Number}\\p{Connector_Punctuation}\\p{Join_Control}]+){${minLength},}`, 'gu')
: new RegExp(`(\\w+){${minLength},}`, 'g');
let result;
const words = {};
while ((result = wordRegex.exec(text)) !== null) {
const word = result[0].toLowerCase();
if (!words[word]) {
words[word] = [];
}
words[word].push(result.index);
words[word].push(result.index + word.length);
}
return words;
};
// Array manipulation
const duplicatePass = ([key, value]) => 2 < value.length;
const occurrenceComparator = ([keyA, valueA], [keyB, valueB]) =>
valueB.length - valueA.length;
const numberComparator = (numberA, numberB) => numberA - numberB;
const differ = (object, [key, value], index) => {
object[key] = {
action: object[key] ? "KEEP" : "ADD",
word: key,
occurrences: value,
index
};
return object;
};
const cleaner = (object, [word, details]) => {
if (details.action) {
delete details.action;
object[word] = details;
}
return object;
};
const flattener = (array, details) => [...array, ...details.occurrences];
// DOM manipulation
const update = (item, details) => {
item.style.zIndex = details.occurrences.length / 2;
item.style.top = `${0.6 + details.index * 3}rem`;
item.querySelector(".count").textContent = details.occurrences.length / 2;
};
const add = (word, details) => {
const node = document.importNode(template.content, true);
list.appendChild(node);
const item = list.lastElementChild;
item.id = `word-${word}`;
const hidden = item.querySelector(".hidden");
hidden.name = word;
hidden.checked = hiddens.includes(word);
hidden.addEventListener("change", handleToggle);
item.querySelector(".word").textContent = word;
update(item, details);
};
const keep = (word, details) => {
const item = document.querySelector(`#word-${word}`);
update(item, details);
};
const markObsolete = word => {
document.querySelector(`#word-${word}`).classList.add("obsolete");
};
const remove = item => item.remove();
const removeObsolete = () => {
document.querySelectorAll(".obsolete").forEach(remove);
};
const render = ([word, details]) => {
if (details.action === "ADD") {
add(word, details);
} else if (details.action === "KEEP") {
keep(word, details);
} else {
markObsolete(word);
}
};
const getHue = word => {
return (
360 -
360 / Object.keys(duplicates).length * duplicates[word.toLowerCase()].index
);
};
const getHSL = word => {
const hue = getHue(word);
return `hsl(${hue + 20}, 100%, ${180 < hue && hue < 360 ? 80 : 50}%)`;
};
const colorize = word => {
const color = getHSL(word);
const count = document.querySelector(`#word-${word} .count`);
const wordElement = document.querySelector(`#word-${word} .word`);
count.style.borderColor = color;
count.style.backgroundColor = color;
wordElement.style.borderColor = color;
wordElement.style.backgroundColor = color;
};
const highlight = (occurrence, index, occurrences) => {
let node;
if (index < occurrences.length - 1) {
if (index % 2) {
const word = textarea.value.substring(occurrence, occurrences[index + 1]);
node = document.createElement("mark");
node.textContent = word;
if (!hiddens.includes(word.toLowerCase())) {
const color = getHSL(word);
node.style.background = color;
node.style.border = `0.2rem solid ${color}`;
}
} else {
const text = textarea.value.substring(occurrence, occurrences[index + 1]);
node = document.createTextNode(text);
}
} else {
const text = textarea.value.substring(occurrence);
node = document.createTextNode(text + "\r\n\r\n");
}
highlights.appendChild(node);
};
const handleChange = () => {
// Count words
const words = count(textarea.value, length.value);
// Diff word stats with previous
const diff = Object.entries(words)
.filter(duplicatePass)
.sort(occurrenceComparator)
.reduce(differ, duplicates);
// Render items
Object.entries(diff).forEach(render);
// Clean obsolete from duplicates
duplicates = Object.entries(diff).reduce(cleaner, {});
// Adjust height of list
list.style.height = `${1.2 + Object.keys(duplicates).length * 3}rem`;
// Colorize items
Object.keys(duplicates).forEach(colorize);
// Flatten occurrences
const occurrences = Object.values(duplicates)
.reduce(flattener, [0])
.sort(numberComparator);
// Highlight
while (highlights.firstChild) {
highlights.removeChild(highlights.firstChild);
}
occurrences.forEach(highlight);
// Remove
window.setTimeout(removeObsolete, 400);
};
const handleToggle = event => {
const word = event.target.name;
if (event.target.checked) {
if (!hiddens.includes(word)) {
hiddens.push(word);
}
} else {
const index = hiddens.indexOf(word);
if (0 <= index) {
hiddens.splice(index, 1);
}
}
handleChange();
};
copy.addEventListener("click", () => {
textarea.select();
document.execCommand("copy");
});
clear.addEventListener("click", () => {
textarea.value = "";
textarea.focus();
handleChange();
});
spellcheck.addEventListener("change", event => {
textarea.setAttribute("spellcheck", event.target.checked.toString());
const { value } = textarea;
textarea.value = value + " ";
textarea.value = value;
});
textarea.addEventListener("scroll", event => {
highlights.scrollTop = event.target.scrollTop;
});
textarea.addEventListener(
"focus",
() => {
textarea.value = "";
handleChange();
},
{
once: true
}
);
textarea.addEventListener("input", handleChange);
length.addEventListener("input", handleChange);
length.addEventListener("input", () => (output.textContent = length.value));
handleChange();