Skip to content

Commit

Permalink
Merge branch 'cleanup' into main
Browse files Browse the repository at this point in the history
* cleanup:
  Cleanup: pass plugin component to renderMarkdown
  Cleanup: clear tab header aria label when dragging
  Cleanup: only show dragged elements if dragType matches
  Cleanup: dragTracker needs to track the dragType
  Cleanup: fix function ordering
  Cleanup: only act on matching drag moves
  Cleanup: pass dragType with dragData from typescript
  Cleanup: construct beaconType from a dragType
  Cleanup: move board reordering logic to the board page
  Cleanup: not using DragData.beaconType so remove it
  Cleanup: tidy Session function ordering and labelling
  Cleanup: fix typo: Session.timeWIthZoneIs -> timeWithZoneIs
  Cleanup: Beacon.identifier -> uniqueId
  Cleanup: move DragTracker into own module
  Cleanup: move BeaconPosition decoding into BeaconPosition module
  Cleanup: simplify dragged item placement offset calculations
  • Loading branch information
roovo committed Nov 18, 2023
2 parents 791510a + a637cb8 commit 6f57a6a
Show file tree
Hide file tree
Showing 15 changed files with 620 additions and 328 deletions.
36 changes: 14 additions & 22 deletions src/DragAndDrop/BeaconPosition.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module DragAndDrop.BeaconPosition exposing
( BeaconPosition(..)
, decoder
, encoder
, identifier
, uniqueId
)

import Json.Decode as JD
import Json.Encode as JE
import TsJson.Decode as TsDecode



Expand All @@ -22,13 +22,12 @@ type BeaconPosition
-- ENCODE / DECODE


decoder : JD.Decoder BeaconPosition
decoder : TsDecode.Decoder BeaconPosition
decoder =
JD.map2
Tuple.pair
(JD.field "position" JD.string)
(JD.field "identifier" JD.string)
|> JD.andThen toPosition
TsDecode.oneOf
[ toElmBeacon "after" After TsDecode.string
, toElmBeacon "before" Before TsDecode.string
]


encoder : BeaconPosition -> JE.Value
Expand All @@ -44,16 +43,16 @@ encoder beaconPosition =
in
JE.object
[ ( "position", JE.string positionStr )
, ( "identifier", JE.string identifierString )
, ( "uniqueId", JE.string identifierString )
]



-- UTILS


identifier : BeaconPosition -> String
identifier beaconPosition =
uniqueId : BeaconPosition -> String
uniqueId beaconPosition =
case beaconPosition of
Before id ->
id
Expand All @@ -66,14 +65,7 @@ identifier beaconPosition =
-- PRIVATE


toPosition : ( String, String ) -> JD.Decoder BeaconPosition
toPosition ( position, identifier_ ) =
case position of
"before" ->
JD.succeed (Before identifier_)

"after" ->
JD.succeed (After identifier_)

_ ->
JD.fail ("Unknown position: " ++ position)
toElmBeacon : String -> (value -> a) -> TsDecode.Decoder value -> TsDecode.Decoder a
toElmBeacon tagName constructor decoder_ =
TsDecode.field "position" (TsDecode.literal constructor (JE.string tagName))
|> TsDecode.andMap (TsDecode.field "uniqueId" decoder_)
30 changes: 3 additions & 27 deletions src/DragAndDrop/DragData.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module DragAndDrop.DragData exposing
( BeaconData
, DragAction(..)
, DragData
, DragTracker
, decoder
)

Expand All @@ -18,7 +17,7 @@ import TsJson.Decode as TsDecode


