/
Main.elm
214 lines (183 loc) · 6.39 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
module Main exposing (..)
import Attachment exposing (Attachment)
import Browser
import Browser.Dom as Dom
import Browser.Events
import Comment exposing (Comment)
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
}
type Msg
= MouseDownOnAttachment Int
| MouseUp
| MouseMove Float
| AttachmentsMoved (List ( Int, Float ))
| GotCommentHeights (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
}
, Cmd.batch
[ findNewAttachmentTops (List.map .id attachments)
, findCommentHeights (List.map .id comments)
]
)
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.keys model.attachments)
)
Nothing ->
( model, Cmd.none )
AttachmentsMoved tops ->
( model, Cmd.none )
GotCommentHeights heights ->
let
_ =
Debug.log "heights" heights
in
( model, Cmd.none )
findNewAttachmentTopsTask : List Int -> Task Never (List ( Int, Float ))
findNewAttachmentTopsTask ids =
-- TODO: this may need Process.sleep 0 to be accurate in all cases
ids
|> List.map
(\id ->
Dom.getElement ("attachment-" ++ String.fromInt id)
|> Task.map (\{ element } -> Just ( id, element.y ))
|> Task.onError (\_ -> Task.succeed Nothing)
)
|> Task.sequence
|> Task.map (List.filterMap identity)
findNewAttachmentTops : List Int -> Cmd Msg
findNewAttachmentTops ids =
Task.perform AttachmentsMoved (findNewAttachmentTopsTask ids)
findCommentHeightsTask : List Int -> Task Never (List ( Int, Float ))
findCommentHeightsTask ids =
-- TODO: this may need Process.sleep 0 to be accurate in all cases
ids
|> 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)
findCommentHeights : List Int -> Cmd Msg
findCommentHeights ids =
Task.perform GotCommentHeights (findCommentHeightsTask ids)
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 "position" "absolute"
, Attrs.style "left" (String.fromInt (horizMargin * 2) ++ "px")
]
[ 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
]
}