fix(ScrollingLyrics): handle empty lyrics input by returning a default empty line and adjust current line index logic
This commit is contained in:
parent
8a16f3cde8
commit
c129b2dd1b
|
@ -188,7 +188,14 @@ function setLineRef(el: HTMLElement | null, index: number) {
|
||||||
|
|
||||||
// 歌词解析函数
|
// 歌词解析函数
|
||||||
function parseLyrics(lrcText: string, minGapDuration: number = 5): (LyricsLine | GapLine)[] {
|
function parseLyrics(lrcText: string, minGapDuration: number = 5): (LyricsLine | GapLine)[] {
|
||||||
if (!lrcText) return []
|
if (!lrcText) return [
|
||||||
|
{
|
||||||
|
type: 'lyric',
|
||||||
|
time: 0,
|
||||||
|
text: '',
|
||||||
|
originalTime: '[00:00]'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
const lines = lrcText.split('\n')
|
const lines = lrcText.split('\n')
|
||||||
const tempParsedLines: (LyricsLine | GapLine)[] = []
|
const tempParsedLines: (LyricsLine | GapLine)[] = []
|
||||||
|
@ -248,15 +255,24 @@ function parseLyrics(lrcText: string, minGapDuration: number = 5): (LyricsLine |
|
||||||
}
|
}
|
||||||
|
|
||||||
finalLines.push(...lyricLines)
|
finalLines.push(...lyricLines)
|
||||||
return finalLines.sort((a, b) => a.time - b.time)
|
const sortedLines = finalLines.sort((a, b) => a.time - b.time)
|
||||||
|
// 在最前面插入一个空行
|
||||||
|
sortedLines.unshift({
|
||||||
|
type: 'lyric',
|
||||||
|
time: 0,
|
||||||
|
text: '',
|
||||||
|
originalTime: '[00:00]'
|
||||||
|
})
|
||||||
|
return sortedLines
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找当前行索引
|
// 查找当前行索引
|
||||||
function findCurrentLineIndex(time: number): number {
|
function findCurrentLineIndex(time: number): number {
|
||||||
if (parsedLyrics.value.length === 0) return -1
|
if (parsedLyrics.value.length === 0) return -1
|
||||||
|
// 如果时间小于第一句歌词,则返回0(空行)
|
||||||
let index = -1
|
if (time < parsedLyrics.value[1]?.time) return 0
|
||||||
for (let i = 0; i < parsedLyrics.value.length; i++) {
|
let index = 0
|
||||||
|
for (let i = 1; i < parsedLyrics.value.length; i++) {
|
||||||
if (time >= parsedLyrics.value[i].time) {
|
if (time >= parsedLyrics.value[i].time) {
|
||||||
index = i
|
index = i
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user