Skip to content

Commit

Permalink
get attachment locations
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Feb 17, 2020
1 parent ee4db50 commit 52f1349
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions src/Main.elm
@@ -1,14 +1,16 @@
module Main exposing (..)

import Attachment exposing (Attachment)
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


type alias Model =
Expand All @@ -21,21 +23,25 @@ type alias Model =
type Msg
= MouseDownOnAttachment Int
| MouseUp
| MouseMove Int
| MouseMove Float
| AttachmentsMoved (List ( Int, Float ))


init : () -> ( Model, Cmd Msg )
init _ =
( { attachments =
let
attachments =
Dict.fromList
[ ( 1, Attachment 20 )
, ( 2, Attachment 40 )
, ( 3, Attachment 100 )
[ ( 1, Attachment 200 )
, ( 2, Attachment 220 )
, ( 3, Attachment 400 )
]
in
( { attachments = attachments
, comments = []
, dragging = Nothing
}
, Cmd.none
, findNewAttachmentTops (Dict.keys attachments)
)


Expand All @@ -55,15 +61,37 @@ update msg model =
| attachments =
Dict.update
id
(Maybe.map (\attachment -> { attachment | top = top }))
(Maybe.map (\_ -> Attachment top))
model.attachments
}
, Cmd.none
, findNewAttachmentTops (Dict.keys model.attachments)
)

Nothing ->
( model, Cmd.none )

AttachmentsMoved tops ->
let
_ =
Debug.log "tops" tops
in
( model, Cmd.none )


findNewAttachmentTops : List Int -> Cmd Msg
findNewAttachmentTops 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)
|> Task.perform AttachmentsMoved


view : Model -> Browser.Document Msg
view model =
Expand Down Expand Up @@ -92,14 +120,17 @@ view model =
]
-- attachments
++ List.map
(\( id, attachment ) ->
(\( id, (Attachment top) as attachment ) ->
Html.div
[ Html.Events.onMouseDown (MouseDownOnAttachment id)
[ 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.fromInt attachment.top ++ "px")
, Attrs.style "top" (String.fromFloat top ++ "px")
]
[ Attachment.view attachment
]
Expand All @@ -123,7 +154,7 @@ main =
Sub.batch
[ Browser.Events.onMouseUp (Decode.succeed MouseUp)
, if dragging /= Nothing then
Browser.Events.onMouseMove (Decode.map MouseMove (Decode.field "pageY" Decode.int))
Browser.Events.onMouseMove (Decode.map MouseMove (Decode.field "pageY" Decode.float))

else
Sub.none
Expand Down

0 comments on commit 52f1349

Please sign in to comment.