Skip to content

Commit

Permalink
fix: sentense mode highlight control
Browse files Browse the repository at this point in the history
  • Loading branch information
eatgrass committed Jan 3, 2024
1 parent 20bbc35 commit 267b46c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/LyricsMarkdownRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default class LyricsMarkdownRender extends MarkdownRenderChild {
private autoScroll: boolean
private sentenceMode: boolean
private lyricsRenderer: LyricsRenderer
private pauseHl: boolean = false

constructor(
plugin: LyricsPlugin,
Expand All @@ -47,12 +48,12 @@ export default class LyricsMarkdownRender extends MarkdownRenderChild {
let time = target?.dataset?.time
if (time !== undefined) {
const sec = parseInt(time) / 1000
this.updateTimestamp(sec, true)
this.player?.seek(sec)
this.updateTimestamp(sec)
}
}

private updateTimestamp = (sec: number) => {
private updateTimestamp = (sec: number, seek: boolean = false) => {
const lyrics = this.container.querySelectorAll(
'.lyrics-line[data-time]',
) as NodeListOf<HTMLElement>
Expand All @@ -64,16 +65,26 @@ export default class LyricsMarkdownRender extends MarkdownRenderChild {
if (
this.sentenceMode &&
!this.player.paused() &&
this.currentHL != -1
this.currentHL != -1 &&
!seek
) {
this.player.pause()
return
this.pauseHl = true
}
}
this.currentHL = hl
}

if (!this.pauseHl) {
if (this.currentHL >= 0) {
let hlels = this.container.findAll('.lyrics-highlighted')
hlels.forEach((el) => {
el.removeClass('lyrics-highlighted')
})
}

if (hl >= 0) {
const hlel = lyrics.item(hl)
console.log(hl, hlel)
if (hlel) {
hlel.addClass('lyrics-highlighted')
if (this.autoScroll) {
Expand All @@ -84,12 +95,6 @@ export default class LyricsMarkdownRender extends MarkdownRenderChild {
}
}
}

if (this.currentHL >= 0) {
lyrics.item(this.currentHL)?.removeClass('lyrics-highlighted')
}

this.currentHL = hl
}
}

Expand Down Expand Up @@ -309,6 +314,9 @@ export default class LyricsMarkdownRender extends MarkdownRenderChild {
props: {
src,
timeupdate: this.updateTimestamp,
onPlay: () => {
this.pauseHl = false
},
},
})
fragment.append(playerEl)
Expand Down
8 changes: 8 additions & 0 deletions src/Player.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export let src: string
export let timeupdate: (time: number) => void
let player: HTMLAudioElement
export let time: number
export let onPlay: () => void
export function seek(t: number) {
time = t
play()
Expand Down Expand Up @@ -33,6 +34,12 @@ const _timeupdate = () => {
timeupdate(time)
}
}
const _play = () => {
if (onPlay) {
onPlay()
}
}
</script>

<div class="audio-wrapper">
Expand All @@ -43,6 +50,7 @@ const _timeupdate = () => {
controls
bind:currentTime={time}
on:timeupdate={_timeupdate}
on:play={_play}
></audio>
</div>

Expand Down

0 comments on commit 267b46c

Please sign in to comment.