-
Notifications
You must be signed in to change notification settings - Fork 491
Reconcile asset selection with visible items #8296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { createPinia, setActivePinia } from 'pinia' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { ref } from 'vue' | ||
|
|
||
| import type { AssetItem } from '@/platform/assets/schemas/assetSchema' | ||
|
|
||
| import { useAssetSelection } from './useAssetSelection' | ||
| import { useAssetSelectionStore } from './useAssetSelectionStore' | ||
|
|
||
| vi.mock('@vueuse/core', () => ({ | ||
| useKeyModifier: vi.fn(() => ref(false)) | ||
| })) | ||
|
|
||
| describe('useAssetSelection', () => { | ||
| beforeEach(() => { | ||
| setActivePinia(createPinia()) | ||
| }) | ||
|
|
||
| it('prunes selection to visible assets', () => { | ||
| const selection = useAssetSelection() | ||
| const store = useAssetSelectionStore() | ||
| const assets: AssetItem[] = [ | ||
| { id: 'a', name: 'a.png', tags: [] }, | ||
| { id: 'b', name: 'b.png', tags: [] } | ||
| ] | ||
|
Comment on lines
+22
to
+25
|
||
|
|
||
| store.setSelection(['a', 'b']) | ||
| store.setLastSelectedIndex(1) | ||
| store.setLastSelectedAssetId('b') | ||
|
|
||
| selection.reconcileSelection([assets[1]]) | ||
|
|
||
| expect(Array.from(store.selectedAssetIds)).toEqual(['b']) | ||
| expect(store.lastSelectedIndex).toBe(0) | ||
| expect(store.lastSelectedAssetId).toBe('b') | ||
| }) | ||
|
|
||
| it('clears selection when no visible assets remain', () => { | ||
| const selection = useAssetSelection() | ||
| const store = useAssetSelectionStore() | ||
|
|
||
| store.setSelection(['a']) | ||
| store.setLastSelectedIndex(0) | ||
| store.setLastSelectedAssetId('a') | ||
|
|
||
| selection.reconcileSelection([]) | ||
|
|
||
| expect(store.selectedAssetIds.size).toBe(0) | ||
| expect(store.lastSelectedIndex).toBe(-1) | ||
| expect(store.lastSelectedAssetId).toBeNull() | ||
| }) | ||
|
|
||
| it('recomputes the anchor index when assets reorder', () => { | ||
| const selection = useAssetSelection() | ||
| const store = useAssetSelectionStore() | ||
| const assets: AssetItem[] = [ | ||
| { id: 'a', name: 'a.png', tags: [] }, | ||
| { id: 'b', name: 'b.png', tags: [] } | ||
| ] | ||
|
|
||
| store.setSelection(['a']) | ||
| store.setLastSelectedIndex(0) | ||
| store.setLastSelectedAssetId('a') | ||
|
|
||
| selection.reconcileSelection([assets[1], assets[0]]) | ||
|
|
||
| expect(store.lastSelectedIndex).toBe(1) | ||
| expect(store.lastSelectedAssetId).toBe('a') | ||
| }) | ||
|
|
||
| it('clears anchor when the anchored asset is no longer visible', () => { | ||
| const selection = useAssetSelection() | ||
| const store = useAssetSelectionStore() | ||
| const assets: AssetItem[] = [ | ||
| { id: 'a', name: 'a.png', tags: [] }, | ||
| { id: 'b', name: 'b.png', tags: [] } | ||
| ] | ||
|
|
||
| store.setSelection(['a', 'b']) | ||
| store.setLastSelectedIndex(0) | ||
| store.setLastSelectedAssetId('a') | ||
|
|
||
| selection.reconcileSelection([assets[1]]) | ||
|
|
||
| expect(Array.from(store.selectedAssetIds)).toEqual(['b']) | ||
| expect(store.lastSelectedIndex).toBe(-1) | ||
| expect(store.lastSelectedAssetId).toBeNull() | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,25 @@ export function useAssetSelection() { | |
| const metaKey = computed(() => isActive.value && metaKeyRaw.value) | ||
| const cmdOrCtrlKey = computed(() => ctrlKey.value || metaKey.value) | ||
|
|
||
| function setAnchor(index: number, assetId: string | null) { | ||
| selectionStore.setLastSelectedIndex(index) | ||
| selectionStore.setLastSelectedAssetId(assetId) | ||
| } | ||
|
|
||
| function syncAnchorFromAssets(assets: AssetItem[]) { | ||
| const anchorId = selectionStore.lastSelectedAssetId | ||
| const anchorIndex = anchorId | ||
| ? assets.findIndex((asset) => asset.id === anchorId) | ||
| : -1 | ||
|
|
||
| if (anchorIndex !== -1) { | ||
| selectionStore.setLastSelectedIndex(anchorIndex) | ||
| return | ||
| } | ||
|
|
||
| setAnchor(-1, null) | ||
| } | ||
|
|
||
| /** | ||
| * Handle asset click with modifier keys for selection | ||
| * @param asset The clicked asset | ||
|
|
@@ -60,14 +79,14 @@ export function useAssetSelection() { | |
| // Ctrl/Cmd + Click: Toggle individual selection | ||
| if (cmdOrCtrlKey.value) { | ||
| selectionStore.toggleSelection(assetId) | ||
| selectionStore.setLastSelectedIndex(index) | ||
| setAnchor(index, assetId) | ||
| return | ||
| } | ||
|
|
||
| // Normal Click: Single selection | ||
| selectionStore.clearSelection() | ||
| selectionStore.addToSelection(assetId) | ||
| selectionStore.setLastSelectedIndex(index) | ||
| setAnchor(index, assetId) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -77,7 +96,8 @@ export function useAssetSelection() { | |
| const allIds = allAssets.map((a) => a.id) | ||
| selectionStore.setSelection(allIds) | ||
| if (allAssets.length > 0) { | ||
| selectionStore.setLastSelectedIndex(allAssets.length - 1) | ||
| const lastIndex = allAssets.length - 1 | ||
| setAnchor(lastIndex, allAssets[lastIndex].id) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -88,6 +108,39 @@ export function useAssetSelection() { | |
| return allAssets.filter((asset) => selectionStore.isSelected(asset.id)) | ||
| } | ||
|
|
||
| function reconcileSelection(assets: AssetItem[]) { | ||
| if (selectionStore.selectedAssetIds.size === 0) { | ||
| return | ||
| } | ||
|
Comment on lines
+111
to
+114
|
||
|
|
||
| if (assets.length === 0) { | ||
| selectionStore.clearSelection() | ||
| return | ||
| } | ||
|
|
||
| const visibleIds = new Set(assets.map((asset) => asset.id)) | ||
| const nextSelectedIds: string[] = [] | ||
|
|
||
| for (const id of selectionStore.selectedAssetIds) { | ||
| if (visibleIds.has(id)) { | ||
| nextSelectedIds.push(id) | ||
| } | ||
| } | ||
|
|
||
| if (nextSelectedIds.length === selectionStore.selectedAssetIds.size) { | ||
| syncAnchorFromAssets(assets) | ||
| return | ||
| } | ||
|
|
||
| if (nextSelectedIds.length === 0) { | ||
| selectionStore.clearSelection() | ||
| return | ||
| } | ||
|
|
||
| selectionStore.setSelection(nextSelectedIds) | ||
| syncAnchorFromAssets(assets) | ||
| } | ||
|
|
||
| /** | ||
| * Get the output count for a single asset | ||
| * Same logic as in AssetsSidebarTab.vue | ||
|
|
@@ -117,7 +170,7 @@ export function useAssetSelection() { | |
| function deactivate() { | ||
| isActive.value = false | ||
| // Reset selection state to ensure clean state when deactivated | ||
| selectionStore.reset() | ||
| selectionStore.clearSelection() | ||
| } | ||
|
|
||
| return { | ||
|
|
@@ -132,10 +185,9 @@ export function useAssetSelection() { | |
| selectAll, | ||
| clearSelection: () => selectionStore.clearSelection(), | ||
| getSelectedAssets, | ||
| reconcileSelection, | ||
| getOutputCount, | ||
| getTotalOutputCount, | ||
| reset: () => selectionStore.reset(), | ||
|
|
||
| // Lifecycle management | ||
| activate, | ||
| deactivate, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
focusAssetInSidebarsetssetSelection([assetId])and now setslastSelectedAssetId, but it leaveslastSelectedIndexunchanged. Since shift-range selection is keyed offlastSelectedIndex, this can produce an incorrect range anchored to a stale index after selecting from the queue overlay. Consider also resettinglastSelectedIndex(or setting it to the focused asset’s index when available) to keep the anchor state consistent.