koel/resources/assets/js/components/ui/album-artist/AlbumOrArtistThumbnail.vue

158 lines
4.5 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2024-05-19 05:49:42 +00:00
<button
2022-05-03 16:51:59 +00:00
:class="{ droppable }"
:style="{ backgroundImage: `url(${defaultCover})` }"
2024-05-19 05:49:42 +00:00
class="thumbnail relative w-full aspect-square bg-no-repeat bg-cover bg-center overflow-hidden rounded-md active:scale-95"
2022-05-04 21:01:35 +00:00
data-testid="album-artist-thumbnail"
2024-05-19 05:49:42 +00:00
@click.prevent="playOrQueue"
@dragenter.prevent="onDragEnter"
@dragleave.prevent="onDragLeave"
@drop.prevent="onDrop"
@dragover.prevent
2022-05-03 16:51:59 +00:00
>
2024-04-04 22:20:42 +00:00
<img
v-koel-hide-broken-icon
2024-05-19 05:49:42 +00:00
alt="Thumbnail"
2024-04-04 22:20:42 +00:00
:src="image"
2024-05-19 05:49:42 +00:00
class="w-full aspect-square object-cover"
2024-04-04 22:20:42 +00:00
loading="lazy"
>
2024-05-19 05:49:42 +00:00
<span class="hidden">{{ buttonLabel }}</span>
<span class="absolute top-0 left-0 w-full h-full group-hover:bg-black/40 no-hover:bg-black/40 z-10" />
2024-05-19 05:49:42 +00:00
<span
class="play-icon absolute flex opacity-0 no-hover:opacity-100 items-center justify-center w-[32px] aspect-square rounded-full top-1/2
2024-05-19 05:49:42 +00:00
left-1/2 -translate-x-1/2 -translate-y-1/2 bg-k-highlight group-hover:opacity-100 duration-500 transition z-20"
2022-04-15 14:24:30 +00:00
>
2024-05-19 05:49:42 +00:00
<Icon :icon="faPlay" class="ml-1 text-white" size="lg" />
</span>
</button>
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'
2024-05-19 05:49:42 +00:00
import { faPlay } from '@fortawesome/free-solid-svg-icons'
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)
2024-05-19 05:49:42 +00:00
const onDragLeave = (e: DragEvent) => {
if ((e.currentTarget as Node)?.contains?.(e.relatedTarget as Node)) {
return
}
droppable.value = false
}
2022-04-15 17:00:08 +00:00
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-05-19 05:49:42 +00:00
.droppable {
@apply border-2 border-dotted border-white brightness-50;
2024-05-19 05:49:42 +00:00
* {
pointer-events: none;
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>