From 9f0234e00e8276f350e040803faa55205c348565 Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Tue, 10 Feb 2026 01:48:02 -0800 Subject: [PATCH] fix play now --- src/lib/player/store.svelte.ts | 64 +++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/lib/player/store.svelte.ts b/src/lib/player/store.svelte.ts index 96a39a0..4753650 100644 --- a/src/lib/player/store.svelte.ts +++ b/src/lib/player/store.svelte.ts @@ -109,22 +109,26 @@ class PlayerStore { if (existingIdx !== -1) { if (playNow) { + this.playNext(track); this.playId(track.id); } return; } - // Add to end - this.queue.push(track); - - if (this.isShuffled) { - this.shuffledIndices.push(this.queue.length - 1); - } - if (playNow) { + this.playNext(track); this.playId(track.id); - } else if (this.queue.length === 1 && !this.currentId) { - this.currentId = track.id; + } else { + // Add to end + this.queue.push(track); + + if (this.isShuffled) { + this.shuffledIndices.push(this.queue.length - 1); + } + + if (this.queue.length === 1 && !this.currentId) { + this.currentId = track.id; + } } } @@ -248,23 +252,35 @@ class PlayerStore { } prev() { - // If history has > 1 item, go back - if (this.history.length > 1) { - // Pop current - this.history.pop(); - const prevId = this.history[this.history.length - 1]; - if (this.hasTrack(prevId)) { - this.currentId = prevId; - } else { - // Track removed? fallback - this.history.pop(); // Remove invalid - this.prev(); // Recurse + if (this.queue.length === 0) return; + + let prevIdxInQueue: number | null = null; + const currentIdx = this.currentIndex; + + if (this.isShuffled) { + const currentShufflePos = this.shuffledIndices.indexOf(currentIdx); + if (currentShufflePos > 0) { + prevIdxInQueue = this.shuffledIndices[currentShufflePos - 1]; + } else if (this.repeatMode === "all" && this.shuffledIndices.length > 0) { + // Wrap to end? Or just stop. + // For now let's stop at start if not wrapping. + // If repeat all, wrap to end? + prevIdxInQueue = this.shuffledIndices[this.shuffledIndices.length - 1]; } } else { - // Restart current song? - // Handled by UI usually (calling audio.currentTime = 0), - // but here we just seek to 0 if we could. - // Store doesn't control audio element directly. + if (currentIdx > 0) { + prevIdxInQueue = currentIdx - 1; + } else if (this.repeatMode === "all" && this.queue.length > 0) { + prevIdxInQueue = this.queue.length - 1; + } + } + + if (prevIdxInQueue !== null) { + this.playId(this.queue[prevIdxInQueue]?.id); + } else { + // At start of queue. + // Just seek to 0? Store doesn't control audio. + // If we can't go back, we do nothing (UI likely handles the seek-to-0 comparison). } }