-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
159 lines (143 loc) · 4.01 KB
/
client.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* global spotifyUrl */
/* global Reveal */
import io from "socket.io-client";
const initClient = function (Reveal) {
// don't emit events from inside the previews themselves
if (window.location.search.match(/receiver/gi)) return;
const server = Reveal.getConfig().server;
const socket = io.connect("/admin", {
transportOptions: {
polling: {
extraHeaders: {
Authorization: server.secret,
},
},
},
});
socket.on("error", (e) => console.error(e));
console.log(`View presenter notes at ${window.location.origin}/notes`);
let spotifyIframe = null;
const removeSpotifyÌframe = function () {
spotifyIframe = document.querySelector("iframe[src*='/player']");
if (spotifyIframe !== null) spotifyIframe.remove();
spotifyIframe = null;
};
window.onbeforeunload = function () {
removeSpotifyÌframe();
};
/**
* Posts the current slide data to the viewers
*/
function postServer() {
socket.emit("statechanged", {
state: Reveal.getState(),
});
}
/**
* Posts the current slide data to the notes window
*/
function post() {
const slideElement = Reveal.getCurrentSlide();
const notesElement = slideElement.querySelector("aside.notes");
const messageData = {
notes: "",
markdown: false,
state: Reveal.getState(),
};
// Look for notes defined in a slide attribute
if (slideElement.hasAttribute("data-notes")) {
messageData.notes = slideElement.getAttribute("data-notes");
}
// Look for notes defined in an aside element
if (notesElement) {
messageData.notes = notesElement.innerHTML;
messageData.markdown =
typeof notesElement.getAttribute("data-markdown") === "string";
}
socket.emit("statechanged", messageData);
}
// When a new notes window connects, post our current state
socket.on("new-subscriber", function (data) {
post();
});
// When the state changes from inside of the speaker view
socket.on("statechanged-speaker", function (data) {
Reveal.setState(data.state);
});
// Monitor events that trigger a change in state
["slidechanged", "ready"].forEach((event) => {
Reveal.on(event, (data) => {
if (data.indexh === 0 || data.indexh === 2) {
removeSpotifyÌframe();
} else if (data.indexh === 1 && spotifyIframe === null) {
const iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.setAttribute("allow", "encrypted-media, autoplay");
iframe.src = `${spotifyUrl}/player`;
document.body.appendChild(iframe);
}
post();
});
});
[
"fragmentshown",
"fragmenthidden",
"overviewhidden",
"overviewshown",
"paused",
"resumed",
].forEach((event) => {
Reveal.on(event, post);
});
socket.on(
"plyrchanged-speaker",
function ({
event,
data,
data: { id, currentTime, paused, playing, ended, volume },
}) {
const player = document.getElementById(id);
switch (event) {
case "play":
player.play();
break;
case "pause":
player.pause();
break;
case "seeked":
player.currentTime = currentTime;
break;
case "currentState":
if (Math.abs(currentTime - player.currentTime) > 0.3)
player.currentTime = currentTime;
if (player.paused !== paused && paused === true) player.pause();
if (player.play !== playing && playing === true) player.play();
break;
case "volumechange":
player.volume = volume;
break;
default:
return;
}
if (
/ready|play|pause|seeked|volumechange|timeupdate|currentState/.test(
event
)
) {
const messageData = {
data,
event,
};
socket.emit("plyrchanged", messageData);
}
}
);
};
export default () => {
return {
id: "RevealClient",
init(Reveal) {
initClient(Reveal);
},
};
};