Line data Source code
1 : <template>
2 : <div
3 40 : v-if="imageUrls.length > 0"
4 : class="video-preview flex size-full min-h-55 min-w-16 flex-col px-2"
5 : @keydown="handleKeyDown"
6 : >
7 : <!-- Video Wrapper -->
8 : <div
9 : ref="videoWrapperEl"
10 : class="relative flex flex-1 overflow-hidden rounded-[5px] bg-node"
11 : tabindex="0"
12 : role="region"
13 : :aria-label="$t('g.videoPreview')"
14 : :aria-busy="showLoader"
15 : @mouseenter="handleMouseEnter"
16 : @mouseleave="handleMouseLeave"
17 : @focusin="handleFocusIn"
18 : @focusout="handleFocusOut"
19 : >
20 : <!-- Error State -->
21 : <div
22 : v-if="videoError"
23 : role="alert"
24 : class="flex flex-auto flex-col items-center justify-center bg-muted py-8 text-center text-foreground"
25 : >
26 : <i class="mb-2 icon-[lucide--video-off] size-12" />
27 : <p class="text-sm">
28 : {{ $t(videoFailureMessageKey([errorCode])) }}
29 : </p>
30 : <p class="mt-1 text-xs">
31 : {{ getVideoFilename(currentVideoUrl) }}
32 : </p>
33 : </div>
34 :
35 : <!-- Loading State -->
36 : <Skeleton
37 : v-if="showLoader && !videoError"
38 : class="absolute inset-0 size-full"
39 : />
40 :
41 : <!-- Main Video -->
42 : <video
43 : v-if="!videoError"
44 : data-testid="main-video"
45 : :src="currentVideoUrl"
46 : :class="
47 : cn(
48 : 'absolute inset-0 size-full object-contain',
49 : showLoader && 'invisible'
50 : )
51 : "
52 : preload="metadata"
53 : controls
54 : loop
55 : playsinline
56 : @loadeddata="handleVideoLoad"
57 : @error="handleVideoError"
58 : />
59 :
60 : <!-- Floating Action Buttons (appear on hover) -->
61 : <div
62 : v-if="isHovered || isFocused"
63 : class="actions absolute top-2 right-2 flex gap-2.5"
64 : >
65 : <!-- Download Button -->
66 : <Button
67 : variant="secondary"
68 : size="icon"
69 : class="shadow-sm"
70 : :title="$t('g.downloadVideo')"
71 : :aria-label="$t('g.downloadVideo')"
72 : @click="handleDownload"
73 : >
74 : <i class="icon-[lucide--download]" />
75 : </Button>
76 :
77 : <!-- Close Button -->
78 : <Button
79 : variant="secondary"
80 : size="icon"
81 : class="shadow-sm"
82 : :title="$t('g.removeVideo')"
83 : :aria-label="$t('g.removeVideo')"
84 : @click="handleRemove"
85 : >
86 : <i class="icon-[lucide--x]" />
87 : </Button>
88 : </div>
89 :
90 : <!-- Multiple Videos Navigation -->
91 : <div
92 : v-if="hasMultipleVideos"
93 : class="absolute inset-x-2 bottom-2 flex justify-center gap-1"
94 : >
95 10 : <button
96 : v-for="(_, index) in imageUrls"
97 : :key="index"
98 : :class="getNavigationDotClass(index)"
99 : :aria-label="
100 : $t('g.viewVideoOfTotal', {
101 : index: index + 1,
102 : total: imageUrls.length
103 : })
104 : "
105 : @click="setCurrentIndex(index)"
106 : />
107 : </div>
108 : </div>
109 :
110 : <!-- Video Dimensions -->
111 : <div class="mt-2 text-center text-muted-foreground">
112 : <span
113 : v-if="videoError"
114 : class="text-destructive"
115 : data-testid="error-loading-video"
116 : >
117 : {{ $t('g.errorLoadingVideo') }}
118 : </span>
119 : <span v-else-if="showLoader"> {{ $t('g.loading') }}... </span>
120 : <span v-else>
121 : {{ actualDimensions || $t('g.calculatingDimensions') }}
122 : </span>
123 : </div>
124 : </div>
125 : </template>
126 :
127 : <script setup lang="ts">
128 : import Button from '@comfyorg/design-system/button/Button'
129 : import { useToast } from '@/components/ui/toast/toastStore'
130 : import { computed, ref, watch } from 'vue'
131 : import { useI18n } from 'vue-i18n'
132 :
133 : import { downloadFile } from '@/base/common/downloadUtil'
134 : import { videoFailureMessageKey } from '@/composables/node/useNodeImage'
135 : import { describeImageLoadFailure } from '@/platform/telemetry/imageFailureDiagnostics'
136 : import { useTelemetry } from '@comfyorg/telemetry'
137 : import Skeleton from '@comfyorg/design-system/skeleton/Skeleton'
138 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
139 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
140 : import { cn } from '@comfyorg/tailwind-utils'
141 :
142 : interface VideoPreviewProps {
143 : /** Array of video URLs to display */
144 : readonly imageUrls: readonly string[] // Named imageUrls for consistency with parent components
145 : /** Optional node ID for context-aware actions */
146 : readonly nodeId?: NodeId
147 : }
148 :
149 15 : const props = defineProps<VideoPreviewProps>()
150 :
151 15 : const { t } = useI18n()
152 15 : const nodeOutputStore = useNodeOutputStore()
153 :
154 : // Component state
155 15 : const currentIndex = ref(0)
156 15 : const isHovered = ref(false)
157 15 : const isFocused = ref(false)
158 15 : const actualDimensions = ref<string | null>(null)
159 15 : const videoError = ref(false)
160 15 : const showLoader = ref(false)
161 15 : const errorCode = ref<number>()
162 :
163 15 : const videoWrapperEl = ref<HTMLDivElement>()
164 :
165 : // Computed values
166 15 : const currentVideoUrl = computed(() => props.imageUrls[currentIndex.value])
167 15 : const hasMultipleVideos = computed(() => props.imageUrls.length > 1)
168 :
169 : // Watch for URL changes and reset state
170 15 : watch(
171 : () => props.imageUrls,
172 : (newUrls, oldUrls) => {
173 : // Only reset state if URLs actually changed (not just array reference)
174 : const urlsChanged =
175 : !oldUrls ||
176 : newUrls.length !== oldUrls.length ||
177 : newUrls.some((url, i) => url !== oldUrls[i])
178 :
179 19 : if (!urlsChanged) return
180 :
181 : // Reset current index if it's out of bounds
182 19 : if (currentIndex.value >= newUrls.length) {
183 0 : currentIndex.value = 0
184 : }
185 :
186 : // Reset loading and error states when URLs change
187 18 : actualDimensions.value = null
188 18 : videoError.value = false
189 18 : showLoader.value = newUrls.length > 0
190 : },
191 : { immediate: true }
192 : )
193 :
194 : // Event handlers
195 15 : const handleVideoLoad = (event: Event) => {
196 15 : if (!event.target || !(event.target instanceof HTMLVideoElement)) return
197 15 : const video = event.target
198 15 : showLoader.value = false
199 15 : videoError.value = false
200 15 : if (video.videoWidth && video.videoHeight) {
201 15 : actualDimensions.value = `${video.videoWidth} x ${video.videoHeight}`
202 : }
203 : }
204 :
205 15 : const handleVideoError = (event: Event) => {
206 4 : showLoader.value = false
207 4 : videoError.value = true
208 4 : actualDimensions.value = null
209 4 : errorCode.value =
210 : event.target instanceof HTMLVideoElement
211 : ? event.target.error?.code
212 : : undefined
213 :
214 4 : const failedUrl = currentVideoUrl.value
215 4 : if (!failedUrl) return
216 4 : void describeImageLoadFailure(failedUrl).then((metadata) => {
217 3 : useTelemetry()?.trackImageLoadFailed({
218 : ...metadata,
219 : source: 'node_video_preview'
220 : })
221 : })
222 : }
223 :
224 15 : const handleDownload = () => {
225 1 : try {
226 1 : downloadFile(currentVideoUrl.value)
227 : } catch (error) {
228 0 : useToast().error(t('g.error'), {
229 : description: t('g.failedToDownloadVideo'),
230 : duration: 3000
231 : })
232 : }
233 : }
234 :
235 15 : const handleRemove = () => {
236 0 : if (!props.nodeId) return
237 0 : nodeOutputStore.removeNodeOutputs(props.nodeId)
238 : }
239 :
240 15 : const setCurrentIndex = (index: number) => {
241 2 : if (currentIndex.value === index) return
242 2 : if (index >= 0 && index < props.imageUrls.length) {
243 1 : const urlChanged = props.imageUrls[index] !== currentVideoUrl.value
244 1 : currentIndex.value = index
245 1 : videoError.value = false
246 1 : if (urlChanged) {
247 1 : actualDimensions.value = null
248 1 : showLoader.value = true
249 : }
250 : }
251 : }
252 :
253 15 : const handleMouseEnter = () => {
254 1 : isHovered.value = true
255 : }
256 :
257 15 : const handleMouseLeave = () => {
258 0 : isHovered.value = false
259 : }
260 :
261 15 : const handleFocusIn = () => {
262 3 : isFocused.value = true
263 : }
264 :
265 15 : const handleFocusOut = (event: FocusEvent) => {
266 2 : if (!videoWrapperEl.value?.contains(event.relatedTarget as Node)) {
267 0 : isFocused.value = false
268 : }
269 : }
270 :
271 15 : const getNavigationDotClass = (index: number) =>
272 : cn(
273 : 'size-2 cursor-pointer rounded-full border-0 transition-all duration-200',
274 : index === currentIndex.value
275 : ? 'bg-foreground'
276 : : 'bg-foreground/50 hover:bg-foreground/80'
277 : )
278 :
279 15 : const handleKeyDown = (event: KeyboardEvent) => {
280 2 : if (props.imageUrls.length <= 1) return
281 :
282 2 : switch (event.key) {
283 : case 'ArrowLeft':
284 0 : event.preventDefault()
285 0 : setCurrentIndex(
286 : currentIndex.value > 0
287 : ? currentIndex.value - 1
288 : : props.imageUrls.length - 1
289 : )
290 0 : break
291 : case 'ArrowRight':
292 0 : event.preventDefault()
293 0 : setCurrentIndex(
294 : currentIndex.value < props.imageUrls.length - 1
295 : ? currentIndex.value + 1
296 : : 0
297 : )
298 0 : break
299 : case 'Home':
300 0 : event.preventDefault()
301 0 : setCurrentIndex(0)
302 0 : break
303 : case 'End':
304 0 : event.preventDefault()
305 0 : setCurrentIndex(props.imageUrls.length - 1)
306 : break
307 : }
308 : }
309 :
310 15 : const getVideoFilename = (url: string): string => {
311 4 : try {
312 4 : return (
313 : new URL(url, window.location.href).searchParams.get('filename') ||
314 : 'Unknown file'
315 : )
316 : } catch {
317 0 : return 'Invalid URL'
318 : }
319 : }
320 : </script>
|