Skip to content

Commit 2e4f3ad

Browse files
authored
feat(lyrics-plus): optimize convert performance & improve code quality (#3311)
1 parent 2d9b637 commit 2e4f3ad

File tree

7 files changed

+305
-216
lines changed

7 files changed

+305
-216
lines changed

CustomApps/lyrics-plus/OptionsMenu.js

-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ const TranslationMenu = react.memo(({ friendlyLanguage, hasTranslation }) => {
139139
}
140140
case "korean": {
141141
modeOptions = {
142-
hangul: "Hangul",
143142
romaja: "Romaja",
144143
};
145144
break;

CustomApps/lyrics-plus/Pages.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ const SyncedLyricsPage = react.memo(({ lyrics = [], provider, copyright, isKara
173173
const lineText = originalText && showTranslatedBelow ? originalText : text;
174174

175175
// Convert lyrics to text for comparison
176-
const belowOrigin = typeof originalText === "object" ? originalText?.props?.children?.[0] : originalText;
177-
const belowTxt = typeof text === "object" ? text?.props?.children?.[0] : text;
176+
const belowOrigin = (typeof originalText === "object" ? originalText?.props?.children?.[0] : originalText)?.replace(/\s+/g, "");
177+
const belowTxt = (typeof text === "object" ? text?.props?.children?.[0] : text)?.replace(/\s+/g, "");
178178

179179
const belowMode = showTranslatedBelow && originalText && belowOrigin !== belowTxt;
180180

@@ -455,8 +455,8 @@ const SyncedExpandedLyricsPage = react.memo(({ lyrics, provider, copyright, isKa
455455
const lineText = originalText && showTranslatedBelow ? originalText : text;
456456

457457
// Convert lyrics to text for comparison
458-
const belowOrigin = typeof originalText === "object" ? originalText?.props?.children?.[0] : originalText;
459-
const belowTxt = typeof text === "object" ? text?.props?.children?.[0] : text;
458+
const belowOrigin = (typeof originalText === "object" ? originalText?.props?.children?.[0] : originalText)?.replace(/\s+/g, "");
459+
const belowTxt = (typeof text === "object" ? text?.props?.children?.[0] : text)?.replace(/\s+/g, "");
460460

461461
const belowMode = showTranslatedBelow && originalText && belowOrigin !== belowTxt;
462462

@@ -531,8 +531,8 @@ const UnsyncedLyricsPage = react.memo(({ lyrics, provider, copyright }) => {
531531
const lineText = originalText && showTranslatedBelow ? originalText : text;
532532

533533
// Convert lyrics to text for comparison
534-
const belowOrigin = typeof originalText === "object" ? originalText?.props?.children?.[0] : originalText;
535-
const belowTxt = typeof text === "object" ? text?.props?.children?.[0] : text;
534+
const belowOrigin = (typeof originalText === "object" ? originalText?.props?.children?.[0] : originalText)?.replace(/\s+/g, "");
535+
const belowTxt = (typeof text === "object" ? text?.props?.children?.[0] : text)?.replace(/\s+/g, "");
536536

537537
const belowMode = showTranslatedBelow && originalText && belowOrigin !== belowTxt;
538538

CustomApps/lyrics-plus/Providers.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ const Providers = {
3636
}));
3737
}
3838

39-
result.provider = lyrics.provider;
39+
/**
40+
* to distinguish it from the existing Musixmatch, the provider will remain as Spotify.
41+
* if Spotify official lyrics support multiple providers besides Musixmatch in the future, please uncomment the under section. */
42+
// result.provider = lyrics.provider;
4043

4144
return result;
4245
},

CustomApps/lyrics-plus/Settings.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const CacheButton = () => {
5151
}
5252

5353
const [count, setCount] = useState(Object.keys(lyrics).length);
54-
const text = count ? "Clear cached lyrics" : "No cached lyrics";
54+
const text = count ? "Clear all cached lyrics" : "No cached lyrics";
5555

5656
return react.createElement(
5757
"button",
@@ -106,6 +106,36 @@ const RefreshTokenButton = ({ setTokenCallback }) => {
106106
);
107107
};
108108

109+
const ConfigButton = ({ name, text, onChange = () => {} }) => {
110+
return react.createElement(
111+
"div",
112+
{
113+
className: "setting-row",
114+
},
115+
react.createElement(
116+
"label",
117+
{
118+
className: "col description",
119+
},
120+
name
121+
),
122+
react.createElement(
123+
"div",
124+
{
125+
className: "col action",
126+
},
127+
react.createElement(
128+
"button",
129+
{
130+
className: "btn",
131+
onClick: onChange,
132+
},
133+
text
134+
)
135+
)
136+
);
137+
};
138+
109139
const ConfigSlider = ({ name, defaultValue, onChange = () => {} }) => {
110140
const [active, setActive] = useState(defaultValue);
111141

@@ -652,6 +682,16 @@ function openConfig() {
652682
type: ConfigSelection,
653683
options: languageOptions,
654684
},
685+
{
686+
desc: "Clear Memory Cache",
687+
info: "Loaded lyrics are cached in memory for faster reloading. Press this button to clear the cached lyrics from memory without restarting Spotify.",
688+
key: "clear-memore-cache",
689+
text: "Clear memory cache",
690+
type: ConfigButton,
691+
onChange: () => {
692+
reloadLyrics?.();
693+
},
694+
},
655695
],
656696
onChange: (name, value) => {
657697
CONFIG.visual[name] = value;

CustomApps/lyrics-plus/Translator.js

+15
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ class Translator {
4242
}
4343
}
4444

45+
async awaitFinished(language) {
46+
return new Promise((resolve) => {
47+
const interval = setInterval(() => {
48+
this.injectExternals(language);
49+
this.createTranslator(language);
50+
51+
const lan = language.slice(0, 2);
52+
if (this.finished[lan]) {
53+
clearInterval(interval);
54+
resolve();
55+
}
56+
}, 100);
57+
});
58+
}
59+
4560
/**
4661
* Fix an issue with kuromoji when loading dict from external urls
4762
* Adapted from: https://github.com/mobilusoss/textlint-browser-runner/pull/7

CustomApps/lyrics-plus/Utils.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,12 @@ const Utils = {
132132

133133
return ((simpPercentage - tradPercentage + 1) / 2) * 100 >= CONFIG.visual["hans-detect-threshold"] ? "zh-hans" : "zh-hant";
134134
},
135-
processTranslatedLyrics(result, lyricsToTranslate, { state, stateName }) {
136-
const translatedLines = result.split("\n");
137-
state[stateName] = [];
138-
for (let i = 0; i < lyricsToTranslate.length; i++) {
139-
const lyric = {
140-
startTime: lyricsToTranslate[i].startTime || 0,
141-
text: this.rubyTextToReact(translatedLines[i]),
142-
originalText: lyricsToTranslate[i].text,
143-
};
144-
state[stateName].push(lyric);
145-
}
135+
processTranslatedLyrics(translated, original) {
136+
return original.map((lyric, index) => ({
137+
startTime: lyric.startTime || 0,
138+
text: this.rubyTextToReact(translated[index]),
139+
originalText: lyric.text,
140+
}));
146141
},
147142
/** It seems that this function is not being used, but I'll keep it just in case it’s needed in the future.*/
148143
processTranslatedOriginalLyrics(lyrics, synced) {

0 commit comments

Comments
 (0)