keanu-weblite/src/components/file_mode/ThumbnailView.vue

177 lines
5.2 KiB
Vue
Raw Normal View History

2023-10-25 10:44:25 +00:00
<template>
<div ref="thumbnailRef" style="width: 100%; height: 100%">
2025-07-10 09:46:07 +02:00
<v-responsive v-if="isVideo && (source || poster)" :class="{ 'thumbnail-item': true, preview: previewOnly }">
<video :src="source" :poster="poster" :controls="!previewOnly" class="w-100 h-100">
2025-06-09 09:44:49 +02:00
{{ $t("fallbacks.video_file") }}
</video>
</v-responsive>
<ImageWithProgress
v-else-if="isImage"
2025-06-09 09:44:49 +02:00
:aspect-ratio="previewOnly ? 16 / 9 : undefined"
:class="{ 'thumbnail-item': true, preview: previewOnly }"
:src="source"
2025-06-09 09:44:49 +02:00
:contain="!previewOnly"
:cover="previewOnly"
:loadingProgress="loadingProgress"
2025-06-09 09:44:49 +02:00
/>
<div v-else :class="{ 'thumbnail-item': true, preview: previewOnly, 'file-item': true }">
<v-icon :class="fileTypeIconClass">{{ fileTypeIcon }}</v-icon>
<div class="file-name">{{ $$sanitize(fileName) }}</div>
2025-06-09 09:44:49 +02:00
<div class="file-size">{{ fileSize }}</div>
2023-10-25 10:44:25 +00:00
</div>
2025-06-09 09:44:49 +02:00
</div>
2023-10-25 10:44:25 +00:00
</template>
<script setup lang="ts">
2025-08-20 15:12:04 +02:00
import { singleOrDoubleTapRecognizer } from "../../plugins/touch";
import { computed, inject, onBeforeUnmount, onMounted, Ref, ref, useTemplateRef, watch } from "vue";
2025-06-09 09:44:49 +02:00
import { EventAttachment } from "../../models/eventAttachment";
import { useThumbnail } from "../messages/composition/useThumbnail";
import { Attachment } from "../../models/attachment";
2025-07-15 10:02:14 +02:00
import ImageWithProgress from "../ImageWithProgress.vue";
2025-08-21 16:07:13 +02:00
function isEventAttachment(source: EventAttachment | Attachment | undefined): source is EventAttachment {
2025-08-25 11:38:41 +02:00
return (source as EventAttachment)?.event !== undefined;
2025-08-21 16:07:13 +02:00
}
function isAttachment(source: EventAttachment | Attachment | undefined): source is Attachment {
2025-08-25 11:38:41 +02:00
return (source as Attachment)?.file !== undefined;
2025-08-21 16:07:13 +02:00
}
2023-12-04 11:29:23 +01:00
const $$sanitize: any = inject("globalSanitize");
const thumbnailRef = useTemplateRef("thumbnailRef");
interface ThumbnailProps {
2025-08-21 16:07:13 +02:00
item?: EventAttachment | Attachment;
previewOnly?: boolean;
}
2025-06-09 09:44:49 +02:00
type ThumbnailEmits = {
2025-08-21 16:07:13 +02:00
(event: "itemclick", value: { item: EventAttachment | Attachment }): void;
};
const props = defineProps<ThumbnailProps>();
2025-08-21 16:07:13 +02:00
const { item, previewOnly = false } = props;
const emits = defineEmits<ThumbnailEmits>();
2025-08-21 16:07:13 +02:00
let { isVideo, isImage, fileTypeIcon, fileTypeIconClass, fileName, fileSize } = useThumbnail(isEventAttachment(props.item) ? props.item.event : isAttachment(props.item) ? props.item.file : undefined);
const fileURL: Ref<string | undefined> = ref(undefined);
2025-07-15 10:02:14 +02:00
const source: Ref<string | undefined> = ref(undefined);
2025-07-10 09:46:07 +02:00
const poster: Ref<string | undefined> = ref(undefined);
2025-07-15 10:02:14 +02:00
const updateSource = () => {
2025-08-21 16:07:13 +02:00
if (isEventAttachment(props.item)) {
const eventAttachment = props.item;
if (isVideo.value || eventAttachment.src) {
source.value = eventAttachment.src;
2025-07-15 10:02:14 +02:00
} else if (previewOnly) {
2025-08-21 16:07:13 +02:00
eventAttachment.loadThumbnail().then((url) => {
2025-07-15 10:02:14 +02:00
source.value = url.data;
})
} else if (isImage.value) {
2025-08-21 16:07:13 +02:00
eventAttachment.loadSrc().then((url) => {
2025-07-15 10:02:14 +02:00
source.value = url.data;
})
}
2025-08-21 16:07:13 +02:00
} else if (isAttachment(props.item)) {
const attachment = props.item;
2025-07-15 10:02:14 +02:00
if (fileURL.value) {
URL.revokeObjectURL(fileURL.value);
}
2025-08-21 16:07:13 +02:00
fileURL.value = URL.createObjectURL(attachment.file);
2025-07-15 10:02:14 +02:00
source.value = fileURL.value;
} else {
source.value = undefined;
}
};
2025-07-10 09:46:07 +02:00
const updatePoster = () => {
2025-08-21 16:07:13 +02:00
if (isEventAttachment(props.item) && isVideo.value) {
const eventAttachment = props.item;
if (eventAttachment.thumbnail) {
poster.value = eventAttachment.thumbnail;
2025-07-10 09:46:07 +02:00
} else {
2025-08-21 16:07:13 +02:00
eventAttachment
2025-07-10 09:46:07 +02:00
.loadThumbnail()
.then((url) => {
poster.value = url.data;
})
.catch(() => {
poster.value = undefined;
});
}
}
};
2025-07-15 10:02:14 +02:00
updateSource();
2025-07-10 09:46:07 +02:00
updatePoster();
2025-07-15 10:02:14 +02:00
watch(props, (props: ThumbnailProps) => {
2025-08-21 16:07:13 +02:00
const updates = useThumbnail(isEventAttachment(props.item) ? props.item.event : isAttachment(props.item) ? props.item.file : undefined);
isVideo.value = updates.isVideo.value;
isImage.value = updates.isImage.value;
fileTypeIcon = updates.fileTypeIcon;
fileTypeIconClass = updates.fileTypeIconClass;
fileName = updates.fileName;
fileSize = updates.fileSize;
2025-07-15 10:02:14 +02:00
updateSource();
2025-07-10 09:46:07 +02:00
updatePoster();
});
const loadingProgress = computed(() => {
2025-08-21 16:07:13 +02:00
if (isEventAttachment(item)) {
const eventAttachment = item;
return previewOnly ? eventAttachment.thumbnailProgress : eventAttachment.srcProgress;
}
return -1;
});
onMounted(() => {
2025-08-21 16:07:13 +02:00
if (thumbnailRef.value && item) {
2025-08-20 15:12:04 +02:00
const hammerInstance = singleOrDoubleTapRecognizer(thumbnailRef.value);
hammerInstance.on("singletap doubletap", (ev: any) => {
if (ev.type === "singletap") {
2025-08-21 16:07:13 +02:00
emits("itemclick", { item: item });
}
});
}
2025-08-21 16:07:13 +02:00
if (!previewOnly && item && isEventAttachment(item)) {
item.loadSrc();
}
});
onBeforeUnmount(() => {
if (fileURL.value) {
URL.revokeObjectURL(fileURL.value);
fileURL.value = undefined;
}
2025-06-09 09:44:49 +02:00
});
2023-10-25 10:44:25 +00:00
</script>
<style lang="scss">
@use "@/assets/css/chat.scss" as *;
2023-10-25 10:44:25 +00:00
.thumbnail-item {
2025-06-09 09:44:49 +02:00
width: 100%;
height: 100%;
margin: 0;
padding: 0;
2023-10-25 10:44:25 +00:00
}
.file-item {
2025-06-09 09:44:49 +02:00
display: flex;
align-items: center;
justify-content: center;
font-size: 0.6rem;
flex-direction: column;
padding: 20px;
.v-icon {
margin-bottom: 10px;
color: currentColor;
}
2023-10-25 10:44:25 +00:00
}
2025-06-09 09:44:49 +02:00
</style>