-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresenter-mode.js
98 lines (81 loc) · 2.33 KB
/
presenter-mode.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
import themeCss from '../styles/theme.css?type=raw';
const template = document.createElement('template');
template.innerHTML = `
<style>
${themeCss}
.fullscreen-container {
display: none;
}
.fullscreen-container-on {
background-color: var(--color-primary);
display: block;
z-index: 100;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
iframe {
background-color: var(--color-primary);
min-width: 100%;
min-height: 100%;
width: 100%;
height: 100%;
}
</style>
<button onclick="this.parentNode.host.enablePresenterMode()">Presenter Mode</button>
<div class="fullscreen-container">
<iframe></iframe>
</div>
`;
class PresenterMode extends HTMLElement {
static get observedAttributes() {
return ['slides'];
}
constructor() {
super();
this.slides = [];
this.index = 0;
}
connectedCallback() {
window.addEventListener('message', (postMessage) => {
this.slideNavigationKeyHandler(postMessage.data);
});
document.addEventListener('keydown', (event) => {
this.slideNavigationKeyHandler(event.key);
});
if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'slides' && newValue) {
this.slides = JSON.parse(newValue);
}
}
enablePresenterMode() {
this.setCurrentSlide();
this.shadowRoot.querySelector('div').classList.add('fullscreen-container-on');
}
setCurrentSlide(index = 0) {
this.shadowRoot.querySelector('iframe').setAttribute('src', this.slides[index].route);
}
slideNavigationKeyHandler(keyName) {
if (keyName === 'ArrowRight' || keyName === 'Spacebar' || keyName === 'Enter') {
if ((this.index + 1) !== this.slides.length) {
this.index = this.index += 1;
this.setCurrentSlide(this.index);
}
} else if (keyName === 'ArrowLeft') {
if (this.index > 0) {
this.index = this.index -= 1;
this.setCurrentSlide(this.index);
}
} else if (keyName === 'Escape') {
this.shadowRoot.querySelector('div').classList.remove('fullscreen-container-on');
}
}
}
customElements.define('presenter-mode', PresenterMode);