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

247 lines
6.3 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2022-05-03 16:51:59 +00:00
<span
:class="{ droppable }"
:style="{ backgroundImage: `url(${defaultCover})` }"
2022-05-03 16:51:59 +00:00
class="cover"
2022-05-04 21:01:35 +00:00
data-testid="album-artist-thumbnail"
2022-05-03 16:51:59 +00:00
>
<img v-koel-hide-broken-icon :alt="entity.name" :src="image" loading="lazy"/>
2022-04-15 14:24:30 +00:00
<a
2022-07-15 07:23:55 +00:00
class="control control-play"
2022-04-15 14:24:30 +00:00
href
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>
<span class="icon"/>
2022-04-15 14:24:30 +00:00
</a>
</span>
</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'
import { computed, ref, toRef, toRefs } from 'vue'
2022-06-10 10:47:46 +00:00
import { albumStore, artistStore, queueStore, songStore, userStore } from '@/stores'
2022-04-24 08:50:45 +00:00
import { playbackService } from '@/services'
import { defaultCover, fileReader, logger, requireInjection } from '@/utils'
import { useAuthorization } from '@/composables'
import { MessageToasterKey, RouterKey } from '@/symbols'
2022-04-15 14:24:30 +00:00
2022-07-16 23:11:24 +00:00
const VALID_IMAGE_TYPES = ['image/jpeg', 'image/gif', 'image/png', 'image/webp']
2022-04-15 14:24:30 +00:00
const toaster = requireInjection(MessageToasterKey)
const router = requireInjection(RouterKey)
2022-04-15 17:00:08 +00:00
const props = defineProps<{ entity: Album | Artist }>()
const { entity } = toRefs(props)
const droppable = ref(false)
const user = toRef(userStore.state, 'current')
2022-04-15 17:00:08 +00:00
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
)
const { isAdmin: allowsUpload } = useAuthorization()
2022-04-15 17:00:08 +00:00
2022-06-10 10:47:46 +00:00
const playOrQueue = async (event: KeyboardEvent) => {
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))
toaster.value.success('Songs added to queue.')
2022-04-30 10:36:35 +00:00
return
}
2022-10-21 20:06:43 +00:00
playbackService.queueAndPlay(songs)
router.go('queue')
2022-04-15 17:00:08 +00:00
}
const onDragEnter = () => (droppable.value = allowsUpload.value)
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
}
return VALID_IMAGE_TYPES.includes(event.dataTransfer.items[0].getAsFile()!.type)
}
const onDrop = async (event: DragEvent) => {
droppable.value = false
if (!allowsUpload.value) {
return
}
if (!validImageDropEvent(event)) {
return
}
try {
const fileData = await fileReader.readAsDataUrl(event.dataTransfer!.files[0])
if (forAlbum.value) {
// Replace the image right away to create an "instant" effect
2022-04-15 17:00:08 +00:00
(entity.value as Album).cover = fileData
await albumStore.uploadCover(entity.value as Album, fileData)
} else {
(entity.value as Artist).image = fileData
await artistStore.uploadImage(entity.value as Artist, fileData)
2022-04-15 14:24:30 +00:00
}
} catch (e) {
2022-07-20 08:00:02 +00:00
logger.error(e)
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>
<style lang="scss" scoped>
.cover {
position: relative;
width: 100%;
2022-07-22 14:25:30 +00:00
aspect-ratio: 1/1;
2022-04-15 14:24:30 +00:00
display: block;
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
border-radius: 5px;
overflow: hidden;
2022-04-15 14:24:30 +00:00
img {
width: 100%;
height: 100%;
object-fit: cover;
position: absolute;
top: 0;
left: 0;
pointer-events: none;
}
2022-04-15 14:24:30 +00:00
&::after {
content: "";
display: block;
padding-top: 100%;
}
.control {
height: 100%;
width: 100%;
position: absolute;
display: flex;
justify-content: center;
align-items: center;
&::before {
position: absolute;
content: "";
width: 100%;
height: 100%;
top: 0;
background: rgba(0, 0, 0, .3);
opacity: 0;
z-index: 1;
}
.icon {
2022-04-15 14:24:30 +00:00
background-color: var(--color-bg-primary);
2022-07-15 07:23:55 +00:00
color: var(--color-highlight);
2022-04-15 14:24:30 +00:00
opacity: 0;
2022-07-15 07:23:55 +00:00
border-radius: 50%;
width: 50%;
height: 50%;
display: flex;
justify-content: center;
align-items: center;
padding-left: 4%; // to balance the play icon
pointer-events: none;
2022-04-15 14:24:30 +00:00
@media (hover: none) {
opacity: 1;
}
&::after {
content: '';
width: 100%;
height: 100%;
background: var(--color-highlight);
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>');
-webkit-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;
-webkit-mask-repeat: no-repeat;
mask-position: center;
-webkit-mask-position: center;
mask-size: 40%;
-webkit-mask-size: 40%;
2022-04-15 14:24:30 +00:00
}
}
&:hover, &:focus {
&::before, .icon {
2022-04-15 14:24:30 +00:00
transition: .3s opacity;
opacity: 1;
}
}
&:active {
&::before {
background: rgba(0, 0, 0, .5);
}
.icon {
2022-04-15 14:24:30 +00:00
transform: scale(.9);
}
}
}
.drop-zone {
font-size: 4rem;
position: absolute;
width: 100%;
height: 100%;
place-content: center;
place-items: center;
background: rgba(0, 0, 0, .7);
display: none;
}
&.droppable {
border: 2px dotted rgba(255, 255, 255, 1);
filter: brightness(0.4);
.control {
opacity: 0;
}
}
}
2022-07-15 07:23:55 +00:00
.compact .icon {
2022-07-15 07:23:55 +00:00
font-size: .3rem; // to control the size of the icon
}
2022-04-15 14:24:30 +00:00
</style>