koel/resources/assets/js/components/ui/ArtistAlbumThumbnail.vue

187 lines
5.1 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2024-04-04 22:20:42 +00:00
<div
2022-05-03 16:51:59 +00:00
:class="{ droppable }"
:style="{ backgroundImage: `url(${defaultCover})` }"
2024-04-04 22:20:42 +00:00
class="cover relative w-full aspect-square bg-no-repeat bg-cover bg-center overflow-hidden rounded-md
after:block after:pt-[100%]"
2022-05-04 21:01:35 +00:00
data-testid="album-artist-thumbnail"
2022-05-03 16:51:59 +00:00
>
2024-04-04 22:20:42 +00:00
<img
v-koel-hide-broken-icon
:alt="entity.name"
:src="image"
class="w-full h-full object-cover absolute left-0 top-0 pointer-events-none
before:absolute before:w-full before:h-full before:opacity-0 before:z-[1] before-top-0"
loading="lazy"
>
2022-04-15 14:24:30 +00:00
<a
2024-04-04 22:20:42 +00:00
class="control control-play h-full w-full absolute flex justify-center items-center"
2022-04-15 14:24:30 +00:00
role="button"
2022-04-21 10:18:11 +00:00
@click.prevent="playOrQueue"
2022-04-15 14:24:30 +00:00
@dragenter.prevent="onDragEnter"
@dragleave.prevent="onDragLeave"
@drop.prevent="onDrop"
2022-04-15 14:24:30 +00:00
@dragover.prevent
>
2022-07-15 07:23:55 +00:00
<span class="hidden">{{ buttonLabel }}</span>
2024-04-04 22:20:42 +00:00
<span
class="icon opacity-0 w-1/2 h-1/2 flex justify-center items-center pointer-events-none pl-[4%] rounded-full
after:w-full after:h-full"
/>
2022-04-15 14:24:30 +00:00
</a>
2024-04-04 22:20:42 +00:00
</div>
2022-04-15 14:24:30 +00:00
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2022-04-15 14:24:30 +00:00
import { orderBy } from 'lodash'
2024-04-04 22:20:42 +00:00
import { computed, ref, toRefs } from 'vue'
import { albumStore, artistStore, queueStore, songStore } from '@/stores'
2022-04-24 08:50:45 +00:00
import { playbackService } from '@/services'
import { defaultCover } from '@/utils'
2024-04-23 21:01:27 +00:00
import { useErrorHandler, useFileReader, useMessageToaster, usePolicies, useRouter } from '@/composables'
2024-02-24 15:37:01 +00:00
import { acceptedImageTypes } from '@/config'
2022-04-15 14:24:30 +00:00
const { toastSuccess } = useMessageToaster()
2022-11-18 18:44:20 +00:00
const { go } = useRouter()
const { currentUserCan } = usePolicies()
2022-04-15 17:00:08 +00:00
const props = defineProps<{ entity: Album | Artist }>()
const { entity } = toRefs(props)
const droppable = ref(false)
2022-06-10 10:47:46 +00:00
const forAlbum = computed(() => entity.value.type === 'albums')
2022-04-15 17:00:08 +00:00
const sortFields = computed(() => forAlbum.value ? ['disc', 'track'] : ['album_id', 'disc', 'track'])
const image = computed(() => {
return forAlbum.value
2022-07-21 09:17:08 +00:00
? (entity.value as Album).cover || defaultCover
: (entity.value as Artist).image || defaultCover
2022-04-21 10:18:11 +00:00
})
2022-04-19 20:53:36 +00:00
2022-04-15 17:00:08 +00:00
const buttonLabel = computed(() => forAlbum.value
? `Play all songs in the album ${entity.value.name}`
2022-07-11 17:35:58 +00:00
: `Play all songs by ${entity.value.name}`
2022-04-15 17:00:08 +00:00
)
2024-04-04 22:20:42 +00:00
const allowsUpload = currentUserCan.changeAlbumOrArtistThumbnails()
2022-04-15 17:00:08 +00:00
2022-12-02 16:17:37 +00:00
const playOrQueue = async (event: MouseEvent) => {
2022-06-10 10:47:46 +00:00
const songs = forAlbum.value
? await songStore.fetchForAlbum(entity.value as Album)
: await songStore.fetchForArtist(entity.value as Artist)
if (event.altKey) {
2022-06-10 10:47:46 +00:00
queueStore.queue(orderBy(songs, sortFields.value))
toastSuccess('Songs added to queue.')
2022-04-30 10:36:35 +00:00
return
}
2022-10-21 20:06:43 +00:00
playbackService.queueAndPlay(songs)
2022-11-18 18:44:20 +00:00
go('queue')
2022-04-15 17:00:08 +00:00
}
2024-04-04 22:20:42 +00:00
const onDragEnter = () => (droppable.value = allowsUpload)
2022-04-15 17:00:08 +00:00
const onDragLeave = () => (droppable.value = false)
const validImageDropEvent = (event: DragEvent) => {
if (!event.dataTransfer || !event.dataTransfer.items) {
return false
}
if (event.dataTransfer.items.length !== 1) {
return false
}
if (event.dataTransfer.items[0].kind !== 'file') {
return false
}
2024-02-24 15:37:01 +00:00
return acceptedImageTypes.includes(event.dataTransfer.items[0].getAsFile()!.type)
2022-04-15 17:00:08 +00:00
}
const onDrop = async (event: DragEvent) => {
droppable.value = false
2024-04-04 22:20:42 +00:00
if (!allowsUpload) {
2022-04-15 17:00:08 +00:00
return
}
if (!validImageDropEvent(event)) {
return
}
const backupImage = forAlbum.value ? (entity.value as Album).cover : (entity.value as Artist).image
2022-04-15 17:00:08 +00:00
try {
2024-03-19 22:48:12 +00:00
useFileReader().readAsDataUrl(event.dataTransfer!.files[0], async url => {
if (forAlbum.value) {
// Replace the image right away to create an "instant" effect
(entity.value as Album).cover = url
await albumStore.uploadCover(entity.value as Album, url)
} else {
(entity.value as Artist).image = url as string
await artistStore.uploadImage(entity.value as Artist, url)
}
})
} catch (error: unknown) {
// restore the backup image
if (forAlbum.value) {
(entity.value as Album).cover = backupImage!
} else {
(entity.value as Artist).image = backupImage!
}
useErrorHandler().handleHttpError(error)
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
}
2022-04-15 14:24:30 +00:00
</script>
2024-04-04 20:13:35 +00:00
<style lang="postcss" scoped>
2024-04-04 22:20:42 +00:00
.icon {
@apply bg-k-bg-primary text-k-highlight no-hover:opacity-100;
2022-04-15 14:24:30 +00:00
&::after {
2024-04-04 22:20:42 +00:00
@apply bg-k-highlight;
2022-04-15 14:24:30 +00:00
2024-04-04 22:20:42 +00:00
mask-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path fill="currentColor" d="M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z"/></svg>');
mask-repeat: no-repeat;
mask-position: center;
mask-size: 40%;
}
}
2024-04-23 21:01:27 +00:00
2024-04-04 22:20:42 +00:00
article {
2022-04-15 14:24:30 +00:00
.control {
&:hover, &:focus {
&::before, .icon {
2024-04-04 22:20:42 +00:00
@apply transition-opacity duration-300 opacity-100;
2022-04-15 14:24:30 +00:00
}
}
&:active {
&::before {
2024-04-04 22:20:42 +00:00
@apply bg-black/50;
2022-04-15 14:24:30 +00:00
}
.icon {
2024-04-04 22:20:42 +00:00
@apply scale-90;
2022-04-15 14:24:30 +00:00
}
}
}
&.droppable {
2024-04-04 22:20:42 +00:00
@apply border-2 border-dotted border-white brightness-50;
2022-04-15 14:24:30 +00:00
.control {
2024-04-04 22:20:42 +00:00
@apply opacity-0;
2022-04-15 14:24:30 +00:00
}
}
}
2022-07-15 07:23:55 +00:00
.compact .icon {
2024-04-23 21:01:27 +00:00
@apply text-[.3rem];
/* to control the size of the icon */
2022-07-15 07:23:55 +00:00
}
2022-04-15 14:24:30 +00:00
</style>