From 28c4646708ab859ed48e7d59a807760c4d537369 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Wed, 28 Dec 2022 19:50:35 -0800 Subject: [PATCH] Preserve row order of selected context menu items --- .../features/albums/components/album-list-content.tsx | 6 ++++-- .../features/songs/components/song-list-content.tsx | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/renderer/features/albums/components/album-list-content.tsx b/src/renderer/features/albums/components/album-list-content.tsx index dca06717..360f6580 100644 --- a/src/renderer/features/albums/components/album-list-content.tsx +++ b/src/renderer/features/albums/components/album-list-content.tsx @@ -40,6 +40,7 @@ import { AnimatePresence } from 'framer-motion'; import debounce from 'lodash/debounce'; import { openContextMenu } from '/@/renderer/features/context-menu'; import { ALBUM_CONTEXT_MENU_ITEMS } from '/@/renderer/features/context-menu/context-menu-items'; +import sortBy from 'lodash/sortBy'; interface AlbumListContentProps { gridRef: MutableRefObject; @@ -276,8 +277,9 @@ export const AlbumListContent = ({ gridRef, tableRef }: AlbumListContentProps) = const clickEvent = e.event as MouseEvent; clickEvent.preventDefault(); - let selectedRows = e.api.getSelectedRows(); - const selectedIds = selectedRows.map((row) => row.id); + const selectedNodes = e.api.getSelectedNodes(); + const selectedIds = selectedNodes.map((node) => node.data.id); + let selectedRows = sortBy(selectedNodes, ['rowIndex']).map((node) => node.data); if (!selectedIds.includes(e.data.id)) { e.api.deselectAll(); diff --git a/src/renderer/features/songs/components/song-list-content.tsx b/src/renderer/features/songs/components/song-list-content.tsx index baf7b5dc..079b1813 100644 --- a/src/renderer/features/songs/components/song-list-content.tsx +++ b/src/renderer/features/songs/components/song-list-content.tsx @@ -31,6 +31,7 @@ import { AnimatePresence } from 'framer-motion'; import debounce from 'lodash/debounce'; import { openContextMenu } from '/@/renderer/features/context-menu'; import { SONG_CONTEXT_MENU_ITEMS } from '/@/renderer/features/context-menu/context-menu-items'; +import sortBy from 'lodash/sortBy'; interface SongListContentProps { tableRef: MutableRefObject; @@ -160,10 +161,11 @@ export const SongListContent = ({ tableRef }: SongListContentProps) => { const clickEvent = e.event as MouseEvent; clickEvent.preventDefault(); - let selectedRows = e.api.getSelectedRows(); - const selectedUniqueIds = selectedRows.map((row) => row.uniqueId); + const selectedNodes = e.api.getSelectedNodes(); + const selectedIds = selectedNodes.map((node) => node.data.id); + let selectedRows = sortBy(selectedNodes, ['rowIndex']).map((node) => node.data); - if (!selectedUniqueIds.includes(e.data.uniqueId)) { + if (!selectedIds.includes(e.data.id)) { e.api.deselectAll(); e.node.setSelected(true); selectedRows = [e.data];