Line data Source code
1 16 : <template>
2 2236 : <Teleport v-if="galleryVisible" to="body">
3 : <div
4 : ref="dialogRef"
5 : role="dialog"
6 : aria-modal="true"
7 : :aria-label="$t('g.gallery')"
8 : tabindex="-1"
9 : class="fixed inset-0 z-overlay flex items-center justify-center bg-black/90 outline-none"
10 : data-mask
11 : @mousedown="onMaskMouseDown"
12 : @mouseup="onMaskMouseUp"
13 : @keydown.stop="handleKeyDown"
14 : >
15 : <!-- Close Button -->
16 : <Button
17 : variant="secondary"
18 : size="icon-lg"
19 : class="absolute top-4 right-4 z-10 rounded-full"
20 : :aria-label="$t('g.close')"
21 : @click="close"
22 : >
23 : <i class="icon-[lucide--x] size-5" />
24 : </Button>
25 :
26 : <!-- Previous Button -->
27 : <Button
28 : v-if="hasMultiple"
29 : variant="secondary"
30 : size="icon-lg"
31 : class="fixed top-1/2 left-4 z-10 -translate-y-1/2 rounded-full"
32 : :aria-label="$t('g.previous')"
33 : @click="navigateImage(-1)"
34 : >
35 : <i class="icon-[lucide--chevron-left] size-6" />
36 : </Button>
37 :
38 : <!-- Content -->
39 : <div class="flex max-h-full max-w-full items-center justify-center">
40 : <template v-if="activeItem">
41 : <KeepAlive :max="RETAINED_VIDEO_COUNT" include="ResultVideo">
42 : <ComfyImage
43 : v-if="isImageResult(activeItem)"
44 : :key="resultItemUrl(activeItem)"
45 : :src="resultItemUrl(activeItem)"
46 : :contain="false"
47 : :alt="activeItem.filename"
48 : class="size-auto max-h-[90vh] max-w-[90vw] object-contain"
49 : />
50 : <ResultVideo
51 : v-else-if="isVideoResult(activeItem)"
52 : :key="resultItemUrl(activeItem)"
53 : :result="activeItem"
54 : :autoplay="autoplayVideo"
55 : />
56 : <ResultAudio
57 : v-else-if="isAudioResult(activeItem)"
58 : :result="activeItem"
59 : />
60 : <ResultText
61 : v-else-if="isTextResult(activeItem)"
62 : :result="activeItem"
63 : />
64 : </KeepAlive>
65 : </template>
66 : </div>
67 :
68 : <!-- Next Button -->
69 : <Button
70 : v-if="hasMultiple"
71 : variant="secondary"
72 : size="icon-lg"
73 : class="fixed top-1/2 right-4 z-10 -translate-y-1/2 rounded-full"
74 : :aria-label="$t('g.next')"
75 : @click="navigateImage(1)"
76 : >
77 : <i class="icon-[lucide--chevron-right] size-6" />
78 : </Button>
79 : </div>
80 : </Teleport>
81 : </template>
82 :
83 : <script setup lang="ts">
84 : import { computed, nextTick, ref, watch } from 'vue'
85 :
86 : import ComfyImage from '@/components/common/ComfyImage.vue'
87 : import Button from '@comfyorg/design-system/button/Button'
88 : import type { AugmentedResultItem } from '@/utils/resultItem'
89 : import { resultItemUrl } from '@/utils/resultItemUrl'
90 : import {
91 : isAudioResult,
92 : isImageResult,
93 : isTextResult,
94 : isVideoResult
95 : } from '@/utils/resultItem'
96 :
97 : import ResultAudio from './ResultAudio.vue'
98 : import ResultText from './ResultText.vue'
99 : import ResultVideo from './ResultVideo.vue'
100 :
101 2092 : const emit = defineEmits<{
102 : (e: 'update:activeIndex', value: number): void
103 : }>()
104 :
105 2092 : const props = defineProps<{
106 : allGalleryItems: AugmentedResultItem[]
107 : activeIndex: number
108 : autoplayVideo?: boolean
109 : }>()
110 :
111 : /* Keeps the active video plus its neighbors buffered across gallery moves. */
112 2355 : const RETAINED_VIDEO_COUNT = 3
113 :
114 2092 : const galleryVisible = ref(false)
115 2092 : const dialogRef = ref<HTMLElement>()
116 2092 : let previouslyFocusedElement: HTMLElement | null = null
117 2092 : const hasMultiple = computed(() => props.allGalleryItems.length > 1)
118 2092 : const activeItem = computed(() => props.allGalleryItems[props.activeIndex])
119 :
120 2092 : watch(
121 : () => props.activeIndex,
122 : (index) => {
123 2101 : galleryVisible.value = index !== -1
124 2101 : if (index !== -1) {
125 6 : previouslyFocusedElement = document.activeElement as HTMLElement | null
126 6 : void nextTick(() => dialogRef.value?.focus())
127 : }
128 : },
129 : { immediate: true }
130 : )
131 :
132 2092 : const neighborImageUrls = computed(() => {
133 2101 : if (!galleryVisible.value || !hasMultiple.value) return []
134 2 : const count = props.allGalleryItems.length
135 2 : return [-1, 1]
136 : .map(
137 : (step) =>
138 : props.allGalleryItems[(props.activeIndex + step + count) % count]
139 : )
140 : .filter(isImageResult)
141 : .map(resultItemUrl)
142 : })
143 :
144 2092 : watch(
145 : neighborImageUrls,
146 : (urls) => {
147 2101 : for (const url of urls) {
148 4 : const image = new Image()
149 4 : image.decoding = 'async'
150 4 : image.src = url
151 : }
152 : },
153 : { immediate: true }
154 : )
155 :
156 3 : function close() {
157 3 : galleryVisible.value = false
158 3 : emit('update:activeIndex', -1)
159 3 : previouslyFocusedElement?.focus()
160 3 : previouslyFocusedElement = null
161 : }
162 :
163 0 : function navigateImage(direction: number) {
164 0 : const newIndex =
165 : (props.activeIndex + direction + props.allGalleryItems.length) %
166 : props.allGalleryItems.length
167 0 : emit('update:activeIndex', newIndex)
168 : }
169 :
170 2092 : let maskMouseDownTarget: EventTarget | null = null
171 :
172 2 : function onMaskMouseDown(event: MouseEvent) {
173 2 : maskMouseDownTarget = event.target
174 : }
175 :
176 2 : function onMaskMouseUp(event: MouseEvent) {
177 2 : if (
178 : maskMouseDownTarget === event.target &&
179 : (event.target as HTMLElement)?.hasAttribute('data-mask')
180 : ) {
181 0 : close()
182 : }
183 : }
184 :
185 1 : function handleKeyDown(event: KeyboardEvent) {
186 : const actions: Record<string, () => void> = {
187 : ArrowLeft: () => navigateImage(-1),
188 : ArrowRight: () => navigateImage(1),
189 : Escape: () => close()
190 : }
191 :
192 1 : const action = actions[event.key]
193 1 : if (action) {
194 1 : event.preventDefault()
195 1 : action()
196 : }
197 : }
198 : </script>
|