Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/components/library/EditLyrics.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const plainTextLintResult = ref([])
const cmContainer = ref(null)
const cmHeight = ref(null)
const currentIndex = ref(null)
const indexToLineCache = ref(new Map())

let view = null
let runner = null
Expand Down Expand Up @@ -317,6 +318,39 @@ const lyricsUpdated = (newLyrics) => {
isDirty.value = true
lyricsLintResult.value = executeLyricsLint(newLyrics)
plainTextLintResult.value = executePlainTextLint(newLyrics)
indexToLineCache.value.clear()
}

const findDocumentLineForLyricIndex = (targetLyricIndex) => {
// Check if we have a cached position for the previous lyric index
const startLyricIndex = targetLyricIndex - 1
const cachedLineNumber = indexToLineCache.value.get(startLyricIndex)

// Validate cached line number is still within document bounds
const startLineNumber = (cachedLineNumber && cachedLineNumber <= view.state.doc.lines)
? cachedLineNumber
: null

// Start search from cached position + 1, or from beginning if no cache
const searchFrom = startLineNumber ? startLineNumber + 1 : 1
let currentLyricIndex = startLineNumber ? startLyricIndex + 1 : 0

for (let lineNum = searchFrom; lineNum <= view.state.doc.lines; lineNum++) {
const line = view.state.doc.line(lineNum)
const parsed = parseLine(line.text)

// Only count TIME lines
if (parsed.type === 'TIME') {
if (currentLyricIndex === targetLyricIndex) {
// Cache the document line number before returning
indexToLineCache.value.set(targetLyricIndex, lineNum)
return lineNum
}
currentLyricIndex++
}
}

return null
}

const syncLine = (moveNext = true) => {
Expand Down Expand Up @@ -589,8 +623,11 @@ watch(progress, (newProgress) => {
currentIndex.value = resultCurrentIndex

if (currentIndex.value >= 0) {
const line = view.state.doc.line(currentIndex.value + 1)
view.dispatch({ effects: addLineHighlight.of(line.from) })
const documentLineNum = findDocumentLineForLyricIndex(currentIndex.value)
if (documentLineNum && documentLineNum <= view.state.doc.lines) {
const line = view.state.doc.line(documentLineNum)
view.dispatch({ effects: addLineHighlight.of(line.from) })
}
} else {
view.dispatch({ effects: addLineHighlight.of(null) })
}
Expand Down