type alias DragData =
{ beaconType : String
{ dragType : String
, dragAction : DragAction
, cursor : Coords
, offset : Coords
Expand All @@ -38,23 +37,14 @@ type alias BeaconData =
}


type alias DragTracker =
{ nodeId : String
, clientPos : Coords
, offsetPos : Coords
, offset : Coords
, draggedNodeRect : Rect
}



-- DECODERS


decoder : TsDecode.Decoder DragData
decoder =
TsDecode.succeed DragData
|> TsDecode.andMap (TsDecode.field "beaconType" TsDecode.string)
|> TsDecode.andMap (TsDecode.field "dragType" TsDecode.string)
|> TsDecode.andMap dragActionDecoder
|> TsDecode.andMap (TsDecode.field "cursor" Coords.decoder)
|> TsDecode.andMap (TsDecode.field "offset" Coords.decoder)
Expand All @@ -69,27 +59,13 @@ decoder =
beaconDataDecoder : TsDecode.Decoder BeaconData
beaconDataDecoder =
TsDecode.succeed BeaconData
|> TsDecode.andMap (TsDecode.field "beaconPosition" beaconPositionDecoder)
|> TsDecode.andMap (TsDecode.field "beaconPosition" BeaconPosition.decoder)
|> TsDecode.andMap (TsDecode.field "rect" Rect.decoder)


beaconPositionDecoder : TsDecode.Decoder BeaconPosition
beaconPositionDecoder =
TsDecode.oneOf
[ toElmBeacon "after" BeaconPosition.After TsDecode.string
, toElmBeacon "before" BeaconPosition.Before TsDecode.string
]


dragActionDecoder : TsDecode.Decoder DragAction
dragActionDecoder =
TsDecode.discriminatedUnion "dragAction"
[ ( "move", TsDecode.succeed Move )
, ( "stop", TsDecode.succeed Stop )
]


toElmBeacon : String -> (value -> a) -> TsDecode.Decoder value -> TsDecode.Decoder a
toElmBeacon tagName constructor decoder_ =
TsDecode.field "position" (TsDecode.literal constructor (JE.string tagName))
|> TsDecode.andMap (TsDecode.field "identifier" decoder_)
110 changes: 110 additions & 0 deletions src/DragAndDrop/DragTracker.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
module DragAndDrop.DragTracker exposing
( ClientData
, DragTracker(..)
, dragType
, init
, isDragging
, moveDragable
, stopTracking
, waitForDrag
)

import DragAndDrop.Coords as Coords exposing (Coords)
import DragAndDrop.DragData exposing (DragData)
import DragAndDrop.Rect as Rect exposing (Rect)



-- TYPES


type DragTracker
= NotDragging
| Waiting ClientData
| Dragging ClientData DomData


type alias ClientData =
{ uniqueId : String
, clientPos : Coords
, offsetPos : Coords
}


type alias DomData =
{ dragType : String
, offset : Coords
, draggedNodeStartRect : Rect
}


init : DragTracker
init =
NotDragging



-- INFO


dragType : DragTracker -> Maybe String
dragType dragTracker =
case dragTracker of
NotDragging ->
Nothing

Waiting _ ->
Nothing

Dragging _ domData ->
Just domData.dragType


isDragging : DragTracker -> Bool
isDragging dragTracker =
case dragTracker of
NotDragging ->
False

Waiting _ ->
False

Dragging _ _ ->
True



-- STATE TRANSFORMS


moveDragable : DragData -> DragTracker -> DragTracker
moveDragable dragData dragTracker =
case dragTracker of
NotDragging ->
dragTracker

Waiting clientData ->
Dragging
{ clientData | clientPos = dragData.cursor }
{ offset = dragData.offset
, draggedNodeStartRect = dragData.draggedNodeRect
, dragType = dragData.dragType
}

Dragging clientData domData ->
Dragging
{ clientData | clientPos = dragData.cursor }
{ domData
| offset = dragData.offset
, dragType = dragData.dragType
}


stopTracking : DragTracker
stopTracking =
NotDragging


waitForDrag : ClientData -> DragTracker
waitForDrag clientData =
Waiting clientData
6 changes: 3 additions & 3 deletions src/InteropDefinitions.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type FromElm
| ElmInitialized
| OpenTaskSourceFile { filePath : String, lineNumber : Int, originalText : String }
| RequestFilterCandidates
| TrackDraggable { beaconType : String, clientPos : Coords, draggableId : String }
| TrackDraggable { dragType : String, clientPos : Coords, draggableId : String }
| UpdateTasks { filePath : String, tasks : List { lineNumber : Int, originalText : String, newText : String } }


Expand Down Expand Up @@ -109,10 +109,10 @@ openTaskSourceFileEncoder =
]


trackDraggableEncoder : TsEncode.Encoder { beaconType : String, clientPos : Coords, draggableId : String }
trackDraggableEncoder : TsEncode.Encoder { dragType : String, clientPos : Coords, draggableId : String }
trackDraggableEncoder =
TsEncode.object
[ required "beaconType" .beaconType TsEncode.string
[ required "dragType" .dragType TsEncode.string
, required "clientPos" .clientPos Coords.encoder
, required "draggableId" .draggableId TsEncode.string
]
Expand Down
4 changes: 2 additions & 2 deletions src/InteropPorts.elm
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ rewriteTasks dataviewTaskCompletion taskCompletionFormat timeWithZone filePath t


trackDraggable : String -> Coords -> String -> Cmd msg
trackDraggable beaconType clientPos draggableId =
{ beaconType = beaconType, clientPos = clientPos, draggableId = draggableId }
trackDraggable dragType clientPos draggableId =
{ dragType = dragType, clientPos = clientPos, draggableId = draggableId }
|> encodeVariant "trackDraggable" InteropDefinitions.trackDraggableEncoder
|> interopFromElm

Expand Down
2 changes: 1 addition & 1 deletion src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ update msg model =
( model, Cmd.none )

( ReceiveTime ( zone, posix ), _ ) ->
( mapSession (Session.timeWIthZoneIs zone posix) model
( mapSession (Session.timeWithZoneIs zone posix) model
, Cmd.none
)

Expand Down
Loading

0 comments on commit 6f57a6a

Please sign in to comment.