forked from samwho/symbol.wtf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.js
463 lines (405 loc) · 14.6 KB
/
symbols.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
const DISPLAY_BOX = "\u25A1";
// Sam Rose's original set of symbols, will be used as ultimate fallback
const symbols_default = ["©", "®", "™", "“", "”", "—", "—", "…", "½", "¼", "∞", "é", "á", "à", "ç", "€", "¥", "¢"];
const collections = [
"default",
"german",
"hebrew",
"numberset",
]
let symbols = []
let file_drop_zone = null;
let symbol_drop_zone = null;
async function getSymbols() {
try {
symbols = sanitise(JSON.parse(window.localStorage.getItem("symbols")));
if (symbols.length === 0) {
res = await fetch("./symbol/default.json");
symbols = sanitise(await res.json());
}
} catch(err) {
console.error(err);
}
if (symbols.length === 0) {
symbols = symbols_default
.map((s) => {
return {
glyph: s,
name: s
};
});
}
}
function sanitise(symbols) {
if (!Array.isArray(symbols)) {
return [];
}
return symbols.filter((s) =>
s !== null &&
typeof s === "object" &&
!Array.isArray(s) &&
s.hasOwnProperty("glyph") &&
s.hasOwnProperty("name")
);
}
function search(searchTerm) {
searchTerm = searchTerm?.toLowerCase() ?? "";
return symbols.filter((s) => {
/* Get hex representation of codepoint, e.g. 00A0 for or 20AC for € */
const codePoint = s.glyph.codePointAt(0)?.toString(16).padStart(4, 0);
const searchTerms = [
s.name,
s.glyph,
...s.searchTerms ?? [],
`U+${codePoint}`,
`0x${codePoint}`
];
return searchTerm === "" || searchTerms.join(" ").toLowerCase().includes(searchTerm);
});
}
function addSymbol() {
symbols.unshift({
glyph: "",
name: "",
// For now: no display and search terms
});
// Better to just add the one div for the symbol
// For now this is fast enough.
renderSymbols();
editSymbol(document.getElementsByClassName("symbol")[0], "glyph");
}
function editSymbol(elem, classname) {
const handleAction = (target) => {
symbols[target.dataset.index][target.dataset.classname] = target.value;
if (!symbols[target.dataset.index].name) {
symbols[target.dataset.index].name = symbols[target.dataset.index].glyph;
target.parentElement.parentElement.getElementsByClassName("name")[0].textContent = symbols[target.dataset.index].name;
}
target.parentElement.parentElement.title = symbols[target.dataset.index].name;
target.parentElement.textContent = target.value;
window.localStorage.setItem("symbols", JSON.stringify(symbols));
return true;
}
elem.classList.remove("clicked");
elemClass = elem.getElementsByClassName(classname)[0];
input = document.createElement("input");
input.dataset.classname = classname;
input.dataset.index = Array.from(elem.parentElement.children).indexOf(elem);
input.value = symbols[input.dataset.index][classname];
input.addEventListener("blur", (e) => handleAction(e.target))
input.addEventListener("keydown", (e) => {
switch (e.key) {
case "Enter":
handleAction(e.target);
break;
case "Escape":
e.target.parentElement.textContent =
symbols[e.target.dataset.index][e.target.dataset.classname];
break;
}
});
elemClass.innerHTML = "";
elemClass.appendChild(input);
input.focus();
}
function isNotEditingSymbol() {
return document
.getElementsByClassName("symbols")[0]
.getElementsByTagName("INPUT")
.length === 0;
}
function removeSymbol(elem) {
symbols.splice(Array.from(elem.parentElement.children).indexOf(elem), 1);
window.localStorage.setItem("symbols", JSON.stringify(symbols));
elem.remove();
}
function saveSymbols() {
const link = document.createElement("a");
const file = new Blob([JSON.stringify(symbols, null, "\t")], { type: "text/plain" });
link.href = URL.createObjectURL(file);
link.download = (document.querySelector('input[type="text"]').value || "symbols") + ".json";
link.click();
URL.revokeObjectURL(link.href);
}
function renderNoSymbols(parent) {
const span = document.createElement("span");
span.innerHTML = document.getElementById("no_symbols").innerHTML;
const collectionsElem = span.querySelector("#collections");
collections.forEach(c => {
const replace_elem = document.createElement("a");
replace_elem.textContent = "⭮";
replace_elem.href = "#";
replace_elem.title = "Replace";
replace_elem.addEventListener("click", async (e) => {
e.preventDefault();
res = await fetch("./symbol/"+c+".json");
symbols = sanitise(await res.json());
renderSymbols();
});
collectionsElem.appendChild(replace_elem);
collectionsElem.append
const insert_elem = document.createElement("a");
insert_elem.textContent = "⮡";
insert_elem.href = "#";
insert_elem.title = "Append";
replace_elem.addEventListener("click", async (e) => {
e.preventDefault();
openElement(file_drop_zone);
res = await fetch("./symbol/"+c+".json");
symbols.push(...sanitise(await res.json()));
renderSymbols();
})
collectionsElem.appendChild(insert_elem);
const name_elem = document.createElement("a");
name_elem.textContent = c;
name_elem.href = "./symbol/"+c+".json";
collectionsElem.appendChild(name_elem);
});
parent.appendChild(span);
}
function renderSymbols(searchTerm) {
const parent = document.querySelector(".symbols");
parent.innerHTML = "";
const results = search(searchTerm);
if (results.length === 0) {
renderNoSymbols(parent);
return;
}
for (const symbol of results) {
const elem = document.createElement("div");
const glyphElem = document.createElement("div");
const nameElem = document.createElement("div");
const copyElem = document.createElement("div");
const removeElem = document.createElement("div");
elem.classList = "symbol";
elem.tabIndex = 0;
elem.title = symbol.name;
elem.setAttribute("draggable", "true");
glyphElem.classList = "glyph";
glyphElem.textContent = symbol.display || symbol.glyph;
nameElem.classList = "name";
nameElem.textContent = symbol.name;
copyElem.classList = "copy";
copyElem.innerHTML = document.getElementById("copied").innerHTML;
removeElem.classList = "remove";
elem.appendChild(glyphElem);
elem.appendChild(nameElem);
elem.appendChild(copyElem);
elem.appendChild(removeElem);
const handleAction = () => {
if (elem.classList.contains("clicked") || !isNotEditingSymbol()) {
return;
}
navigator.clipboard.writeText(symbol.glyph);
console.log(`Copied ${symbol.name} (${symbol.glyph})!`);
elem.classList.add("clicked");
setTimeout(() => {
elem.classList.remove("clicked");
}, 1000);
};
elem.addEventListener("click", handleAction);
elem.addEventListener("keydown", (event) => {
if (isNotEditingSymbol()) {
switch (event.key) {
case " ":
case "Enter":
event.preventDefault();
handleAction();
break;
case "Delete":
removeSymbol(event.target);
break;
}
}
});
parent.appendChild(elem);
}
}
function openElement(elem) {
elem.classList.add("open");
elem.dataset.open_counter |= 0;
elem.dataset.open_counter++;
window.setTimeout((elem) => {
elem.dataset.open_counter--;
if (+elem.dataset.open_counter === 0) {
elem.classList.remove("open");
}
}, 5000, elem);
}
function fileHandler(file) {
if (file.type === "application/json") {
const dataset = file_drop_zone.dataset;
+dataset.todo++;
const reader = new FileReader()
reader.readAsText(file)
reader.onloadend = () => {
+dataset.todo--;
try {
const content = sanitise(JSON.parse(reader.result));
if (content.length) {
if (
!+dataset.uploads &&
!document
.getElementById("save_symbols")
.classList
.contains("open")
) {
symbols = [];
}
openElement(file_drop_zone);
+dataset.uploads++;
symbols.push(...content);
}
} catch(err) {
console.error(err);
}
if (!+dataset.todo) {
console.log("Number of files loaded:", dataset.uploads);
window.localStorage.setItem("symbols", JSON.stringify(symbols));
renderSymbols();
}
}
}
}
function dragOverHandler(ev) {
ev.preventDefault();
ev.target.classList.add("over");
}
function dragLeaveHandler(ev) {
ev.preventDefault();
ev.target.classList.remove("over");
}
function dropHandler(ev) {
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
ev.target.classList.remove("over");
file_drop_zone.dataset.uploads = 0;
file_drop_zone.dataset.todo = 0;
if (ev.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
[...ev.dataTransfer.items].forEach((item, i) => {
// If dropped items aren't files, reject them
if (item.kind === "file") {
fileHandler(item.getAsFile());
}
});
} else {
// Use DataTransfer interface to access the file(s)
[...ev.dataTransfer.files].forEach((file) => {
fileHandler(file);
});
}
}
function handleDragStart(e) {
e.target.classList.add("drag");
symbol_drop_zone.dataset.dragIndex =
Array.from(e.target.parentElement.children).indexOf(e.target);
}
function handleDragEnd(e) {
e.target.classList.remove("drag");
Array.from(e.target.parentElement.children).forEach(elem => elem.classList.remove("over"));
}
function handleDragOver(e) {
e.preventDefault();
return false;
}
function handleDragEnter(e) {
Array.from(document.getElementsByClassName("over"))
.forEach(elem => elem.classList.remove("over"));
let target = e.target;
while (target) {
if (target.classList?.contains("symbol")) {
target.classList.add("over");
break;
}
target = target.parentElement
}
}
function handleDrop(e) {
e.preventDefault();
let target = e.target;
while (target) {
if (target.classList?.contains("symbol")) {
const parentElem = target.parentElement;
const dragIndex = parseInt(parentElem.dataset.dragIndex);
const dragTarget = Array.from(parentElem.children).indexOf(target);
if (!(dragIndex >= 0)) {
return false;
}
symbols.splice(dragTarget, 0, symbols.splice(dragIndex, 1)[0]);
window.localStorage.setItem("symbols", JSON.stringify(symbols));
if (dragIndex < dragTarget) {
if (target.nextElementSibling) {
target.nextElementSibling.before(
parentElem.children[dragIndex]
);
} else {
parentElem.appendChild(parentElem.children[dragIndex]);
}
} else {
target.before(parentElem.children[dragIndex]);
}
break;
}
target = target.parentElement;
}
e.stopPropagation();
return false;
}
document.addEventListener("DOMContentLoaded", async () => {
await getSymbols();
file_drop_zone = document.getElementById("save_symbols");
symbol_drop_zone = document.getElementsByClassName("symbols")[0];
const search = window.location.hash ? window.location.hash.substring(1) : "";
renderSymbols(search);
const searchInput = document.querySelector(".search input");
searchInput.value = search;
searchInput.addEventListener("input", (e) => {
renderSymbols(e.target.value);
});
searchInput.addEventListener("blur", (e) => {
window.location.hash = e.target.value;
return false;
});
window.addEventListener("hashchange", () => {
const search = window.location.hash ? window.location.hash.substring(1) : "";
searchInput.value = search;
renderSymbols(search);
});
window.addEventListener("click", (e) => {
if (e.target.classList.contains("add")) {
e.preventDefault();
addSymbol();
}
});
window.addEventListener("dblclick", (e) => {
let target = e.target;
while(target) {
if (target.classList?.contains("remove")) {
return removeSymbol(target.parentElement);
}
if (target.classList?.contains("glyph")) {
return editSymbol(target.parentElement, "glyph");
}
if (target.classList?.contains("name")) {
return editSymbol(target.parentElement, "name");
}
if (target.classList?.contains("copy")) {
return editSymbol(target.parentElement, "name");
}
if (target.classList?.contains("symbol")) {
return editSymbol(target, "glyph");
}
target = target.parentElement;
}
});
file_drop_zone.addEventListener("click", () => saveSymbols());
file_drop_zone.addEventListener("drop", dropHandler);
file_drop_zone.addEventListener("dragover", dragOverHandler);
file_drop_zone.addEventListener("dragleave", dragLeaveHandler);
symbol_drop_zone.addEventListener("dragstart", handleDragStart);
symbol_drop_zone.addEventListener("dragover", handleDragOver);
symbol_drop_zone.addEventListener("dragenter", handleDragEnter);
symbol_drop_zone.addEventListener("dragend", handleDragEnd);
symbol_drop_zone.addEventListener("drop", handleDrop);
});