|
| 1 | +<template> |
| 2 | + <Galleria |
| 3 | + v-model:visible="galleryVisible" |
| 4 | + @update:visible="handleVisibilityChange" |
| 5 | + :activeIndex="activeIndex" |
| 6 | + @update:activeIndex="handleActiveIndexChange" |
| 7 | + :value="allGalleryItems" |
| 8 | + :showIndicators="false" |
| 9 | + changeItemOnIndicatorHover |
| 10 | + showItemNavigators |
| 11 | + fullScreen |
| 12 | + circular |
| 13 | + :showThumbnails="false" |
| 14 | + > |
| 15 | + <template #item="{ item }"> |
| 16 | + <img :src="item.url" alt="gallery item" class="galleria-image" /> |
| 17 | + </template> |
| 18 | + </Galleria> |
| 19 | +</template> |
| 20 | + |
| 21 | +<script setup lang="ts"> |
| 22 | +import { defineProps, ref, watch, onMounted, onUnmounted } from 'vue' |
| 23 | +import Galleria from 'primevue/galleria' |
| 24 | +import { ResultItemImpl } from '@/stores/queueStore' |
| 25 | +
|
| 26 | +const galleryVisible = ref(false) |
| 27 | +
|
| 28 | +const emit = defineEmits<{ |
| 29 | + (e: 'update:activeIndex', value: number): void |
| 30 | +}>() |
| 31 | +
|
| 32 | +const props = defineProps<{ |
| 33 | + allGalleryItems: ResultItemImpl[] |
| 34 | + activeIndex: number |
| 35 | +}>() |
| 36 | +
|
| 37 | +watch( |
| 38 | + () => props.activeIndex, |
| 39 | + (index) => { |
| 40 | + if (index !== -1) { |
| 41 | + galleryVisible.value = true |
| 42 | + } |
| 43 | + } |
| 44 | +) |
| 45 | +
|
| 46 | +const handleVisibilityChange = (visible: boolean) => { |
| 47 | + if (!visible) { |
| 48 | + emit('update:activeIndex', -1) |
| 49 | + } |
| 50 | +} |
| 51 | +
|
| 52 | +const handleActiveIndexChange = (index: number) => { |
| 53 | + emit('update:activeIndex', index) |
| 54 | +} |
| 55 | +
|
| 56 | +const handleKeyDown = (event: KeyboardEvent) => { |
| 57 | + if (!galleryVisible.value) return |
| 58 | +
|
| 59 | + switch (event.key) { |
| 60 | + case 'ArrowLeft': |
| 61 | + navigateImage(-1) |
| 62 | + break |
| 63 | + case 'ArrowRight': |
| 64 | + navigateImage(1) |
| 65 | + break |
| 66 | + case 'Escape': |
| 67 | + galleryVisible.value = false |
| 68 | + break |
| 69 | + } |
| 70 | +} |
| 71 | +
|
| 72 | +const navigateImage = (direction: number) => { |
| 73 | + const newIndex = |
| 74 | + (props.activeIndex + direction + props.allGalleryItems.length) % |
| 75 | + props.allGalleryItems.length |
| 76 | + emit('update:activeIndex', newIndex) |
| 77 | +} |
| 78 | +
|
| 79 | +onMounted(() => { |
| 80 | + window.addEventListener('keydown', handleKeyDown) |
| 81 | +}) |
| 82 | +
|
| 83 | +onUnmounted(() => { |
| 84 | + window.removeEventListener('keydown', handleKeyDown) |
| 85 | +}) |
| 86 | +</script> |
| 87 | + |
| 88 | +<style scoped> |
| 89 | +.galleria-image { |
| 90 | + max-width: 100%; |
| 91 | + max-height: 100%; |
| 92 | + object-fit: contain; |
| 93 | + /* Set z-index so the close button doesn't get hidden behind the image when image is large */ |
| 94 | + z-index: -1; |
| 95 | +} |
| 96 | +</style> |
0 commit comments