forked from ofalvai/speed-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
152 lines (136 loc) · 4 KB
/
app.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
/*TODO
Bookmarklet!
Night mode
Mobile
*/
var readInterval,
readIndex = 0,
textArray = [],
speed = 200;
function prepare(text) {
textArray = [];
for(var i = 0, l = text.length; i < l; i++) {
var word = text[i];
if(word !== '') {
if(/\n/.test(word)) {
var tempArray = word.split('\n');
for(var i2 = 0, l2 = tempArray.length; i2 < l2; i2++) {
if(tempArray[i2] !== '') {
var trimmedWord = $('<div />').text($.trim(tempArray[i2])).html();
textArray.push(trimmedWord);
}
}
} else {
var trimmedWord = $('<div />').text($.trim(word)).html();
textArray.push(trimmedWord);
}
}
}
$('#text-info').html('Words: ' + textArray.length).show();
$('body').data('prepared', true);
}
function start() {
if(textArray.length === 0) {
return false;
}
interval = 1000 / (speed / 60);
readInterval = window.setInterval(flashWords, interval, textArray);
$('#start').html('Pause');
$('body').data('reading', true);
}
function stop() {
window.clearInterval(readInterval);
$('#start').html('Read!');
$('body').data('reading', false);
}
function flashWords(array) {
var word = array[readIndex],
length = array.length;
if(readIndex == length) {
stop();
readIndex = 0;
} else {
readIndex++;
}
$('#word').html(word);
$('#text-progress').attr({
'max': textArray.length,
'value': readIndex,
'step': 1
}).show();
}
$(document).ready(function() {
$('body').data({'reading': false, 'prepared': false});
prepare($('#text-to-read').val().split(' '));
$('#start').on('click', function() {
var data = $('body').data();
if(data.reading === false) {
if(data.prepared === false) {
var text = $('#text-to-read').val().split(' ');
speed = $('#reading-speed').val();
if(text.length > 1 && speed > 0) {
prepare(text);
} else {
alert('No text to read (or invalid speed settings)');
}
}
$('#text-to-read').fadeOut(250, function() {
start();
$('#reading-screen, #new').fadeIn(250);
});
$('h1').animate({height: 0, opacity: 0}, 500);
$('#other').fadeOut(500);
} else {
stop();
}
});
$('#text-to-read').on('keyup', function() {
readIndex = 0;
prepare($(this).val().split(' '));
});
$('#reading-speed').on('change', function() {
speed = $(this).val();
if($('body').data('reading') === true) {
stop();
start();
}
});
$('#text-progress').on('mousedown', function() {
if($('body').data('reading')) {
stop();
$('body').data('reading', 'paused');
}
$(this).on('mousemove', function() {
var newIndex = $(this).val();
$('#word').html(textArray[newIndex]);
});
}).on('mouseup', function() {
readIndex = $(this).val();
$(this).off('mousemove');
if($('body').data('reading') === 'paused') {
start();
}
});
$('#new').on('click', function() {
$('#reading-screen').hide();
$('#text-to-read, #other').fadeIn(500);
$('h1').animate({height: '26px', opacity: 1}, 500);
$(this).fadeOut(500);
if($('body').data('reading') === true) {
stop();
}
});
$('body').on('keyup', function(e) {
if($('#text-to-read').is(':focus')) {
return false;
}
//P or Space
if(e.keyCode === 80 || e.keyCode == 32) {
if($('body').data('reading') === true) {
stop();
} else {
start();
}
}
});
});