Line data Source code
1 16 : <template>
2 : <!-- Compact: [▶] [waveform] [time] -->
3 : <div
4 : v-if="variant === 'compact'"
5 : :class="
6 : cn('flex w-full gap-2', align === 'center' ? 'items-center' : 'items-end')
7 : "
8 : @pointerdown.stop
9 : @click.stop
10 : >
11 : <Button
12 : variant="textonly"
13 : size="icon-sm"
14 : class="size-7 shrink-0 rounded-full bg-muted-foreground/15 hover:bg-muted-foreground/25"
15 : :aria-label="isPlaying ? $t('g.pause') : $t('g.play')"
16 : :loading="loading"
17 : :disabled="failed"
18 : @click.stop="togglePlayPause"
19 : >
20 : <i
21 : v-if="!isPlaying"
22 : class="ml-0.5 icon-[lucide--play] size-3 text-foreground"
23 : />
24 : <i v-else class="icon-[lucide--pause] size-3 text-foreground" />
25 : </Button>
26 :
27 : <div
28 : :ref="(el) => (waveformRef = el as HTMLElement)"
29 : data-testid="wave-audio-waveform"
30 : :class="
31 : cn(
32 : 'flex min-w-0 flex-1 cursor-pointer gap-px',
33 : align === 'center' ? 'items-center' : 'items-end'
34 : )
35 : "
36 : :style="{ height: height + 'px' }"
37 : @click="onWaveformClick"
38 : >
39 240 : <div
40 : v-for="(bar, index) in bars"
41 : :key="index"
42 : :class="
43 : cn(
44 : 'min-h-0.5 flex-1 rounded-full',
45 : loading
46 : ? 'bg-muted-foreground/20'
47 : : index <= playedBarIndex
48 : ? 'bg-foreground'
49 : : 'bg-muted-foreground/40'
50 : )
51 : "
52 : :style="{ height: (bar.height / 100) * height + 'px' }"
53 : />
54 : </div>
55 :
56 : <span class="shrink-0 text-xs text-muted-foreground tabular-nums">
57 : {{ formattedCurrentTime }} / {{ formattedDuration }}
58 : </span>
59 : </div>
60 :
61 : <!-- Expanded: waveform / progress bar + times / transport -->
62 : <div v-else class="flex w-full flex-col gap-4" @pointerdown.stop @click.stop>
63 : <div
64 : class="flex w-full items-center gap-0.5"
65 : :style="{ height: height + 'px' }"
66 : >
67 0 : <div
68 : v-for="(bar, index) in bars"
69 : :key="index"
70 : :class="
71 : cn(
72 : 'min-h-0.5 flex-1 rounded-full',
73 : loading ? 'bg-muted-foreground/20' : 'bg-foreground'
74 : )
75 : "
76 : :style="{ height: (bar.height / 100) * height + 'px' }"
77 : />
78 : </div>
79 :
80 : <div class="flex flex-col gap-1">
81 : <div
82 : ref="progressRef"
83 : data-testid="wave-audio-progress"
84 : class="relative h-1 w-full cursor-pointer rounded-full bg-muted-foreground/20"
85 : @click="handleProgressClick"
86 : >
87 : <div
88 : class="absolute top-0 left-0 h-full rounded-full bg-foreground"
89 : :style="{ width: progressRatio + '%' }"
90 : />
91 : </div>
92 : <div
93 : class="flex justify-between text-xs text-muted-foreground tabular-nums"
94 : >
95 : <span>{{ formattedCurrentTime }}</span>
96 : <span>{{ formattedDuration }}</span>
97 : </div>
98 : </div>
99 :
100 : <div class="flex items-center gap-2">
101 : <div class="w-20" />
102 :
103 : <div class="flex flex-1 items-center justify-center gap-2">
104 : <Button
105 : variant="textonly"
106 : size="icon-sm"
107 : class="size-8 rounded-full"
108 : :aria-label="$t('g.skipToStart')"
109 : :disabled="loading || failed"
110 : @click="seekToStart"
111 : >
112 : <i class="icon-[lucide--skip-back] text-foreground" />
113 : </Button>
114 : <Button
115 : variant="textonly"
116 : size="icon-sm"
117 : class="size-10 rounded-full bg-muted-foreground/15 hover:bg-muted-foreground/25"
118 : :aria-label="isPlaying ? $t('g.pause') : $t('g.play')"
119 : :loading="loading"
120 : :disabled="failed"
121 : @click="togglePlayPause"
122 : >
123 : <i
124 : v-if="!isPlaying"
125 : class="ml-0.5 icon-[lucide--play] size-5 text-foreground"
126 : />
127 : <i v-else class="icon-[lucide--pause] size-5 text-foreground" />
128 : </Button>
129 : <Button
130 : variant="textonly"
131 : size="icon-sm"
132 : class="size-8 rounded-full"
133 : :aria-label="$t('g.skipToEnd')"
134 : :disabled="loading || failed"
135 : @click="seekToEnd"
136 : >
137 : <i class="icon-[lucide--skip-forward] text-foreground" />
138 : </Button>
139 : </div>
140 :
141 : <div class="flex w-20 items-center gap-1">
142 : <Button
143 : variant="textonly"
144 : size="icon-sm"
145 : class="size-8 shrink-0 rounded-full"
146 : :aria-label="$t('g.volume')"
147 : :disabled="loading || failed"
148 : @click="toggleMute"
149 : >
150 : <i :class="cn(volumeIcon, 'text-foreground')" />
151 : </Button>
152 : <Slider
153 : :model-value="[volume * 100]"
154 : :min="0"
155 : :max="100"
156 : :step="1"
157 : :disabled="failed"
158 : class="flex-1"
159 : @update:model-value="(v) => (volume = (v?.[0] ?? 100) / 100)"
160 : />
161 : </div>
162 : </div>
163 : </div>
164 :
165 : <audio
166 : :ref="(el) => (audioRef = el as HTMLAudioElement)"
167 : data-testid="wave-audio-media"
168 : :src="mediaSrc"
169 : preload="metadata"
170 : class="hidden"
171 : @error="onError"
172 : />
173 : </template>
174 :
175 : <script setup lang="ts">
176 : import { computed, ref, toRef } from 'vue'
177 :
178 : import Button from '@comfyorg/design-system/button/Button'
179 : import Slider from '@comfyorg/design-system/slider/Slider'
180 : import { useRetryableMediaSrc } from '@/composables/media/useRetryableMediaSrc'
181 : import { useWaveAudioPlayer } from '@/composables/useWaveAudioPlayer'
182 : import { cn } from '@comfyorg/tailwind-utils'
183 :
184 : const {
185 : src,
186 : barCount = 40,
187 : height = 32,
188 : align = 'center',
189 : variant = 'compact'
190 : } = defineProps<{
191 : src: string
192 : barCount?: number
193 : height?: number
194 : align?: 'center' | 'bottom'
195 : variant?: 'compact' | 'expanded'
196 : }>()
197 :
198 2 : const progressRef = ref<HTMLElement>()
199 :
200 2 : const { src: mediaSrc, status, onError } = useRetryableMediaSrc(() => src)
201 2 : const failed = computed(() => status.value === 'failed')
202 :
203 2 : const {
204 : audioRef,
205 : waveformRef,
206 : bars,
207 : loading,
208 : isPlaying,
209 : playedBarIndex,
210 : progressRatio,
211 : formattedCurrentTime,
212 : formattedDuration,
213 : togglePlayPause,
214 : seekToStart,
215 : seekToEnd,
216 : volume,
217 : volumeIcon,
218 : toggleMute,
219 : seekToRatio,
220 : handleWaveformClick
221 : } = useWaveAudioPlayer({
222 : src: mediaSrc,
223 : logicalSrc: toRef(() => src),
224 : barCount
225 : })
226 :
227 0 : function onWaveformClick(event: MouseEvent) {
228 0 : if (failed.value) return
229 0 : handleWaveformClick(event)
230 : }
231 :
232 0 : function handleProgressClick(event: MouseEvent) {
233 0 : if (failed.value) return
234 0 : if (!progressRef.value) return
235 0 : const rect = progressRef.value.getBoundingClientRect()
236 0 : seekToRatio((event.clientX - rect.left) / rect.width)
237 : }
238 : </script>
|