-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
85 lines (68 loc) · 2.04 KB
/
script.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
var id = Math.floor(Math.random() * 10000);
var state = window.sessionStorage;
$.page('index', function () {
var p = 0;
if ('ontouchend' in document || state.getItem('tele') === 'true') {
$('body').addClass('is-tele');
}
$('.switch').click(function () {
state.setItem('tele', !$('body').hasClass('is-tele'));
$('body').toggleClass('is-tele', !$('body').hasClass('is-tele'));
update();
return false;
});
$('#content > *').eq(p).addClass('current');
$(window).keydown(e => {
if ($('input:focus, textarea:focus').length) return;
if (e.keyCode === 83) {
// S
$('.switch').eq(0).trigger('click');
update();
}
if (e.keyCode === 34 || e.keyCode === 39 || e.keyCode === 40) {
p++;
update();
return false;
}
if (e.keyCode === 33 || e.keyCode === 37 || e.keyCode === 38) {
p--;
update();
return false;
}
});
function update(fromSocket) {
if (p < 0) p = 0;
if (p >= $('#content > *').length - 1) p = $('#content > *').length - 1;
if (!fromSocket) {
socket.emit('paragraph', { p, id });
}
$('#content > .current').removeClass('current');
var $p = $('#content > *').eq(p);
$p.addClass('current');
var commands = $p.text().match(/\[(\w*):(\w*)\]/g);
commands.forEach(function (c) {
var command = c.replace(/[\[\]]/g, '').split(':');
socket.emit(command[0], { value: command[1] });
});
//var offset = ($p.position().top) - (($('#tele').height() - $p.outerHeight()) / 2);
var offset =
$p.position().top -
$('#content-parent').height() +
$p.outerHeight() +
120;
$('#content').css('top', offset * -1);
$('#content').css('top', offset * -1);
}
var socket = io('/');
socket.on('paragraph', function (data) {
if (!data || !data.id) return;
if (data.id == id) return;
p = data.p;
console.log(p);
update(true);
});
socket.on('restart', function (data) {
window.location.reload();
});
//socket.emit('my other event', { my: 'data' });
});