diff --git a/front/src/components/audio/Player.vue b/front/src/components/audio/Player.vue index c2d9a324f..a9ea489e7 100644 --- a/front/src/components/audio/Player.vue +++ b/front/src/components/audio/Player.vue @@ -24,7 +24,6 @@ import { tracks, currentIndex, currentTrack, - isShuffling, shuffle } from '~/composables/audio/queue' @@ -338,14 +337,7 @@ const currentTimeFormatted = computed(() => time.parse(Math.round(currentTime.va :aria-label="labels.shuffle" @click.prevent.stop="shuffle()" > -
- +
diff --git a/front/src/composables/audio/queue.ts b/front/src/composables/audio/queue.ts index 2b76841e3..74dae77f5 100644 --- a/front/src/composables/audio/queue.ts +++ b/front/src/composables/audio/queue.ts @@ -3,11 +3,37 @@ import type { Track } from '~/types' import { isPlaying, looping, LoopingMode } from '~/composables/audio/player' import { currentSound } from '~/composables/audio/tracks' import { toReactive, useStorage } from '@vueuse/core' +import { shuffle as shuffleArray } from 'lodash-es' import { useClamp } from '@vueuse/math' -import { computed, ref } from 'vue' +import { computed } from 'vue' + +// import useWebWorker from '~/composables/useWebWorker' + +// const { post, onMessageReceived } = useWebWorker('queue') // Queue export const tracks = toReactive(useStorage('queue:tracks', [] as Track[])) +export const queue = computed(() => { + if (isShuffled.value) { + const tracksById = tracks.reduce((acc, track) => { + acc[track.id] = track + return acc + }, {} as Record) + + return shuffledIds.value.map(id => tracksById[id]) + } + + return tracks +}) + +export const enqueue = (...newTracks: Track[]) => { + tracks.push(...newTracks) + + // Shuffle new tracks + if (isShuffled.value) { + shuffledIds.value.push(...shuffleIds(newTracks)) + } +} // Current Index export const currentIndex = useClamp(useStorage('queue:index', 0), 0, () => tracks.length) @@ -54,6 +80,14 @@ export const playNext = async () => { } // Shuffle -export const isShuffling = ref(false) -// TODO: Shuffle -export const shuffle = () => undefined +const shuffleIds = (tracks: Track[]) => shuffleArray(tracks.map(track => track.id)) +const shuffledIds = useStorage('queue:shuffled-ids', [] as number[]) +export const isShuffled = computed(() => shuffledIds.value.length !== 0) +export const shuffle = () => { + if (isShuffled.value) { + shuffledIds.value.length = 0 + return + } + + shuffledIds.value = shuffleIds(tracks) +}