-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
248 lines (170 loc) · 6.04 KB
/
Main.elm
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
module Main exposing (..)
import Html exposing (Attribute, Html, button, div, span, text, textarea)
import Html.Attributes exposing (attribute, class, value)
import Html.Events exposing (on, onClick, onInput, onMouseLeave)
import Http
import Json.Decode exposing (Decoder, at, field, int, list, map3, string)
main : Program Never Model Msg
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
sampleText : String
sampleText =
"What do you thinks of grammar checkers? Please not that they are not perfect. Style issues get a blue marker: It's 5 P.M. in the afternoon. LanguageTool 3.8 was released on Thursday, 27 June 2017."
type alias Comment =
{ message : String
, offset : Int
, length : Int
}
type alias Model =
{ text : String
, comments : List Comment
, activeCommentId : Maybe Int
}
-- INIT
init : ( Model, Cmd Msg )
init =
( Model "" [] Nothing, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- UPDATE
type Msg
= SetText String
| Proofread
| ProofreadResult (Result Http.Error (List Comment))
| AddSampleText
| SetActiveComment String
| ResetActiveComment
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SetText text ->
( { model | text = text, comments = [] }, Cmd.none )
Proofread ->
( model, requestProofread model.text )
ProofreadResult (Ok comments) ->
( { model | comments = comments }, Cmd.none )
ProofreadResult (Err error) ->
( { model | text = toString error }, Cmd.none )
AddSampleText ->
( { model | text = sampleText, comments = [] }, Cmd.none )
SetActiveComment activeCommentIdStr ->
let
result =
String.toInt activeCommentIdStr
in
case result of
Ok activeCommentId ->
( { model | activeCommentId = Just activeCommentId }, Cmd.none )
Err _ ->
( model, Cmd.none )
ResetActiveComment ->
( { model | activeCommentId = Nothing }, Cmd.none )
-- VIEW
view : Model -> Html Msg
view model =
div []
[ textarea [ onInput SetText, value model.text, class "text-editor" ] []
, button [ onClick AddSampleText ] [ text "Add Sample Text" ]
, button [ onClick Proofread ] [ text "Proofread" ]
, viewCommentsPanel model.text model.comments
, viewCommentsCount model
, viewActiveCommentPanel model.activeCommentId model.comments
]
viewCommentsCount : Model -> Html msg
viewCommentsCount model =
div [ class "note" ] [ text ("comments: " ++ toString (List.length model.comments)) ]
viewCommentsPanel : String -> List Comment -> Html Msg
viewCommentsPanel reviewedText comments =
div [ class "proofread-panel" ]
(viewHighlightedText reviewedText 0 comments)
viewHighlightedText : String -> Int -> List Comment -> List (Html Msg)
viewHighlightedText reviewedText lastProcessedIndex comments =
case comments of
[] ->
let
restOfText =
String.slice lastProcessedIndex (String.length reviewedText) reviewedText
in
span [ class "text" ] [ text restOfText ] :: []
comment :: xComments ->
let
leftText =
String.slice lastProcessedIndex comment.offset reviewedText
commentEndIndex =
comment.offset + comment.length
commentText =
String.slice comment.offset commentEndIndex reviewedText
in
span [ class "text" ] [ text leftText ]
:: span
[ class "comment"
, attribute "id" (toString comment.offset)
, handleOnMouseEnter SetActiveComment
, handleOnMouseLeave ResetActiveComment
, handleOnClick SetActiveComment
]
[ text commentText ]
:: viewHighlightedText reviewedText commentEndIndex xComments
viewActiveCommentPanel : Maybe Int -> List Comment -> Html msg
viewActiveCommentPanel activeComment comments =
case activeComment of
Nothing ->
div [] []
Just activeCommentId ->
let
comment =
List.head <| List.filter (\comment -> comment.offset == activeCommentId) comments
in
case comment of
Nothing ->
div [] []
Just comment ->
div [ class "comment-description" ] [ text comment.message ]
-- HTTP
requestProofread : String -> Cmd Msg
requestProofread text =
let
url =
"https://languagetool.org/api/v2/check"
body =
Http.stringBody "application/x-www-form-urlencoded" (encodeProofreadRequest text)
request =
Http.post url body commentListDecoder
in
Http.send ProofreadResult request
encodeProofreadRequest : String -> String
encodeProofreadRequest text =
String.join "&"
[ "text=" ++ Http.encodeUri text
, "language=" ++ "en-US"
]
commentDecoder : Decoder Comment
commentDecoder =
map3 Comment
(field "message" string)
(field "offset" int)
(field "length" int)
commentListDecoder : Decoder (List Comment)
commentListDecoder =
at [ "matches" ] (list commentDecoder)
-- HELPERS
handleOnMouseLeave : msg -> Attribute msg
handleOnMouseLeave msg =
onMouseLeave msg
handleOnMouseEnter : (String -> msg) -> Attribute msg
handleOnMouseEnter tagger =
on "mouseenter" (Json.Decode.map tagger targetDataId)
handleOnClick : (String -> msg) -> Attribute msg
handleOnClick tagger =
on "click" (Json.Decode.map tagger targetDataId)
targetDataId : Decoder String
targetDataId =
at [ "target", "id" ] string