diff --git a/CHANGELOG.md b/CHANGELOG.md index 10d81f13..055e6c94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Adds ability to download a playlist as a zip file - Fixes dark styles for add-to-playlist overlay - Improves CORS information a little bit +- Improves playlist drag & drop (now moves selection instead of item under cursor) - No longer scrolls track list to the top when moving things in a playlist or when processing tracks - Switches key bindings for arrow up and down diff --git a/src/Applications/UI/Tracks.elm b/src/Applications/UI/Tracks.elm index 0cb91713..102a565f 100644 --- a/src/Applications/UI/Tracks.elm +++ b/src/Applications/UI/Tracks.elm @@ -451,12 +451,18 @@ translateReply reply model = in return { model | selectedTrackIndexes = selection } - MoveTrackInSelectedPlaylist moveFromTo -> + MoveTrackInSelectedPlaylist { to } -> case model.selectedPlaylist of Just p -> let + moveParams = + { from = Maybe.withDefault 0 (List.head model.selectedTrackIndexes) + , to = to + , amount = List.length model.selectedTrackIndexes + } + updatedPlaylist = - { p | tracks = List.move moveFromTo p.tracks } + { p | tracks = List.move moveParams p.tracks } in { model | selectedPlaylist = Just updatedPlaylist } |> reviseCollection arrange diff --git a/src/Applications/UI/Tracks/Reply.elm b/src/Applications/UI/Tracks/Reply.elm index 3442fcf2..15f5ee06 100644 --- a/src/Applications/UI/Tracks/Reply.elm +++ b/src/Applications/UI/Tracks/Reply.elm @@ -13,7 +13,7 @@ type Reply = Transcend (List UI.Reply) -- | MarkAsSelected Int { shiftKey : Bool } - | MoveTrackInSelectedPlaylist { from : Int, to : Int } + | MoveTrackInSelectedPlaylist { to : Int } | ShowTrackMenuWithoutDelay Int { alt : Bool } Coordinates | ShowTrackMenuWithSmallDelay Int { alt : Bool } Coordinates | SortBy SortBy diff --git a/src/Applications/UI/Tracks/Scene/List.elm b/src/Applications/UI/Tracks/Scene/List.elm index 94e39785..1e1022f8 100644 --- a/src/Applications/UI/Tracks/Scene/List.elm +++ b/src/Applications/UI/Tracks/Scene/List.elm @@ -81,8 +81,7 @@ update msg model = returnRepliesWithModel { model | dnd = newDnD } [ MoveTrackInSelectedPlaylist - { from = Maybe.withDefault 0 <| DnD.modelSubject newDnD - , to = Maybe.withDefault 0 <| DnD.modelTarget newDnD + { to = Maybe.withDefault 0 (DnD.modelTarget newDnD) } , Transcend uiReplies ] diff --git a/src/Library/List/Ext.elm b/src/Library/List/Ext.elm index 852948e7..04b76488 100644 --- a/src/Library/List/Ext.elm +++ b/src/Library/List/Ext.elm @@ -28,45 +28,27 @@ addTo list item = {-| Move an item "from" an index "to" another index. Putting the item in front of the `to` index. - >>> move { from = 0, to = 2 } [1, 2, 3] + >>> move { from = 0, to = 2, amount = 1 } [1, 2, 3] [2, 1, 3] - >>> move { from = 2, to = 0 } [1, 2, 3] + >>> move { from = 2, to = 0, amount = 1 } [1, 2, 3] [3, 1, 2] + >>> move { from = 2, to = 7, amount = 3 } [0, 1, 2, 3, 4, 5, 6, 7] + [0, 1, 5, 6, 2, 3, 4, 7] + + >>> move { from = 2, to = 1, amount = 3 } [0, 1, 2, 3, 4, 5, 6, 7] + [0, 2, 3, 4, 1, 5, 6, 7] + -} -move : { from : Int, to : Int } -> List a -> List a -move opts list = - let - from = - opts.from - - to = - if opts.to > from then - opts.to - 1 - - else - opts.to - - maybeItemToMove = - List.getAt from list - in - list - |> List.removeAt from - |> List.indexedFoldr - (\idx existingItem acc -> - if idx == to then - case maybeItemToMove of - Just itemToMove -> - List.append [ itemToMove, existingItem ] acc - - Nothing -> - existingItem :: acc - - else - existingItem :: acc - ) - [] +move : { amount : Int, from : Int, to : Int } -> List a -> List a +move { from, to, amount } list = + [] + ++ (list |> List.take (min from to)) + ++ (list |> List.take to |> List.drop (from + amount)) + ++ (list |> List.drop from |> List.take amount) + ++ (list |> List.take from |> List.drop to) + ++ (list |> List.drop (max (from + amount) to)) pickIndexes : List Int -> List a -> List a