This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
View.elm
465 lines (415 loc) · 16.1 KB
/
View.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
module View exposing (root)
import Bootstrap.Badge as Badge
import Bootstrap.Button as Button
import Bootstrap.ButtonGroup as ButtonGroup
import Bootstrap.Form.Radio as Radio
import Bootstrap.Form.Textarea as Textarea
import Bootstrap.Grid as Grid
import Bootstrap.Grid.Col as Col
import Bootstrap.Grid.Row as Row
import Bootstrap.Modal as Modal
import Bootstrap.Text as Text
import Bootstrap.Utilities.Spacing as Spacing
import Html exposing (..)
import Html.Attributes exposing (..)
import LanguageExtension
import Types exposing (..)
pageNotFound : Html Msg
pageNotFound =
div []
[ h1 [] [ text "Not found" ]
, text "Sorry couldn't find that page"
]
sourceInput : Model -> Html Msg
sourceInput model =
Textarea.textarea
[ Textarea.id "source"
, Textarea.rows 3
, Textarea.onInput SourceInputUpdated
, Textarea.attrs [ placeholder "Paste your source code here", spellcheck False ]
, Textarea.defaultValue model.sourceInput
]
matchTemplateInput : Model -> Html Msg
matchTemplateInput model =
Textarea.textarea
[ Textarea.id "match_template"
, Textarea.rows 3
, Textarea.onInput MatchTemplateInputUpdated
, Textarea.attrs [ placeholder "Match Template", spellcheck False ]
, Textarea.defaultValue model.matchTemplateInput
]
ruleInput : Model -> Html Msg
ruleInput model =
Textarea.textarea
[ Textarea.id "rule"
, Textarea.rows 1
, Textarea.onInput RuleInputUpdated
, Textarea.attrs [ placeholder "where true", spellcheck False ]
, Textarea.defaultValue model.ruleInput
]
ruleDisplaySyntaxErrors : Model -> Html Msg
ruleDisplaySyntaxErrors model =
Html.p
[ Html.Attributes.class "rule-syntax-errors" ]
[ text model.ruleSyntaxErrors ]
rewriteTemplateInput : Model -> Html Msg
rewriteTemplateInput model =
Textarea.textarea
[ Textarea.id "rewrite"
, Textarea.rows 3
, Textarea.onInput RewriteTemplateInputUpdated
, Textarea.attrs [ placeholder "Rewrite Template", spellcheck False ]
, Textarea.defaultValue model.rewriteTemplateInput
]
highlightableSourceListing : Model -> Html Msg
highlightableSourceListing model =
Html.div
[ Html.Attributes.class "context" ]
[ Html.pre [ Html.Attributes.class "source-box" ]
[ Html.code [ Html.Attributes.id "listing" ]
-- we set the text in index.html to synchronize highlighting
[ text "" ]
]
]
highlightableRewriteResult : Model -> Html Msg
highlightableRewriteResult model =
Html.div
[ Html.Attributes.class "context2" ]
[ Html.pre [ Html.Attributes.class "rewrite-box" ]
[ Html.code [ Html.Attributes.id "listing2" ]
-- we set the text in index.html to synchronize highlighting
[ text "" ]
]
]
languageSelection : String -> Model -> List LanguageExtension -> Html Msg
languageSelection header_text model languages =
div [ class "language_selection" ]
([ h6 [] [ text header_text ] ]
++ Radio.radioList "languageRadios"
(languages
|> List.map
(\l ->
let
radioChecked =
if model.language == l then
Radio.checked True
else
Radio.checked False
in
Radio.create
[ Radio.id (LanguageExtension.toString l)
, radioChecked
, Radio.onClick <| LanguageInputUpdated l
]
(LanguageExtension.prettyName l)
)
)
)
substitutionKindSelection : Model -> Html Msg
substitutionKindSelection model =
div [ class "substitution_kind" ]
([ h6 [] [ text "Substitution" ] ]
++ Radio.radioList "inPlaceMatchingRadios"
[ Radio.create
[ Radio.id "InPlace"
, if model.substitutionKind == InPlace then
Radio.checked True
else
Radio.checked False
, Radio.onClick <| SubstitutionKindInputUpdated InPlace
]
"In-place"
, Radio.create
[ Radio.id "NewlineSeparated"
, if model.substitutionKind == NewlineSeparated then
Radio.checked True
else
Radio.checked False
, Radio.onClick <| SubstitutionKindInputUpdated NewlineSeparated
]
"Just Matches"
]
)
footerShareLink : Model -> Html Msg
footerShareLink model =
div [ class "text-left" ] <|
[ Button.button
[ Button.small
, Button.warning
, Button.onClick ShareLinkClicked
]
[ i [ class "fa-fw fas fa-link" ] []
, text "Share Link"
]
]
++ (if model.prettyUrl == "" then
[]
else
[ ButtonGroup.buttonGroup
[ ButtonGroup.small
, ButtonGroup.attrs [ Spacing.ml2 ]
]
[ ButtonGroup.button
[ Button.warning
, Button.onClick CopyShareLinkClicked
]
[ text model.prettyUrl ]
, ButtonGroup.button
[ Button.outlineWarning
, Button.onClick CopyShareLinkClicked
, Button.small
, Button.attrs
[ class model.copyButtonLinkText
, id "copyableLink"
]
]
[]
]
]
)
docsLink : () -> Html Msg
docsLink () =
div []
[ a [ href "https://comby.dev", class "btn btn-secondary documentation-button" ]
[ i [ class "fa-fw fas fa-file-alt" ] []
, text "Documentation"
]
]
footerServerConnected : Model -> Html Msg
footerServerConnected model =
if model.serverConnected then
Badge.pillSuccess
[ Html.Attributes.class "green-pill"
, Html.Attributes.class "float-right"
]
[ i [ class "fa-fw fas fa-server" ] []
, text "Server Connected"
]
else
Badge.pillDanger
[ Html.Attributes.class "red-pill"
, Html.Attributes.class "float-right"
]
[ i [ class "fa-fw fas fa-server" ] []
, text "No Server Connected"
]
footerAbout : Html Msg
footerAbout =
Button.button
[ Button.small
, Button.onClick ShowAboutModal
]
[ i [ class "fa fa-info-circle info-button" ] [] ]
aboutModal : Model -> Html Msg
aboutModal model =
Modal.config CloseAboutModal
|> Modal.small
|> Modal.h5 [] [ text "About" ]
|> Modal.body []
[ div []
[ p []
[ text "This is "
, a [ href "https://github.com/comby-tools/comby-ui" ] [ text "comby-ui" ]
, text ", a friendly interface for creating code rewrite patterns. The backend is powered by "
, a [ href "https://github.com/comby-tools/comby" ] [ text "comby" ]
, text "."
]
, p []
[ text "This web app is made possible by "
, a [ href "https://elm-lang.org/" ] [ text "Elm" ]
, text " and many wonderful libraries. "
, a [ href "https://github.com/comby-tools/comby-ui/tree/master/third-party-licenses" ] [ text "Credits." ]
]
]
]
|> Modal.view model.modalAboutVisibility
terminalModal : Model -> Html Msg
terminalModal model =
let
language =
let
s =
LanguageExtension.toString model.language
in
if s == ".generic" then
""
else
s
in
Modal.config CloseTerminalModal
|> Modal.large
|> Modal.h5 [] [ text ("Paste the command in your terminal to run on all " ++ language ++ " files in the current directory") ]
|> Modal.body []
[ Grid.containerFluid []
[ Grid.row []
[ Grid.col [ Col.md2 ] []
, Grid.col [ Col.md8 ]
[ Html.pre [ class "modal-pre" ]
[ Html.code []
[ text model.modalText ]
]
]
]
]
]
|> Modal.footer []
[ Grid.containerFluid []
[ Grid.row []
[ Grid.col [ Col.md12 ]
[ ButtonGroup.buttonGroup
[ ButtonGroup.small
, ButtonGroup.attrs [ class "w-100" ]
]
[ ButtonGroup.button
[ Button.secondary
, Button.attrs [ Spacing.mt1, class "w-100 terminal-button" ]
, Button.onClick CopyTerminalCommandClicked
]
[ text "Copy above command. Output will print to terminal." ]
, ButtonGroup.button
[ Button.outlineSecondary
, Button.attrs [ Spacing.mt1, class "terminal-small-button" ]
, Button.onClick CopyTerminalCommandClicked
]
[ i [ class model.copyButtonTerminalText ] [] ]
]
]
]
, Grid.row []
[ Grid.col [ Col.md12 ]
[ ButtonGroup.buttonGroup
[ ButtonGroup.small
, ButtonGroup.attrs [ class "w-100" ]
]
[ ButtonGroup.button
[ Button.danger
, Button.attrs [ Spacing.mt1, class "w-100 terminal-button-add-in-place" ]
, Button.onClick CopyTerminalCommandInPlaceClicked
]
[ text "Add -i option to the command. Files will change in place. Make sure you have a backup or version control." ]
, ButtonGroup.button
[ Button.outlineDanger
, Button.attrs [ Spacing.mt1, class "terminal-small-button-add-in-place" ]
, Button.onClick CopyTerminalCommandInPlaceClicked
]
[ i [ class model.copyButtonTextInPlace ] [] ]
]
]
]
]
]
|> Modal.view model.modalTerminalVisibility
terminalButtonGroup : Model -> Html Msg
terminalButtonGroup model =
div [ class "text-right" ]
[ ButtonGroup.buttonGroup
[ ButtonGroup.small
]
[ ButtonGroup.button
[ Button.secondary
, Button.small
, Button.onClick ShowTerminalModal
, Button.attrs [ class "terminal-button" ]
]
[ i [ class "fa-fw fas fa-chevron-right" ] []
, text "Run in Terminal"
]
, ButtonGroup.button
[ Button.outlineSecondary
, Button.small
, Button.attrs [ class "terminal-small-button" ]
, Button.onClick CopyTerminalCommandClicked
]
[ i [ class model.copyButtonTerminalText ] [] ]
]
]
halves : List LanguageExtension -> ( List LanguageExtension, List LanguageExtension )
halves l =
let
newl =
List.indexedMap (\i x -> ( i, x )) l
n =
(List.length l // 2) + 2
-- + 4
( left, right ) =
List.partition (\( i, _ ) -> i < n) newl
in
( List.map (\( _, x ) -> x) left, List.map (\( _, x ) -> x) right )
sourcePage : Model -> Html Msg
sourcePage model =
let
( left, right ) =
halves LanguageExtension.all
in
Grid.containerFluid []
[ Grid.row [ Row.attrs [ Spacing.mt3 ] ]
[ Grid.col [ Col.md10 ]
[ Grid.row [ Row.rightMd ]
-- changing to md12 makes this flush on left
[ Grid.col [ Col.md11 ]
[ Grid.row []
[ Grid.col [ Col.xs12 ]
[ br [] []
, sourceInput model
]
]
, Grid.row [ Row.attrs [ Spacing.mt3 ] ]
[ Grid.col [ Col.md6 ]
[ matchTemplateInput model
]
, Grid.col [ Col.md6 ]
[ rewriteTemplateInput model
]
]
, Grid.row [ Row.attrs [ Spacing.mt3 ] ]
[ Grid.col [ Col.md6 ]
[ ruleInput model
]
, Grid.col [ Col.md6 ]
[ ruleDisplaySyntaxErrors model
]
]
, Grid.row [ Row.attrs [ Spacing.mt3 ] ]
[ Grid.col [ Col.md6 ]
[ highlightableSourceListing model
]
, Grid.col [ Col.md6 ]
[ highlightableRewriteResult model
]
]
, Grid.row [ Row.betweenXs, Row.attrs [ Spacing.mt3, class "text-center" ] ]
[ Grid.col []
[ footerShareLink model ]
, Grid.col [ Col.md4 ]
[ docsLink () ]
, Grid.col []
[ terminalButtonGroup model ]
]
]
]
]
, Grid.col [ Col.md2 ]
[ substitutionKindSelection model
, h6 [ Spacing.mt3 ] [ text "Language" ]
, Grid.row []
[ Grid.col [ Col.md6 ] [ languageSelection "" model left ]
, Grid.col [ Col.md6 ] [ languageSelection "" model right ]
]
, Grid.row [ Row.attrs [ Spacing.mt5 ], Row.middleXs ]
[ Grid.col [ Col.md6, Col.offsetMd3 ]
[ footerServerConnected model ]
, Grid.col [ Col.md3 ]
[ footerAbout ]
]
]
]
, terminalModal model
, aboutModal model
]
root : Model -> Html Msg
root model =
case model.page of
SourcePage ->
sourcePage model
NotFound ->
pageNotFound