-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
186 lines (154 loc) · 4.27 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
module Main exposing (main)
import Html exposing (Html)
import Html.Attributes as Attr
import Html.Events as Events
import Markdown
import Regex as Re
type alias Flags =
{ template : String
}
type alias Template = String
type Field =
Field
{ token : String
, question : String
, answer : String
, type_ : String
, placeholder : String
}
type Model =
Model
{ template : Template
, fields : List Field
}
type Msg
= UpdateField Field String
{-| init
-}
init : Flags -> ( Model, Cmd Msg )
init flags =
let
-- Assign model values
( template, fields ) = parseTemplate flags.template
-- Construct model object
model = Model
{ template = template
, fields = fields
}
in
model ! [ Cmd.none ]
{-| view
-}
view : Model -> Html Msg
view ( Model { template, fields } ) =
Html.section [ Attr.id "template-editor", Attr.class "flex two" ]
[ Html.div [ Attr.id "edit" ]
[ Html.form [ ]
( List.map (\field -> fieldView field) fields )
]
, Html.div [ Attr.id "preview" ]
[ preview template fields ]
]
fieldView : Field -> Html Msg
fieldView (Field field) =
Html.label [ ]
[ Html.text field.question
, if "textarea" == field.type_ then
Html.textarea
[ Events.onInput (UpdateField (Field field))
, Attr.value field.answer
, Attr.placeholder field.placeholder
] [ ]
else
Html.input
[ Events.onInput (UpdateField (Field field))
, Attr.type_ field.type_
, Attr.value field.answer
, Attr.placeholder field.placeholder
] [ ]
]
preview : Template -> List Field -> Html Msg
preview template fields =
let
withDefault = (\s def -> if String.isEmpty s then def else s)
final = List.foldl (\(Field field) result ->
-- Use Regex.replace if String.split proves too unflexible; I may need
-- mode control over field.t in order to pull that of.
-- Re.replace (Re.AtMost 1) field.re (\_ -> field.a) result
String.split field.token result
|> String.join (withDefault field.answer "…")
) template fields
in
Markdown.toHtml [ ] final
{-| update
-}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg ( Model model ) =
case msg of
UpdateField field val ->
let
fields = List.map (\(Field f) ->
if (Field f) == field then
Field { f | answer = val }
else
Field f
) model.fields
in
( Model { model | fields = fields } ) ! [ Cmd.none ]
{-| main
-}
main : Program Flags Model Msg
main =
Html.programWithFlags
{ init = init
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
-- Helper functions
parseTemplate : String -> ( Template, List Field )
parseTemplate str =
let
fields = List.map (\match ->
let
-- utility function for extracting submatches
submatch = (\i default ->
match.submatches
|> List.drop i
|> List.head
|> Maybe.withDefault Nothing
|> Maybe.withDefault default
)
attrs = (\list i default ->
list
|> List.drop i
|> List.head
|> Maybe.withDefault ""
|> (\val -> if (String.isEmpty val) then default else val)
) (String.split ":" (submatch 1 ""))
-- value assignment
token = match.match
question = submatch 0 ""
type_ = attrs 0 "text"
value = attrs 1 ""
placeholder = attrs 2 ""
-- type_ = attrs |> List.drop 0 |> List.head |> Maybe.withDefault "text"
-- placeholder = attrs |> List.drop 1 |> List.head |> Maybe.withDefault ""
in
Field
{ token = token
, question = question
, answer = value
, type_ = type_
, placeholder = placeholder
}
) (Re.find Re.All regex str)
in
( str, fields )
-- I'll need to construct a better regular expression, preferably one that's
-- unicode aware. More information on MDN at:
-- https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
regex : Re.Regex
regex =
Re.regex "\\[=(.+?)\\](?:\\((.+?)\\))?"
-- Re.regex "\\[=(.+?)\\](?:\\((.+?)\\))?"