Line data Source code
1 2 : <template>
2 : <div
3 : class="relative size-full overflow-hidden rounded-sm bg-black"
4 : @mouseenter="isHovered = true"
5 : @mouseleave="isHovered = false"
6 : >
7 : <video
8 : v-if="status !== 'failed'"
9 : ref="videoElement"
10 : data-testid="media-asset-video"
11 : :src="src"
12 : :controls="shouldShowControls"
13 : preload="metadata"
14 : muted
15 : loop
16 : playsinline
17 : class="relative size-full object-contain transition-transform duration-300 group-hover:scale-105 group-data-[selected=true]:scale-105"
18 : @click="onVideoClick"
19 : @play="onVideoPlay"
20 : @pause="onVideoPause"
21 : @error="handleVideoError"
22 : ></video>
23 : <div
24 : v-else
25 : role="img"
26 : :aria-label="$t('g.videoFailedToLoad')"
27 : class="flex size-full items-center justify-center bg-muted"
28 : >
29 : <i class="icon-[lucide--video-off] size-8 text-muted-foreground" />
30 : </div>
31 : <VideoPlayOverlay :visible="!isPlaying && status !== 'failed'" size="md" />
32 : </div>
33 : </template>
34 :
35 : <script setup lang="ts">
36 : import { computed, ref } from 'vue'
37 :
38 : import { useRetryableMediaSrc } from '@/composables/media/useRetryableMediaSrc'
39 : import type { AssetMeta } from '@/platform/assets/schemas/mediaAssetSchema'
40 :
41 : import VideoPlayOverlay from '@comfyorg/design-system/media/VideoPlayOverlay'
42 :
43 : const { asset, showNativeControls = true } = defineProps<{
44 : asset: AssetMeta
45 : showNativeControls?: boolean
46 : }>()
47 :
48 3 : const videoElement = ref<HTMLVideoElement | null>(null)
49 3 : const isHovered = ref(false)
50 3 : const isPlaying = ref(false)
51 :
52 3 : const { src, status, onError } = useRetryableMediaSrc(
53 : () => asset.src || undefined
54 : )
55 :
56 : // Show native controls only while actively playing and hovered.
57 3 : const shouldShowControls = computed(
58 : () => showNativeControls && isPlaying.value && isHovered.value
59 : )
60 :
61 3 : const onVideoPlay = () => {
62 0 : isPlaying.value = true
63 : }
64 :
65 3 : const onVideoPause = () => {
66 0 : isPlaying.value = false
67 : }
68 :
69 3 : const handleVideoError = () => {
70 2 : isPlaying.value = false
71 2 : onError()
72 : }
73 :
74 0 : async function onVideoClick(event: MouseEvent) {
75 0 : if (event.detail > 1) return event.preventDefault()
76 0 : if (
77 : event.shiftKey ||
78 : event.metaKey ||
79 : event.ctrlKey ||
80 : shouldShowControls.value
81 : ) {
82 0 : return
83 : }
84 :
85 0 : const video = videoElement.value
86 0 : if (!video) return
87 :
88 0 : if (video.paused || video.ended) {
89 0 : await video.play().catch(() => {})
90 0 : return
91 : }
92 :
93 0 : video.pause()
94 : }
95 : </script>
|