Line data Source code
1 16 : <script setup lang="ts">
2 : import { useIntersectionObserver } from '@vueuse/core'
3 : import { computed, inject, ref, watch } from 'vue'
4 : import { useI18n } from 'vue-i18n'
5 :
6 : import { cn } from '@comfyorg/tailwind-utils'
7 :
8 : import Tooltip from '@comfyorg/design-system/tooltip/Tooltip'
9 : import TooltipContent from '@comfyorg/design-system/tooltip/TooltipContent'
10 : import TooltipTrigger from '@comfyorg/design-system/tooltip/TooltipTrigger'
11 : import {
12 : findServerPreviewUrl,
13 : isAssetPreviewSupported
14 : } from '@/platform/assets/utils/assetPreviewUtil'
15 :
16 : import { AssetKindKey } from './types'
17 : import type { FormDropdownMenuItemProps } from './types'
18 :
19 847 : const props = defineProps<FormDropdownMenuItemProps>()
20 :
21 41 : const { t } = useI18n()
22 :
23 41 : const emit = defineEmits<{
24 : click: [index: number]
25 : mediaLoad: [event: Event]
26 : }>()
27 :
28 41 : const actualDimensions = ref<string | null>(null)
29 :
30 41 : const assetKind = inject(AssetKindKey)
31 :
32 41 : const isVideo = computed(() => assetKind?.value === 'video')
33 41 : const isMesh = computed(() => assetKind?.value === 'mesh')
34 :
35 41 : const mediaContainerRef = ref<HTMLElement>()
36 41 : const resolvedMeshPreview = ref<string | null>(null)
37 41 : const meshPreviewAttempted = ref(false)
38 :
39 0 : function toLookupName(name: string): string {
40 0 : const stripped = name.replace(/ \[output\]$/, '')
41 0 : const slash = stripped.lastIndexOf('/')
42 0 : return slash === -1 ? stripped : stripped.substring(slash + 1)
43 : }
44 :
45 0 : async function resolveMeshPreview() {
46 0 : if (!isAssetPreviewSupported()) return
47 0 : const url = await findServerPreviewUrl(toLookupName(props.name))
48 0 : if (url) resolvedMeshPreview.value = url
49 : }
50 :
51 41 : useIntersectionObserver(mediaContainerRef, ([entry]) => {
52 41 : if (!entry.isIntersecting) return
53 41 : if (!isMesh.value || meshPreviewAttempted.value) return
54 0 : meshPreviewAttempted.value = true
55 0 : void resolveMeshPreview()
56 : })
57 :
58 41 : watch(
59 : () => props.name,
60 : () => {
61 0 : meshPreviewAttempted.value = false
62 0 : resolvedMeshPreview.value = null
63 : }
64 : )
65 :
66 41 : const displayedPreviewUrl = computed(() =>
67 : isMesh.value ? resolvedMeshPreview.value : props.previewUrl
68 : )
69 :
70 4 : function handleClick() {
71 4 : emit('click', props.index)
72 : }
73 :
74 28 : function handleImageLoad(event: Event) {
75 28 : emit('mediaLoad', event)
76 28 : if (!event.target || !(event.target instanceof HTMLImageElement)) return
77 28 : const img = event.target
78 28 : if (img.naturalWidth && img.naturalHeight) {
79 28 : actualDimensions.value = `${img.naturalWidth} x ${img.naturalHeight}`
80 : }
81 : }
82 :
83 0 : function handleVideoLoad(event: Event) {
84 0 : emit('mediaLoad', event)
85 0 : if (!event.target || !(event.target instanceof HTMLVideoElement)) return
86 0 : const video = event.target
87 0 : if (video.videoWidth && video.videoHeight) {
88 0 : actualDimensions.value = `${video.videoWidth} x ${video.videoHeight}`
89 : }
90 : }
91 41 : </script>
92 :
93 : <template>
94 70 : <div
95 : :class="
96 : cn(
97 : 'group/item flex cursor-pointer gap-1 bg-input select-none',
98 : 'transition-[transform,box-shadow,background-color] duration-150',
99 : {
100 : 'flex-col text-center': layout === 'grid',
101 : 'max-h-16 flex-row rounded-lg text-left hover:scale-102 active:scale-98':
102 : layout === 'list',
103 : 'flex-row rounded-lg text-left hover:bg-muted':
104 : layout === 'list-small',
105 : // selection
106 : 'ring-2 ring-muted-foreground': layout === 'list' && selected
107 : },
108 : candidate && !selected && layout !== 'grid' && 'bg-muted'
109 : )
110 : "
111 : @click="handleClick"
112 : >
113 : <!-- Image -->
114 : <div
115 : v-if="layout !== 'list-small'"
116 : ref="mediaContainerRef"
117 : :class="
118 : cn(
119 : 'relative',
120 : 'aspect-square w-full overflow-hidden outline-1 -outline-offset-1 outline-border',
121 : 'transition-[transform,box-shadow] duration-150',
122 : {
123 : 'max-w-16 min-w-16 rounded-l-lg': layout === 'list',
124 : 'rounded-sm group-hover/item:scale-108 group-active/item:scale-95':
125 : layout === 'grid',
126 : // selection
127 : 'ring-2 ring-muted-foreground':
128 : layout === 'grid' && (selected || candidate)
129 : }
130 : )
131 : "
132 : >
133 : <!-- Selected Icon -->
134 : <div
135 : v-if="selected"
136 : :aria-label="t('g.selected')"
137 : role="img"
138 : class="absolute top-1 left-1 size-4 rounded-full border border-foreground bg-foreground"
139 : >
140 : <i
141 : class="bold icon-[lucide--check] size-3 translate-y-[-0.5px] text-background"
142 : aria-hidden="true"
143 : />
144 : </div>
145 : <video
146 : v-if="previewUrl && isVideo"
147 : :src="previewUrl"
148 : :aria-label="label ?? name"
149 : class="size-full object-cover"
150 : preload="metadata"
151 : muted
152 : @loadeddata="handleVideoLoad"
153 : />
154 : <img
155 : v-else-if="displayedPreviewUrl"
156 : :src="displayedPreviewUrl"
157 : :alt="name"
158 : draggable="false"
159 : class="size-full object-cover"
160 : @load="handleImageLoad"
161 : />
162 : <div
163 : v-else-if="isMesh"
164 : data-testid="dropdown-item-mesh-placeholder"
165 : class="flex size-full items-center justify-center bg-muted"
166 : >
167 : <i class="icon-[lucide--box] text-3xl text-muted-foreground" />
168 : </div>
169 : <div
170 : v-else
171 : data-testid="dropdown-item-media-placeholder"
172 : class="size-full bg-linear-to-tr from-blue-400 via-teal-500 to-green-400"
173 : />
174 : </div>
175 : <!-- Name -->
176 : <div
177 : :class="
178 : cn('flex gap-1', {
179 : 'flex-col': layout === 'grid',
180 : 'w-full min-w-0 flex-col justify-center px-4 py-1': layout === 'list',
181 : 'w-full flex-row items-center justify-between p-2':
182 : layout === 'list-small'
183 : })
184 : "
185 : >
186 : <Tooltip :disabled="layout !== 'grid'">
187 : <TooltipTrigger as-child>
188 : <span
189 : :class="
190 : cn(
191 : 'line-clamp-2 block overflow-hidden text-xs wrap-break-word',
192 : 'transition-colors duration-150',
193 : // selection
194 : !!selected && 'text-foreground'
195 : )
196 : "
197 : >
198 : {{ label ?? name }}
199 : </span>
200 : </TooltipTrigger>
201 : <TooltipContent>{{ label ?? name }}</TooltipContent>
202 : </Tooltip>
203 : <!-- Meta Data -->
204 : <span v-if="actualDimensions" class="block text-xs text-muted-foreground">
205 : {{ actualDimensions }}
206 : </span>
207 : </div>
208 : </div>
209 : </template>
|