forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (65 loc) Β· 2.09 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
addEventListener("DOMContentLoaded", () => {
for (let time of document.body.getElementsByTagName('time')) {
time.setAttribute('title', new Date(time.textContent));
}
let next = document.querySelector('a.next');
let prev = document.querySelector('a.prev');
window.addEventListener('keydown', e => {
if (document.activeElement.tagName == 'INPUT') {
return;
}
switch (e.key) {
case 'ArrowRight':
if (next) {
window.location = next.href;
}
return;
case 'ArrowLeft':
if (prev) {
window.location = prev.href;
}
return;
}
});
const search = document.querySelector('form[action="/search"]');
const query = search.querySelector('input[name="query"]');
search.addEventListener('submit', (e) => {
if (!query.value) {
e.preventDefault();
}
});
let collapse = document.getElementsByClassName('collapse');
let context = document.createElement('canvas').getContext('2d');
function resize() {
for (let node of collapse) {
if (!('original' in node.dataset)) {
node.dataset.original = node.textContent.trim();
}
let original = node.dataset.original;
let length = original.length;
let width = node.clientWidth;
if (width == 0) {
width = node.parentNode.getBoundingClientRect().width;
}
context.font = window.getComputedStyle(node).font;
let capacity = width / (context.measureText(original).width / length);
if (capacity >= length) {
node.textContent = original
} else {
let count = Math.floor((capacity - 1) / 2);
let start = original.substring(0, count);
let end = original.substring(length - count);
node.textContent = `${start}β¦${end}`;
}
}
}
function copy(e) {
if ('original' in e.target.dataset && window.getSelection().toString().includes('β¦')) {
e.clipboardData.setData('text/plain', e.target.dataset.original);
e.preventDefault();
}
}
addEventListener('resize', resize);
addEventListener('copy', copy);
resize();
});