funkwhale/front/src/components/audio/Player.vue

390 wiersze
14 KiB
Vue
Czysty Zwykły widok Historia

<script setup lang="ts">
2022-10-18 21:48:47 +00:00
import {
LoopingMode,
initializeFirstTrack,
isPlaying,
mute,
volume,
toggleLooping,
looping,
seekBy,
seekTo,
currentTime,
duration,
progress,
bufferProgress,
loading as isLoadingAudio
} from '~/composables/audio/player'
2022-10-20 08:51:41 +00:00
import {
hasPrevious,
playPrevious,
hasNext,
playNext,
2022-10-23 07:41:38 +00:00
queue,
2022-10-20 08:51:41 +00:00
currentIndex,
currentTrack,
shuffle
} from '~/composables/audio/queue'
import { useMouse, useWindowSize } from '@vueuse/core'
import { useGettext } from 'vue3-gettext'
import { computed, ref } from 'vue'
import { useStore } from '~/store'
import onKeyboardShortcut from '~/composables/onKeyboardShortcut'
2022-10-18 21:48:47 +00:00
import time from '~/utils/time'
2022-10-23 07:41:38 +00:00
// import TrackFavoriteIcon from '~/components/favorites/TrackFavoriteIcon.vue'
// import TrackPlaylistIcon from '~/components/playlists/TrackPlaylistIcon.vue'
import VolumeControl from './VolumeControl.vue'
const store = useStore()
const { $pgettext } = useGettext()
const toggleMobilePlayer = () => {
store.commit('ui/queueFocused', ['queue', 'player'].includes(store.state.ui.queueFocused as string) ? null : 'player')
}
// Key binds
onKeyboardShortcut('e', toggleMobilePlayer)
2022-10-18 21:48:47 +00:00
onKeyboardShortcut('p', () => { isPlaying.value = !isPlaying.value })
onKeyboardShortcut('s', shuffle)
onKeyboardShortcut('q', () => store.dispatch('queue/clean'))
2022-10-18 21:48:47 +00:00
onKeyboardShortcut('m', mute)
onKeyboardShortcut('l', toggleLooping)
onKeyboardShortcut('f', () => store.dispatch('favorites/toggle', currentTrack.value?.id))
onKeyboardShortcut('escape', () => store.commit('ui/queueFocused', null))
2022-10-18 21:48:47 +00:00
onKeyboardShortcut(['shift', 'up'], () => (volume.value += 0.1), true)
onKeyboardShortcut(['shift', 'down'], () => (volume.value -= 0.1), true)
2022-10-18 21:48:47 +00:00
onKeyboardShortcut('right', () => seekBy(5), true)
onKeyboardShortcut(['shift', 'right'], () => seekBy(30), true)
onKeyboardShortcut('left', () => seekBy(-5), true)
onKeyboardShortcut(['shift', 'left'], () => seekBy(-30), true)
2022-10-20 08:51:41 +00:00
onKeyboardShortcut(['ctrl', 'shift', 'left'], playPrevious, true)
onKeyboardShortcut(['ctrl', 'shift', 'right'], playNext, true)
const labels = computed(() => ({
audioPlayer: $pgettext('Sidebar/Player/Hidden text', 'Media player'),
previous: $pgettext('Sidebar/Player/Icon.Tooltip', 'Previous track'),
play: $pgettext('Sidebar/Player/Icon.Tooltip/Verb', 'Play'),
pause: $pgettext('Sidebar/Player/Icon.Tooltip/Verb', 'Pause'),
next: $pgettext('Sidebar/Player/Icon.Tooltip', 'Next track'),
unmute: $pgettext('Sidebar/Player/Icon.Tooltip/Verb', 'Unmute'),
mute: $pgettext('Sidebar/Player/Icon.Tooltip/Verb', 'Mute'),
expandQueue: $pgettext('Sidebar/Player/Icon.Tooltip/Verb', 'Expand queue'),
shuffle: $pgettext('Sidebar/Player/Icon.Tooltip/Verb', 'Shuffle your queue'),
clear: $pgettext('Sidebar/Player/Icon.Tooltip/Verb', 'Clear your queue'),
addArtistContentFilter: $pgettext('Sidebar/Player/Icon.Tooltip/Verb', 'Hide content from this artist…')
}))
const switchTab = () => {
store.commit('ui/queueFocused', store.state.ui.queueFocused === 'player' ? 'queue' : 'player')
}
2022-07-21 15:05:24 +00:00
const progressBar = ref()
const touchProgress = (event: MouseEvent) => {
const time = ((event.clientX - ((event.target as Element).closest('.progress')?.getBoundingClientRect().left ?? 0)) / progressBar.value.offsetWidth) * duration.value
2022-10-18 21:48:47 +00:00
seekTo(time)
2022-07-21 15:05:24 +00:00
}
2022-07-25 02:05:33 +00:00
2022-10-18 21:48:47 +00:00
const { x } = useMouse({ type: 'client' })
const { width: screenWidth } = useWindowSize({ includeScrollbar: false })
initializeFirstTrack()
2022-10-18 21:48:47 +00:00
const loopingTitle = computed(() => {
const mode = looping.value
return mode === LoopingMode.None
? $pgettext('Sidebar/Player/Icon.Tooltip', 'Looping disabled. Click to switch to single-track looping.')
: mode === LoopingMode.LoopTrack
? $pgettext('Sidebar/Player/Icon.Tooltip', 'Looping on a single track. Click to switch to whole queue looping.')
: $pgettext('Sidebar/Player/Icon.Tooltip', 'Looping on whole queue. Click to disable looping.')
})
const currentTimeFormatted = computed(() => time.parse(Math.round(currentTime.value)))
</script>
<template>
2021-12-06 10:35:20 +00:00
<section
v-if="currentTrack"
role="complementary"
class="player-wrapper ui bottom-player component-player"
aria-labelledby="player-label"
>
<h1
id="player-label"
class="visually-hidden"
>
<translate translate-context="*/*/*">
Audio player and controls
</translate>
</h1>
2021-12-06 10:35:20 +00:00
<div
class="ui inverted segment fixed-controls"
@click.prevent.stop="toggleMobilePlayer"
>
<div
2022-07-21 15:05:24 +00:00
ref="progressBar"
2021-12-06 10:35:20 +00:00
:class="['ui', 'top attached', 'small', 'inverted', {'indicating': isLoadingAudio}, 'progress']"
2022-07-21 15:05:24 +00:00
@click.prevent.stop="touchProgress"
2021-12-06 10:35:20 +00:00
>
<div
class="buffer bar"
:style="{ 'transform': `translateX(${bufferProgress - 100}%)` }"
/>
2021-12-06 10:35:20 +00:00
<div
class="position bar"
2022-10-18 21:48:47 +00:00
:style="{ 'transform': `translateX(${progress - 100}%)` }"
2021-12-06 10:35:20 +00:00
/>
2022-07-25 02:05:33 +00:00
<div
class="seek bar"
:style="{ 'transform': `translateX(${x / screenWidth * 100 - 100}%)` }"
2022-07-25 02:05:33 +00:00
/>
</div>
<div class="controls-row">
<div class="controls track-controls queue-not-focused desktop-and-up">
2021-12-06 10:35:20 +00:00
<div
class="ui tiny image"
@click.stop.prevent="$router.push({name: 'library.tracks.detail', params: {id: currentTrack.id }})"
>
<img
ref="cover"
alt=""
2022-10-23 07:41:38 +00:00
:src="$store.getters['instance/absoluteUrl'](currentTrack.coverUrl)"
2021-12-06 10:35:20 +00:00
>
</div>
2021-12-06 10:35:20 +00:00
<div
class="middle aligned content ellipsis"
@click.stop.prevent=""
>
<strong>
2021-12-06 10:35:20 +00:00
<router-link
class="small header discrete link track"
:to="{name: 'library.tracks.detail', params: {id: currentTrack.id }}"
@click.stop.prevent=""
>
{{ currentTrack.title }}
</router-link>
</strong>
<div class="meta">
2021-12-06 10:35:20 +00:00
<router-link
class="discrete link"
2022-10-23 07:41:38 +00:00
:to="{name: 'library.artists.detail', params: {id: currentTrack.artistId }}"
2021-12-06 10:35:20 +00:00
@click.stop.prevent=""
>
2022-10-23 07:41:38 +00:00
{{ currentTrack.artistName }}
2021-12-06 10:35:20 +00:00
</router-link>
2022-10-23 07:41:38 +00:00
<template v-if="currentTrack.albumId !== -1">
2021-12-06 10:35:20 +00:00
/
<router-link
class="discrete link"
2022-10-23 07:41:38 +00:00
:to="{name: 'library.albums.detail', params: {id: currentTrack.albumId }}"
2021-12-06 10:35:20 +00:00
@click.stop.prevent=""
>
2022-10-23 07:41:38 +00:00
{{ currentTrack.albumTitle }}
2021-12-06 10:35:20 +00:00
</router-link>
2021-01-03 16:26:09 +00:00
</template>
</div>
</div>
</div>
<div class="controls track-controls queue-not-focused desktop-and-below">
<div class="ui tiny image">
2021-12-06 10:35:20 +00:00
<img
ref="cover"
alt=""
2022-10-23 07:41:38 +00:00
:src="$store.getters['instance/absoluteUrl'](currentTrack.coverUrl)"
2021-12-06 10:35:20 +00:00
>
</div>
<div class="middle aligned content ellipsis">
<strong>
{{ currentTrack.title }}
</strong>
<div class="meta">
2022-10-23 07:41:38 +00:00
{{ currentTrack.artistName }}
<template v-if="currentTrack.albumId !== -1">
/ {{ currentTrack.albumTitle }}
2021-12-06 10:35:20 +00:00
</template>
</div>
</div>
</div>
2021-12-06 10:35:20 +00:00
<div
v-if="$store.state.auth.authenticated"
class="controls desktop-and-up fluid align-right"
>
2022-10-23 07:41:38 +00:00
<!-- TODO (wvffle): Uncomment -->
<!-- <track-favorite-icon
class="control white"
2021-12-06 10:35:20 +00:00
:track="currentTrack"
/>
<track-playlist-icon
class="control white"
2021-12-06 10:35:20 +00:00
:track="currentTrack"
/>
<button
:class="['ui', 'really', 'basic', 'circular', 'icon', 'button', 'control']"
:aria-label="labels.addArtistContentFilter"
2021-12-06 10:35:20 +00:00
:title="labels.addArtistContentFilter"
@click="$store.dispatch('moderation/hide', {type: 'artist', target: currentTrack.artist})"
>
<i :class="['eye slash outline', 'basic', 'icon']" />
2022-10-23 07:41:38 +00:00
</button> -->
</div>
<div class="player-controls controls queue-not-focused">
<button
:title="labels.previous"
:aria-label="labels.previous"
:disabled="!hasPrevious"
class="circular button control tablet-and-up"
2022-10-20 08:51:41 +00:00
@click.prevent.stop="playPrevious"
2021-12-06 10:35:20 +00:00
>
<i :class="['ui', 'large', {'disabled': !hasPrevious}, 'backward step', 'icon']" />
</button>
<button
2022-10-18 21:48:47 +00:00
v-if="!isPlaying"
:title="labels.play"
:aria-label="labels.play"
2021-12-06 10:35:20 +00:00
class="circular button control"
2022-10-18 21:48:47 +00:00
@click.prevent.stop="isPlaying = true"
2021-12-06 10:35:20 +00:00
>
<i :class="['ui', 'big', 'play', {'disabled': !currentTrack}, 'icon']" />
</button>
<button
v-else
:title="labels.pause"
:aria-label="labels.pause"
2021-12-06 10:35:20 +00:00
class="circular button control"
2022-10-18 21:48:47 +00:00
@click.prevent.stop="isPlaying = false"
2021-12-06 10:35:20 +00:00
>
<i :class="['ui', 'big', 'pause', {'disabled': !currentTrack}, 'icon']" />
</button>
<button
:title="labels.next"
:aria-label="labels.next"
:disabled="!hasNext"
class="circular button control"
2022-10-20 08:51:41 +00:00
@click.prevent.stop="playNext"
2021-12-06 10:35:20 +00:00
>
<i :class="['ui', 'large', {'disabled': !hasNext}, 'forward step', 'icon']" />
</button>
</div>
<div class="controls progress-controls queue-not-focused tablet-and-up small align-left">
<div class="timer">
<template v-if="!isLoadingAudio">
2021-12-06 10:35:20 +00:00
<span
class="start"
2022-10-18 21:48:47 +00:00
@click.stop.prevent="seekTo(0)"
2022-07-03 21:36:27 +00:00
>
{{ currentTimeFormatted }}
</span>
2022-07-21 01:21:36 +00:00
|
2022-10-18 21:48:47 +00:00
<span class="total">{{ time.parse(Math.round(duration)) }}</span>
</template>
</div>
</div>
<div class="controls queue-controls when-queue-focused align-right">
<div class="group">
<volume-control class="expandable" />
<button
2021-12-06 10:35:20 +00:00
class="circular control button"
2022-10-18 21:48:47 +00:00
:class="{ looping: looping !== LoopingMode.None }"
:title="loopingTitle"
:aria-label="loopingTitle"
:disabled="!currentTrack"
2022-10-18 21:48:47 +00:00
@click.prevent.stop="toggleLooping"
2021-12-06 10:35:20 +00:00
>
2022-10-18 21:48:47 +00:00
<i class="repeat icon">
<span
v-if="looping !== LoopingMode.None"
class="ui circular tiny vibrant label"
>
<template v-if="looping === LoopingMode.LoopTrack">1</template>
<template v-else-if="looping === LoopingMode.LoopQueue">&infin;</template>
</span>
</i>
</button>
2022-10-18 21:48:47 +00:00
<button
class="circular control button"
2022-10-23 07:41:38 +00:00
:disabled="queue.length === 0"
:title="labels.shuffle"
:aria-label="labels.shuffle"
@click.prevent.stop="shuffle()"
2021-12-06 10:35:20 +00:00
>
2022-10-23 07:41:38 +00:00
<i :class="['ui', 'random', {'disabled': queue.length === 0}, 'icon']" />
</button>
</div>
<div class="group">
<div class="fake-dropdown">
2021-12-06 10:35:20 +00:00
<button
class="position circular control button desktop-and-up"
aria-expanded="true"
@click.stop="toggleMobilePlayer"
>
<i class="stream icon" />
<translate
translate-context="Sidebar/Queue/Text"
2022-10-23 07:41:38 +00:00
:translate-params="{index: currentIndex + 1, length: queue.length}"
2021-12-06 10:35:20 +00:00
>
%{ index } of %{ length }
</translate>
</button>
2021-12-06 10:35:20 +00:00
<button
class="position circular control button desktop-and-below"
2021-12-06 10:35:20 +00:00
@click.stop="switchTab"
>
<i class="stream icon" />
<translate
translate-context="Sidebar/Queue/Text"
2022-10-23 07:41:38 +00:00
:translate-params="{index: currentIndex + 1, length: queue.length}"
2021-12-06 10:35:20 +00:00
>
%{ index } of %{ length }
</translate>
</button>
<button
v-if="$store.state.ui.queueFocused"
2021-12-06 10:35:20 +00:00
class="circular control button close-control desktop-and-up"
@click.stop="toggleMobilePlayer"
>
<i class="large down angle icon" />
</button>
<button
v-else
2021-12-06 10:35:20 +00:00
class="circular control button desktop-and-up"
@click.stop="toggleMobilePlayer"
>
<i class="large up angle icon" />
</button>
<button
v-if="$store.state.ui.queueFocused === 'player'"
class="circular control button close-control desktop-and-below"
2021-12-06 10:35:20 +00:00
@click.stop="switchTab"
>
<i class="large up angle icon" />
</button>
<button
v-if="$store.state.ui.queueFocused === 'queue'"
class="circular control button desktop-and-below"
2021-12-06 10:35:20 +00:00
@click.stop="switchTab"
>
<i class="large down angle icon" />
</button>
</div>
<button
class="circular control button close-control desktop-and-below"
2021-12-06 10:35:20 +00:00
@click.stop="$store.commit('ui/queueFocused', null)"
>
<i class="x icon" />
</button>
</div>
</div>
</div>
</div>
</section>
</template>