2021-10-17 20:20:00 -05:00
|
|
|
<template>
|
|
|
|
<div id="epub-frame" class="w-full">
|
2023-03-25 17:40:46 -05:00
|
|
|
<div id="viewer" class="h-full w-full"></div>
|
|
|
|
|
2023-06-01 17:19:16 -05:00
|
|
|
<div class="fixed left-0 h-8 w-full bg-primary px-2 flex items-center" :style="{ bottom: playerLibraryItemId ? '120px' : '0px' }">
|
2021-10-17 20:20:00 -05:00
|
|
|
<p class="text-xs">epub</p>
|
|
|
|
<div class="flex-grow" />
|
|
|
|
|
|
|
|
<p class="text-sm">{{ progress }}%</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import ePub from 'epubjs'
|
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
2023-03-25 17:40:46 -05:00
|
|
|
url: String,
|
|
|
|
libraryItem: {
|
|
|
|
type: Object,
|
|
|
|
default: () => {}
|
2023-05-21 15:02:49 -05:00
|
|
|
},
|
|
|
|
isLocal: Boolean
|
2021-10-17 20:20:00 -05:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2023-03-25 17:40:46 -05:00
|
|
|
/** @type {ePub.Book} */
|
2021-10-17 20:20:00 -05:00
|
|
|
book: null,
|
2023-03-25 17:40:46 -05:00
|
|
|
/** @type {ePub.Rendition} */
|
2021-10-17 20:20:00 -05:00
|
|
|
rendition: null,
|
2023-03-25 17:40:46 -05:00
|
|
|
progress: 0
|
2021-10-17 20:20:00 -05:00
|
|
|
}
|
|
|
|
},
|
2022-12-04 08:29:55 -06:00
|
|
|
watch: {
|
|
|
|
playerLibraryItemId() {
|
|
|
|
this.updateHeight()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2023-03-25 17:40:46 -05:00
|
|
|
/** @returns {string} */
|
|
|
|
libraryItemId() {
|
|
|
|
return this.libraryItem?.id
|
|
|
|
},
|
2023-05-21 15:02:49 -05:00
|
|
|
localLibraryItem() {
|
|
|
|
if (this.isLocal) return this.libraryItem
|
|
|
|
return this.libraryItem.localLibraryItem || null
|
|
|
|
},
|
|
|
|
localLibraryItemId() {
|
|
|
|
return this.localLibraryItem?.id
|
|
|
|
},
|
|
|
|
serverLibraryItemId() {
|
|
|
|
if (!this.isLocal) return this.libraryItem.id
|
|
|
|
// Check if local library item is connected to the current server
|
|
|
|
if (!this.libraryItem.serverAddress || !this.libraryItem.libraryItemId) return null
|
|
|
|
if (this.$store.getters['user/getServerAddress'] === this.libraryItem.serverAddress) {
|
|
|
|
return this.libraryItem.libraryItemId
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
},
|
2022-12-04 08:29:55 -06:00
|
|
|
playerLibraryItemId() {
|
|
|
|
return this.$store.state.playerLibraryItemId
|
|
|
|
},
|
|
|
|
readerHeightOffset() {
|
2023-03-06 11:40:00 -06:00
|
|
|
return this.playerLibraryItemId ? 196 : 96
|
2023-03-25 17:40:46 -05:00
|
|
|
},
|
|
|
|
/** @returns {Array<ePub.NavItem>} */
|
|
|
|
chapters() {
|
|
|
|
return this.book ? this.book.navigation.toc : []
|
|
|
|
},
|
2023-05-21 15:02:49 -05:00
|
|
|
userItemProgress() {
|
|
|
|
if (this.isLocal) return this.localItemProgress
|
|
|
|
return this.serverItemProgress
|
|
|
|
},
|
|
|
|
localItemProgress() {
|
|
|
|
return this.$store.getters['globals/getLocalMediaProgressById'](this.localLibraryItemId)
|
|
|
|
},
|
|
|
|
serverItemProgress() {
|
|
|
|
return this.$store.getters['user/getUserMediaProgress'](this.serverLibraryItemId)
|
2023-03-25 17:40:46 -05:00
|
|
|
},
|
|
|
|
localStorageLocationsKey() {
|
|
|
|
return `ebookLocations-${this.libraryItemId}`
|
2022-12-04 08:29:55 -06:00
|
|
|
}
|
|
|
|
},
|
2021-10-17 20:20:00 -05:00
|
|
|
methods: {
|
2022-12-04 08:29:55 -06:00
|
|
|
updateHeight() {
|
|
|
|
if (this.rendition && this.rendition.resize) {
|
|
|
|
this.rendition.resize(window.innerWidth, window.innerHeight - this.readerHeightOffset)
|
|
|
|
}
|
|
|
|
},
|
2021-10-17 20:20:00 -05:00
|
|
|
prev() {
|
|
|
|
if (this.rendition) {
|
|
|
|
this.rendition.prev()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
next() {
|
|
|
|
if (this.rendition) {
|
|
|
|
this.rendition.next()
|
|
|
|
}
|
|
|
|
},
|
2023-03-25 17:40:46 -05:00
|
|
|
/**
|
|
|
|
* @param {object} payload
|
|
|
|
* @param {string} payload.ebookLocation - CFI of the current location
|
|
|
|
* @param {string} payload.ebookProgress - eBook Progress Percentage
|
|
|
|
*/
|
2023-05-21 15:02:49 -05:00
|
|
|
async updateProgress(payload) {
|
|
|
|
// Update local item
|
|
|
|
if (this.localLibraryItemId) {
|
|
|
|
const localPayload = {
|
|
|
|
localLibraryItemId: this.localLibraryItemId,
|
|
|
|
...payload
|
|
|
|
}
|
|
|
|
const localResponse = await this.$db.updateLocalEbookProgress(localPayload)
|
|
|
|
if (localResponse.localMediaProgress) {
|
|
|
|
this.$store.commit('globals/updateLocalMediaProgress', localResponse.localMediaProgress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update server item
|
|
|
|
if (this.serverLibraryItemId) {
|
|
|
|
this.$axios.$patch(`/api/me/progress/${this.serverLibraryItemId}`, payload).catch((error) => {
|
|
|
|
console.error('EpubReader.updateProgress failed:', error)
|
|
|
|
})
|
|
|
|
}
|
2023-03-25 17:40:46 -05:00
|
|
|
},
|
|
|
|
getAllEbookLocationData() {
|
|
|
|
const locations = []
|
|
|
|
let totalSize = 0 // Total in bytes
|
|
|
|
|
|
|
|
for (const key in localStorage) {
|
|
|
|
if (!localStorage.hasOwnProperty(key) || !key.startsWith('ebookLocations-')) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const ebookLocations = JSON.parse(localStorage[key])
|
|
|
|
if (!ebookLocations.locations) throw new Error('Invalid locations object')
|
|
|
|
|
|
|
|
ebookLocations.key = key
|
|
|
|
ebookLocations.size = (localStorage[key].length + key.length) * 2
|
|
|
|
locations.push(ebookLocations)
|
|
|
|
totalSize += ebookLocations.size
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to parse ebook locations', key, error)
|
|
|
|
localStorage.removeItem(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by oldest lastAccessed first
|
|
|
|
locations.sort((a, b) => a.lastAccessed - b.lastAccessed)
|
|
|
|
|
|
|
|
return {
|
|
|
|
locations,
|
|
|
|
totalSize
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/** @param {string} locationString */
|
|
|
|
checkSaveLocations(locationString) {
|
|
|
|
const maxSizeInBytes = 3000000 // Allow epub locations to take up to 3MB of space
|
|
|
|
const newLocationsSize = JSON.stringify({ lastAccessed: Date.now(), locations: locationString }).length * 2
|
|
|
|
|
|
|
|
// Too large overall
|
|
|
|
if (newLocationsSize > maxSizeInBytes) {
|
|
|
|
console.error('Epub locations are too large to store. Size =', newLocationsSize)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const ebookLocationsData = this.getAllEbookLocationData()
|
|
|
|
|
|
|
|
let availableSpace = maxSizeInBytes - ebookLocationsData.totalSize
|
|
|
|
|
|
|
|
// Remove epub locations until there is room for locations
|
|
|
|
while (availableSpace < newLocationsSize && ebookLocationsData.locations.length) {
|
|
|
|
const oldestLocation = ebookLocationsData.locations.shift()
|
|
|
|
console.log(`Removing cached locations for epub "${oldestLocation.key}" taking up ${oldestLocation.size} bytes`)
|
|
|
|
availableSpace += oldestLocation.size
|
|
|
|
localStorage.removeItem(oldestLocation.key)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Cacheing epub locations with key "${this.localStorageLocationsKey}" taking up ${newLocationsSize} bytes`)
|
|
|
|
this.saveLocations(locationString)
|
|
|
|
},
|
|
|
|
/** @param {string} locationString */
|
|
|
|
saveLocations(locationString) {
|
|
|
|
localStorage.setItem(
|
|
|
|
this.localStorageLocationsKey,
|
|
|
|
JSON.stringify({
|
|
|
|
lastAccessed: Date.now(),
|
|
|
|
locations: locationString
|
|
|
|
})
|
|
|
|
)
|
|
|
|
},
|
|
|
|
loadLocations() {
|
|
|
|
const locationsObjString = localStorage.getItem(this.localStorageLocationsKey)
|
|
|
|
if (!locationsObjString) return null
|
|
|
|
|
|
|
|
const locationsObject = JSON.parse(locationsObjString)
|
|
|
|
|
|
|
|
// Remove invalid location objects
|
|
|
|
if (!locationsObject.locations) {
|
|
|
|
console.error('Invalid epub locations stored', this.localStorageLocationsKey)
|
|
|
|
localStorage.removeItem(this.localStorageLocationsKey)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update lastAccessed
|
|
|
|
this.saveLocations(locationsObject.locations)
|
|
|
|
|
|
|
|
return locationsObject.locations
|
|
|
|
},
|
|
|
|
/** @param {string} location - CFI of the new location */
|
|
|
|
relocated(location) {
|
2023-05-21 15:02:49 -05:00
|
|
|
if (this.userItemProgress?.ebookLocation === location.start.cfi) {
|
2023-03-26 13:50:21 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:40:46 -05:00
|
|
|
if (location.end.percentage) {
|
|
|
|
this.updateProgress({
|
|
|
|
ebookLocation: location.start.cfi,
|
|
|
|
ebookProgress: location.end.percentage
|
|
|
|
})
|
|
|
|
this.progress = Math.round(location.end.percentage * 100)
|
|
|
|
} else {
|
|
|
|
this.updateProgress({
|
|
|
|
ebookLocation: location.start.cfi
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2021-10-17 20:20:00 -05:00
|
|
|
initEpub() {
|
2023-05-21 15:02:49 -05:00
|
|
|
this.progress = Math.round((this.userItemProgress?.ebookProgress || 0) * 100)
|
2023-03-25 17:40:46 -05:00
|
|
|
|
|
|
|
/** @type {EpubReader} */
|
|
|
|
const reader = this
|
2021-10-17 20:20:00 -05:00
|
|
|
|
2023-03-25 17:40:46 -05:00
|
|
|
/** @type {ePub.Book} */
|
|
|
|
reader.book = new ePub(reader.url, {
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight - this.readerHeightOffset
|
|
|
|
})
|
|
|
|
|
|
|
|
/** @type {ePub.Rendition} */
|
|
|
|
reader.rendition = reader.book.renderTo('viewer', {
|
2021-10-17 20:20:00 -05:00
|
|
|
width: window.innerWidth,
|
2022-12-04 08:29:55 -06:00
|
|
|
height: window.innerHeight - this.readerHeightOffset,
|
2021-10-17 20:20:00 -05:00
|
|
|
snap: true,
|
|
|
|
manager: 'continuous',
|
|
|
|
flow: 'paginated'
|
|
|
|
})
|
|
|
|
|
2023-03-25 17:40:46 -05:00
|
|
|
// load saved progress
|
2023-05-21 15:02:49 -05:00
|
|
|
reader.rendition.display(this.userItemProgress?.ebookLocation || reader.book.locations.start)
|
2021-10-17 20:20:00 -05:00
|
|
|
|
2023-03-25 17:40:46 -05:00
|
|
|
// load style
|
2023-06-01 17:19:16 -05:00
|
|
|
reader.rendition.themes.default({ '*': { color: '#fff!important', 'background-color': 'rgb(35 35 35)!important' } })
|
2023-03-25 17:40:46 -05:00
|
|
|
|
|
|
|
reader.book.ready.then(() => {
|
|
|
|
// set up event listeners
|
|
|
|
reader.rendition.on('relocated', reader.relocated)
|
|
|
|
|
|
|
|
// load ebook cfi locations
|
|
|
|
const savedLocations = this.loadLocations()
|
|
|
|
if (savedLocations) {
|
|
|
|
reader.book.locations.load(savedLocations)
|
|
|
|
} else {
|
|
|
|
reader.book.locations.generate().then(() => {
|
|
|
|
this.checkSaveLocations(reader.book.locations.save())
|
|
|
|
})
|
|
|
|
}
|
2021-10-17 20:20:00 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2023-03-25 17:40:46 -05:00
|
|
|
beforeDestroy() {
|
|
|
|
this.book?.destroy()
|
|
|
|
},
|
2021-10-17 20:20:00 -05:00
|
|
|
mounted() {
|
|
|
|
this.initEpub()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
#epub-frame {
|
2023-03-06 11:40:00 -06:00
|
|
|
height: calc(100% - 32px);
|
|
|
|
max-height: calc(100% - 32px);
|
2021-10-17 20:20:00 -05:00
|
|
|
overflow: hidden;
|
|
|
|
}
|
2022-12-04 08:29:55 -06:00
|
|
|
.reader-player-open #epub-frame {
|
2023-03-06 11:40:00 -06:00
|
|
|
height: calc(100% - 132px);
|
|
|
|
max-height: calc(100% - 132px);
|
2022-12-04 08:29:55 -06:00
|
|
|
overflow: hidden;
|
|
|
|
}
|
2021-10-17 20:20:00 -05:00
|
|
|
</style>
|