Repository navigation
Expand file tree
/
Copy pathpresentation.js
More file actions
73 lines (60 loc) · 2.06 KB
/
Copy pathpresentation.js
File metadata and controls
73 lines (60 loc) · 2.06 KB
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
// A small JS lib that turns a website into a
// presentation.
const isNext = (el) => {
var rect = el.getBoundingClientRect();
return rect.top >= 500
}
const isPrev = (el) => {
var rect = el.getBoundingClientRect();
return rect.bottom < -100
}
(() => {
// List of elements which we want to focus on for
// our presentation (all other elements would be
// ignored.
const elementsToFocus = "img, h1, h2, h3, h4, h5, h6, .definition, .theorem, table, pre, .prev-next"
// Set scrolling behavior
const scrollConfig = {
behavior: "smooth",
block: "center"
}
const params = new URLSearchParams(window.location.search);
console.log(params)
if (params.get("presentation") === "true") {
// A list of all elements that we want to present
let elements = []
window.onload = () => {
//setTimeout(() => {
// Add a CSS style to the page
document.getElementsByClassName('content')[0].classList.add('presentation')
// Amend links, so they preserve the url params
Array.from(document.getElementsByTagName('a'))
.forEach(a => {a.href = a.href + document.location.search})
elements = [...document.querySelectorAll(elementsToFocus)]
//next()
//}, 2000)
}
const next = () => {
elements.find(isNext).scrollIntoView(scrollConfig)
}
const prev = () => {
[...elements].reverse().find(isPrev).scrollIntoView(scrollConfig)
}
// UI controls for next and previous element
document.getElementById('next').onclick = next
document.getElementById('prev').onclick = prev
document.getElementById('next').style.display = "none"
document.getElementById('prev').style.display = "none"
// Enable scrolling through the presentation
// with keyboard left and right arrows
document.addEventListener("keydown", e => {
if (e.target.tagName !== "INPUT" && e.target.tagName !== "TEXTAREA") {
if (e.key === "ArrowRight" ) {
next()
} else if (e.key === "ArrowLeft" ) {
prev()
}
}
});
}
})();