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

375 lines
13 KiB
Vue
Raw Normal View History

2021-10-17 20:20:00 -05:00
<template>
<div v-if="show" :data-theme="ereaderTheme" class="group fixed top-0 left-0 right-0 layout-wrapper w-full z-40 pt-8 data-[theme=dark]:bg-[#232323] data-[theme=dark]:text-white data-[theme=light]:bg-white data-[theme=light]:text-black" :class="{ 'reader-player-open': isPlayerOpen }">
<!-- toolbar -->
<div class="h-32 pt-10 w-full px-2 fixed top-0 left-0 z-30 transition-transform bg-bg text-fg" :class="showingToolbar ? 'translate-y-0' : '-translate-y-32'" :style="{ boxShadow: showingToolbar ? '0px 8px 8px #11111155' : '' }" @touchstart.stop @mousedown.stop @touchend.stop @mouseup.stop>
2023-06-17 17:34:08 -05:00
<div class="flex items-center mb-2">
2023-06-19 17:42:15 -05:00
<button type="button" class="inline-flex mx-2" @click.stop="show = false">
<span class="material-icons-outlined text-3xl text-fg">chevron_left</span>
2023-06-19 17:42:15 -05:00
</button>
2023-06-17 17:34:08 -05:00
<div class="flex-grow" />
2023-06-19 17:42:15 -05:00
<button v-if="isComic || isEpub" type="button" class="inline-flex mx-2" @click.stop="clickTOCBtn">
<span class="material-icons-outlined text-2xl text-fg">format_list_bulleted</span>
2023-06-19 17:42:15 -05:00
</button>
<button v-if="isEpub" type="button" class="inline-flex mx-2" @click.stop="clickSettingsBtn">
<span class="material-icons text-2xl text-fg">settings</span>
2023-06-19 17:42:15 -05:00
</button>
<button v-if="comicHasMetadata" type="button" class="inline-flex mx-2" @click.stop="clickMetadataBtn">
<span class="material-icons text-2xl text-fg">more</span>
2023-06-19 17:42:15 -05:00
</button>
2023-06-17 17:34:08 -05:00
</div>
<p class="text-center truncate">{{ title }}</p>
2021-10-17 20:20:00 -05:00
</div>
2023-06-17 17:34:08 -05:00
<!-- ereader -->
<component v-if="readerComponentName" ref="readerComponent" :is="readerComponentName" :url="ebookUrl" :library-item="selectedLibraryItem" :is-local="isLocal" :keep-progress="keepProgress" @touchstart="touchstart" @touchend="touchend" @loaded="readerLoaded" @hook:mounted="readerMounted" />
<!-- table of contents modal -->
<modals-fullscreen-modal v-model="showTOCModal" :theme="ereaderTheme">
<div class="flex items-end justify-between h-20 px-4 pb-2">
<h1 class="text-lg">{{ $strings.HeaderTableOfContents }}</h1>
<button class="flex" @click.stop="showTOCModal = false">
<span class="material-icons">close</span>
</button>
</div>
<!-- chapters list -->
<div class="w-full overflow-y-auto overflow-x-hidden h-full max-h-[calc(100vh-85px)]">
<div class="w-full h-full px-4">
<ul>
<li v-for="chapter in chapters" :key="chapter.id" class="py-1">
<a :href="chapter.href" class="opacity-80 hover:opacity-100" @click.prevent="goToChapter(chapter.href)">{{ chapter.label }}</a>
<ul v-if="chapter.subitems.length">
<li v-for="subchapter in chapter.subitems" :key="subchapter.id" class="py-1 pl-4">
<a :href="subchapter.href" class="opacity-80 hover:opacity-100" @click.prevent="goToChapter(subchapter.href)">{{ subchapter.label }}</a>
</li>
</ul>
</li>
</ul>
<div v-if="!chapters.length" class="flex h-full items-center justify-center">
<p class="text-xl">{{ $strings.MessageNoChapters }}</p>
</div>
</div>
</div>
</modals-fullscreen-modal>
<!-- ereader settings modal -->
<modals-fullscreen-modal v-model="showSettingsModal" :theme="ereaderTheme" half-screen>
<div style="box-shadow: 0px -8px 8px #11111155">
<div class="flex items-end justify-between h-20 px-4 pb-2 mb-8">
<h1 class="text-lg">{{ $strings.HeaderEreaderSettings }}</h1>
<button class="flex" @click="showSettingsModal = false">
<span class="material-icons">close</span>
</button>
</div>
<div class="w-full overflow-y-auto overflow-x-hidden h-full max-h-[calc(100vh-85px)]">
<div class="w-full h-full px-4">
<div class="flex items-center mb-8">
<div class="w-32">
<p class="text-base">{{ $strings.LabelTheme }}:</p>
</div>
<ui-toggle-btns v-model="ereaderSettings.theme" :items="themeItems" @input="settingsUpdated" />
</div>
<div class="flex items-center mb-8">
<div class="w-32">
<p class="text-base">{{ $strings.LabelFontScale }}:</p>
</div>
<ui-range-input v-model="ereaderSettings.fontScale" :min="5" :max="300" :step="5" input-width="180px" @input="settingsUpdated" />
</div>
<div class="flex items-center mb-8">
<div class="w-32">
<p class="text-base">{{ $strings.LabelLineSpacing }}:</p>
</div>
<ui-range-input v-model="ereaderSettings.lineSpacing" :min="100" :max="300" :step="5" input-width="180px" @input="settingsUpdated" />
</div>
</div>
</div>
</div>
</modals-fullscreen-modal>
2021-10-17 20:20:00 -05:00
</div>
</template>
<script>
import { Capacitor } from '@capacitor/core'
2021-10-17 20:20:00 -05:00
export default {
data() {
return {
touchstartX: 0,
2022-12-06 17:07:27 -06:00
touchstartY: 0,
touchendX: 0,
touchendY: 0,
2023-06-17 17:34:08 -05:00
touchstartTime: 0,
touchIdentifier: null,
showingToolbar: false,
showTOCModal: false,
showSettingsModal: false,
comicHasMetadata: false,
chapters: [],
ereaderSettings: {
theme: 'dark',
fontScale: 100,
lineSpacing: 115
}
2021-10-17 20:20:00 -05:00
}
},
watch: {
show: {
handler(newVal) {
if (newVal) {
2023-06-17 17:34:08 -05:00
this.comicHasMetadata = false
2021-10-17 20:20:00 -05:00
this.registerListeners()
this.hideToolbar()
2023-06-19 17:42:15 -05:00
console.log('showReader for ebookFile', JSON.stringify(this.ebookFile))
2021-10-17 20:20:00 -05:00
} else {
this.unregisterListeners()
2023-06-17 17:34:08 -05:00
this.$showHideStatusBar(true)
2021-10-17 20:20:00 -05:00
}
}
}
},
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?.media || null
2022-04-16 06:10:10 -05:00
},
mediaMetadata() {
return this.media?.metadata || {}
2021-10-17 20:20:00 -05:00
},
ereaderTheme() {
if (this.isEpub) return this.ereaderSettings.theme
return document.documentElement.dataset.theme || 'dark'
},
themeItems() {
return [
{
text: this.$strings.LabelThemeDark,
value: 'dark'
},
{
text: this.$strings.LabelThemeLight,
value: 'light'
}
]
},
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'
2022-12-06 17:07:27 -06:00
else if (this.ebookType === 'pdf') return 'readers-pdf-reader'
2021-10-17 20:20:00 -05:00
return null
},
2022-04-16 06:10:10 -05:00
ebookFile() {
2023-06-11 11:12:52 -05:00
// ebook file id is passed when reading a supplementary ebook
if (this.ebookFileId) {
return this.selectedLibraryItem.libraryFiles.find((lf) => lf.ino === this.ebookFileId)
}
return this.media.ebookFile
2022-04-16 06:10:10 -05:00
},
ebookFormat() {
2023-06-11 11:12:52 -05:00
if (!this.ebookFile) return null
// Use file extension for supplementary ebook
if (!this.ebookFile.ebookFormat) {
return this.ebookFile.metadata.ext.toLowerCase().slice(1)
}
return this.ebookFile.ebookFormat
2022-04-16 06:10:10 -05:00
},
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'
},
isLocal() {
2023-06-19 17:42:15 -05:00
return !!this.ebookFile?.isLocal || !!this.ebookFile?.localFileId
},
localContentUrl() {
return this.ebookFile?.contentUrl
},
2022-04-16 06:10:10 -05:00
ebookUrl() {
if (!this.ebookFile) return null
if (this.localContentUrl) {
return Capacitor.convertFileSrc(this.localContentUrl)
}
2022-12-06 17:07:27 -06:00
const serverAddress = this.$store.getters['user/getServerAddress']
2023-06-11 11:12:52 -05:00
if (this.ebookFileId) {
return `${serverAddress}/api/items/${this.selectedLibraryItem.id}/ebook/${this.ebookFileId}`
}
return `${serverAddress}/api/items/${this.selectedLibraryItem.id}/ebook`
2022-12-04 08:29:55 -06:00
},
isPlayerOpen() {
return this.$store.getters['getIsPlayerOpen']
2023-06-11 11:12:52 -05:00
},
keepProgress() {
return this.$store.state.ereaderKeepProgress
},
ebookFileId() {
return this.$store.state.ereaderFileId
2021-10-17 20:20:00 -05:00
}
},
methods: {
settingsUpdated() {
this.$refs.readerComponent?.updateSettings?.(this.ereaderSettings)
localStorage.setItem('ereaderSettings', JSON.stringify(this.ereaderSettings))
},
goToChapter(href) {
this.showTOCModal = false
this.$refs.readerComponent?.goToChapter(href)
},
readerMounted() {
if (this.isEpub) {
this.loadEreaderSettings()
}
},
2023-06-17 17:34:08 -05:00
readerLoaded(data) {
if (this.isComic) {
this.comicHasMetadata = data.hasMetadata
}
},
clickMetadataBtn() {
this.$refs.readerComponent?.clickShowInfoMenu()
},
clickTOCBtn() {
this.hideToolbar()
2023-06-17 17:34:08 -05:00
if (this.isComic) {
this.$refs.readerComponent?.clickShowPageMenu?.()
} else {
this.chapters = this.$refs.readerComponent?.chapters || []
this.showTOCModal = true
}
},
clickSettingsBtn() {
this.hideToolbar()
2023-06-17 17:34:08 -05:00
this.showSettingsModal = true
},
2021-10-17 20:20:00 -05:00
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() {
2023-06-17 17:34:08 -05:00
// Touch must be less than 1s. Must be > 60px drag and X distance > Y distance
2022-12-06 17:07:27 -06:00
const touchTimeMs = Date.now() - this.touchstartTime
if (touchTimeMs >= 1000) {
return
}
const touchDistanceX = Math.abs(this.touchendX - this.touchstartX)
const touchDistanceY = Math.abs(this.touchendY - this.touchstartY)
2023-06-17 17:34:08 -05:00
const touchDistance = Math.sqrt(Math.pow(this.touchstartX - this.touchendX, 2) + Math.pow(this.touchstartY - this.touchendY, 2))
if (touchDistance < 30) {
if (this.showSettingsModal) {
this.showSettingsModal = false
} else {
this.toggleToolbar()
}
2023-03-06 10:58:08 -06:00
return
}
2022-12-06 17:07:27 -06:00
2023-06-17 17:34:08 -05:00
if (touchDistanceX < 60 || touchDistanceY > touchDistanceX) {
return
}
this.hideToolbar()
if (!this.isEpub) {
if (this.touchendX < this.touchstartX) {
this.next()
}
if (this.touchendX > this.touchstartX) {
this.prev()
}
}
2021-10-17 20:20:00 -05:00
},
2023-06-17 17:34:08 -05:00
showToolbar() {
this.showingToolbar = true
this.$showHideStatusBar(true)
2023-06-17 17:34:08 -05:00
},
hideToolbar() {
this.showingToolbar = false
this.$showHideStatusBar(false)
2023-06-17 17:34:08 -05:00
},
toggleToolbar() {
if (this.showingToolbar) this.hideToolbar()
else this.showToolbar()
},
2021-10-17 20:20:00 -05:00
touchstart(e) {
2023-06-17 17:34:08 -05:00
// Ignore rapid touch
if (this.touchstartTime && Date.now() - this.touchstartTime < 250) {
return
}
2023-03-06 10:58:08 -06:00
this.touchstartX = e.touches[0].screenX
this.touchstartY = e.touches[0].screenY
2022-12-06 17:07:27 -06:00
this.touchstartTime = Date.now()
2023-06-17 17:34:08 -05:00
this.touchIdentifier = e.touches[0].identifier
2021-10-17 20:20:00 -05:00
},
touchend(e) {
2023-06-17 17:34:08 -05:00
if (this.touchIdentifier !== e.changedTouches[0].identifier) {
return
}
2021-10-17 20:20:00 -05:00
this.touchendX = e.changedTouches[0].screenX
2022-12-06 17:07:27 -06:00
this.touchendY = e.changedTouches[0].screenY
2021-10-17 20:20:00 -05:00
this.handleGesture()
},
2023-06-04 15:38:26 -05:00
closeEvt() {
this.show = false
},
loadEreaderSettings() {
try {
const settings = localStorage.getItem('ereaderSettings')
if (settings) {
this.ereaderSettings = JSON.parse(settings)
this.settingsUpdated()
}
} catch (error) {
console.error('Failed to load ereader settings', error)
}
},
2021-10-17 20:20:00 -05:00
registerListeners() {
2023-06-04 15:38:26 -05:00
this.$eventBus.$on('close-ebook', this.closeEvt)
2021-10-17 20:20:00 -05:00
document.body.addEventListener('touchstart', this.touchstart)
document.body.addEventListener('touchend', this.touchend)
},
unregisterListeners() {
2023-06-04 15:38:26 -05:00
this.$eventBus.$on('close-ebook', this.closeEvt)
2021-10-17 20:20:00 -05:00
document.body.removeEventListener('touchstart', this.touchstart)
document.body.removeEventListener('touchend', this.touchend)
}
},
beforeDestroy() {
this.unregisterListeners()
}
}
</script>