Line data Source code
1 : <template>
2 : <!--
3 : Root @click.stop keeps clicks outside the explicit selection regions from
4 : reaching the panel's empty-space deselection handler.
5 : -->
6 472 : <div
7 : :class="
8 : cn(
9 : 'flex cursor-pointer flex-col overflow-hidden rounded-lg p-2 transition-colors duration-200',
10 : 'group gap-2 select-none',
11 : selected
12 : ? 'ring-3 ring-muted-foreground ring-inset'
13 : : 'hover:bg-accent/20'
14 : )
15 : "
16 : :data-selected="selected"
17 : :data-asset-id="asset?.id"
18 : :draggable="true"
19 : @click.stop
20 : @contextmenu.prevent.stop="handleContextMenu"
21 : @dragstart="dragStart"
22 : >
23 : <!-- Top Area: Media Preview -->
24 : <div
25 : class="relative aspect-square overflow-hidden"
26 : @click.stop="handlePreviewClick"
27 : @dblclick.stop="
28 : (fileKind === 'image' || fileKind === 'video' || isHdr) &&
29 : handleZoomClick()
30 : "
31 : >
32 : <!-- Loading State -->
33 : <div v-if="loading" class="size-full animate-pulse rounded-lg bg-muted" />
34 :
35 : <!-- Content based on asset type -->
36 : <component
37 : :is="getTopComponent(previewKind)"
38 : v-else-if="asset && adaptedAsset"
39 : :asset="adaptedAsset"
40 : :context="{ type: assetType }"
41 : :show-native-controls="
42 : fileKind === 'video' ? showNativeVideoControls : undefined
43 : "
44 : class="absolute inset-0"
45 : @download="handleDownload"
46 : @image-loaded="handleImageLoaded"
47 : />
48 :
49 : <LoadingOverlay :loading="isDeleting">
50 : <i class="icon-[lucide--trash-2] size-5" />
51 : </LoadingOverlay>
52 :
53 : <Button
54 : v-if="asset && !loading && !isDeleting"
55 : variant="overlay-white"
56 : size="icon"
57 : :class="
58 : cn(
59 : 'pointer-events-none absolute top-2 left-2 z-1 size-6 rounded-full opacity-0 transition-opacity',
60 : 'group-hover:pointer-events-auto group-hover:opacity-100',
61 : 'focus-visible:pointer-events-auto focus-visible:opacity-100',
62 : !selected &&
63 : 'bg-transparent text-white shadow-sm ring-2 ring-white ring-inset hover:bg-white/20'
64 : )
65 : "
66 : :aria-label="
67 : $t('assetBrowser.ariaLabel.assetCard', {
68 : name: getAssetDisplayName(asset),
69 : type: fileKind
70 : })
71 : "
72 : :aria-pressed="selected ?? false"
73 : @click.stop="emit('toggle-selection')"
74 : >
75 : <i
76 : aria-hidden="true"
77 : :class="cn('icon-[lucide--check]', !selected && 'opacity-0')"
78 : />
79 : </Button>
80 :
81 : <!-- Action buttons overlay (top-right) -->
82 : <div
83 : v-if="asset && !loading && !isDeleting"
84 : :class="
85 : cn(
86 : 'absolute top-2 right-2 z-1 flex flex-wrap justify-end gap-2',
87 : 'pointer-events-none opacity-0 transition-opacity',
88 : 'group-hover:pointer-events-auto group-hover:opacity-100',
89 : '[.group:has(:focus-visible)_&]:pointer-events-auto [.group:has(:focus-visible)_&]:opacity-100',
90 : 'touch:pointer-events-auto touch:opacity-100'
91 : )
92 : "
93 : >
94 : <IconGroup background-class="bg-white">
95 : <Button
96 : variant="overlay-white"
97 : size="icon"
98 : :aria-label="$t('mediaAsset.actions.download')"
99 : @click.stop="handleDownload"
100 : >
101 : <i class="icon-[lucide--download]" />
102 : </Button>
103 : <Button
104 : variant="overlay-white"
105 : size="icon"
106 : :aria-label="$t('mediaAsset.actions.moreOptions')"
107 : @click.stop="handleContextMenu"
108 : >
109 : <i class="icon-[lucide--ellipsis]" />
110 : </Button>
111 : </IconGroup>
112 : </div>
113 : </div>
114 :
115 : <!-- Bottom Area: Media Info -->
116 : <div class="flex-1">
117 : <!-- Loading State -->
118 : <div v-if="loading" class="flex items-start justify-between">
119 : <div class="flex flex-col gap-1">
120 : <div class="h-4 w-24 animate-pulse rounded-sm bg-secondary" />
121 : <div class="h-3 w-20 animate-pulse rounded-sm bg-secondary" />
122 : </div>
123 : <div class="h-6 w-12 animate-pulse rounded-sm bg-secondary" />
124 : </div>
125 :
126 : <!-- Content -->
127 : <div
128 : v-else-if="asset && adaptedAsset"
129 : class="flex items-end justify-between gap-1.5"
130 : @click.stop="emit('select')"
131 : >
132 : <div class="flex min-w-0 flex-col gap-1">
133 : <MediaTitle :file-name="fileName" />
134 : <div class="flex items-center gap-1.5 text-xs text-muted-foreground">
135 : <span v-if="formattedDuration">{{ formattedDuration }}</span>
136 : <span v-if="metaInfo">{{ metaInfo }}</span>
137 : </div>
138 : </div>
139 : <div v-if="showOutputCount" class="shrink-0">
140 : <Button
141 : :tooltip="$t('mediaAsset.actions.seeMoreOutputs')"
142 : :aria-label="$t('mediaAsset.actions.seeMoreOutputs')"
143 : variant="secondary"
144 : @click.stop="handleOutputCountClick"
145 : >
146 : <i class="icon-[lucide--layers]" />
147 : <span>{{ outputCount }}</span>
148 : </Button>
149 : </div>
150 : </div>
151 : </div>
152 : </div>
153 : </template>
154 :
155 : <script setup lang="ts">
156 : import type { Size2D } from '@comfyorg/shared-frontend-utils/geometry'
157 :
158 : import { cn } from '@comfyorg/tailwind-utils'
159 : import { computed, defineAsyncComponent, provide, ref, toRef } from 'vue'
160 :
161 : import IconGroup from '@comfyorg/design-system/button-group/IconGroup'
162 : import LoadingOverlay from '@comfyorg/design-system/common/LoadingOverlay'
163 : import Button from '@comfyorg/design-system/button/Button'
164 : import { useAssetsStore } from '@/stores/assetsStore'
165 : import {
166 : formatDuration,
167 : formatSize,
168 : getFilenameDetails,
169 : getMediaTypeFromFilename,
170 : isPreviewableMediaType
171 : } from '@/utils/formatUtil'
172 : import { isHdrImageFilename } from '@/utils/hdrFormatUtil'
173 :
174 : import { getAssetType } from '@/platform/assets/composables/media/assetMappers'
175 : import { startAssetDrag } from '@/platform/assets/utils/assetDragUtil'
176 : import {
177 : getAssetInlineMediaUrl,
178 : getAssetUrl,
179 : resolveMediaSrc
180 : } from '@/platform/assets/utils/assetUrlUtil'
181 : import { useMediaAssetActions } from '@/platform/assets/composables/useMediaAssetActions'
182 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
183 : import {
184 : getAssetDisplayName,
185 : resolveDisplayImageDimensions
186 : } from '@/platform/assets/utils/assetMetadataUtils'
187 : import type { MediaKind } from '@/platform/assets/schemas/mediaAssetSchema'
188 : import { MediaAssetKey } from '@/platform/assets/schemas/mediaAssetSchema'
189 : import MediaTitle from '@comfyorg/design-system/media/MediaTitle'
190 :
191 : type PreviewKind = ReturnType<typeof getMediaTypeFromFilename>
192 :
193 206 : const mediaComponents = {
194 : top: {
195 : video: defineAsyncComponent(() => import('./MediaVideoTop.vue')),
196 : audio: defineAsyncComponent(() => import('./MediaAudioTop.vue')),
197 : image: defineAsyncComponent(() => import('./MediaImageTop.vue')),
198 : '3D': defineAsyncComponent(() => import('./Media3DTop.vue')),
199 : text: defineAsyncComponent(() => import('./MediaTextTop.vue')),
200 : other: defineAsyncComponent(() => import('./MediaOtherTop.vue'))
201 : }
202 : }
203 :
204 472 : function getTopComponent(kind: PreviewKind) {
205 472 : return mediaComponents.top[kind] || mediaComponents.top.other
206 : }
207 :
208 : const {
209 : asset,
210 : loading,
211 : selected,
212 : showOutputCount,
213 : outputCount,
214 : showNativeVideoControls = true
215 : } = defineProps<{
216 : asset?: AssetItem
217 : loading?: boolean
218 : selected?: boolean
219 : showOutputCount?: boolean
220 : outputCount?: number
221 : showNativeVideoControls?: boolean
222 : }>()
223 :
224 206 : const assetsStore = useAssetsStore()
225 :
226 : // Get deletion state from store
227 206 : const isDeleting = computed(() =>
228 : asset ? assetsStore.isAssetDeleting(asset.id) : false
229 : )
230 :
231 206 : const emit = defineEmits<{
232 : // Image and info clicks use the standard selection rules.
233 : select: []
234 : // The selection control toggles only this asset in the current selection.
235 : 'toggle-selection': []
236 : zoom: [asset: AssetItem]
237 : 'output-count-click': []
238 : 'context-menu': [event: MouseEvent, asset: AssetItem]
239 : }>()
240 :
241 : // Store actual image dimensions
242 206 : const imageDimensions = ref<Size2D | undefined>()
243 :
244 206 : const actions = useMediaAssetActions()
245 :
246 : // Get asset type from tags
247 206 : const assetType = computed(() => {
248 384 : return getAssetType(asset?.tags)
249 : })
250 :
251 : // Determine file type from extension
252 206 : const fileKind = computed((): MediaKind => {
253 384 : return getMediaTypeFromFilename(asset?.name || '')
254 : })
255 :
256 206 : const isHdr = computed(() => isHdrImageFilename(asset?.name))
257 :
258 206 : const previewKind = computed((): PreviewKind => {
259 384 : if (asset?.preview_url && isHdr.value) return 'image'
260 384 : return getMediaTypeFromFilename(asset?.name || '')
261 : })
262 :
263 206 : const canInspect = computed(
264 : () => isPreviewableMediaType(fileKind.value) || isHdr.value
265 : )
266 :
267 : // Get filename without extension
268 206 : const fileName = computed(() => {
269 384 : return getFilenameDetails(asset ? getAssetDisplayName(asset) : '').filename
270 : })
271 :
272 : // Adapt AssetItem to legacy AssetMeta format for existing components
273 206 : const adaptedAsset = computed(() => {
274 429 : if (!asset) return undefined
275 429 : return {
276 : id: asset.id,
277 : name: asset.name,
278 : display_name: asset.display_name,
279 : kind: fileKind.value,
280 : src:
281 : fileKind.value === '3D'
282 : ? getAssetUrl(asset)
283 : : resolveMediaSrc(asset.thumbnail_url || asset.preview_url) ||
284 : (fileKind.value === 'video' || fileKind.value === 'audio'
285 : ? getAssetInlineMediaUrl(asset)
286 : : ''),
287 : preview_url: asset.preview_url,
288 : preview_id: asset.preview_id,
289 : size: asset.size,
290 : tags: asset.tags || [],
291 : created_at: asset.created_at,
292 : updated_at: asset.updated_at,
293 : duration: asset.user_metadata?.duration
294 : ? Number(asset.user_metadata.duration)
295 : : undefined,
296 : dimensions: imageDimensions.value
297 : }
298 : })
299 :
300 206 : provide(MediaAssetKey, {
301 : asset: toRef(() => adaptedAsset.value),
302 : context: toRef(() => ({ type: assetType.value }))
303 : })
304 :
305 206 : const formattedDuration = computed(() => {
306 : // Check for execution time first (from history API)
307 384 : const executionTime = asset?.user_metadata?.executionTimeInSeconds
308 384 : if (executionTime !== undefined && executionTime !== null) {
309 356 : return `${Number(executionTime).toFixed(2)}s`
310 : }
311 :
312 : // Fall back to duration for media files
313 384 : const duration = asset?.user_metadata?.duration
314 384 : if (!duration) return ''
315 0 : return formatDuration(Number(duration))
316 : })
317 :
318 206 : const displayImageDimensions = computed(() =>
319 : resolveDisplayImageDimensions(asset, imageDimensions.value)
320 : )
321 :
322 206 : const format = computed(() => {
323 384 : const suffix = getFilenameDetails(asset?.name ?? '').suffix
324 384 : return suffix ? suffix.toUpperCase() : ''
325 : })
326 :
327 206 : const metaInfo = computed(() => {
328 429 : if (!asset) return ''
329 429 : const parts: string[] = []
330 429 : if (format.value) parts.push(format.value)
331 :
332 429 : if (fileKind.value === 'image' && displayImageDimensions.value) {
333 48 : parts.push(
334 : `${displayImageDimensions.value.width}x${displayImageDimensions.value.height}`
335 : )
336 381 : } else if (asset.size && ['video', 'audio', '3D'].includes(fileKind.value)) {
337 0 : parts.push(formatSize(asset.size))
338 : }
339 :
340 429 : return parts.join(' ')
341 : })
342 :
343 25 : function handlePreviewClick(event: MouseEvent) {
344 25 : const hasSelectionModifier = event.shiftKey || event.metaKey || event.ctrlKey
345 25 : if (fileKind.value === 'video' && !hasSelectionModifier) return
346 25 : emit('select')
347 : }
348 :
349 206 : const handleZoomClick = () => {
350 3 : if (asset && canInspect.value) {
351 3 : emit('zoom', asset)
352 : }
353 : }
354 :
355 206 : const handleImageLoaded = (width: number, height: number) => {
356 45 : imageDimensions.value = { width, height }
357 : }
358 :
359 206 : const handleOutputCountClick = () => {
360 3 : emit('output-count-click')
361 : }
362 :
363 25 : function handleContextMenu(event: MouseEvent) {
364 25 : if (!asset) return
365 25 : emit('context-menu', event, asset)
366 : }
367 :
368 0 : function handleDownload() {
369 0 : if (!asset) return
370 0 : actions.downloadAssets([asset])
371 : }
372 :
373 6 : function dragStart(e: DragEvent) {
374 6 : startAssetDrag(e, asset)
375 : }
376 : </script>
|