Line data Source code
1 : import { useKeyModifier } from '@vueuse/core'
2 : import { computed, ref } from 'vue'
3 :
4 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
5 : import { useAssetSelectionStore } from '@/platform/assets/composables/useAssetSelectionStore'
6 : import {
7 : getAssetOutputCount,
8 : getTotalAssetOutputCount
9 : } from '@/platform/assets/utils/outputAssetCountUtil'
10 :
11 84 : export function useAssetSelection() {
12 84 : const selectionStore = useAssetSelectionStore()
13 :
14 : // Track whether the asset selection is active (e.g., when sidebar is open)
15 84 : const isActive = ref<boolean>(true)
16 :
17 : // Key modifiers - raw values
18 84 : const shiftKeyRaw = useKeyModifier('Shift')
19 84 : const ctrlKeyRaw = useKeyModifier('Control')
20 84 : const metaKeyRaw = useKeyModifier('Meta')
21 :
22 : // Only respond to key modifiers when active
23 84 : const shiftKey = computed(() => isActive.value && shiftKeyRaw.value)
24 84 : const ctrlKey = computed(() => isActive.value && ctrlKeyRaw.value)
25 84 : const metaKey = computed(() => isActive.value && metaKeyRaw.value)
26 84 : const cmdOrCtrlKey = computed(() => ctrlKey.value || metaKey.value)
27 :
28 78 : function setAnchor(index: number, assetId: string | null) {
29 78 : selectionStore.setLastSelectedIndex(index)
30 78 : selectionStore.setLastSelectedAssetId(assetId)
31 : }
32 :
33 0 : function syncAnchorFromAssets(assets: AssetItem[]) {
34 0 : const anchorId = selectionStore.lastSelectedAssetId
35 0 : const anchorIndex = anchorId
36 : ? assets.findIndex((asset) => asset.id === anchorId)
37 : : -1
38 :
39 0 : if (anchorIndex !== -1) {
40 0 : selectionStore.setLastSelectedIndex(anchorIndex)
41 0 : return
42 : }
43 :
44 0 : setAnchor(-1, null)
45 : }
46 :
47 5 : function toggleAssetSelection(
48 : asset: AssetItem,
49 : index: number,
50 : allAssets: AssetItem[]
51 : ) {
52 5 : if (!asset.id || index < 0 || index >= allAssets.length) {
53 0 : console.warn('Invalid asset selection parameters')
54 0 : return
55 : }
56 :
57 5 : selectionStore.toggleSelection(asset.id)
58 5 : setAnchor(index, asset.id)
59 : }
60 :
61 : /**
62 : * Handle asset click with modifier keys for selection
63 : * @param asset The clicked asset
64 : * @param index The index of the clicked asset in the current list
65 : * @param allAssets All assets in the current view for range selection
66 : */
67 25 : function handleAssetClick(
68 : asset: AssetItem,
69 : index: number,
70 : allAssets: AssetItem[]
71 : ) {
72 : // Input validation
73 25 : if (!asset.id || index < 0 || index >= allAssets.length) {
74 0 : console.warn('Invalid asset selection parameters')
75 0 : return
76 : }
77 :
78 25 : const assetId = asset.id
79 :
80 : // Shift + Click: Range selection
81 25 : if (shiftKey.value && selectionStore.lastSelectedIndex >= 0) {
82 0 : const start = Math.min(selectionStore.lastSelectedIndex, index)
83 0 : const end = Math.max(selectionStore.lastSelectedIndex, index)
84 :
85 : // Select only the range from anchor to clicked item
86 0 : const rangeIds = allAssets.slice(start, end + 1).map((a) => a.id)
87 0 : selectionStore.setSelection(rangeIds)
88 :
89 : // Don't update lastSelectedIndex for shift selection
90 0 : return
91 : }
92 :
93 : // Ctrl/Cmd + Click: Toggle individual selection
94 25 : if (cmdOrCtrlKey.value) {
95 5 : toggleAssetSelection(asset, index, allAssets)
96 5 : return
97 : }
98 :
99 : // Clicking the only selected asset does not clear the selection
100 25 : if (
101 : selectionStore.isSelected(assetId) &&
102 : selectionStore.selectedCount === 1
103 : ) {
104 3 : return
105 : }
106 :
107 : // Otherwise, a normal click collapses the selection to this asset
108 17 : selectionStore.clearSelection()
109 17 : selectionStore.addToSelection(assetId)
110 17 : setAnchor(index, assetId)
111 : }
112 :
113 : /**
114 : * Select all assets in the current view
115 : */
116 2 : function selectAll(allAssets: AssetItem[]) {
117 2 : const allIds = allAssets.map((a) => a.id)
118 2 : selectionStore.setSelection(allIds)
119 2 : if (allAssets.length > 0) {
120 2 : const lastIndex = allAssets.length - 1
121 2 : setAnchor(lastIndex, allAssets[lastIndex].id)
122 : }
123 : }
124 :
125 : /**
126 : * Replace the selection (e.g. from a marquee) and keep the shift-range anchor
127 : * on the last selected asset, the same way selectAll maintains it.
128 : */
129 54 : function setSelectedIds(ids: string[], allAssets: AssetItem[]) {
130 54 : selectionStore.setSelection(ids)
131 54 : const selected = new Set(ids)
132 54 : const anchorIndex = allAssets.findLastIndex((asset) =>
133 : selected.has(asset.id)
134 : )
135 54 : setAnchor(anchorIndex, anchorIndex >= 0 ? allAssets[anchorIndex].id : null)
136 : }
137 :
138 : /**
139 : * Get the actual asset objects for selected IDs
140 : */
141 97 : function getSelectedAssets(allAssets: AssetItem[]): AssetItem[] {
142 97 : return allAssets.filter((asset) => selectionStore.isSelected(asset.id))
143 : }
144 :
145 134 : function reconcileSelection(assets: AssetItem[]) {
146 134 : if (selectionStore.selectedAssetIds.size === 0) {
147 132 : return
148 : }
149 :
150 4 : if (assets.length === 0) {
151 1 : selectionStore.clearSelection()
152 1 : return
153 : }
154 :
155 1 : const visibleIds = new Set(assets.map((asset) => asset.id))
156 1 : const nextSelectedIds: string[] = []
157 :
158 1 : for (const id of selectionStore.selectedAssetIds) {
159 1 : if (visibleIds.has(id)) {
160 0 : nextSelectedIds.push(id)
161 : }
162 : }
163 :
164 3 : if (nextSelectedIds.length === selectionStore.selectedAssetIds.size) {
165 0 : syncAnchorFromAssets(assets)
166 0 : return
167 : }
168 :
169 1 : if (nextSelectedIds.length === 0) {
170 1 : selectionStore.clearSelection()
171 1 : return
172 : }
173 :
174 0 : selectionStore.setSelection(nextSelectedIds)
175 0 : syncAnchorFromAssets(assets)
176 : }
177 :
178 : /**
179 : * Get the output count for a single asset
180 : * Same logic as in AssetsSidebarTab.vue
181 : */
182 1520 : function getOutputCount(item: AssetItem): number {
183 1520 : return getAssetOutputCount(item)
184 : }
185 :
186 : /**
187 : * Get the total output count for given assets
188 : */
189 76 : function getTotalOutputCount(assets: AssetItem[]): number {
190 76 : return getTotalAssetOutputCount(assets)
191 : }
192 :
193 : /**
194 : * Activate key event listeners (when sidebar opens)
195 : */
196 84 : function activate() {
197 84 : isActive.value = true
198 : }
199 :
200 : /**
201 : * Deactivate key event listeners (when sidebar closes)
202 : */
203 4 : function deactivate() {
204 4 : isActive.value = false
205 : // Reset selection state to ensure clean state when deactivated
206 4 : selectionStore.clearSelection()
207 : }
208 :
209 84 : return {
210 : // Selection state
211 : selectedIds: computed(() => selectionStore.selectedAssetIds),
212 : selectedCount: computed(() => selectionStore.selectedCount),
213 : hasSelection: computed(() => selectionStore.hasSelection),
214 : isSelected: (assetId: string) => selectionStore.isSelected(assetId),
215 :
216 : // Selection actions
217 : handleAssetClick,
218 : toggleAssetSelection,
219 : selectAll,
220 : setSelectedIds,
221 : clearSelection: () => selectionStore.clearSelection(),
222 : getSelectedAssets,
223 : reconcileSelection,
224 : getOutputCount,
225 : getTotalOutputCount,
226 : // Lifecycle management
227 : activate,
228 : deactivate,
229 :
230 : // Key states (for UI feedback)
231 : shiftKey,
232 : cmdOrCtrlKey
233 : }
234 : }
|