2022-11-14 06:33:09 +03:00
|
|
|
<script setup lang="ts">
|
2022-11-21 16:38:10 +03:00
|
|
|
import { clamp } from '@vueuse/core'
|
2023-01-08 09:21:09 +03:00
|
|
|
import type { mastodon } from 'masto'
|
2023-02-15 13:34:23 +03:00
|
|
|
import { decode } from 'blurhash'
|
2022-11-14 06:33:09 +03:00
|
|
|
|
2022-12-13 22:26:53 +03:00
|
|
|
const {
|
|
|
|
attachment,
|
|
|
|
fullSize = false,
|
2023-02-15 13:34:23 +03:00
|
|
|
isPreview = false,
|
2022-12-13 22:26:53 +03:00
|
|
|
} = defineProps<{
|
2023-01-08 09:21:09 +03:00
|
|
|
attachment: mastodon.v1.MediaAttachment
|
|
|
|
attachments?: mastodon.v1.MediaAttachment[]
|
2022-12-13 22:26:53 +03:00
|
|
|
fullSize?: boolean
|
2023-02-15 13:34:23 +03:00
|
|
|
isPreview?: boolean
|
2022-11-14 06:33:09 +03:00
|
|
|
}>()
|
2022-11-21 16:21:53 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const src = computed(() => attachment.previewUrl || attachment.url || attachment.remoteUrl!)
|
|
|
|
const srcset = computed(() => [
|
2022-12-02 19:48:44 +03:00
|
|
|
[attachment.url, attachment.meta?.original?.width],
|
2022-12-04 15:48:07 +03:00
|
|
|
[attachment.remoteUrl, attachment.meta?.original?.width],
|
2022-11-30 20:19:35 +03:00
|
|
|
[attachment.previewUrl, attachment.meta?.small?.width],
|
|
|
|
].filter(([url]) => url).map(([url, size]) => `${url} ${size}w`).join(', '))
|
2022-11-29 08:01:51 +03:00
|
|
|
|
2022-11-21 16:38:10 +03:00
|
|
|
const rawAspectRatio = computed(() => {
|
2022-11-21 16:21:53 +03:00
|
|
|
if (attachment.meta?.original?.aspect)
|
|
|
|
return attachment.meta.original.aspect
|
|
|
|
if (attachment.meta?.small?.aspect)
|
|
|
|
return attachment.meta.small.aspect
|
|
|
|
return undefined
|
|
|
|
})
|
2022-11-21 16:38:10 +03:00
|
|
|
|
|
|
|
const aspectRatio = computed(() => {
|
2022-12-13 22:26:53 +03:00
|
|
|
if (fullSize)
|
|
|
|
return rawAspectRatio.value
|
2022-11-21 16:38:10 +03:00
|
|
|
if (rawAspectRatio.value)
|
2023-01-16 14:25:05 +03:00
|
|
|
return clamp(rawAspectRatio.value, 0.8, 6)
|
2022-11-21 16:38:10 +03:00
|
|
|
return undefined
|
|
|
|
})
|
2022-11-30 21:22:15 +03:00
|
|
|
|
|
|
|
const objectPosition = computed(() => {
|
2023-01-05 19:49:59 +03:00
|
|
|
const focusX = attachment.meta?.focus?.x || 0
|
|
|
|
const focusY = attachment.meta?.focus?.y || 0
|
|
|
|
const x = ((focusX / 2) + 0.5) * 100
|
|
|
|
const y = ((focusY / -2) + 0.5) * 100
|
|
|
|
|
|
|
|
return `${x}% ${y}%`
|
2022-11-30 21:22:15 +03:00
|
|
|
})
|
2022-12-01 06:15:31 +03:00
|
|
|
|
|
|
|
const typeExtsMap = {
|
|
|
|
video: ['mp4', 'webm', 'mov', 'avi', 'mkv', 'flv', 'wmv', 'mpg', 'mpeg'],
|
|
|
|
audio: ['mp3', 'wav', 'ogg', 'flac', 'aac', 'm4a', 'wma'],
|
|
|
|
image: ['jpg', 'jpeg', 'png', 'svg', 'webp', 'bmp'],
|
|
|
|
gifv: ['gifv', 'gif'],
|
|
|
|
}
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const type = computed(() => {
|
2022-12-01 06:15:31 +03:00
|
|
|
if (attachment.type && attachment.type !== 'unknown')
|
|
|
|
return attachment.type
|
|
|
|
// some server returns unknown type, we need to guess it based on file extension
|
|
|
|
for (const [type, exts] of Object.entries(typeExtsMap)) {
|
2024-02-21 18:20:08 +03:00
|
|
|
if (exts.some(ext => src.value?.toLowerCase().endsWith(`.${ext}`)))
|
2022-12-01 06:15:31 +03:00
|
|
|
return type
|
|
|
|
}
|
|
|
|
return 'unknown'
|
|
|
|
})
|
2022-12-29 01:47:28 +03:00
|
|
|
|
|
|
|
const video = ref<HTMLVideoElement | undefined>()
|
|
|
|
const prefersReducedMotion = usePreferredReducedMotion()
|
2024-02-21 18:20:08 +03:00
|
|
|
const isAudio = computed(() => attachment.type === 'audio')
|
|
|
|
const isVideo = computed(() => attachment.type === 'video')
|
2024-04-04 13:27:52 +03:00
|
|
|
const isGif = computed(() => attachment.type === 'gifv')
|
2022-12-29 01:47:28 +03:00
|
|
|
|
2023-01-19 21:33:50 +03:00
|
|
|
const enableAutoplay = usePreferences('enableAutoplay')
|
|
|
|
|
2022-12-29 01:47:28 +03:00
|
|
|
useIntersectionObserver(video, (entries) => {
|
2023-01-08 17:13:03 +03:00
|
|
|
const ready = video.value?.dataset.ready === 'true'
|
2023-01-19 21:33:50 +03:00
|
|
|
if (prefersReducedMotion.value === 'reduce' || !enableAutoplay.value) {
|
2023-01-08 17:13:03 +03:00
|
|
|
if (ready && !video.value?.paused)
|
|
|
|
video.value?.pause()
|
|
|
|
|
2022-12-29 01:47:28 +03:00
|
|
|
return
|
2023-01-08 17:13:03 +03:00
|
|
|
}
|
2022-12-29 01:47:28 +03:00
|
|
|
|
|
|
|
entries.forEach((entry) => {
|
2023-01-08 17:13:03 +03:00
|
|
|
if (entry.intersectionRatio <= 0.75) {
|
|
|
|
ready && !video.value?.paused && video.value?.pause()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
video.value?.play().then(() => {
|
|
|
|
video.value!.dataset.ready = 'true'
|
|
|
|
}).catch(noop)
|
|
|
|
}
|
2022-12-29 01:47:28 +03:00
|
|
|
})
|
2022-12-29 02:13:20 +03:00
|
|
|
}, { threshold: 0.75 })
|
2023-02-03 20:12:48 +03:00
|
|
|
|
|
|
|
const userSettings = useUserSettings()
|
2023-02-15 13:34:23 +03:00
|
|
|
|
|
|
|
const shouldLoadAttachment = ref(isPreview || !getPreferences(userSettings.value, 'enableDataSaving'))
|
|
|
|
|
|
|
|
function loadAttachment() {
|
|
|
|
shouldLoadAttachment.value = true
|
|
|
|
}
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const blurHashSrc = computed(() => {
|
2023-02-15 13:34:23 +03:00
|
|
|
if (!attachment.blurhash)
|
|
|
|
return ''
|
|
|
|
const pixels = decode(attachment.blurhash, 32, 32)
|
|
|
|
return getDataUrlFromArr(pixels, 32, 32)
|
|
|
|
})
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const videoThumbnail = ref(shouldLoadAttachment.value
|
2023-02-15 13:34:23 +03:00
|
|
|
? attachment.previewUrl
|
2024-02-21 18:20:08 +03:00
|
|
|
: blurHashSrc.value)
|
2023-02-15 13:34:23 +03:00
|
|
|
|
|
|
|
watch(shouldLoadAttachment, () => {
|
2024-02-21 18:20:08 +03:00
|
|
|
videoThumbnail.value = shouldLoadAttachment.value
|
2023-02-15 13:34:23 +03:00
|
|
|
? attachment.previewUrl
|
2024-02-21 18:20:08 +03:00
|
|
|
: blurHashSrc.value
|
2023-02-15 13:34:23 +03:00
|
|
|
})
|
2022-11-14 06:33:09 +03:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2023-01-09 22:33:21 +03:00
|
|
|
<div relative ma flex :gap="isAudio ? '2' : ''">
|
2022-12-13 14:18:21 +03:00
|
|
|
<template v-if="type === 'video'">
|
2023-02-15 13:34:23 +03:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
relative
|
|
|
|
@click="!shouldLoadAttachment ? loadAttachment() : null"
|
2022-12-13 14:18:21 +03:00
|
|
|
>
|
2023-02-15 13:34:23 +03:00
|
|
|
<video
|
|
|
|
ref="video"
|
|
|
|
preload="none"
|
|
|
|
:poster="videoThumbnail"
|
|
|
|
muted
|
|
|
|
loop
|
|
|
|
playsinline
|
|
|
|
:controls="shouldLoadAttachment"
|
|
|
|
rounded-lg
|
|
|
|
object-cover
|
|
|
|
fullscreen:object-contain
|
|
|
|
:width="attachment.meta?.original?.width"
|
|
|
|
:height="attachment.meta?.original?.height"
|
|
|
|
:style="{
|
|
|
|
aspectRatio,
|
|
|
|
objectPosition,
|
|
|
|
}"
|
|
|
|
:class="!shouldLoadAttachment ? 'brightness-60 hover:brightness-70 transition-filter' : ''"
|
|
|
|
>
|
|
|
|
<source :src="attachment.url || attachment.previewUrl" type="video/mp4">
|
|
|
|
</video>
|
|
|
|
<span
|
|
|
|
v-if="!shouldLoadAttachment"
|
|
|
|
class="status-attachment-load"
|
|
|
|
absolute
|
|
|
|
text-sm
|
|
|
|
text-white
|
|
|
|
flex flex-col justify-center items-center
|
|
|
|
gap-3 w-6 h-6
|
|
|
|
pointer-events-none
|
|
|
|
i-ri:video-download-line
|
|
|
|
/>
|
|
|
|
</button>
|
2022-12-13 14:18:21 +03:00
|
|
|
</template>
|
|
|
|
<template v-else-if="type === 'gifv'">
|
2023-02-15 13:34:23 +03:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
relative
|
2024-04-04 13:27:52 +03:00
|
|
|
@click="!shouldLoadAttachment ? loadAttachment() : openMediaPreview(attachments ? attachments : [attachment], attachments?.indexOf(attachment) || 0)"
|
2022-12-13 14:18:21 +03:00
|
|
|
>
|
2023-02-15 13:34:23 +03:00
|
|
|
<video
|
|
|
|
ref="video"
|
|
|
|
preload="none"
|
|
|
|
:poster="videoThumbnail"
|
|
|
|
muted
|
|
|
|
loop
|
|
|
|
playsinline
|
|
|
|
rounded-lg
|
|
|
|
object-cover
|
|
|
|
:width="attachment.meta?.original?.width"
|
|
|
|
:height="attachment.meta?.original?.height"
|
|
|
|
:style="{
|
|
|
|
aspectRatio,
|
|
|
|
objectPosition,
|
|
|
|
}"
|
|
|
|
>
|
|
|
|
<source :src="attachment.url || attachment.previewUrl" type="video/mp4">
|
|
|
|
</video>
|
|
|
|
<span
|
|
|
|
v-if="!shouldLoadAttachment"
|
|
|
|
class="status-attachment-load"
|
|
|
|
absolute
|
|
|
|
text-sm
|
|
|
|
text-white
|
|
|
|
flex flex-col justify-center items-center
|
|
|
|
gap-3 w-6 h-6
|
|
|
|
pointer-events-none
|
|
|
|
i-ri:video-download-line
|
|
|
|
/>
|
|
|
|
</button>
|
2022-12-13 14:18:21 +03:00
|
|
|
</template>
|
|
|
|
<template v-else-if="type === 'audio'">
|
2023-01-09 22:33:21 +03:00
|
|
|
<audio controls h-15>
|
2022-12-13 14:18:21 +03:00
|
|
|
<source :src="attachment.url || attachment.previewUrl" type="audio/mp3">
|
|
|
|
</audio>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
|
|
|
<button
|
2023-01-03 16:02:54 +03:00
|
|
|
type="button"
|
2022-12-13 14:18:21 +03:00
|
|
|
focus:outline-none
|
|
|
|
focus:ring="2 primary inset"
|
|
|
|
rounded-lg
|
2022-12-13 22:26:53 +03:00
|
|
|
h-full
|
|
|
|
w-full
|
2024-04-21 13:26:35 +03:00
|
|
|
:aria-label="$t('action.open_image_preview_dialog')"
|
2023-02-15 13:34:23 +03:00
|
|
|
relative
|
|
|
|
@click="!shouldLoadAttachment ? loadAttachment() : openMediaPreview(attachments ? attachments : [attachment], attachments?.indexOf(attachment) || 0)"
|
2022-12-13 14:18:21 +03:00
|
|
|
>
|
|
|
|
<CommonBlurhash
|
2023-04-26 23:46:00 +03:00
|
|
|
:blurhash="attachment.blurhash || ''"
|
2022-12-13 14:18:21 +03:00
|
|
|
class="status-attachment-image"
|
|
|
|
:src="src"
|
|
|
|
:srcset="srcset"
|
|
|
|
:width="attachment.meta?.original?.width"
|
|
|
|
:height="attachment.meta?.original?.height"
|
2022-12-31 01:52:32 +03:00
|
|
|
:alt="attachment.description ?? 'Image'"
|
2022-12-13 14:18:21 +03:00
|
|
|
:style="{
|
|
|
|
aspectRatio,
|
|
|
|
objectPosition,
|
|
|
|
}"
|
2023-02-15 13:34:23 +03:00
|
|
|
:should-load-image="shouldLoadAttachment"
|
2022-12-13 14:18:21 +03:00
|
|
|
rounded-lg
|
|
|
|
h-full
|
|
|
|
w-full
|
|
|
|
object-cover
|
2023-02-15 13:34:23 +03:00
|
|
|
:draggable="shouldLoadAttachment"
|
|
|
|
:class="!shouldLoadAttachment ? 'brightness-60 hover:brightness-70 transition-filter' : ''"
|
|
|
|
/>
|
|
|
|
<span
|
|
|
|
v-if="!shouldLoadAttachment"
|
|
|
|
class="status-attachment-load"
|
|
|
|
absolute
|
|
|
|
text-sm
|
|
|
|
text-white
|
|
|
|
flex flex-col justify-center items-center
|
|
|
|
gap-3 w-6 h-6
|
|
|
|
pointer-events-none
|
|
|
|
i-ri:file-download-line
|
2022-12-13 14:18:21 +03:00
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
</template>
|
2023-11-24 11:19:53 +03:00
|
|
|
<div
|
2024-04-04 13:27:52 +03:00
|
|
|
:class="isAudio ? [] : [
|
2023-11-24 11:19:53 +03:00
|
|
|
'absolute left-2',
|
|
|
|
isVideo ? 'top-2' : 'bottom-2',
|
|
|
|
]"
|
2024-04-04 13:27:52 +03:00
|
|
|
flex gap-col-2
|
2023-11-24 11:19:53 +03:00
|
|
|
>
|
2024-04-04 13:27:52 +03:00
|
|
|
<VDropdown v-if="attachment.description && !getPreferences(userSettings, 'hideAltIndicatorOnPosts')" :distance="6" placement="bottom-start">
|
2023-01-09 22:33:21 +03:00
|
|
|
<button
|
|
|
|
font-bold text-sm
|
|
|
|
:class="isAudio
|
|
|
|
? 'rounded-full h-15 w-15 btn-outline border-base text-secondary hover:bg-active hover:text-active'
|
|
|
|
: 'rounded-1 bg-black/65 text-white hover:bg-black px1.2 py0.2'"
|
|
|
|
>
|
2022-12-13 14:18:21 +03:00
|
|
|
<div hidden>
|
2023-02-03 00:42:21 +03:00
|
|
|
{{ $t('status.img_alt.read', [attachment.type]) }}
|
2022-12-13 14:18:21 +03:00
|
|
|
</div>
|
2023-02-03 00:42:21 +03:00
|
|
|
{{ $t('status.img_alt.ALT') }}
|
2022-12-13 14:18:21 +03:00
|
|
|
</button>
|
|
|
|
<template #popper>
|
|
|
|
<div p4 flex flex-col gap-2 max-w-130>
|
|
|
|
<div flex justify-between>
|
|
|
|
<h2 font-bold text-xl text-secondary>
|
2022-12-14 08:38:14 +03:00
|
|
|
{{ $t('status.img_alt.desc') }}
|
2022-12-13 14:18:21 +03:00
|
|
|
</h2>
|
|
|
|
<button v-close-popper text-sm btn-outline py0 px2 text-secondary border-base>
|
2022-12-14 08:38:14 +03:00
|
|
|
{{ $t('status.img_alt.dismiss') }}
|
2022-12-13 14:18:21 +03:00
|
|
|
</button>
|
|
|
|
</div>
|
2023-01-15 22:28:29 +03:00
|
|
|
<p whitespace-pre-wrap>
|
2022-12-13 14:18:21 +03:00
|
|
|
{{ attachment.description }}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
</VDropdown>
|
2024-04-04 13:27:52 +03:00
|
|
|
<div v-if="isGif && !getPreferences(userSettings, 'hideGifIndicatorOnPosts')">
|
|
|
|
<button
|
|
|
|
aria-hidden font-bold text-sm
|
|
|
|
rounded-1 bg-black:65 text-white px1.2 py0.2 pointer-events-none
|
|
|
|
>
|
|
|
|
{{ $t('status.gif') }}
|
|
|
|
</button>
|
|
|
|
</div>
|
2022-12-13 14:18:21 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-14 06:33:09 +03:00
|
|
|
</template>
|
2023-02-15 13:34:23 +03:00
|
|
|
|
|
|
|
<style lang="postcss">
|
|
|
|
.status-attachment-load {
|
|
|
|
left: 50%;
|
|
|
|
top: 50%;
|
|
|
|
translate: -50% -50%;
|
|
|
|
}
|
|
|
|
</style>
|