Line data Source code
1 : <template>
2 2 : <div class="flex h-full flex-col">
3 : <VirtualGrid
4 : class="flex-1"
5 : :items="pagedItems(assetItems)"
6 : :grid-style="listGridStyle"
7 : :max-columns="1"
8 : :default-item-height="48"
9 : :on-load-more="loadMore"
10 : :can-load-more="isPaged(assetItems) && toValue(assetItems.hasMore)"
11 : >
12 : <template #item="{ item }">
13 : <div class="relative">
14 : <LoadingOverlay
15 : :loading="assetsStore.isAssetDeleting(item.asset.id)"
16 : size="sm"
17 : >
18 : <i class="icon-[lucide--trash-2] size-3" />
19 : </LoadingOverlay>
20 : <AssetsListItem
21 : role="button"
22 : tabindex="0"
23 : :aria-label="
24 : t('assetBrowser.ariaLabel.assetCard', {
25 : name: getAssetDisplayName(item.asset),
26 : type: getAssetMediaType(item.asset)
27 : })
28 : "
29 : :class="
30 : cn(
31 : getAssetCardClass(isSelected(item.asset.id)),
32 : item.isChild && 'pl-6'
33 : )
34 : "
35 : :preview-url="getAssetPreviewUrl(item.asset)"
36 : :preview-alt="getAssetDisplayName(item.asset)"
37 : :icon-name="iconForMediaType(getAssetMediaType(item.asset))"
38 : :is-video-preview="isVideoAsset(item.asset)"
39 : :primary-text="getAssetPrimaryText(item.asset)"
40 : :secondary-text="getAssetSecondaryText(item.asset)"
41 : :stack-count="getStackCount(item.asset)"
42 : :stack-indicator-label="t('mediaAsset.actions.seeMoreOutputs')"
43 : :stack-expanded="isStackExpanded(item.asset)"
44 : :draggable="true"
45 : @dragstart="startAssetDrag($event, item.asset)"
46 : @mouseenter="onAssetEnter(item.asset.id)"
47 : @mouseleave="onAssetLeave(item.asset.id)"
48 : @contextmenu.prevent.stop="emit('context-menu', $event, item.asset)"
49 : @click.stop="emit('select-asset', item.asset, selectableAssets)"
50 : @dblclick.stop="emit('preview-asset', item.asset)"
51 : @preview-click="emit('preview-asset', item.asset)"
52 : @stack-toggle="void toggleStack(item.asset)"
53 : >
54 : <template v-if="hoveredAssetId === item.asset.id" #actions>
55 : <Button
56 : variant="secondary"
57 : size="icon"
58 : :aria-label="t('mediaAsset.actions.moreOptions')"
59 : @click.stop="emit('context-menu', $event, item.asset)"
60 : >
61 : <i class="icon-[lucide--ellipsis]" />
62 : </Button>
63 : </template>
64 : </AssetsListItem>
65 : </div>
66 : </template>
67 : </VirtualGrid>
68 : </div>
69 : </template>
70 :
71 : <script setup lang="ts">
72 : import { ref, toValue } from 'vue'
73 : import { useI18n } from 'vue-i18n'
74 :
75 : import LoadingOverlay from '@comfyorg/design-system/common/LoadingOverlay'
76 : import VirtualGrid from '@comfyorg/design-system/common/VirtualGrid'
77 : import Button from '@comfyorg/design-system/button/Button'
78 : import AssetsListItem from '@/platform/assets/components/AssetsListItem.vue'
79 : import type { OutputStackListItem } from '@/platform/assets/composables/useOutputStacks'
80 : import { getOutputAssetMetadata } from '@/platform/assets/schemas/assetMetadataSchema'
81 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
82 : import { startAssetDrag } from '@/platform/assets/utils/assetDragUtil'
83 : import { getAssetDisplayName } from '@/platform/assets/utils/assetMetadataUtils'
84 : import { resolveMediaSrc } from '@/platform/assets/utils/assetUrlUtil'
85 : import { iconForMediaType } from '@/platform/assets/utils/mediaIconUtil'
86 : import { useAssetsStore } from '@/stores/assetsStore'
87 : import {
88 : formatDuration,
89 : formatSize,
90 : getMediaTypeFromFilename,
91 : truncateFilename
92 : } from '@/utils/formatUtil'
93 : import { hdrPreviewAssetUrl, isHdrImageFilename } from '@/utils/hdrFormatUtil'
94 : import { isPaged, pagedItems } from '@/utils/pagedList'
95 : import type { MaybePaged } from '@/utils/pagedList'
96 : import { cn } from '@comfyorg/tailwind-utils'
97 :
98 : const {
99 : assetItems,
100 : selectableAssets,
101 : isSelected,
102 : isStackExpanded,
103 : toggleStack
104 : } = defineProps<{
105 : assetItems: MaybePaged<OutputStackListItem>
106 : selectableAssets: AssetItem[]
107 : isSelected: (assetId: string) => boolean
108 : isStackExpanded: (asset: AssetItem) => boolean
109 : toggleStack: (asset: AssetItem) => Promise<void>
110 : }>()
111 :
112 2 : const assetsStore = useAssetsStore()
113 :
114 2 : const emit = defineEmits<{
115 : (e: 'select-asset', asset: AssetItem, assets?: AssetItem[]): void
116 : (e: 'preview-asset', asset: AssetItem): void
117 : (e: 'context-menu', event: MouseEvent, asset: AssetItem): void
118 : }>()
119 :
120 2 : const { t } = useI18n()
121 2 : const hoveredAssetId = ref<string | null>(null)
122 :
123 0 : function loadMore() {
124 0 : return isPaged(assetItems) ? assetItems.loadMore() : undefined
125 : }
126 :
127 2 : const listGridStyle = {
128 : display: 'grid',
129 : gridTemplateColumns: 'minmax(0, 1fr)',
130 : padding: '0 0.5rem',
131 : gap: '0.5rem'
132 : }
133 :
134 6 : function getAssetPrimaryText(asset: AssetItem): string {
135 6 : return truncateFilename(getAssetDisplayName(asset))
136 : }
137 :
138 24 : function getAssetMediaType(asset: AssetItem) {
139 24 : return getMediaTypeFromFilename(asset.name)
140 : }
141 :
142 6 : function isVideoAsset(asset: AssetItem): boolean {
143 6 : return getAssetMediaType(asset) === 'video'
144 : }
145 :
146 6 : function getAssetPreviewUrl(asset: AssetItem): string {
147 6 : const mediaType = getAssetMediaType(asset)
148 6 : if (asset.preview_id && isHdrImageFilename(asset.name))
149 0 : return hdrPreviewAssetUrl(asset.preview_id)
150 6 : if (mediaType === 'image' || mediaType === 'video')
151 6 : return resolveMediaSrc(asset.preview_url)
152 0 : return ''
153 : }
154 :
155 6 : function getAssetSecondaryText(asset: AssetItem): string {
156 6 : const metadata = getOutputAssetMetadata(asset.user_metadata)
157 6 : if (typeof metadata?.executionTimeInSeconds === 'number') {
158 6 : return `${metadata.executionTimeInSeconds.toFixed(2)}s`
159 : }
160 :
161 6 : const duration = asset.user_metadata?.duration
162 6 : if (typeof duration === 'number') {
163 0 : return formatDuration(duration)
164 : }
165 :
166 0 : if (typeof asset.size === 'number') {
167 0 : return formatSize(asset.size)
168 : }
169 :
170 0 : return ''
171 : }
172 :
173 6 : function getStackCount(asset: AssetItem): number | undefined {
174 6 : const metadata = getOutputAssetMetadata(asset.user_metadata)
175 6 : if (typeof metadata?.outputCount === 'number') {
176 6 : return metadata.outputCount
177 : }
178 :
179 6 : if (Array.isArray(metadata?.allOutputs)) {
180 0 : return metadata.allOutputs.length
181 : }
182 :
183 : return undefined
184 : }
185 :
186 6 : function getAssetCardClass(selected: boolean): string {
187 6 : return cn(
188 : 'w-full text-foreground transition-colors hover:bg-accent',
189 : 'cursor-pointer',
190 : selected && 'bg-accent ring-1 ring-muted-foreground ring-inset'
191 : )
192 : }
193 :
194 0 : function onAssetEnter(assetId: string) {
195 0 : hoveredAssetId.value = assetId
196 : }
197 :
198 0 : function onAssetLeave(assetId: string) {
199 0 : if (hoveredAssetId.value === assetId) {
200 0 : hoveredAssetId.value = null
201 : }
202 : }
203 : </script>
|