koel/resources/assets/js/components/ui/LyricsPane.vue

92 lines
2.2 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<article id="lyrics">
<div class="content">
<template v-if="song">
<div v-show="song.lyrics">
<pre ref="lyricsContainer">{{ lyrics }}</pre>
2022-12-02 16:17:37 +00:00
<Magnifier class="magnifier" @in="zoomLevel++" @out="zoomLevel--" />
2022-04-15 14:24:30 +00:00
</div>
2022-04-21 09:38:24 +00:00
<p v-if="song.id && !song.lyrics" class="none text-secondary">
2022-04-15 14:24:30 +00:00
<template v-if="isAdmin">
No lyrics found.
<button class="text-highlight" type="button" @click.prevent="showEditSongForm">
2022-04-15 14:24:30 +00:00
Click here
</button>
to add lyrics.
</template>
<span v-else>No lyrics available. Are you listening to Bach?</span>
</p>
</template>
</div>
</article>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { computed, defineAsyncComponent, onMounted, ref, toRefs, watch } from 'vue'
2022-04-15 14:24:30 +00:00
import { eventBus } from '@/utils'
import { useAuthorization } from '@/composables'
import { preferenceStore as preferences } from '@/stores'
2022-04-15 14:24:30 +00:00
2022-07-21 10:45:23 +00:00
const Magnifier = defineAsyncComponent(() => import('@/components/ui/Magnifier.vue'))
2022-04-15 14:24:30 +00:00
2022-04-29 13:32:12 +00:00
const props = defineProps<{ song: Song }>()
2022-04-15 17:00:08 +00:00
const { song } = toRefs(props)
2022-04-15 14:24:30 +00:00
const { isAdmin } = useAuthorization()
const lyricsContainer = ref<HTMLElement>()
const zoomLevel = ref(preferences.lyrics_zoom_level || 1)
2022-04-15 14:24:30 +00:00
const showEditSongForm = () => eventBus.emit('MODAL_SHOW_EDIT_SONG_FORM', song.value, 'lyrics')
2022-07-21 10:45:23 +00:00
const lyrics = computed(() => {
// This trick converts CRs (\r) to LF (\n) using JavaScript's implicit DOM-writing behavior
const div = document.createElement('div')
div.innerHTML = song.value.lyrics
return div.innerHTML
})
const setFontSize = () => {
if (lyricsContainer.value) {
lyricsContainer.value.style.fontSize = `${1 + (zoomLevel.value - 1) * 0.2}rem`
}
}
watch(zoomLevel, level => {
setFontSize()
preferences.lyrics_zoom_level = level
})
onMounted(() => setFontSize())
2022-04-15 14:24:30 +00:00
</script>
2024-04-04 20:13:35 +00:00
<style lang="postcss" scoped>
2022-04-15 14:24:30 +00:00
.content {
position: relative;
.magnifier {
2022-04-15 14:24:30 +00:00
opacity: 0;
position: absolute;
top: 0;
right: 0;
@media (hover: none) {
opacity: 1;
}
}
&:hover .magnifier {
2022-04-15 14:24:30 +00:00
opacity: .5;
&:hover {
opacity: 1;
}
}
}
2022-06-10 10:47:46 +00:00
pre {
white-space: pre-wrap;
2022-07-21 10:45:23 +00:00
line-height: 1.7;
2022-06-10 10:47:46 +00:00
}
2022-04-15 14:24:30 +00:00
</style>