-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-ppt.js
More file actions
124 lines (111 loc) · 3.57 KB
/
markdown-ppt.js
File metadata and controls
124 lines (111 loc) · 3.57 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
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
(function () {
"use strict";
const {
mode
} = (function (query) {
let obj = {}
query.replace(/(\w+)=([^\?\&]+)/g, function (_a, k, v) {
obj[k] = v
})
return obj
})(location.search);
document.body.classList.add(mode);
let online = true;
let index = location.hash && Number(location.hash.substring(1)) || 0;
const sections = document.querySelectorAll('section.hero');
let token = localStorage.getItem('_MDPPT_TOKEN_')
const pageTo = (n, withFetch) => {
n = Math.min(n, sections.length - 1);
n = Math.max(n, 0);
const prev = sections.item(index)
prev.classList.remove(prev.dataset['animated'])
prev.classList.remove('active')
const current = sections.item(n)
current.classList.add(current.dataset['animated'])
current.classList.add('active')
index = n
location.hash = '#' + index
if (online && withFetch) {
let _token = token || prompt('token:')
if (_token) {
fetch(`/markdown-ppt-event?index=${index}&token=${_token}`)
.then(res => res.json())
.then(res => {
if (res.success) {
localStorage.setItem('_MDPPT_TOKEN_', _token)
}
token = res.success && _token
})
}
}
}
const next = () => pageTo(index + 1, mode === 'speaker');
const prev = () => pageTo(index - 1, mode === 'speaker');
pageTo(index)
addEventListener('keyup', e => {
switch (e.keyCode) {
case 38:
prev();
break;
case 40:
next();
break;
default:
break;
}
});
let swipe = null
addEventListener('touchstart', e => {
const { pageX, pageY } = e.touches[0];
swipe = { pageX, pageY }
})
addEventListener('touchend', e => {
swipe = null
})
addEventListener('touchmove', e => {
const { pageX, pageY } = e.touches[0];
if (swipe) {
if (swipe.pageY - pageY > 30) {
next()
swipe = null
} else if (swipe.pageY - pageY < -30) {
prev()
swipe = null
}
}
})
let _n = 0
const createSSE = function createSSE () {
online = true
const sse = new EventSource('/markdown-ppt-event', { withCredentials: true });
const onMessage = e => {
if (e.data != 'false') {
pageTo(Number(e.data) % sections.length)
}
};
const onError = e => {
online = false
sse.removeEventListener('message', onMessage)
sse.removeEventListener('error', onError)
setTimeout(createSSE, ++_n * 1000);
}
sse.addEventListener('message', onMessage)
sse.addEventListener('error', onError)
}
createSSE();
// imageview
const preview = document.querySelector('#image-preview')
preview.addEventListener('dblclick', e => {
preview.classList.remove('is-active')
})
addEventListener('click', e => {
const target = e.target
if (target.matches('.content img')) {
preview.querySelector('.image-preview').innerHTML = `<img src="${target.src}"/>`
preview.classList.add('is-active')
}
if (target.matches('.modal-close')) {
preview.classList.remove('is-active')
}
})
})()