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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
{-
- This file is part of `typers`.
-
- `typers` is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- `typers` is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero Public License for more details.
-
- You should have received a copy of the GNU Affero Public License
- along with `typers`. If not, see <https://www.gnu.org/licenses/>.
-}
module Main exposing (..)
import Array exposing (..)
import Base exposing (..)
import Browser
import Css
import Css.Global exposing (body, everything, global, selector)
import Data exposing (..)
import Html
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (..)
import Html.Styled.Events exposing (onClick, onInput)
import Random
import Styles exposing (styledButton, styledInput, styledTextBox, wordStyle)
import Task
import Time
import Utils exposing (flip, isJust, isNothing)
import Views exposing (..)
main =
Browser.document
{ init = init
, view = \m -> { title = "typers", body = [ view m |> toUnstyled ] }
, update = update
, subscriptions = subscriptions
}
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
stringsToWords : List String -> List Word
stringsToWords ls =
List.map (flip Word <| Todo) ls
generateWords : Config -> Cmd Msg
generateWords config =
Random.generate LoadWords (randomParagraph config)
defaultModel : Model
defaultModel =
Model Nothing Nothing Array.empty Nothing defaultConfig 0 "" False
init : () -> ( Model, Cmd Msg )
init _ =
( defaultModel
, generateWords defaultConfig
)
setStartTime : Cmd Msg
setStartTime =
Task.perform Started Time.now
setEndTime : Cmd Msg
setEndTime =
Task.perform Finished Time.now
firstWord : Model -> Bool
firstWord model =
String.length model.inputBox == 1 && model.currentWord == 0
lastWord : Model -> Bool
lastWord model =
model.currentWord == model.config.length - 1
wordEnded : String -> Bool
wordEnded s =
String.endsWith " " s
handleWordEnded : Model -> ( Model, Cmd Msg )
handleWordEnded model =
let
s =
model.inputBox
current =
Array.get model.currentWord model.words |> Maybe.withDefault (Word "" Todo)
currentContent =
current.content
prevWordStatus =
if s == currentContent then
Correct
else
Wrong
newWords =
Array.set model.currentWord { current | status = prevWordStatus } model.words
newModel =
{ model | inputBox = "", words = newWords, currentWord = model.currentWord + 1 }
cmd =
if lastWord model then
setEndTime
else
Cmd.none
in
( newModel, cmd )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Started t ->
( { model | begin = Just t }
, Cmd.none
)
Finished t ->
( { model | end = Just t }
, Cmd.none
)
LoadWords ls ->
( { model | words = Array.fromList <| stringsToWords ls }
, Cmd.none
)
InputChanged s ->
let
cmd =
if firstWord model && isNothing model.begin then
setStartTime
else if s == currentContents model && lastWord model then
setEndTime
else
Cmd.none
inpStat =
String.startsWith s (currentContents model)
in
( { model | inputBox = s, inputCorrect = inpStat }
, cmd
)
WordLengthChanged n ->
let
oldConfig =
model.config
newConfig =
{ oldConfig | length = n }
in
( { defaultModel | config = newConfig }
, generateWords newConfig
)
Redo ->
let
oldConfig =
model.config
newModel =
{ defaultModel | config = oldConfig }
in
( newModel, generateWords oldConfig )
NextWord ->
if isJust model.end then
( model, Cmd.none )
else
handleWordEnded model
_ ->
( model
, Cmd.none
)
toPara : Array Word -> Int -> String -> Html Msg
toPara words current content =
words
|> Array.map (\w -> span [ css [ wordStyle w.status ] ] [ text w.content ])
|> Array.set current (span [ css [ wordStyle CurrentWord ] ] [ text content ])
|> Array.toList
|> List.intersperse (span [] [ text " " ])
|> styledTextBox []
currentContents : Model -> String
currentContents model =
Array.get model.currentWord model.words
|> Maybe.map .content
|> Maybe.withDefault ""
view : Model -> Html Msg
view model =
div
[]
[ div
[ style "width" "95%"
, style "max-width" "650px"
, style "margin" "15% auto auto auto"
]
[ div
[ style "width" "100%"
]
[ viewWordLengthOptions model.config
, div
[ style "float" "right" ]
[ styledButton [ onClick Redo ] [ text "redo" ] ]
]
, div []
[ toPara model.words model.currentWord (currentContents model)
, div []
[ text "> "
, styledInput model.inputCorrect [ onInput handleInputChanged, value model.inputBox ] []
]
]
, div [] [ viewStats model ]
, global [ selector "li:not(:last-child)::after" [ Css.property "content" "' · '" ] ]
, global [ selector "*,*:focus,*:hover" [ Css.property "outline" "none" ] ]
, global [ everything [ Css.fontFamily Css.monospace, Css.fontSize (Css.px 18) ] ]
]
, div
[ style "position" "absolute"
, style "bottom" "0"
, style "left" "50%"
, style "transform" "translate(-50%)"
]
[ viewFooter ]
]
handleInputChanged : String -> Msg
handleInputChanged s =
if wordEnded s then
NextWord
else
InputChanged s
|