-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspeedread.coffee
229 lines (181 loc) · 5.71 KB
/
speedread.coffee
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict'
class Page
constructor: (@win) ->
@doc = @win.document
@body = @doc.body
@event = @win.event
createElement: (type) ->
@doc.createElement type
appendChild: (child) ->
@body.appendChild child
removeChild: (child) ->
@body.removeChild child
getSelectedText: ->
if @win.getSelection
return @win.getSelection().toString()
if @doc.getSelection
return @doc.getSelection()
if @doc.selection
return @doc.selection.createRange().text
''
setInterval: (callback, time) ->
@win.setInterval callback, time
clearInterval: (interval) ->
@win.clearInterval interval
class Splitter
@stringReverse: (str) ->
str.split('').reverse().join('')
@stringNotEmpty: (str) ->
str isnt ''
@splitIntoWords: (text) ->
text.split(/\s+/).filter(Splitter.stringNotEmpty)
@arrayNotEmpty: (array) ->
array.length > 0
@appendEmptyElement: (array) ->
array.concat ''
@splitIntoSentences: (text) ->
@stringReverse(text) \
.split(/(?=[\s\n\r]+["']?[.?!]+)/) \
.map(Splitter.stringReverse) \
.reverse() \
.map(Splitter.splitIntoWords) \
.filter(Splitter.arrayNotEmpty) \
.map(Splitter.appendEmptyElement)
class TextSplitter
constructor: (@sentences) ->
@sentence = 0
@word = 0
hasNext: -> @sentence < @sentences.length
nextWord: ->
nextWord = @sentences[@sentence][@word]
@word += 1
if @word is @sentences[@sentence].length
@word = 0
@sentence += 1
nextWord
class ElementCreator
constructor: (@page) ->
createCssText = (css) ->
cssProperties = for key, val of css when css.hasOwnProperty key
key + ':' + val
cssProperties.join ';'
create: (type, id, css) ->
elem = @page.createElement type
elem.id = id
elem.style.cssText = createCssText css
elem
createDiv: (id, css) -> @create 'div', id, css
createPara: (id, css) -> @create 'p', id, css
class SrDialog
constructor: (@page, @elementCreator) ->
@dialog = null
@p = null
@keyEvents = {}
create: ->
@dialog = @elementCreator.createDiv 'srDialog', {
'background-color': 'white',
'opacity': '.95',
'filter': 'alpha(opacity=95)',
'position': 'fixed',
'top': '0',
'left': '0',
'width': '100%',
'height': '100%',
'z-index': '1000'
}
@dialog.tabIndex = 0
@p = @elementCreator.createPara 'srWord', {
'text-align': 'center',
'background-color': 'white',
'color': 'black',
'font-size': '40px',
'position': 'fixed',
'top': '50%',
'left': '50%',
'width': '400px',
'margin-left': '-200px',
'height': '100px',
'margin-top': '-50px'
}
@p.innerHTML = ''
@dialog.appendChild @p
@page.appendChild @dialog
@dialog.focus()
remove: -> @page.removeChild @dialog unless @dialog is null
onKeyDown: (evt) ->
evt = evt or @page.event
if @keyEvents.hasOwnProperty evt.keyCode
@keyEvents[evt.keyCode]()
if evt.preventDefault
evt.preventDefault()
else
event.returnValue = false
onKeyPress: (evt) ->
evt = evt or @page.event
not @keyEvents.hasOwnProperty evt.keyCode
addKeyEvent: (key, action) ->
if @dialog.onkeydown is null
@dialog.onkeydown = (evt) => @onKeyDown evt
@dialog.onkeypress = (evt) => @onKeyPress evt
@keyEvents[key] = action
clearKeyEvents: ->
@keyEvents.length = 0
showWord: (word) ->
@p.innerHTML = word
class WordDisplayer
constructor: (@words, @srDialog, @finished) ->
nextWord: ->
if @words.hasNext()
@srDialog.showWord @words.nextWord()
else
@finished()
class Looper
constructor: (@page, @callback, @time) ->
@interval = null
start: -> @interval = @page.setInterval @callback, @time
stop: -> @page.clearInterval @interval
class SpeedReader
constructor: (@page, @dialog, @interval) ->
@looper = null
@playing = true
@ESCAPE_KEY_CODE = 27
@SPACE_KEY_CODE = 32
finish: ->
@looper.stop()
@dialog.remove()
pauseResume: ->
if @playing
@looper.stop()
else
@looper.start()
@playing = not @playing
handleKeyPresses: ->
@dialog.addKeyEvent @ESCAPE_KEY_CODE, () => @finish()
@dialog.addKeyEvent @SPACE_KEY_CODE, () => @pauseResume()
displayWords: (words) ->
displayer = new WordDisplayer words, @dialog, () => @finish()
@looper = new Looper @page, (() => displayer.nextWord()), @interval
@looper.start()
class WpmConverter
@toInterval: (wpm) ->
Math.round 60000 / wpm
speedRead = (win, wpm) ->
page = new Page(win)
sentences = Splitter.splitIntoSentences page.getSelectedText()
return if sentences.length is 0
words = new TextSplitter sentences
elementCreator = new ElementCreator page
dialog = new SrDialog page, elementCreator
dialog.create()
interval = WpmConverter.toInterval wpm
sr = new SpeedReader page, dialog, interval
sr.handleKeyPresses()
sr.displayWords words
return
if exports?
exports.Page = Page
exports.Splitter = Splitter
else
win = this
win['speedRead'] = speedRead
speedRead win, 350