This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
/
Component.purs
492 lines (444 loc) · 16.2 KB
/
Component.purs
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
-- | This module defines a component for rendering SlamDown documents to HTML
module Text.Markdown.SlamDown.Halogen.Component
( SlamDownConfig(..)
, defaultBrowserFeatures
, SlamDownMessage(..)
, slamDownComponent
, renderSlamDown
, evalSlamDownQuery
, module SDS
, module SDQ
) where
import Prelude
import Control.Alt ((<|>))
import Data.Array as A
import Data.BrowserFeatures (BrowserFeatures)
import Data.Either as E
import Data.Foldable as F
import Data.Functor.Compose (Compose(..))
import Data.Identity (Identity(..))
import Data.List as L
import Data.Maybe as M
import Data.Newtype (unwrap)
import Data.Set as Set
import Data.String as S
import Data.StrMap as SM
import Data.Traversable (traverse)
import Data.Validation.Semigroup as V
import DOM.HTML.Indexed.StepValue (StepValue(..))
import Halogen as H
import Halogen.HTML.Core as HC
import Halogen.HTML.Events as HE
import Halogen.HTML as HH
import Halogen.HTML.Properties as HP
import Text.Markdown.SlamDown as SD
import Text.Markdown.SlamDown.Halogen.Component.Query as SDQ
import Text.Markdown.SlamDown.Halogen.Component.State as SDS
import Text.Markdown.SlamDown.Halogen.Fresh as Fresh
import Text.Markdown.SlamDown.Halogen.InputType as SDIT
import Text.Markdown.SlamDown.Parser.Inline as SDPI
import Text.Markdown.SlamDown.Pretty as SDPR
import Text.Parsing.Parser as P
data SlamDownMessage v
= TextBoxChanged String (SD.TextBox M.Maybe)
| CheckBoxChanged String v Boolean
| RadioButtonChanged String v
| DropDownChanged String (M.Maybe v)
-- | By default, no features are enabled.
defaultBrowserFeatures ∷ BrowserFeatures
defaultBrowserFeatures =
{ inputTypeSupported : const false
}
evalSlamDownQuery
∷ ∀ m v
. (SD.Value v)
⇒ SDQ.SlamDownQuery v
~> H.ComponentDSL (SDS.SlamDownState v) (SDQ.SlamDownQuery v) (SlamDownMessage v) m
evalSlamDownQuery e =
case e of
SDQ.ChangeTextBox key tb next → do
H.modify <<< SDS.modifyFormState $
SM.insert key (SD.TextBox $ SD.transTextBox (Compose <<< map pure) tb)
H.raise $ TextBoxChanged key tb
pure next
SDQ.ChangeRadioButton key val next → do
H.modify \state →
flip SDS.modifyFormState state <<< SM.insert key $
case SDS.getFormFieldValue key state of
M.Just rb @ (SD.RadioButtons (Identity _) (Identity vals)) →
case L.elemIndex val vals of
M.Just _ → SD.RadioButtons (pure val) (pure vals)
M.Nothing → rb
_ → SD.RadioButtons (pure val) (pure (L.singleton val))
H.raise $ RadioButtonChanged key val
pure next
SDQ.ChangeDropDown key mval next → do
H.modify \state →
flip SDS.modifyFormState state <<< SM.insert key $
case SDS.getFormFieldValue key state of
M.Just rb @ (SD.DropDown _ (Identity vals)) →
case mval of
M.Just val →
if F.elem val vals
then SD.DropDown (M.Just $ pure val) (pure vals)
else rb
M.Nothing → SD.DropDown M.Nothing (pure vals)
_ → SD.DropDown (pure <$> mval) (pure $ M.maybe L.Nil L.singleton mval)
H.raise $ DropDownChanged key mval
pure next
SDQ.ChangeCheckBox key val checked next → do
let
updateSel ∷ Set.Set v → Set.Set v
updateSel =
if checked
then Set.insert val
else Set.delete val
update ∷ M.Maybe (SDS.FormFieldValue v) → SDS.FormFieldValue v
update (M.Just cb @ (SD.CheckBoxes (Identity sel) (Identity vals))) =
SD.CheckBoxes (pure $ sel') (pure vals)
where
sel' = L.fromFoldable $ Set.intersection (Set.fromFoldable vals) $ updateSel $ Set.fromFoldable sel
update _ = SD.CheckBoxes (pure $ L.fromFoldable $ updateSel Set.empty) (pure $ L.singleton val)
H.modify \state →
SDS.modifyFormState
(SM.insert key <<< update $ SDS.getFormFieldValue key state)
state
H.raise $ CheckBoxChanged key val checked
pure next
SDQ.GetFormState k → do
SDS.SlamDownState state ← H.get
let defaultFormState = SDS.formStateFromDocument state.document
pure $ k $ SM.union state.formState defaultFormState
SDQ.PopulateForm values next → do
H.modify \(SDS.SlamDownState { document }) →
SDS.syncState document values
pure next
SDQ.SetDocument doc next → do
H.modify $ SDS.replaceDocument doc
pure next
type SlamDownConfig =
{ formName ∷ String
, browserFeatures ∷ BrowserFeatures
}
type FreshRenderer p v a = a → Fresh.Fresh (H.HTML p (SDQ.SlamDownQuery v))
type FormFieldDef a =
{ label ∷ String
, required ∷ Boolean
, field ∷ SD.FormField a
}
type FormFieldBaked p v =
{ label ∷ String
, required ∷ Boolean
, field ∷ H.HTML p (SDQ.SlamDownQuery v)
, ident ∷ M.Maybe String
}
-- | Render a `SlamDown` document into an HTML form.
renderSlamDown
∷ ∀ v
. (SD.Value v)
⇒ SlamDownConfig
→ SDS.SlamDownState v
→ H.ComponentHTML (SDQ.SlamDownQuery v)
renderSlamDown config (SDS.SlamDownState state) =
case state.document of
SD.SlamDown bs →
HH.div_
$ Fresh.runFresh config.formName
$ renderBlocks bs
where
defaultFormState ∷ SDS.SlamDownFormState v
defaultFormState = SDS.formStateFromDocument state.document
h_ ∷ ∀ p a. Int → Array (H.HTML p a) → H.HTML p a
h_ 1 = HH.h1_
h_ 2 = HH.h2_
h_ 3 = HH.h3_
h_ 4 = HH.h4_
h_ 5 = HH.h5_
h_ _ = HH.h6_
el_ ∷ ∀ p a. SD.ListType → Array (H.HTML p a) → H.HTML p a
el_ (SD.Bullet _) = HH.ul_
el_ (SD.Ordered _) = HH.ol_
renderInline ∷ ∀ p. FreshRenderer p v (SD.Inline v)
renderInline i =
case i of
SD.Str s → pure $ HH.text s
SD.Entity s → pure $ HH.text s
SD.Space → pure $ HH.text " "
SD.SoftBreak → pure $ HH.text "\n"
SD.LineBreak → pure $ HH.br_
SD.Emph is → HH.em_ <$> traverse renderInline (A.fromFoldable is)
SD.Strong is → HH.strong_ <$> traverse renderInline (A.fromFoldable is)
SD.Code _ c → pure $ HH.code_ [ HH.text c ]
SD.Link body tgt → do
let
href (SD.InlineLink url) = url
href (SD.ReferenceLink tgt') = M.maybe "" ("#" <> _) tgt'
HH.a [ HP.href $ href tgt ] <$> traverse renderInline (A.fromFoldable body)
SD.Image body url →
pure $ HH.img
[ HP.src url
, HP.alt $ F.foldMap stripInline body
]
SD.FormField label required field →
renderInlineFormField <$> bakeFormField { label, required, field }
bakeFormField ∷ ∀ p. FormFieldDef v → Fresh.Fresh (FormFieldBaked p v)
bakeFormField { label, required, field } = do
ident ← Fresh.fresh
let
unquote = SD.traverseFormField (SD.getLiteral >>> map pure)
quote = SD.transFormField (unwrap >>> SD.Literal)
field' =
unquote <<< ensureValidField <<< M.maybe field quote $
SM.lookup label state.formState <|> SM.lookup label defaultFormState
el ←
case field' of
M.Nothing → pure $ HH.text "Unsupported form element"
M.Just fv → renderFormElement config state ident label fv
let
requiresId =
case field' of
M.Just (SD.CheckBoxes _ _) → false
M.Just (SD.RadioButtons _ _) → false
_ → true
pure
{ label
, required
, field: el
, ident: if requiresId then M.Just ident else M.Nothing
}
renderInlineFormField ∷ ∀ p. FormFieldBaked p v → H.HTML p (SDQ.SlamDownQuery v)
renderInlineFormField { field, label, required, ident } =
HH.span
[ HP.class_ (HH.ClassName "slamdown-field") ]
[ HH.label
(M.maybe [] (pure <<< HP.for) ident)
[ HH.text (label <> if required then "*" else "") ]
, field
]
renderFormSetFormField ∷ ∀ p. FormFieldBaked p v → H.HTML p (SDQ.SlamDownQuery v)
renderFormSetFormField { field, label, required, ident } =
HH.tr_
[ HH.th_
[ HH.label
(M.maybe [] (pure <<< HP.for) ident)
[ HH.text (label <> if required then "*" else "") ]
]
, HH.td_
[ field ]
]
renderFormSet ∷ ∀ p. FreshRenderer p v (L.List (FormFieldDef v))
renderFormSet fs =
HH.table [ HP.class_ (HH.ClassName "slamdown-formset") ]
<$> traverse (map renderFormSetFormField <<< bakeFormField) (A.fromFoldable fs)
stripInline ∷ SD.Inline v → String
stripInline i =
case i of
SD.Str s → s
SD.Entity s → s
SD.Space → " "
SD.SoftBreak → "\n"
SD.LineBreak → "\n"
SD.Emph is → F.foldMap stripInline is
SD.Strong is → F.foldMap stripInline is
SD.Code _ c → c
SD.Link body _ → F.foldMap stripInline body
_ → ""
renderBlock ∷ ∀ p. FreshRenderer p v (SD.Block v)
renderBlock b =
case b of
SD.Paragraph is →
HH.p_ <$> traverse renderInline (A.fromFoldable is)
SD.Header lvl is →
h_ lvl <$> traverse renderInline (A.fromFoldable is)
SD.Blockquote bs →
HH.blockquote_ <$> renderBlocks bs
SD.Lst lt bss → do
let
item ∷ FreshRenderer p v (L.List (SD.Block v))
item bs = HH.li_ <$> renderBlocks bs
el_ lt <$> traverse item (A.fromFoldable bss)
SD.CodeBlock _ ss →
pure $ HH.pre_ [ HH.code_ [ HH.text (S.joinWith "\n" $ A.fromFoldable ss) ] ]
SD.LinkReference l url →
pure $ HH.p_
[ HH.text (l <> ": ")
, HH.a [ HP.id_ l, HP.href url ] [ HH.text url ]
]
SD.Rule →
pure HH.hr_
renderBlocks ∷ ∀ p. L.List (SD.Block v) → Fresh.Fresh (Array (H.HTML p (SDQ.SlamDownQuery v)))
renderBlocks = go [] L.Nil
where
go html fs bs =
case bs of
L.Cons (SD.Paragraph (L.Cons (SD.FormField label required field) L.Nil)) bs' →
go html (L.Cons { label, required, field } fs) bs'
L.Cons b bs' → do
bHtml ← renderBlock b
if L.null fs
then go (html <> pure bHtml) L.Nil bs'
else do
fsHtml ← renderFormSet (L.reverse fs)
go (html <> pure fsHtml <> pure bHtml) L.Nil bs'
L.Nil →
if L.null fs
then pure html
else do
fsHtml ← renderFormSet (L.reverse fs)
pure (html <> pure fsHtml)
-- | Make sure that the default value of a form field is valid, and if it is not, strip it out.
ensureValidField ∷ SD.FormField v → SD.FormField v
ensureValidField field =
V.unV
(const $ stripDefaultValue field)
id
(SDPI.validateFormField field)
where
stripDefaultValue ∷ SD.FormField v → SD.FormField v
stripDefaultValue =
case _ of
SD.TextBox tb → SD.TextBox $ SD.transTextBox (\_ → Compose M.Nothing) tb
SD.DropDown _ ls → SD.DropDown M.Nothing ls
field' → field'
renderRadioButton
∷ ∀ p v
. (SD.Value v)
⇒ String -- label
→ v -- value
→ FreshRenderer p v Boolean
renderRadioButton label value checked = do
id ← Fresh.fresh
let renderedValue = SD.renderValue value
pure $ HH.li_
[ HH.input
[ HP.checked checked
, HP.type_ HP.InputRadio
, HP.id_ id
, HP.name label
, HP.value renderedValue
, HE.onValueChange (HE.input_ (SDQ.ChangeRadioButton label value))
]
, HH.label [ HP.for id ] [ HH.text renderedValue ]
]
renderCheckBox
∷ ∀ p v
. (SD.Value v)
⇒ String -- label
→ v -- value
→ FreshRenderer p v Boolean
renderCheckBox label value checked = do
id ← Fresh.fresh
let renderedValue = SD.renderValue value
pure $ HH.li_
[ HH.input
[ HP.checked checked
, HP.type_ HP.InputCheckbox
, HP.id_ id
, HP.name label
, HP.value renderedValue
, HE.onChecked (HE.input (SDQ.ChangeCheckBox label value))
]
, HH.label [ HP.for id ] [ HH.text renderedValue ]
]
renderDropDown
∷ ∀ p v
. (SD.Value v)
⇒ String -- id
→ String -- label
→ L.List v -- choices
→ M.Maybe v -- value
→ H.HTML p (SDQ.SlamDownQuery v)
renderDropDown id label ls sel =
HH.select
[ HP.id_ id
, HP.name label
, HE.onSelectedIndexChange (HE.input (SDQ.ChangeDropDown label <<< L.index ls))
]
$ A.fromFoldable $ M.maybe option option' sel <$> ls
where
option ∷ v → H.HTML p (SDQ.SlamDownQuery v)
option value =
let renderedValue = SD.renderValue value
in HH.option [ HP.value renderedValue ] [ HH.text renderedValue ]
option' ∷ v → v → H.HTML p (SDQ.SlamDownQuery v)
option' sel' value =
let renderedValue = SD.renderValue value
in HH.option [ HP.selected (value == sel'), HP.value renderedValue ] [ HH.text renderedValue ]
renderFormElement
∷ ∀ p v
. (SD.Value v)
⇒ SlamDownConfig
→ SDS.SlamDownStateR v
→ String -- element id
→ String -- label
→ FreshRenderer p v (SDS.FormFieldValue v)
renderFormElement config st id label field =
case field of
SD.TextBox tb →
pure $ renderTextInput tb
SD.RadioButtons (Identity sel) (Identity ls) → do
let
renderRadioButton' val = renderRadioButton label val $ sel == val
options = A.fromFoldable $ if sel `F.elem` ls then ls else L.Cons sel ls
radios ← traverse renderRadioButton' options
pure $ HH.ul [ HP.class_ (HH.ClassName "slamdown-radios") ] radios
SD.CheckBoxes (Identity sel) (Identity ls) → do
let
sel' = Set.fromFoldable sel
renderCheckBox' val = renderCheckBox label val (Set.member val sel')
checkBoxes ← traverse renderCheckBox' ls
pure $ HH.ul [ HP.class_ (HH.ClassName "slamdown-checkboxes") ] $ A.fromFoldable checkBoxes
SD.DropDown msel (Identity ls) →
pure $
case msel of
M.Nothing → renderDropDown id label ls M.Nothing
M.Just (Identity sel) →
let options = if sel `F.elem` ls then ls else L.Cons sel ls
in renderDropDown id label options $ M.Just sel
where
renderTextInput
∷ SD.TextBox (Compose M.Maybe Identity)
→ H.HTML p (SDQ.SlamDownQuery v)
renderTextInput tb =
HH.input $
[ HP.type_ compatibleInputType
, HP.id_ id
, HP.name label
, HE.onValueInput (HE.input (SDQ.ChangeTextBox label <<< parseInput))
]
<> typeSettings
<> M.maybe [] (\x → [HP.value x]) renderedValue
where
renderedValue =
SDPR.prettyPrintTextBoxValue <$>
SD.traverseTextBox unwrap tb
parser ∷ P.Parser String (SD.TextBox Identity)
parser = SDPI.parseTextBox (\_ → true) (map pure) tb
parseInput ∷ String → SD.TextBox M.Maybe
parseInput str =
case P.runParser str parser of
E.Right tb' → SD.transTextBox (unwrap >>> pure) tb'
E.Left err → SD.transTextBox (\_ → M.Nothing) tb
typeSettings :: forall r i. Array (HP.IProp (step ∷ StepValue | r) i)
typeSettings = case compatibleInputType, tb of
HP.InputTime, SD.Time SD.Seconds _ → [ step (Step 1.0) ]
HP.InputDatetimeLocal, SD.DateTime SD.Seconds _ → [ step (Step 1.0) ]
_, _ → []
compatibleInputType =
SDIT.inputTypeToHalogenInputType $
SDIT.availableInputType config.browserFeatures tb
step ∷ ∀ r i. StepValue → HP.IProp (step ∷ StepValue | r) i
step = HP.prop (HC.PropName "step")
-- | Bundles up the SlamDown renderer and state machine into a Halogen component.
slamDownComponent
∷ ∀ m v
. (SD.Value v)
⇒ SlamDownConfig
→ H.Component HH.HTML (SDQ.SlamDownQuery v) Unit (SlamDownMessage v) m
slamDownComponent config =
H.component
{ render: renderSlamDown config
, eval: evalSlamDownQuery
, initialState: const SDS.emptySlamDownState
, receiver: const M.Nothing
}