Skip to content

Commit

Permalink
fix(translator): force setting currentLyrics on cache hit (#2983)
Browse files Browse the repository at this point in the history
Co-authored-by: Delusoire <deluso7re@outlook.com>
  • Loading branch information
rxri and Delusoire committed Apr 30, 2024
1 parent d0faddf commit 1723d79
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions CustomApps/lyrics-plus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,59 @@ class LyricsContainer extends react.Component {
if (CACHE[info.uri]?.[CONFIG.modes[mode]]) {
this.resetDelay();
this.setState({ ...CACHE[info.uri], isCached });
{
let mode = -1;
if (this.state.explicitMode !== -1) {
mode = this.state.explicitMode;
} else if (this.state.lockMode !== -1) {
mode = this.state.lockMode;
} else {
// Auto switch
if (this.state.karaoke) {
mode = KARAOKE;
} else if (this.state.synced) {
mode = SYNCED;
} else if (this.state.unsynced) {
mode = UNSYNCED;
} else if (this.state.genius) {
mode = GENIUS;
}
}
const lyricsState = CACHE[info.uri][CONFIG.modes[mode]];
if (lyricsState) {
this.state.currentLyrics = this.state[CONFIG.visual["translate:translated-lyrics-source"]] ?? lyricsState;
}
}
this.translateLyrics();
return;
}
} else {
if (CACHE[info.uri]) {
this.resetDelay();
this.setState({ ...CACHE[info.uri], isCached });
{
let mode = -1;
if (this.state.explicitMode !== -1) {
mode = this.state.explicitMode;
} else if (this.state.lockMode !== -1) {
mode = this.state.lockMode;
} else {
// Auto switch
if (this.state.karaoke) {
mode = KARAOKE;
} else if (this.state.synced) {
mode = SYNCED;
} else if (this.state.unsynced) {
mode = UNSYNCED;
} else if (this.state.genius) {
mode = GENIUS;
}
}
const lyricsState = CACHE[info.uri][CONFIG.modes[mode]];
if (lyricsState) {
this.state.currentLyrics = this.state[CONFIG.visual["translate:translated-lyrics-source"]] ?? lyricsState;
}
}
this.translateLyrics();
return;
}
Expand Down Expand Up @@ -385,16 +431,15 @@ class LyricsContainer extends react.Component {

// Seemingly long delay so it can be cleared later for accurate timing
showNotification(10000);
const lyricText = lyrics.map(lyric => lyric.text).join("\n");

for (const params of [
["romaji", "spaced", "romaji"],
["hiragana", "furigana", "furigana"],
["hiragana", "normal", "hiragana"],
["katakana", "normal", "katakana"]
]) {
if (language !== "ja") continue;
this.translator.romajifyText(lyricText, params[0], params[1]).then(result => {
Promise.all(lyrics.map(lyric => this.translator.romajifyText(lyric.text, params[0], params[1]))).then(results => {
const result = results.join("\n");
Utils.processTranslatedLyrics(result, lyrics, { state: this.state, stateName: params[2] });
showNotification(200);
lyricContainerUpdate?.();
Expand All @@ -406,7 +451,8 @@ class LyricsContainer extends react.Component {
["romaja", "romaja"]
]) {
if (language !== "ko") continue;
this.translator.convertToRomaja(lyricText, params[1]).then(result => {
Promise.all(lyrics.map(lyric => this.translator.convertToRomaja(lyric.text, params[1]))).then(results => {
const result = results.join("\n");
Utils.processTranslatedLyrics(result, lyrics, { state: this.state, stateName: params[1] });
showNotification(200);
lyricContainerUpdate?.();
Expand All @@ -421,7 +467,8 @@ class LyricsContainer extends react.Component {
["t", "tw"]
]) {
if (!language.includes("zh") || (language === "zh-hans" && params[0] === "t") || (language === "zh-hant" && params[0] === "cn")) continue;
this.translator.convertChinese(lyricText, params[0], params[1]).then(result => {
Promise.all(lyrics.map(lyric => this.translator.convertChinese(lyric.text, params[0], params[1]))).then(results => {
const result = results.join("\n");
Utils.processTranslatedLyrics(result, lyrics, { state: this.state, stateName: params[1] });
showNotification(200);
lyricContainerUpdate?.();
Expand Down

0 comments on commit 1723d79

Please sign in to comment.