Files
zoeissleeping 8c2cefea49 feat: add image viewer with zoom and pan
Full-featured image viewer with pinch-zoom, scroll-zoom, drag-pan,
double-click reset, and keyboard shortcuts. Animated open/close
transitions match the origin element position. Integrates into
attachment display with click-to-open behavior.
2026-04-27 12:05:58 -05:00

76 lines
3.1 KiB
Vue

<script setup lang="ts">
import ImageViewer from '~/components/ImageViewer.vue';
const props = defineProps<{
file: {
id: string;
name: string;
mimeType: string;
status?: 'uploading' | 'uploaded' | 'error';
url: string;
progress?: number;
};
}>();
const isImage = computed(() => props.file.mimeType.startsWith('image/'));
const isVideo = computed(() => props.file.mimeType.startsWith('video/'));
const isAudio = computed(() => props.file.mimeType.startsWith('audio/'));
const isPdf = computed(() => props.file.mimeType === 'application/pdf');
const imageViewerOpen = ref(false);
const imageRef = ref<HTMLImageElement | null>(null);
const imageViewerOriginRect = ref<DOMRect | null>(null);
const fileIcon = computed(() => {
if (isImage.value) return 'i-mynaui-image';
if (isVideo.value) return 'i-mynaui-video';
if (isAudio.value) return 'i-mynaui-music';
if (isPdf.value) return 'i-tabler-file-type-pdf';
if (props.file.mimeType.includes('text')) return 'i-mynaui-file-text';
if (props.file.mimeType.includes('zip') || props.file.mimeType.includes('archive')) return 'i-mynaui-archive';
return 'i-mynaui-file';
});
const fileExtension = computed(() => {
const parts = props.file.name.split('.');
return parts.length > 1 ? parts.pop()?.toUpperCase() : 'FILE';
});
function openImageViewer() {
imageViewerOriginRect.value = imageRef.value?.getBoundingClientRect() ?? null;
imageViewerOpen.value = true;
}
</script>
<template>
<div class="relative h-full w-fit flex">
<div v-if="isImage">
<img ref="imageRef" :src="props.file.url"
class="rounded-lg h-full w-full max-h-36 max-w-64 object-cover cursor-zoom-in hover:opacity-90 transition-opacity"
@click="openImageViewer" />
<ImageViewer v-if="imageViewerOpen" :src="props.file.url" :alt="props.file.name"
:origin-rect="imageViewerOriginRect" :origin-element="imageRef"
@close="imageViewerOpen = false" />
</div>
<video v-else-if="isVideo" controls :src="props.file.url"
class="rounded-lg h-full w-full max-h-36 max-w-64 object-cover" />
<audio v-else-if="isAudio" controls :src="props.file.url" class="rounded-lg h-full w-full max-h-36 max-w-64" />
<div v-else class="flex items-center gap-2 p-2 rounded-lg bg-[var(--color-hover)] min-w-40 max-w-48">
<span :class="[fileIcon, 'text-6 text-[var(--color-accent)]']"></span>
<div class="flex-1 min-w-0">
<p class="text-sm truncate">{{ props.file.name }}</p>
<p class="text-xs text-[var(--text-tertiary)]">{{ fileExtension }}</p>
</div>
</div>
<div v-if="file.status === 'uploading'"
class="absolute inset-0 bg-black/50 rounded-lg flex items-center justify-center">
<div class="text-center">
<span class="i-svg-spinners-90-ring-with-bg text-6 text-white block mb-1"></span>
<span class="text-xs text-white">{{ file.progress || 0 }}%</span>
</div>
</div>
</div>
</template>