forked from braver/programmingfonts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
457 lines (395 loc) · 14.8 KB
/
index.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
/* global CodeMirror window document Set */
/* eslint-disable no-implicit-globals */
// CodeMirror
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
theme: 'pastel-on-dark',
lineWrapping: true
});
// CodeMirror theme selector
var input = document.getElementById('select-theme');
var font_data;
var filters = {
'style': false,
'rendering': false,
'liga': false,
'zerostyle': false,
'author': 'all',
'name': ''
};
function selectTheme() {
var theme = 'monokai';
if (input.selectedIndex > -1) {
theme = input.options[input.selectedIndex].innerHTML;
}
editor.setOption('theme', theme);
document.cookie = 'theme=' + theme + ';max-age=172800';
}
// ProgrammingFonts font selector
function selectFont() {
var font = window.location.hash.substring(1);
var status_msg = document.querySelector('footer .subtitle');
var code_mirror = document.querySelector('.CodeMirror');
if (! font) {
font = 'cartograph';
status_msg.innerHTML = 'Test drive all the programming fonts!';
} else if (typeof font_data !== 'undefined') {
status_msg.innerHTML = 'Test drive <a rel="external" href="' + font_data[font].website + '">' + font_data[font].name + '!</a>';
}
if (typeof font_data !== 'undefined' && font_data[font].rendering === 'bitmap') {
code_mirror.classList.add('no-smooth');
} else {
code_mirror.classList.remove('no-smooth');
}
if (font === 'input') {
code_mirror.style.fontFamily = 'Input Mono, monospace';
code_mirror.querySelectorAll('pre, textarea').forEach(function(element){
element.style.fontFamily = 'Input Mono, monospace';
});
} else {
code_mirror.style.fontFamily = font + ', monospace';
code_mirror.querySelectorAll('pre, textarea').forEach(function(element){
element.style.fontFamily = font + ', monospace';
});
}
document.querySelectorAll('#select-font [data-alias]').forEach(function(element) {
element.classList.remove('active');
});
document.querySelectorAll('#select-font [data-alias=\'' + font + '\']').forEach(function(element) {
element.classList.add('active');
});
document.cookie = 'font=' + font + ';max-age=172800';
}
window.onhashchange = selectFont;
function setSize() {
var size = document.getElementById('size').value;
document.querySelector('.CodeMirror').style.fontSize = size + 'px';
document.cookie = 'size=' + size + ';max-age=172800';
}
function setSpacing() {
var spacing = document.getElementById('spacing').value;
document.querySelector('.CodeMirror').style.lineHeight = spacing;
document.cookie = 'spacing=' + spacing + ';max-age=172800';
}
const codeCacheData = new Map();
/**
*
* @param {string} code
*/
function setCodeToView(code) {
if (!code.length) return;
editor.setValue(code);
}
/**
*
* @param {string} lang
* @returns {string}
*/
async function getCodeData(lang) {
var _value = lang.toLocaleLowerCase();
if (_value == "clike") _value = "cpp";
// use cached value
var cacheCode = codeCacheData.get(_value);
if (!!cacheCode) return cacheCode;
var data = await (await fetch(`https://microsoft.github.io/monaco-editor/index/samples/sample.${ _value }.txt`)).text();
if (data.length <= 0) return "";
codeCacheData.set(_value, data);
return data
}
/**
* @param {string} lang
*/
async function updateLanguageCodeView(lang) {
var code = await getCodeData(lang);
setCodeToView(code);
}
function selectLanguage() {
var lang = document.getElementById('select-language').value;
editor.setOption('mode', lang.toLowerCase());
document.cookie = 'language=' + lang + ';max-age=172800';
updateLanguageCodeView(lang);
}
function setCounter(amount) {
var counter_element = document.querySelector('h1 a:first-child');
if (amount === 1) {
counter_element.innerHTML = amount + ' Programming Font';
} else {
counter_element.innerHTML = amount + ' Programming Fonts';
}
}
function applyFilters() {
var count = 0;
Object.keys(filters).forEach(function(filter) {
var button = document.querySelector('button[value="' + filter + '"]');
if (! button) {
return;
}
if (filters[filter]) {
button.classList.add('selected');
button.querySelectorAll('svg').forEach(function(image){
image.classList.remove('selected');
});
button.querySelector('svg[alt="' + filters[filter] + '"]').classList.add('selected');
} else {
button.classList.remove('selected');
button.querySelectorAll('svg').forEach(function(image){
image.classList.remove('selected');
});
}
});
document.querySelectorAll('.entry[data-alias]').forEach(function(element) {
var data = font_data[element.dataset.alias];
if (
(!filters.style || data.style === filters.style) &&
(!filters.rendering || data.rendering === filters.rendering) &&
(!filters.liga || data.ligatures === false && filters.liga === 'no' || data.ligatures === true && filters.liga === 'yes') &&
(!filters.zerostyle || data.zerostyle === filters.zerostyle) &&
(filters.author === 'all' || data.author === filters.author) &&
(!filters.name || data.name.toLowerCase().indexOf(filters.name) > -1)
) {
element.classList.remove('filtered-out');
count++;
} else {
element.classList.add('filtered-out');
}
});
setCounter(count);
}
function renderSelectList() {
var icon = '<svg class="octicon" viewBox="0 0 12 14" version="1.1" width="12" height="14" aria-hidden="true"><path fill-rule="evenodd" d="M11 10h1v3c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V3c0-.55.45-1 1-1h3v1H1v10h10v-3zM6 2l2.25 2.25L5 7.5 6.5 9l3.25-3.25L12 8V2H6z"></path></svg>';
var pinIcon = '<svg class="octicon octicon-pin" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M10 1.2V2l.5 1L6 6H2.2c-.44 0-.67.53-.34.86L5 10l-4 5 5-4 3.14 3.14a.5.5 0 0 0 .86-.34V10l3-4.5 1 .5h.8c.44 0 .67-.53.34-.86L10.86.86a.5.5 0 0 0-.86.34z"></path></svg>';
var favoritesMap = {};
var favorites = [];
var ajax = new XMLHttpRequest();
document.getElementById('select-font').innerHTML = '';
try {
favorites = JSON.parse(localStorage.getItem('favorites')) || [];
favoritesMap = favorites.reduce(function(acc, alias) {
acc[alias] = true;
return acc;
}, {});
} catch (err) {
// eslint-disable-next-line no-console
console.error('could not render favorites', err);
}
ajax.onreadystatechange = function() {
var fonts = [];
var authors = [];
if (ajax.readyState === 4 && ajax.status === 200) {
font_data = ajax.response;
Object.keys(font_data).forEach(function(key){
var v= font_data[key];
v.alias = key;
fonts.push(v);
if (authors.indexOf(v.author) < 0) {
authors.push(v.author);
}
});
}
authors.sort();
fonts.sort(function(a, b) {
if (favoritesMap[a.alias] && !favoritesMap[b.alias]) {return -1;}
if (!favoritesMap[a.alias] && favoritesMap[b.alias]) {return 1;}
if (a.name.toLowerCase() < b.name.toLowerCase()) {return -1;}
if (a.name.toLowerCase() > b.name.toLowerCase()) {return 1;}
return 0;
});
authors.forEach(function(author) {
var option = document.createElement('option');
option.innerHTML = author;
document.getElementById('authors-list').querySelector('.other').appendChild(option);
});
fonts.forEach(function(v) {
var option = document.createElement('div');
option.classList.add('entry');
if (favoritesMap[v.alias]) {
option.classList.add('pinned');
}
option.setAttribute('data-alias', v.alias);
option.innerHTML = '<a href="#' + v.alias + '" data-style="' + v.style + '">' +
'<span class="name">' + v.name + '</span>' +
'<span class="details">' + v.year + ' — ' + v.author + '</span>' +
'</a>' +
'<a class="favoritelink" onclick="toggleFavorite(\'' + v.alias + '\')">' +
pinIcon +
'</a>' +
'<a class="website" href="' + v.website + '" rel="external"> <span>Website</span>' + icon + '</a>';
document.getElementById('select-font').appendChild(option);
});
selectFont();
applyFilters();
};
ajax.responseType = 'json';
ajax.open('GET', 'fonts.json', true);
ajax.send();
}
// eslint-disable-next-line no-unused-vars
function toggleFavorite(alias) {
var favorites = [];
try {
favorites = JSON.parse(localStorage.getItem('favorites')) || [];
if (favorites.indexOf(alias) > -1) {
favorites = favorites.filter(function(v) {
return v !== alias;
});
} else {
favorites.push(alias);
}
localStorage.setItem('favorites', JSON.stringify(Array.from(new Set(favorites))));
} catch (err) {
// eslint-disable-next-line no-console
console.error('could not save favorite', err);
}
renderSelectList();
return false;
}
function walk(direction) {
var activeEntry = document.querySelector('.entry.active');
var target = null;
var next = direction === 'up' ? activeEntry.previousElementSibling : activeEntry.nextElementSibling;
while (target === null) {
if (next) {
if (next.matches('.entry:not(.filtered-out)')) {
target = next;
} else {
next = direction === 'up' ? next.previousElementSibling : next.nextElementSibling;
}
} else {
target = false;
}
}
function isVisible(el) {
var offset = document.getElementById('filters').getBoundingClientRect().height;
var container = document.querySelector('section.select-list').getBoundingClientRect().height;
if (direction === 'up') {
if (el.getBoundingClientRect().top < offset) {
return false;
}
} else {
if (el.getBoundingClientRect().bottom > offset + container) {
return false;
}
}
return true;
}
if (target) {
target.querySelector('a').click();
if (!isVisible(target)){
target.scrollIntoView();
}
}
}
function increaseFontSize() {
var sizeEl = document.getElementById('size');
sizeEl.value = Number(sizeEl.value) + 1;
sizeEl.onchange();
}
function decreaseFontSize() {
var sizeEl = document.getElementById('size');
sizeEl.value = Number(sizeEl.value) - 1;
sizeEl.onchange();
}
function toggleFilter(filter) {
// cycle through the possible values for each filter
// and set the filters[filter] value,
// or at the end of the cycle set it to false
var options = {
'style': [false, 'sans', 'serif'],
'rendering': [false, 'vector', 'bitmap'],
'liga': [false, 'yes', 'no'],
'zerostyle': [false, 'slashed', 'dotted', 'empty'],
};
var current_index = options[filter].indexOf(filters[filter]);
var next = current_index + 1;
if (next < options[filter].length) {
filters[filter] = options[filter][next];
} else {
filters[filter] = options[filter][0];
}
applyFilters();
}
window.addEventListener('DOMContentLoaded', function() {
var cookieValueSpacing = document.cookie.replace(/(?:(?:^|.*;\s*)spacing\s*=\s*([^;]*).*$)|^.*$/, '$1');
var cookieValueSize = document.cookie.replace(/(?:(?:^|.*;\s*)size\s*=\s*([^;]*).*$)|^.*$/, '$1');
var cookieValueTheme = document.cookie.replace(/(?:(?:^|.*;\s*)theme\s*=\s*([^;]*).*$)|^.*$/, '$1');
var cookieValueLanguage = document.cookie.replace(/(?:(?:^|.*;\s*)language\s*=\s*([^;]*).*$)|^.*$/, '$1');
if (cookieValueSpacing !== '') {
document.getElementById('spacing').value = cookieValueSpacing;
}
if (cookieValueSize !== '') {
document.getElementById('size').value = cookieValueSize;
}
if (cookieValueTheme !== '') {
document.getElementById('select-theme').value = cookieValueTheme;
}
if (cookieValueLanguage !== '') {
document.getElementById('select-language').value = cookieValueLanguage;
}
renderSelectList();
selectTheme();
setSize();
setSpacing();
selectLanguage();
function walkThemes(direction) {
var select = document.getElementById('select-theme');
var current = select.selectedOptions[0];
var next;
if (current) {
next = direction === 'up' ? current.previousElementSibling : current.nextElementSibling;
}
if (next) {
select.value = next.value;
}
selectTheme();
}
document.getElementById('theme-next').onclick = function() {
walkThemes('down');
};
document.getElementById('theme-previous').onclick = function() {
walkThemes('up');
};
document.getElementById('filters').querySelectorAll('button').forEach(function(button){
button.onclick = function(event) {
event.preventDefault();
event.stopPropagation();
toggleFilter(button.value);
};
});
document.getElementById('authors-list').onchange = function(event) {
filters.author = event.target.value;
applyFilters();
};
document.getElementById('name-search').onkeyup = function(event) {
filters.name = event.target.value.toLowerCase();
applyFilters();
};
document.querySelector('.select-list').onkeyup = function(event) {
if (event.ctrlKey || event.altKey || event.metaKey || event.shiftKey) {
return;
}
if (event.key === 'ArrowUp') {
event.preventDefault();
event.stopPropagation();
walk('up');
} else if (event.key === 'ArrowDown') {
event.preventDefault();
event.stopPropagation();
walk('down');
}
};
document.body.addEventListener('keydown', function(event) {
if (event.ctrlKey || event.metaKey) {
if (event.key === '-') {
event.preventDefault();
event.stopPropagation();
decreaseFontSize();
} else if (event.key === '=') {
event.preventDefault();
event.stopPropagation();
increaseFontSize();
}
}
});
});