-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (75 loc) · 2.1 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
import './scss/index.scss';
Reveal.initialize({
controls: false,
history: true,
progress: true,
dependencies: [
{ src: '/assets/js/lib/notes.js', async: true },
{
src: '/assets/js/lib/highlight.js',
async: true,
callback() {
hljs.initHighlightingOnLoad();
},
},
],
});
const urlParams = new URLSearchParams(window.location.search);
const allSlides = Reveal.getTotalSlides();
const slides = {};
let currentSlide = 0;
const noop = () => ({});
const addSlide = (module, number) => {
if (!slides[number]) {
const show = module.default;
slides[number] = typeof show === 'function' ? show : noop;
}
return slides[number];
};
const loadSlide = number => new Promise((resolve, reject) => {
if (slides[number]) return reject();
return import(/* webpackPrefetch: true */ `./slides/${number}/`)
.then((module) => {
resolve({ number, slide: addSlide(module, number) });
})
.catch(noop);
});
const selectSlide = (number, action = 'show', forward = null) => {
const slide = slides[number];
if (slide) {
slide(action === 'show', forward);
} else {
loadSlide(number)
.then((response) => {
const { number: num, slide: sl } = response;
if (num === currentSlide) {
sl(action === 'show', forward);
}
})
.catch(noop);
}
};
Reveal.addEventListener('ready', (event) => {
currentSlide = event.indexh;
// load all slides at once
if (urlParams.has('preload')) {
const slidesToLoad = [];
for (let i = 0; i < allSlides; i += 1) {
slidesToLoad.push(import(`./slides/${i}/`).catch(noop));
}
Promise.all(slidesToLoad).then((loadedSlides) => {
loadedSlides.forEach(addSlide);
selectSlide(currentSlide);
});
} else {
selectSlide(currentSlide);
}
});
Reveal.addEventListener('slidechanged', (event) => {
const forward = event.indexh > currentSlide;
const range = forward ? -1 : 1;
currentSlide = event.indexh;
selectSlide(currentSlide, 'show', forward);
selectSlide(currentSlide + range, 'hide', forward);
loadSlide(currentSlide + range * 2).catch(noop);
});