-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.elm
659 lines (528 loc) · 17.1 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
port module Main exposing (..)
-- cambio questo in quello che c'è sotto, era: import Dom
import Browser
import Browser.Dom as Dom
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Encode exposing (encode, string)
import List.Extra as ListExtra
import Markdown
import Regex as R
import Task
import Tuple exposing (first, second)
main : Program (Maybe Model) Model Msg
main =
Browser.element
-- programWithFlags cambiato con Browser.element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
port dirtyToJS : Bool -> Cmd msg
port execCommand : String -> Cmd msg
-- MODEL
type alias Model =
{ data : List Card
, visible : List String
, editing : Maybe EditState
, dirty : Bool
}
type alias EditState =
{ title : String
, fieldTitle : String
, fieldBody : String
}
type alias Card =
{ title : String
, body : String
}
init : Maybe Model -> ( Model, Cmd Msg )
init savedState =
case savedState of
Just model ->
( model
, Cmd.batch []
)
Nothing ->
( { data = [ Card "test1" "test content", Card "test2" "test content 2" ]
, visible = [ "test1", "test2" ]
, editing = Nothing
, dirty = False
}
, Cmd.none
)
-- UPDATE
type Msg
= NoOp
-- === Card Creation ===
| NewCard
| AddCard String
| LinkClicked String
-- === Card Edit/Delete ===
| SaveCard
| EditCard String
| UpdateFieldTitle String
| UpdateFieldBody String
| CancelCard
| DeleteCard String
-- === Card Visibility ===
| OpenCard String
| CloseCard String
-- === Ports ===
| HandleKey String
| Dirty Bool
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model
, Cmd.batch []
)
NewCard ->
update (AddCard "") model
AddCard title ->
let
card_ =
ListExtra.find (\c -> c.title == title) model.data
in
case ( card_, model.editing ) of
( Nothing, Nothing ) ->
( { model
| visible = "" :: model.visible
, editing = Just (EditState "" title "")
}
, Cmd.batch
[ dirtyToJS True
, focus
(if title == "" then
"card-title-edit-"
else
"card-body-edit-"
)
, execCommand "selectAll"
]
)
_ ->
( model
, Cmd.batch []
)
LinkClicked title ->
let
card_ =
ListExtra.find (\c -> c.title == title) model.data
cardVisible_ =
ListExtra.find (\v -> v == title) model.visible
in
case ( card_, cardVisible_ ) of
( Just card, Nothing ) ->
update (OpenCard title) model
( Nothing, _ ) ->
update (AddCard title) model
_ ->
( model
, Cmd.batch []
)
SaveCard ->
case model.editing of
Nothing ->
( model
, Cmd.batch []
)
Just { title, fieldTitle, fieldBody } ->
case ( title, fieldTitle ) of
-- empty titles not allowed
( _, "" ) ->
( model
, Cmd.batch []
)
-- new card saved from draft
( "", _ ) ->
( { model
| data = Card (fieldTitle |> String.trim) fieldBody :: model.data
, visible =
model.visible
|> List.map
(\v ->
if v == title then
fieldTitle
else
v
)
, editing = Nothing
}
, dirtyToJS True
)
-- existing card modified
( _, _ ) ->
( { model
| data =
model.data
|> List.map
(\c ->
if c.title == title then
Card fieldTitle fieldBody
else
c
)
, visible =
model.visible
|> List.map
(\v ->
if v == title then
fieldTitle
else
v
)
, editing = Nothing
}
, dirtyToJS True
)
EditCard title ->
let
card_ =
ListExtra.find (\c -> c.title == title) model.data
in
case ( card_, model.editing ) of
( Just card, Nothing ) ->
( { model
| editing = Just (EditState title card.title card.body)
}
, focus ("card-body-edit-" ++ title)
)
_ ->
( model
, Cmd.batch []
)
UpdateFieldTitle fieldTitleNew ->
case model.editing of
Nothing ->
( model
, Cmd.batch []
)
Just editState ->
( { model
| editing = Just { editState | fieldTitle = fieldTitleNew }
}
, Cmd.batch []
-- avevo messo Cmd.none non da errori ma non sembra funzionare
)
UpdateFieldBody fieldBodyNew ->
case model.editing of
Nothing ->
( model
, Cmd.batch []
)
Just editState ->
( { model
| editing = Just { editState | fieldBody = fieldBodyNew }
}
, Cmd.batch []
-- Cmd.none ma non funzionava
)
CancelCard ->
case model.editing of
Nothing ->
( model
, Cmd.batch []
)
Just editState ->
( { model
| editing = Nothing
}
, Cmd.batch []
-- Cmd.none ma non funzionava
)
DeleteCard title ->
let
newEditState =
case model.editing of
Just editState ->
if editState.title == title then
Nothing
else
Just editState
Nothing ->
Nothing
in
( { model
| data = List.filter (\c -> c.title /= title) model.data
, visible = List.filter (\v -> v /= title) model.visible
, editing = newEditState
}
, Cmd.batch []
-- Cmd.none ma non funzionava
)
OpenCard title ->
let
card_ =
ListExtra.find (\c -> c.title == title) model.data
cardVisible_ =
ListExtra.find (\v -> v == title) model.visible
in
case ( card_, cardVisible_ ) of
( Just card, Nothing ) ->
( { model
| visible = card.title :: model.visible
}
, Cmd.batch []
-- Cmd.none ma non funzionava
)
_ ->
( model
, Cmd.batch []
)
CloseCard title ->
let
card_ =
ListExtra.find (\c -> c.title == title) model.data
cardVisible_ =
ListExtra.find (\v -> v == title) model.visible
in
case ( card_, cardVisible_ ) of
( Just card, Just cardVisible ) ->
( { model
| visible = List.filter (\v -> v /= title) model.visible
}
, Cmd.batch []
-- Cmd.none ma non funzionava
)
_ ->
( model
, Cmd.batch []
)
HandleKey str ->
case str of
"mod+enter" ->
case model.editing of
Just editState ->
update SaveCard model
Nothing ->
( model
, Cmd.batch []
)
"esc" ->
update CancelCard model
"mod+option+n" ->
normalMode NewCard model
"mod+x" ->
Debug.log "model" ( model, Cmd.batch [] )
_ ->
( model
, Cmd.batch []
)
Dirty bool ->
( { model
| dirty = bool
}
, Cmd.batch []
)
-- SUBSCRIPTIONS
port linkClicked : (String -> msg) -> Sub msg
port shortcut : (String -> msg) -> Sub msg
port dirty : (Bool -> msg) -> Sub msg
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ linkClicked LinkClicked
, shortcut HandleKey
, dirty Dirty
]
-- VIEW
view : Model -> Html Msg
view model =
div
[ id "main" ]
[ div
[ id "app" ]
[ viewContents model.data
, viewStory model.editing (getStory model.data model.visible)
]
, div []
[ text
(if model.dirty then
"dirty!"
else
""
)
]
, viewModel model
]
-- View: Story (visible cards)
viewStory : Maybe EditState -> List Card -> Html Msg
viewStory editState_ visibleCards =
div
[ id "story"
]
(List.map (viewCard editState_) visibleCards)
viewCard : Maybe EditState -> Card -> Html Msg
viewCard editState_ card =
let
viewTemplate =
div
[ id ("card-" ++ card.title)
, classList
[ ( "card", True )
, ( "editing", False )
]
, onDoubleClick (EditCard card.title)
]
[ h2 [] [ text card.title ]
, viewBody card.body
, br [] []
, button [ onClick (CloseCard card.title) ] [ text "close" ]
, button [ onClick (EditCard card.title) ] [ text "edit" ]
, button [ onClick (DeleteCard card.title) ] [ text "delete" ]
]
editTemplate fieldTitle =
div
[ id ("card-" ++ card.title)
, classList
[ ( "card", True )
, ( "editing", True )
]
]
[ input
[ id ("card-title-edit-" ++ card.title)
, classList
[ ( "mousetrap", True )
]
, Html.Attributes.property "defaultValue" (Json.Encode.string fieldTitle)
, onInput UpdateFieldTitle
]
[]
, br [] []
, textarea
[ id ("card-body-edit-" ++ card.title)
, classList
[ ( "mousetrap", True )
]
, Html.Attributes.property "defaultValue" (Json.Encode.string card.body)
, onInput UpdateFieldBody
]
[]
, button
[ onClick SaveCard ]
[ text "save" ]
]
in
case editState_ of
Just { title, fieldTitle, fieldBody } ->
if card.title == title then
editTemplate fieldTitle
else
viewTemplate
Nothing ->
viewTemplate
viewBody : String -> Html Msg
viewBody str =
let
matchToLink { match } =
let
m =
match |> String.dropLeft 2 |> String.dropRight 2 |> String.trim
in
String.join ""
[ "<a href=\"javascript:linkClicked('", m, "')\">", m, "</a>" ]
wikiLinksRegex =
case R.fromString "\\[\\[([^\\]]+)\\]\\]" of
Just regex ->
regex
Nothing ->
-- TODO: Do something smarter here
R.never
parsedWikiLinks =
R.replace
wikiLinksRegex
matchToLink
str
--parsedWikiLinks =
-- R.replace R.Match --cambiato da R.All ad R.Match
-- (R.fromString "\\[\\[([^\\]]+)\\]\\]") -- cambiato da R.regex ad R.fromString
-- matchToLink
-- str
in
Markdown.toHtml [] parsedWikiLinks
-- View: Contents List
viewContents : List Card -> Html Msg
viewContents cards =
div
[ id "content" ]
[ button [ onClick NewCard ] [ text "+" ]
, ul
[]
(List.map viewCardItem cards)
]
viewCardItem : Card -> Html Msg
viewCardItem card =
li
[ id ("card-item-" ++ card.title)
, classList
[ ( "card-item", True )
]
, onClick (OpenCard card.title)
]
[ text card.title ]
-- View: Model Data
viewModel : Model -> Html Msg
viewModel model =
div
[ id "model"
]
[ div [ id "model-data" ]
(viewListCardData model.data)
, div [ id "model-visible" ]
(List.map (\v -> div [ attribute "title" v ] []) model.visible)
]
viewListCardData : List Card -> List (Html Msg)
viewListCardData cards =
let
viewCardData card =
div
[ attribute "title" card.title ]
[ pre [] [ text card.body ] ]
in
List.map viewCardData cards
-- HELPERS
getStory : List Card -> List String -> List Card
getStory cards visible =
let
mapFn v =
if v == "" then
Just (Card "" "")
else
ListExtra.find (\c -> c.title == v) cards
in
visible
|> List.filterMap mapFn
relativeGetCard : Int -> List Card -> String -> Maybe String
relativeGetCard delta cards title =
let
currIndex_ =
ListExtra.findIndex (\c -> c.title == title) cards
in
case currIndex_ of
Just currIndex ->
ListExtra.getAt (currIndex + delta) cards
|> Maybe.map (\c -> c.title)
Nothing ->
Nothing
getNext : List Card -> String -> Maybe String
getNext cards title =
relativeGetCard 1 cards title
getPrev : List Card -> String -> Maybe String
getPrev cards title =
relativeGetCard -1 cards title
normalMode : Msg -> Model -> ( Model, Cmd Msg )
normalMode msg model =
case model.editing of
Just title ->
( model
, Cmd.batch []
)
Nothing ->
update msg model
focus : String -> Cmd Msg
focus elemId =
Task.attempt (\_ -> NoOp) (Dom.focus elemId)