Files
music-v2/app/components/Player/Fullscreen/LyricsDisplay.vue
T

271 lines
5.5 KiB
Vue

<script setup lang="ts">
const { animationDuration, offset, showScrollBar } = defineProps<{
animationDuration: number
offset: number
showScrollBar: boolean
}>()
const song = useSong()
const { fullscreen } = useFullscreen()
const { secondsPlayed } = usePlayer()
const parsedLyrics = computed(() => {
if (!song.value?.lyrics?.synced) return
if (song.value.lyrics.synced) return parseLyrics(song.value.lyrics.text)
})
const findLyricIndex = (time: number) => {
if (!parsedLyrics.value) return -1
const index = parsedLyrics.value.findIndex(
(lyric, i) =>
lyric.time <= time &&
(!parsedLyrics.value![i + 1] ||
parsedLyrics.value![i + 1]!.time > time),
)
return index
}
const activeIndex = computed(() => {
return findLyricIndex(secondsPlayed.value)
})
const scrollActiveIndex = computed(() => {
if (!parsedLyrics.value) return -1
const time = secondsPlayed.value + animationDuration
let index = findLyricIndex(time)
if (index === -1 && time < parsedLyrics.value[0]!.time) index = 0
if (index >= parsedLyrics.value.length - 1)
index = parsedLyrics.value.length - 2
return index
})
const height = ref<number>()
const top = ref(0)
const syncedHeight = ref<number>()
const topFadeExtension = 75
const updateLyricsLayout = () => {
const trackDisplay = document.querySelector(".track-display")
if (!trackDisplay) return
height.value = trackDisplay.clientHeight
top.value = trackDisplay.getBoundingClientRect().top
syncedHeight.value = window.innerHeight - top.value - 100
}
watch(
fullscreen,
async () => {
await nextTick()
updateLyricsLayout()
},
{ immediate: true },
)
onMounted(() => window.addEventListener("resize", updateLyricsLayout))
onUnmounted(() => window.removeEventListener("resize", updateLyricsLayout))
const containerRef = useTemplateRef("container-ref")
const outerRef = useTemplateRef("outer-ref")
const transformY = computed(() => {
const lines = containerRef.value?.children
if (scrollActiveIndex.value < 0 || !lines?.length) return 0
const firstLyric = lines[0] as HTMLElement
const activeLyric = lines[scrollActiveIndex.value] as HTMLElement
if (!firstLyric || !activeLyric) return 0
return (
firstLyric.getBoundingClientRect().top -
activeLyric.getBoundingClientRect().bottom
)
})
const manualOffset = ref(0)
const isScrolling = ref(false)
let scrollingTimeout: any = null
const onWheel = (event: WheelEvent) => {
if (!containerRef.value || !outerRef.value) return
if (!isScrolling.value) {
manualOffset.value = finalTransform.value
}
isScrolling.value = true
clearTimeout(scrollingTimeout)
const delta = event.deltaY
manualOffset.value -= delta
const maxTranslate = finalTransform.value + offset
const lowerViewportLimit = Math.max(
outerRef.value.offsetHeight,
window.innerHeight - 300,
)
const minTranslate =
-containerRef.value.scrollHeight + lowerViewportLimit
manualOffset.value = Math.min(
maxTranslate,
Math.max(minTranslate, manualOffset.value),
)
scrollingTimeout = setTimeout(() => (isScrolling.value = false), 3000)
}
const onLyricClick = (time: number) => {
clearTimeout(scrollingTimeout)
isScrolling.value = false
secondsPlayed.value = time
}
const finalTransform = computed(() => {
if (isScrolling.value) return manualOffset.value
const lyricsTop = top.value - topFadeExtension
const guideY = import.meta.client ? window.innerHeight * 0.45 : 0
const laterLyricOffset = scrollActiveIndex.value > 0 ? 100 : 0
return transformY.value + guideY - lyricsTop + laterLyricOffset
})
</script>
<template>
<div
ref="outer-ref"
v-if="song && song.lyrics && song.lyrics.synced"
class="synced-lyrics-container"
:style="{
height: `${syncedHeight + topFadeExtension}px`,
marginTop: `${top - topFadeExtension}px`,
}"
@wheel.prevent="onWheel"
>
<div
ref="container-ref"
class="synced-lyrics"
:style="{
transform: `translateY(${finalTransform}px)`,
transition: `transform ${animationDuration}s ease`,
}"
>
<p
v-for="(lyric, i) in parsedLyrics"
:key="lyric.time"
:class="{
active: i == activeIndex,
past: i < activeIndex && !isScrolling,
}"
@click="onLyricClick(lyric.time)"
>
{{ lyric.text }}
</p>
</div>
</div>
<div
v-else-if="song?.lyrics?.text"
class="lyrics"
:style="{
height: `${height}px`,
}"
:class="{ show: showScrollBar }"
>
<p>{{ song.lyrics.text }}</p>
</div>
</template>
<style lang="scss" scoped>
@use "@/styles/util" as *;
@use "@/styles/variables" as *;
.synced-lyrics-container {
position: relative;
overflow: hidden;
align-self: start;
mask-image: linear-gradient(
to bottom,
transparent 0%,
#000 15%,
#000 calc(100% - 200px),
transparent 100%
);
}
.synced-lyrics {
position: absolute;
scrollbar-width: none;
will-change: transform;
top: 0;
& > p {
@include fontSize(40px);
font-weight: 600;
color: white;
opacity: 0.4;
margin: 0;
padding: 10px 0;
transition:
opacity 0.1s ease,
filter 0.1s ease;
filter: blur(1.5px);
&.active {
opacity: 1;
filter: initial;
}
&:hover {
cursor: pointer;
opacity: 1;
filter: initial;
}
&.past {
opacity: 0;
filter: blur(1.5px);
transition: opacity 0.4s ease;
&:hover {
cursor: default;
opacity: 0;
filter: blur(1.5px);
}
}
}
}
.lyrics {
overflow: hidden;
& > p {
height: 100%;
overflow-y: scroll;
white-space: pre-wrap;
&::-webkit-scrollbar {
width: 10px;
}
&::-webkit-scrollbar-thumb {
background-color: transparent;
border-radius: 5px;
}
}
&.show > p::-webkit-scrollbar-thumb {
background-color: $color-secondary-background;
}
}
</style>