diff --git a/web-src/src/App.vue b/web-src/src/App.vue index 6df10719..abab1da2 100644 --- a/web-src/src/App.vue +++ b/web-src/src/App.vue @@ -311,7 +311,7 @@ export default { const track = this.$store.getters.now_playing if (track && track.track_id) { webapi.library_track(track.track_id).then(({ data }) => { - this.$store.commit(types.UPDATE_LYRICS, data) + this.$store.commit(types.UPDATE_LYRICS, data.lyrics) }) } else { this.$store.commit(types.UPDATE_LYRICS) diff --git a/web-src/src/components/LyricsPane.vue b/web-src/src/components/LyricsPane.vue index 57ea77cc..9161eae9 100644 --- a/web-src/src/components/LyricsPane.vue +++ b/web-src/src/components/LyricsPane.vue @@ -74,24 +74,27 @@ export default { return this.lastIndex if (this.lastIndex < la.length - 2 && la[this.lastIndex + 2][1] > curTime) return this.lastIndex + 1 - // Not found in the next 2 items, so start dichotomous search for the best time - let start = 0, - end = la.length - 1, - index - while (start <= end) { - index = (start + end) >> 1 - if ( - la[index][1] <= curTime && - (la[index + 1]?.[1] > curTime || !la[index + 1]) - ) { - return index - } - la[index][1] < curTime ? (start = index + 1) : (end = index - 1) - } - return -1 + // Not found in the next 2 items, so start searching for the best time + return la.findLastIndex((verse) => verse[1] <= curTime) }, lyrics() { - return this.$store.getters.lyrics + const lyrics = this.$store.getters.lyrics + const lyricsObj = [] + if (lyrics) { + const regex = /(\[(\d+):(\d+)(?:\.\d+)?\] ?)?(.*)/ + lyrics.split('\n').forEach((item) => { + const matches = regex.exec(item) + if (matches && matches[4]) { + const obj = [matches[4]] + if (matches[2] && matches[3]) + obj.push( + matches[2] * 60 + matches[3] * 1 + ) + lyricsObj.push(obj) + } + }) + } + return lyricsObj }, player() { return this.$store.state.player diff --git a/web-src/src/store/index.js b/web-src/src/store/index.js index 9b160d62..53ea89f1 100644 --- a/web-src/src/store/index.js +++ b/web-src/src/store/index.js @@ -191,27 +191,8 @@ export default createStore({ [types.UPDATE_QUEUE](state, queue) { state.queue = queue }, - [types.UPDATE_LYRICS](state, track) { - // Parse from .LRC or text format to synchronized lyrics - function parse(lyrics) { - const lyricsObj = [] - if (lyrics) { - const regex = /(\[(\d+):(\d+)(?:\.\d+)?\] ?)?(.*)/ - lyrics.split('\n').forEach((item) => { - const matches = regex.exec(item) - if (matches !== null && matches[4].length) { - const obj = [matches[4]] - if (matches[2] != null && matches[3] != null) - obj.push( - parseInt(matches[2], 10) * 60 + parseInt(matches[3], 10) - ) - lyricsObj.push(obj) - } - }) - } - return lyricsObj - } - state.lyrics.content = track ? parse(track.lyrics) : '' + [types.UPDATE_LYRICS](state, lyrics) { + state.lyrics.content = lyrics }, [types.UPDATE_LASTFM](state, lastfm) { state.lastfm = lastfm