Skip to content

Commit ddc8b65

Browse files
committed
scroll up when restarting a song
1 parent 36922a3 commit ddc8b65

2 files changed

Lines changed: 30 additions & 20 deletions

File tree

media/scripts/lyricsScript.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ window.addEventListener('message', (event) => {
3232
renderLyrics(lyrics, textColor);
3333
lastLyricsHash = lyricsHash;
3434
}
35-
if (pick !== undefined && pick !== null) {
36-
pickLyrics(pick, textColor);
37-
}
35+
pickLyrics(pick, textColor);
3836
} else if (command === 'clearLyrics') {
3937
clearLyrics();
4038
} else if (command === 'pickLyrics') {
@@ -95,20 +93,19 @@ function clearLyrics() {
9593
}
9694

9795
function pickLyrics(id, textColor) {
98-
if (id === lastPickId || id === null || id === undefined) {
96+
if (id === lastPickId) {
9997
return;
10098
}
101-
console.log(textColor);
10299
lastPickId = id;
103-
104100
const lines = Array.from(document.querySelectorAll('.line'));
105101
if (!lines.length) {
106102
return;
107103
}
108-
109-
const pickedIndex = lines.findIndex((line) => line.dataset.lyricsId === String(id));
110-
if (pickedIndex === -1) {
111-
return;
104+
let pickedIndex;
105+
if (id === -1) {
106+
pickedIndex = -1;
107+
} else {
108+
pickedIndex = lines.findIndex((line) => line.dataset.lyricsId === String(id));
112109
}
113110

114111
lines.forEach((line, index) => {
@@ -140,6 +137,9 @@ function pickLyrics(id, textColor) {
140137
}
141138
}
142139
});
143-
144-
lines[pickedIndex].scrollIntoView({ behavior: 'smooth', block: 'center' });
140+
if (pickedIndex === -1) {
141+
window.scrollTo({ top: 0, behavior: 'smooth' });
142+
} else {
143+
lines[pickedIndex].scrollIntoView({ behavior: 'smooth', block: 'center' });
144+
}
145145
}

src/extension.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,16 +533,26 @@ async function updateLyrics() {
533533
const value = currentPlayingState.synchronizedLyricsMap.floorEntry(
534534
currentlyPlayingResponse.progress_ms
535535
);
536-
if (value && panel) {
536+
if (panel) {
537537
const mobileMode: boolean =
538538
vscode.workspace.getConfiguration('spotilyrics').get('mobileMode') ?? false;
539-
panel.webview.postMessage({
540-
command: 'pickLyrics',
541-
pick: value[1].id,
542-
color: currentPlayingState.coverColor,
543-
textColor: '#' + currentPlayingState.textColor,
544-
mobileMode: mobileMode,
545-
});
539+
if (value) {
540+
panel.webview.postMessage({
541+
command: 'pickLyrics',
542+
pick: value[1].id,
543+
color: currentPlayingState.coverColor,
544+
textColor: '#' + currentPlayingState.textColor,
545+
mobileMode: mobileMode,
546+
});
547+
} else {
548+
panel.webview.postMessage({
549+
command: 'pickLyrics',
550+
pick: -1,
551+
color: currentPlayingState.coverColor,
552+
textColor: '#' + currentPlayingState.textColor,
553+
mobileMode: mobileMode,
554+
});
555+
}
546556
}
547557
}
548558
}

0 commit comments

Comments
 (0)