/
Main.elm
162 lines (138 loc) · 4.71 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
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 : List Comment
, dragging : Maybe Int
}
type Msg
= MouseDownOnAttachment Int
| MouseUp
| MouseMove Float
| AttachmentsMoved (List ( Int, Float ))
init : () -> ( Model, Cmd Msg )
init _ =
let
attachments =
Dict.fromList
[ ( 1, Attachment 200 )
, ( 2, Attachment 220 )
, ( 3, Attachment 400 )
]
in
( { attachments = attachments
, comments = []
, dragging = Nothing
}
, findNewAttachmentTops (Dict.keys attachments)
)
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 top))
model.attachments
}
, findNewAttachmentTops (Dict.keys model.attachments)
)
Nothing ->
( model, Cmd.none )
AttachmentsMoved tops ->
( 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, (Debug.log "element" 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)
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 top) as 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 top ++ "px")
]
[ Attachment.view attachment
]
)
(Dict.toList model.attachments)
-- comments
++ List.map Comment.view 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
]
}