koel/resources/assets/js/components/song/SongCard.vue

197 lines
4 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<article
2022-06-10 10:47:46 +00:00
:class="{ playing: song.playback_state === 'Playing' || song.playback_state === 'Paused' }"
data-testid="song-card"
2022-04-23 22:15:08 +00:00
draggable="true"
2022-04-15 14:24:30 +00:00
tabindex="0"
2022-04-23 22:15:08 +00:00
@dragstart="dragStart"
2022-04-30 11:55:54 +00:00
@contextmenu.prevent="requestContextMenu"
2022-04-23 22:15:08 +00:00
@dblclick.prevent="play"
2022-04-15 14:24:30 +00:00
>
2022-07-30 15:08:20 +00:00
<aside :style="{ backgroundImage: `url(${song.album_cover ?? ''}), url(${defaultCover})` }" class="cover">
<a class="control" @click.prevent="changeSongState" data-testid="play-control">
2022-07-15 07:23:55 +00:00
<icon :icon="song.playback_state === 'Playing' ? faPause : faPlay" class="text-highlight"/>
2022-04-15 14:24:30 +00:00
</a>
2022-07-30 15:08:20 +00:00
</aside>
<main>
<div class="details">
<h3>{{ song.title }}</h3>
<p class="by text-secondary">
2022-06-10 10:47:46 +00:00
<a :href="`#!/artist/${song.artist_id}`">{{ song.artist_name }}</a>
2022-07-30 15:08:20 +00:00
- {{ pluralize(song.play_count, 'play') }}
</p>
</div>
<LikeButton :song="song"/>
</main>
2022-04-15 14:24:30 +00:00
</article>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2022-07-15 07:23:55 +00:00
import { faPause, faPlay } from '@fortawesome/free-solid-svg-icons'
2022-07-30 15:08:20 +00:00
import { toRefs } from 'vue'
import { defaultCover, eventBus, pluralize, startDragging } from '@/utils'
2022-04-15 14:24:30 +00:00
import { queueStore } from '@/stores'
2022-04-24 08:50:45 +00:00
import { playbackService } from '@/services'
2022-04-15 14:24:30 +00:00
import LikeButton from '@/components/song/SongLikeButton.vue'
2022-04-15 14:24:30 +00:00
2022-07-30 15:08:20 +00:00
const props = defineProps<{ song: Song }>()
const { song } = toRefs(props)
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const requestContextMenu = (event: MouseEvent) => eventBus.emit('SONG_CONTEXT_MENU_REQUESTED', event, song.value)
const dragStart = (event: DragEvent) => startDragging(event, song.value, 'Song')
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const play = () => {
queueStore.queueIfNotQueued(song.value)
2022-04-24 08:50:45 +00:00
playbackService.play(song.value)
2022-04-15 17:00:08 +00:00
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const changeSongState = () => {
2022-06-10 10:47:46 +00:00
if (song.value.playback_state === 'Stopped') {
2022-04-15 17:00:08 +00:00
play()
2022-06-10 10:47:46 +00:00
} else if (song.value.playback_state === 'Paused') {
2022-04-24 08:50:45 +00:00
playbackService.resume()
2022-04-15 17:00:08 +00:00
} else {
2022-04-24 08:50:45 +00:00
playbackService.pause()
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>
article {
display: flex;
gap: 12px;
padding: 8px 12px 8px 8px;
background: var(--color-bg-secondary);
border: 1px solid var(--color-bg-secondary);
border-radius: 5px;
align-items: center;
2022-04-15 14:24:30 +00:00
&:focus, &:focus-within {
box-shadow: 0 0 1px 1px var(--color-accent);
}
2022-04-15 14:24:30 +00:00
&.playing {
color: var(--color-accent);
2022-04-15 14:24:30 +00:00
}
2022-07-30 15:08:20 +00:00
button {
opacity: 0;
}
&:hover {
2022-07-30 15:08:20 +00:00
button {
opacity: 1;
}
}
@media (hover: none) {
button {
opacity: 1;
}
}
2022-04-15 14:24:30 +00:00
&:hover .cover, &:focus .cover {
.control {
2022-07-15 07:23:55 +00:00
display: flex;
2022-04-15 14:24:30 +00:00
}
&::before {
opacity: .7;
}
}
.cover {
2022-07-22 14:25:30 +00:00
width: 48px;
2022-08-03 21:01:47 +00:00
min-width: 48px;
2022-07-22 14:25:30 +00:00
aspect-ratio: 1/1;
2022-04-15 14:24:30 +00:00
background-size: cover;
position: relative;
border-radius: 4px;
overflow: hidden;
2022-07-15 07:23:55 +00:00
display: flex;
align-items: center;
justify-content: center;
2022-04-15 14:24:30 +00:00
&::before {
content: " ";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
background: #000;
opacity: 0;
@media (hover: none) {
opacity: .7;
}
}
.control {
border-radius: 50%;
width: 28px;
height: 28px;
background: rgba(0, 0, 0, .5);
2022-04-15 14:24:30 +00:00
font-size: 1rem;
z-index: 1;
display: none;
color: var(--color-text-primary);
transition: .3s;
2022-07-15 07:23:55 +00:00
justify-content: center;
align-items: center;
2022-04-15 14:24:30 +00:00
@media (hover: none) {
2022-07-15 07:23:55 +00:00
display: flex;
2022-04-15 14:24:30 +00:00
}
}
}
2022-07-30 15:08:20 +00:00
main {
2022-08-03 21:01:47 +00:00
flex: 1 1 auto;
min-width: 0;
2022-04-15 14:24:30 +00:00
display: flex;
2022-07-30 15:08:20 +00:00
align-items: flex-start;
gap: 8px;
2022-04-15 14:24:30 +00:00
.play-count {
background: rgba(255, 255, 255, 0.08);
position: absolute;
height: 100%;
top: 0;
left: 0;
pointer-events: none;
}
.by {
font-size: .9rem;
opacity: .8;
a {
color: var(--color-text-primary);
&:hover {
color: var(--color-accent);
2022-04-15 14:24:30 +00:00
}
}
}
.details {
flex: 1;
display: flex;
flex-direction: column;
gap: 4px;
2022-08-03 21:01:47 +00:00
overflow: hidden;
2022-04-15 14:24:30 +00:00
}
}
2022-08-03 21:01:47 +00:00
h3 {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 100%;
}
2022-04-15 14:24:30 +00:00
}
</style>