-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.elm
201 lines (168 loc) · 4.99 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
module Main exposing (main)
import Browser
import Element exposing (Element, alignBottom, alignTop, column, el, fill, fillPortion, height, padding, paragraph, px, row, spacing, text, textColumn, width, wrappedRow)
import Element.Font as Font
import Element.Input as Input
import Html
import Html.Parser
import List.Extra
type alias Model =
List (List ( String, String ))
type alias Msg =
Model
main : Program () Model Msg
main =
Browser.sandbox
{ init = init
, view =
\model ->
Element.layout
[ width fill
, height fill
, padding 8
]
(view model)
, update = update
}
init : Model
init =
[]
view : Model -> Element Msg
view model =
let
stringVersion : String
stringVersion =
modelToString model
in
column
[ spacing 8
, width fill
, height fill
]
[ row
[ spacing 8
, width fill
, height fill
]
[ viewLines model
, stringVersion
|> String.split "\n"
|> List.map (\line -> paragraph [] [ text line ])
|> textColumn
[ width fill
, alignTop
, Font.size 14
]
]
, viewPreview stringVersion
]
viewPreview : String -> Element Msg
viewPreview input =
case Html.Parser.run Html.Parser.allCharRefs input of
Ok parsed ->
Html.Parser.nodesToHtml parsed
|> Html.div []
|> Element.html
|> el [ width fill, alignBottom ]
Err _ ->
text "Could not parse HTML"
viewLines : Model -> Element Msg
viewLines model =
(model ++ [ [] ])
|> List.indexedMap
(\i line ->
viewLine line
|> Element.map
(\newLine ->
if i == List.length model then
model ++ [ newLine ]
else
List.Extra.setAt i newLine model
)
)
|> column
[ spacing 8
, width (fillPortion 3)
, alignTop
]
viewLine : List ( String, String ) -> Element (List ( String, String ))
viewLine chunks =
(chunks ++ [ ( "", "" ) ])
|> List.indexedMap
(\i chunk ->
viewChunk chunk
|> Element.map
(\newChunk ->
if i == List.length chunks then
chunks ++ [ newChunk ]
else
List.Extra.setAt i newChunk chunks
)
)
|> wrappedRow
[ spacing 2
, width fill
]
viewChunk : ( String, String ) -> Element ( String, String )
viewChunk ( text, ruby ) =
let
w : Element.Length
w =
if String.isEmpty text && String.isEmpty ruby then
px 40
else
Element.minimum
(max 50
(10 * max (String.length text) (String.length ruby))
)
fill
in
column [ spacing 2, width w ]
[ Input.text [ width w ]
{ label = Input.labelHidden ""
, text = ruby
, onChange = \newRuby -> ( text, newRuby )
, placeholder = Nothing
}
, Input.text [ width w ]
{ label = Input.labelHidden ""
, text = text
, onChange = \newText -> ( newText, ruby )
, placeholder = Nothing
}
]
modelToString : Model -> String
modelToString lines =
lines
|> List.map lineToString
|> String.join "\n"
lineToString : List ( String, String ) -> String
lineToString chunks =
let
middle : String
middle =
chunks
|> List.map chunkToString
|> String.concat
in
"<p>" ++ middle ++ "</p>"
chunkToString : ( String, String ) -> String
chunkToString ( text, ruby ) =
if String.isEmpty ruby then
text
else
let
rubied : String -> String
rubied input =
-- "<ruby style=\"ruby-align: center\">" ++ input ++ "<rp> (</rp><rt>" ++ ruby ++ "</rt><rp>)</rp></ruby>"
"<ruby style=\"ruby-align: start\">" ++ input ++ "<rp> (</rp><rt>" ++ ruby ++ "</rt><rp>)</rp></ruby>"
in
if String.endsWith " " text then
rubied (String.dropRight 1 text) ++ " "
else
rubied text
update : Msg -> Model -> Model
update msg _ =
msg
|> List.map (List.Extra.removeWhen (\( a, b ) -> String.isEmpty a && String.isEmpty b))
|> List.Extra.removeWhen List.isEmpty