Line data Source code
1 16 : <template>
2 5 : <div class="flex min-h-0 flex-1 flex-col gap-6">
3 : <div class="flex flex-col gap-2">
4 : <span class="text-sm text-foreground select-none">
5 : {{ $t('comfyHubPublish.selectAThumbnail') }}
6 : </span>
7 : <ToggleGroup
8 : type="single"
9 : :model-value="thumbnailType"
10 : class="grid w-full grid-cols-3 gap-4"
11 : @update:model-value="handleThumbnailTypeChange"
12 : >
13 45 : <ToggleGroupItem
14 : v-for="option in thumbnailOptions"
15 : :key="option.value"
16 : :value="option.value"
17 : class="flex h-auto w-full gap-2 bg-card p-2 font-inter text-foreground data-[state=on]:bg-muted"
18 : >
19 : <i :class="option.icon" />
20 : <span class="text-center text-sm font-bold">
21 : {{ option.label }}
22 : </span>
23 : </ToggleGroupItem>
24 : </ToggleGroup>
25 : </div>
26 :
27 : <div class="flex min-h-0 flex-1 flex-col gap-2">
28 : <div class="flex items-center justify-between">
29 : <span class="text-sm text-foreground select-none">
30 : {{ uploadSectionLabel }}
31 : </span>
32 : <Button
33 : v-if="hasThumbnailContent"
34 : variant="muted-textonly"
35 : size="sm"
36 : @click="clearAllPreviews"
37 : >
38 : {{ $t('g.clear') }}
39 : </Button>
40 : </div>
41 :
42 : <template v-if="thumbnailType === 'imageComparison'">
43 : <div
44 : class="grid flex-1 grid-cols-1 grid-rows-1 place-content-center-safe overflow-hidden"
45 : >
46 : <div
47 : v-if="hasBothComparisonImages"
48 : ref="comparisonPreviewRef"
49 : class="relative col-span-full row-span-full cursor-crosshair overflow-hidden rounded-lg"
50 : >
51 : <img
52 : :src="comparisonPreviewUrls.after!"
53 : :alt="$t('comfyHubPublish.uploadComparisonAfterPrompt')"
54 : class="size-full object-contain"
55 : />
56 : <img
57 : :src="comparisonPreviewUrls.before!"
58 : :alt="$t('comfyHubPublish.uploadComparisonBeforePrompt')"
59 : class="absolute inset-0 size-full object-contain"
60 : :style="{
61 : clipPath: `inset(0 ${100 - previewSliderPosition}% 0 0)`
62 : }"
63 : />
64 : <div
65 : class="pointer-events-none absolute inset-y-0 w-0.5 bg-white/30 backdrop-blur-sm"
66 : :style="{ left: `${previewSliderPosition}%` }"
67 : />
68 : </div>
69 :
70 : <div
71 : :class="
72 : cn(
73 : 'col-span-full row-span-full flex items-center-safe justify-center-safe gap-2',
74 : hasBothComparisonImages && 'invisible'
75 : )
76 : "
77 : >
78 0 : <label
79 : v-for="slot in comparisonSlots"
80 : :key="slot.key"
81 : :ref="(el) => (comparisonDropRefs[slot.key] = el as HTMLElement)"
82 : :class="
83 : cn(
84 : 'flex aspect-square h-full min-h-0 flex-[0_1_auto] cursor-pointer flex-col items-center justify-center rounded-lg border border-dashed text-center transition-colors',
85 : comparisonPreviewUrls[slot.key]
86 : ? 'self-start'
87 : : 'flex-[0_1_1]',
88 : comparisonOverStates[slot.key]
89 : ? 'border-muted-foreground'
90 : : 'border-border hover:border-muted-foreground'
91 : )
92 : "
93 : @dragenter.stop
94 : @dragleave.stop
95 : @dragover.prevent.stop
96 : @drop.prevent.stop
97 : >
98 : <input
99 : type="file"
100 : accept="image/*"
101 : class="hidden"
102 : @change="(e) => handleComparisonSelect(e, slot.key)"
103 : />
104 : <template v-if="comparisonPreviewUrls[slot.key]">
105 : <img
106 : :src="comparisonPreviewUrls[slot.key]!"
107 : :alt="slot.label"
108 : class="max-h-full max-w-full object-contain"
109 : />
110 : </template>
111 : <template v-else>
112 : <span class="text-sm font-medium text-muted-foreground">
113 : {{ slot.label }}
114 : </span>
115 : <span class="text-xs text-muted-foreground">
116 : {{ $t('comfyHubPublish.uploadThumbnailHint') }}
117 : </span>
118 : </template>
119 : </label>
120 : </div>
121 : </div>
122 : </template>
123 :
124 : <template v-else>
125 : <label
126 : ref="singleDropRef"
127 : :class="
128 : cn(
129 : 'm-auto flex aspect-square min-h-0 w-full max-w-48 cursor-pointer flex-col items-center justify-center rounded-lg border border-dashed text-center transition-colors',
130 : thumbnailPreviewUrl ? 'self-center p-1' : 'flex-1',
131 : isOverSingleDrop
132 : ? 'border-muted-foreground'
133 : : 'border-border hover:border-muted-foreground'
134 : )
135 : "
136 : @dragenter.stop
137 : @dragleave.stop
138 : @dragover.prevent.stop
139 : @drop.prevent.stop
140 : >
141 : <input
142 : type="file"
143 : :accept="
144 : thumbnailType === 'video'
145 : ? 'video/*,image/gif,image/webp'
146 : : 'image/*'
147 : "
148 : class="hidden"
149 : @change="handleFileSelect"
150 : />
151 : <template v-if="thumbnailPreviewUrl">
152 : <video
153 : v-if="showVideoPreview"
154 : :src="thumbnailPreviewUrl"
155 : :aria-label="$t('comfyHubPublish.videoPreview')"
156 : class="max-h-full max-w-full object-contain"
157 : muted
158 : loop
159 : autoplay
160 : />
161 : <img
162 : v-else
163 : :src="thumbnailPreviewUrl"
164 : :alt="$t('comfyHubPublish.thumbnailPreview')"
165 : class="max-h-full max-w-full object-contain"
166 : />
167 : </template>
168 : <template v-else>
169 : <span class="text-sm text-muted-foreground">
170 : {{ $t('comfyHubPublish.uploadPromptClickToBrowse') }}
171 : </span>
172 : <span class="text-sm text-muted-foreground">
173 : {{ uploadDropText }}
174 : </span>
175 : <span class="text-xs text-muted-foreground">
176 : {{ $t('comfyHubPublish.uploadThumbnailHint') }}
177 : </span>
178 : </template>
179 : </label>
180 : </template>
181 : </div>
182 : </div>
183 : </template>
184 :
185 : <script setup lang="ts">
186 : import Button from '@comfyorg/design-system/button/Button'
187 : import ToggleGroup from '@comfyorg/design-system/toggle-group/ToggleGroup'
188 : import ToggleGroupItem from '@comfyorg/design-system/toggle-group/ToggleGroupItem'
189 : import { useSliderFromMouse } from '@/platform/workflow/sharing/composables/useSliderFromMouse'
190 : import type { ThumbnailType } from '@/platform/workflow/sharing/types/comfyHubTypes'
191 : import {
192 : isFileTooLarge,
193 : MAX_IMAGE_SIZE_MB,
194 : MAX_VIDEO_SIZE_MB
195 : } from '@/platform/workflow/sharing/utils/validateFileSize'
196 : import { cn } from '@comfyorg/tailwind-utils'
197 : import { useDropZone, useObjectUrl } from '@vueuse/core'
198 : import { computed, reactive, ref } from 'vue'
199 : import { useI18n } from 'vue-i18n'
200 :
201 : const {
202 : thumbnailType = 'image',
203 : thumbnailFile = null,
204 : thumbnailUrl = null,
205 : existingThumbnailType = null,
206 : comparisonBeforeFile = null,
207 : comparisonAfterFile = null,
208 : comparisonAfterUrl = null
209 : } = defineProps<{
210 : thumbnailType?: ThumbnailType
211 : thumbnailFile?: File | null
212 : thumbnailUrl?: string | null
213 : existingThumbnailType?: ThumbnailType | null
214 : comparisonBeforeFile?: File | null
215 : comparisonAfterFile?: File | null
216 : comparisonAfterUrl?: string | null
217 : }>()
218 :
219 5 : const emit = defineEmits<{
220 : 'update:thumbnailType': [value: ThumbnailType]
221 : 'update:thumbnailFile': [value: File | null]
222 : 'update:thumbnailUrl': [value: string | null]
223 : 'update:comparisonBeforeFile': [value: File | null]
224 : 'update:comparisonAfterFile': [value: File | null]
225 : 'update:comparisonAfterUrl': [value: string | null]
226 : }>()
227 :
228 5 : const { t } = useI18n()
229 :
230 0 : function isThumbnailType(value: string): value is ThumbnailType {
231 0 : return value === 'image' || value === 'video' || value === 'imageComparison'
232 : }
233 :
234 0 : function handleThumbnailTypeChange(value: unknown) {
235 0 : if (typeof value === 'string' && isThumbnailType(value)) {
236 : // Keep existing URLs; they stay inert until the type matches again (see existingSingleUrl).
237 0 : emit('update:thumbnailFile', null)
238 0 : emit('update:comparisonBeforeFile', null)
239 0 : emit('update:comparisonAfterFile', null)
240 0 : emit('update:thumbnailType', value)
241 : }
242 : }
243 :
244 5 : const uploadSectionLabel = computed(() => {
245 5 : if (thumbnailType === 'video') return t('comfyHubPublish.uploadVideo')
246 5 : if (thumbnailType === 'imageComparison') {
247 0 : return t('comfyHubPublish.uploadComparison')
248 : }
249 5 : return t('comfyHubPublish.uploadThumbnail')
250 : })
251 :
252 5 : const uploadDropText = computed(() =>
253 : thumbnailType === 'video'
254 : ? t('comfyHubPublish.uploadPromptDropVideo')
255 : : t('comfyHubPublish.uploadPromptDropImage')
256 : )
257 :
258 5 : const thumbnailOptions = [
259 : {
260 : value: 'image' as const,
261 : label: t('comfyHubPublish.thumbnailImage'),
262 : icon: 'icon-[lucide--image]'
263 : },
264 : {
265 : value: 'video' as const,
266 : label: t('comfyHubPublish.thumbnailVideo'),
267 : icon: 'icon-[lucide--video]'
268 : },
269 : {
270 : value: 'imageComparison' as const,
271 : label: t('comfyHubPublish.thumbnailImageComparison'),
272 : icon: 'icon-[lucide--diff]'
273 : }
274 : ]
275 5 : const existingSingleUrl = computed(() =>
276 : existingThumbnailType === thumbnailType
277 : ? (thumbnailUrl ?? undefined)
278 : : undefined
279 : )
280 : // For imageComparison, thumbnailUrl is the "before" image (the backend's primary thumbnail_url).
281 5 : const existingComparisonUrls = computed(() =>
282 : existingThumbnailType === 'imageComparison'
283 : ? {
284 : before: thumbnailUrl ?? undefined,
285 : after: comparisonAfterUrl ?? undefined
286 : }
287 : : { before: undefined, after: undefined }
288 : )
289 :
290 5 : const thumbnailFileUrl = useObjectUrl(() => thumbnailFile ?? undefined)
291 5 : const thumbnailPreviewUrl = computed(
292 : () => thumbnailFileUrl.value ?? existingSingleUrl.value
293 : )
294 :
295 0 : function isAnimatedImageUrl(url: string): boolean {
296 0 : return /\.(gif|webp)(\?|#|$)/i.test(url)
297 : }
298 5 : const showVideoPreview = computed(() => {
299 0 : if (thumbnailFile) return thumbnailFile.type.startsWith('video/')
300 0 : return thumbnailType === 'video' && !!existingSingleUrl.value
301 : ? !isAnimatedImageUrl(existingSingleUrl.value)
302 : : false
303 : })
304 :
305 0 : function setThumbnailPreview(file: File) {
306 : const maxSize = file.type.startsWith('video/')
307 : ? MAX_VIDEO_SIZE_MB
308 : : MAX_IMAGE_SIZE_MB
309 0 : if (isFileTooLarge(file, maxSize)) return
310 0 : emit('update:thumbnailFile', file)
311 0 : emit('update:thumbnailUrl', null)
312 : }
313 :
314 5 : const comparisonBeforeFileUrl = useObjectUrl(
315 : () => comparisonBeforeFile ?? undefined
316 : )
317 5 : const comparisonAfterFileUrl = useObjectUrl(
318 : () => comparisonAfterFile ?? undefined
319 : )
320 5 : const comparisonPreviewUrls = computed(() => ({
321 : before: comparisonBeforeFileUrl.value ?? existingComparisonUrls.value.before,
322 : after: comparisonAfterFileUrl.value ?? existingComparisonUrls.value.after
323 : }))
324 :
325 5 : const hasBothComparisonImages = computed(
326 : () =>
327 : !!(comparisonPreviewUrls.value.before && comparisonPreviewUrls.value.after)
328 : )
329 :
330 5 : const comparisonPreviewRef = ref<HTMLElement | null>(null)
331 5 : const previewSliderPosition = useSliderFromMouse(comparisonPreviewRef)
332 :
333 5 : const hasThumbnailContent = computed(() => {
334 5 : if (thumbnailType === 'imageComparison') {
335 0 : return !!(
336 : comparisonPreviewUrls.value.before || comparisonPreviewUrls.value.after
337 : )
338 : }
339 5 : return !!thumbnailPreviewUrl.value
340 : })
341 :
342 0 : function clearAllPreviews() {
343 0 : if (thumbnailType === 'imageComparison') {
344 0 : emit('update:comparisonBeforeFile', null)
345 0 : emit('update:comparisonAfterFile', null)
346 0 : emit('update:thumbnailUrl', null)
347 0 : emit('update:comparisonAfterUrl', null)
348 0 : return
349 : }
350 :
351 0 : emit('update:thumbnailFile', null)
352 0 : emit('update:thumbnailUrl', null)
353 : }
354 :
355 0 : function handleFileSelect(event: Event) {
356 0 : if (!(event.target instanceof HTMLInputElement)) return
357 0 : const file = event.target.files?.[0]
358 0 : if (file) setThumbnailPreview(file)
359 : }
360 :
361 5 : const singleDropRef = ref<HTMLElement | null>(null)
362 :
363 0 : function isImageType(types: readonly string[]) {
364 0 : return types.some((type) => type.startsWith('image/'))
365 : }
366 :
367 0 : function isVideoModeMedia(types: readonly string[]) {
368 0 : return types.some(
369 : (type) =>
370 : type.startsWith('video/') || type === 'image/gif' || type === 'image/webp'
371 : )
372 : }
373 :
374 5 : const { isOverDropZone: isOverSingleDrop } = useDropZone(singleDropRef, {
375 : dataTypes: (types: readonly string[]) =>
376 : thumbnailType === 'video' ? isVideoModeMedia(types) : isImageType(types),
377 : multiple: false,
378 : onDrop(files) {
379 0 : const file = files?.[0]
380 0 : if (file) {
381 0 : setThumbnailPreview(file)
382 : }
383 : }
384 : })
385 :
386 : type ComparisonSlot = 'before' | 'after'
387 :
388 5 : const comparisonSlots = [
389 : {
390 : key: 'before' as const,
391 : label: t('comfyHubPublish.uploadComparisonBeforePrompt')
392 : },
393 : {
394 : key: 'after' as const,
395 : label: t('comfyHubPublish.uploadComparisonAfterPrompt')
396 : }
397 : ]
398 :
399 0 : function setComparisonPreview(file: File, slot: ComparisonSlot) {
400 0 : if (isFileTooLarge(file, MAX_IMAGE_SIZE_MB)) return
401 0 : if (slot === 'before') {
402 0 : emit('update:comparisonBeforeFile', file)
403 0 : emit('update:thumbnailUrl', null)
404 0 : return
405 : }
406 0 : emit('update:comparisonAfterFile', file)
407 0 : emit('update:comparisonAfterUrl', null)
408 : }
409 :
410 0 : function handleComparisonSelect(event: Event, slot: ComparisonSlot) {
411 0 : if (!(event.target instanceof HTMLInputElement)) return
412 0 : const file = event.target.files?.[0]
413 0 : if (file) setComparisonPreview(file, slot)
414 : }
415 :
416 5 : const comparisonDropRefs = reactive<Record<ComparisonSlot, HTMLElement | null>>(
417 : { before: null, after: null }
418 : )
419 10 : function useComparisonDropZone(slot: ComparisonSlot) {
420 10 : return useDropZone(
421 : computed(() => comparisonDropRefs[slot]),
422 : {
423 : dataTypes: isImageType,
424 : multiple: false,
425 : onDrop(files) {
426 0 : const file = files?.[0]
427 0 : if (file) setComparisonPreview(file, slot)
428 : }
429 : }
430 : )
431 : }
432 :
433 5 : const { isOverDropZone: isOverBefore } = useComparisonDropZone('before')
434 5 : const { isOverDropZone: isOverAfter } = useComparisonDropZone('after')
435 :
436 5 : const comparisonOverStates = computed(() => ({
437 : before: isOverBefore.value,
438 : after: isOverAfter.value
439 : }))
440 : </script>
|