/
Main.elm
251 lines (219 loc) · 8 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
module Main exposing (..)
import Attachment exposing (Attachment)
import Browser
import Browser.Dom as Dom
import Browser.Events
import Comment exposing (Comment)
import Constraint
import Dict exposing (Dict)
import Html exposing (Html)
import Html.Attributes as Attrs
import Html.Events
import Json.Decode as Decode
import Task exposing (Task)
type alias Model =
{ attachments : Dict Int Attachment
, comments : Dict Int Comment
, dragging : Maybe Int
, commentPositions : Maybe Constraint.Model
}
type Msg
= MouseDownOnAttachment Int
| MouseUp
| MouseMove Float
| AttachmentsMoved (List ( Int, Float ))
| SetUpCommentConstraints ( List ( Int, Float ), List ( Int, Float ) )
init : () -> ( Model, Cmd Msg )
init _ =
let
attachments =
[ Attachment 1 200 1
, Attachment 2 220 2
, Attachment 3 400 3
]
comments =
[ Comment 1 180
, Comment 2 120
, Comment 3 100
]
in
( { attachments =
attachments
|> List.map (\({ id } as attachment) -> ( id, attachment ))
|> Dict.fromList
, comments =
comments
|> List.map (\({ id } as comment) -> ( id, comment ))
|> Dict.fromList
, dragging = Nothing
, commentPositions = Nothing
}
, Task.map2 Tuple.pair
(findNewAttachmentTopsTask attachments)
(findCommentHeightsTask comments)
|> Task.perform SetUpCommentConstraints
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
MouseDownOnAttachment id ->
( { model | dragging = Just id }, Cmd.none )
MouseUp ->
( { model | dragging = Nothing }, Cmd.none )
MouseMove top ->
case model.dragging of
Just id ->
( { model
| attachments =
Dict.update
id
(Maybe.map (\attachment -> { attachment | top = top }))
model.attachments
}
, findNewAttachmentTops (Dict.values model.attachments)
)
Nothing ->
( model, Cmd.none )
AttachmentsMoved attachments ->
( { model
| commentPositions =
Maybe.map
(Constraint.updateAttachments (finalAttachments attachments))
model.commentPositions
}
, Cmd.none
)
SetUpCommentConstraints ( attachments, commentHeights ) ->
( { model
| commentPositions =
Just <|
Constraint.init
{ heights = Dict.fromList commentHeights
, attachments = finalAttachments attachments
, margin = 10
}
}
, Cmd.none
)
finalAttachments : List ( Int, Float ) -> Dict Int Float
finalAttachments =
List.foldl
(\( commentId, top ) soFar ->
Dict.update commentId
(\current ->
case current of
Nothing ->
Just top
Just otherTop ->
Just (min top otherTop)
)
soFar
)
Dict.empty
findNewAttachmentTopsTask : List Attachment -> Task Never (List ( Int, Float ))
findNewAttachmentTopsTask attachments =
-- TODO: this may need Process.sleep 0 to be accurate in all cases
attachments
|> List.map
(\{ id, commentId } ->
Dom.getElement ("attachment-" ++ String.fromInt id)
|> Task.map (\{ element } -> Just ( commentId, element.y ))
|> Task.onError (\_ -> Task.succeed Nothing)
)
|> Task.sequence
|> Task.map (List.filterMap identity)
findNewAttachmentTops : List Attachment -> Cmd Msg
findNewAttachmentTops attachments =
Task.perform AttachmentsMoved (findNewAttachmentTopsTask attachments)
findCommentHeightsTask : List Comment -> Task Never (List ( Int, Float ))
findCommentHeightsTask comments =
-- TODO: this may need Process.sleep 0 to be accurate in all cases
comments
|> List.map
(\{ id } ->
Dom.getElement ("comment-" ++ String.fromInt id)
|> Task.map (\{ element } -> Just ( id, element.height ))
|> Task.onError (\_ -> Task.succeed Nothing)
)
|> Task.sequence
|> Task.map (List.filterMap identity)
view : Model -> Browser.Document Msg
view model =
{ title = "Comment Constraint Experiment"
, body =
[ Html.node "style" [] [ Html.text "body { user-select: none; }" ]
, Html.main_
[ Attrs.style "width" "100%"
, Attrs.style "height" "100vh"
, Attrs.style "background-color" "aliceblue"
]
(let
horizMargin =
50
in
-- line
[ Html.div
[ Attrs.style "width" "1px"
, Attrs.style "height" "94vh"
, Attrs.style "position" "absolute"
, Attrs.style "left" (String.fromInt horizMargin ++ "px")
, Attrs.style "top" "3vh"
, Attrs.style "background-color" "blue"
]
[]
]
-- attachments
++ List.map
(\( id, attachment ) ->
Html.div
[ Attrs.id ("attachment-" ++ String.fromInt id)
-- events
, Html.Events.onMouseDown (MouseDownOnAttachment id)
-- position
, Attrs.style "position" "absolute"
, Attrs.style "left" (String.fromInt horizMargin ++ "px")
, Attrs.style "top" (String.fromFloat attachment.top ++ "px")
]
[ Attachment.view attachment
]
)
(Dict.toList model.attachments)
-- comments
++ List.map
(\( id, comment ) ->
Html.div
[ Attrs.id ("comment-" ++ String.fromInt id)
-- position
, Attrs.style "transition" "top 0.5s ease"
, Attrs.style "position" "absolute"
, Attrs.style "left" (String.fromInt (horizMargin * 2) ++ "px")
, model.commentPositions
|> Maybe.map Constraint.positions
|> Maybe.andThen (Dict.get id)
-- render way off the screen so we can still get heights
|> Maybe.withDefault -9999
|> (\top -> String.fromFloat top ++ "px")
|> Attrs.style "top"
]
[ Comment.view comment ]
)
(Dict.toList model.comments)
)
]
}
main : Program () Model Msg
main =
Browser.document
{ init = init
, update = update
, view = view
, subscriptions =
\{ dragging } ->
Sub.batch
[ Browser.Events.onMouseUp (Decode.succeed MouseUp)
, if dragging /= Nothing then
Browser.Events.onMouseMove (Decode.map MouseMove (Decode.field "pageY" Decode.float))
else
Sub.none
]
}