advplyr.audiobookshelf-app/components/readers/Reader.vue

151 lines
4.3 KiB
Vue
Raw Normal View History

2021-10-17 20:20:00 -05:00
<template>
2022-12-04 08:29:55 -06:00
<div v-if="show" class="absolute top-0 left-0 w-full h-full bg-bg z-40 pt-8" :class="{ 'reader-player-open': !!playerLibraryItemId }">
2021-10-17 20:20:00 -05:00
<div class="h-8 w-full bg-primary flex items-center px-2 fixed top-0 left-0 z-30 box-shadow-sm">
<p class="w-5/6 truncate">{{ title }}</p>
<div class="flex-grow" />
<span class="material-icons text-xl text-white" @click.stop="show = false">close</span>
</div>
<component v-if="readerComponentName" ref="readerComponent" :is="readerComponentName" :url="ebookUrl" />
</div>
</template>
<script>
export default {
data() {
return {
touchstartX: 0,
touchendX: 0
}
},
watch: {
show: {
handler(newVal) {
if (newVal) {
this.registerListeners()
} else {
this.unregisterListeners()
}
}
}
},
computed: {
show: {
get() {
return this.$store.state.showReader
},
set(val) {
this.$store.commit('setShowReader', val)
}
},
title() {
2022-04-16 06:10:10 -05:00
return this.mediaMetadata.title || 'No Title'
2021-10-17 20:20:00 -05:00
},
2022-04-16 06:10:10 -05:00
selectedLibraryItem() {
return this.$store.state.selectedLibraryItem
},
media() {
return this.selectedLibraryItem ? this.selectedLibraryItem.media : null
},
mediaMetadata() {
return this.media ? this.media.metadata || {} : {}
2021-10-17 20:20:00 -05:00
},
readerComponentName() {
if (this.ebookType === 'epub') return 'readers-epub-reader'
else if (this.ebookType === 'mobi') return 'readers-mobi-reader'
else if (this.ebookType === 'comic') return 'readers-comic-reader'
return null
},
folderId() {
2022-04-16 06:10:10 -05:00
return this.selectedLibraryItem ? this.selectedLibraryItem.folderId : null
2021-10-17 20:20:00 -05:00
},
libraryId() {
2022-04-16 06:10:10 -05:00
return this.selectedLibraryItem ? this.selectedLibraryItem.libraryId : null
},
ebookFile() {
return this.media ? this.media.ebookFile : null
},
ebookFormat() {
if (!this.ebookFile) return null
return this.ebookFile.ebookFormat
},
ebookType() {
if (this.isMobi) return 'mobi'
else if (this.isEpub) return 'epub'
else if (this.isPdf) return 'pdf'
else if (this.isComic) return 'comic'
return null
},
isEpub() {
return this.ebookFormat == 'epub'
},
isMobi() {
return this.ebookFormat == 'mobi' || this.ebookFormat == 'azw3'
2021-10-17 20:20:00 -05:00
},
2022-04-16 06:10:10 -05:00
isPdf() {
return this.ebookFormat == 'pdf'
},
isComic() {
return this.ebookFormat == 'cbz' || this.ebookFormat == 'cbr'
},
ebookUrl() {
if (!this.ebookFile) return null
let filepath = ''
if (this.selectedLibraryItem.isFile) {
filepath = this.$encodeUriPath(this.ebookFile.metadata.filename)
} else {
const itemRelPath = this.selectedLibraryItem.relPath
if (itemRelPath.startsWith('/')) itemRelPath = itemRelPath.slice(1)
const relPath = this.ebookFile.metadata.relPath
if (relPath.startsWith('/')) relPath = relPath.slice(1)
2022-04-16 06:10:10 -05:00
filepath = this.$encodeUriPath(`${itemRelPath}/${relPath}`)
}
return `/ebook/${this.libraryId}/${this.folderId}/${filepath}`
2022-12-04 08:29:55 -06:00
},
playerLibraryItemId() {
return this.$store.state.playerLibraryItemId
2021-10-17 20:20:00 -05:00
}
},
methods: {
next() {
if (this.$refs.readerComponent && this.$refs.readerComponent.next) {
this.$refs.readerComponent.next()
}
},
prev() {
if (this.$refs.readerComponent && this.$refs.readerComponent.prev) {
this.$refs.readerComponent.prev()
}
},
handleGesture() {
if (this.touchendX < this.touchstartX) {
console.log('swiped left')
this.next()
}
if (this.touchendX > this.touchstartX) {
console.log('swiped right')
this.prev()
}
},
touchstart(e) {
this.touchstartX = e.changedTouches[0].screenX
},
touchend(e) {
this.touchendX = e.changedTouches[0].screenX
this.handleGesture()
},
registerListeners() {
document.body.addEventListener('touchstart', this.touchstart)
document.body.addEventListener('touchend', this.touchend)
},
unregisterListeners() {
document.body.removeEventListener('touchstart', this.touchstart)
document.body.removeEventListener('touchend', this.touchend)
}
},
beforeDestroy() {
this.unregisterListeners()
}
}
</script>