2021-10-18 01:05:43 +02:00
|
|
|
<template>
|
2023-05-27 18:58:01 +02:00
|
|
|
<div id="epub-reader" class="h-full w-full">
|
2023-03-26 21:44:59 +02:00
|
|
|
<div class="h-full flex items-center justify-center">
|
2023-06-15 00:30:08 +02:00
|
|
|
<button type="button" aria-label="Previous page" class="w-24 max-w-24 h-full hidden sm:flex items-center overflow-x-hidden justify-center opacity-50 hover:opacity-100">
|
|
|
|
<span v-if="hasPrev" class="material-icons text-6xl" @mousedown.prevent @click="prev">chevron_left</span>
|
|
|
|
</button>
|
2023-03-21 13:36:06 +01:00
|
|
|
<div id="frame" class="w-full" style="height: 80%">
|
2023-03-24 23:57:41 +01:00
|
|
|
<div id="viewer"></div>
|
2021-10-18 01:05:43 +02:00
|
|
|
</div>
|
2023-06-15 00:30:08 +02:00
|
|
|
<button type="button" aria-label="Next page" class="w-24 max-w-24 h-full hidden sm:flex items-center justify-center overflow-x-hidden opacity-50 hover:opacity-100">
|
|
|
|
<span v-if="hasNext" class="material-icons text-6xl" @mousedown.prevent @click="next">chevron_right</span>
|
|
|
|
</button>
|
2021-10-18 01:05:43 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-03-24 23:57:41 +01:00
|
|
|
import ePub from 'epubjs'
|
2021-10-18 01:05:43 +02:00
|
|
|
|
2023-03-24 23:57:41 +01:00
|
|
|
/**
|
2023-03-22 11:16:01 +01:00
|
|
|
* @typedef {object} EpubReader
|
2023-03-21 13:27:21 +01:00
|
|
|
* @property {ePub.Book} book
|
|
|
|
* @property {ePub.Rendition} rendition
|
|
|
|
*/
|
2021-10-18 01:05:43 +02:00
|
|
|
export default {
|
|
|
|
props: {
|
2023-03-21 13:27:21 +01:00
|
|
|
libraryItem: {
|
|
|
|
type: Object,
|
2023-03-24 23:57:41 +01:00
|
|
|
default: () => {}
|
2023-05-27 18:58:01 +02:00
|
|
|
},
|
2023-06-10 19:46:57 +02:00
|
|
|
playerOpen: Boolean,
|
|
|
|
keepProgress: Boolean,
|
|
|
|
fileId: String
|
2021-10-18 01:05:43 +02:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2023-03-26 21:44:59 +02:00
|
|
|
windowWidth: 0,
|
2023-05-27 18:58:01 +02:00
|
|
|
windowHeight: 0,
|
2023-03-21 13:27:21 +01:00
|
|
|
/** @type {ePub.Book} */
|
2021-10-18 01:05:43 +02:00
|
|
|
book: null,
|
2023-03-21 13:27:21 +01:00
|
|
|
/** @type {ePub.Rendition} */
|
2023-06-15 00:30:08 +02:00
|
|
|
rendition: null,
|
|
|
|
ereaderSettings: {
|
|
|
|
theme: 'dark',
|
|
|
|
fontScale: 100,
|
|
|
|
lineSpacing: 115,
|
|
|
|
spread: 'auto'
|
|
|
|
}
|
2023-03-24 23:57:41 +01:00
|
|
|
}
|
2021-10-18 01:05:43 +02:00
|
|
|
},
|
2023-05-27 18:58:01 +02:00
|
|
|
watch: {
|
|
|
|
playerOpen() {
|
|
|
|
this.resize()
|
|
|
|
}
|
|
|
|
},
|
2023-03-21 13:27:21 +01:00
|
|
|
computed: {
|
2023-05-28 17:47:28 +02:00
|
|
|
userToken() {
|
|
|
|
return this.$store.getters['user/getToken']
|
|
|
|
},
|
2023-03-22 11:16:01 +01:00
|
|
|
/** @returns {string} */
|
2023-03-24 23:57:41 +01:00
|
|
|
libraryItemId() {
|
|
|
|
return this.libraryItem?.id
|
|
|
|
},
|
|
|
|
hasPrev() {
|
|
|
|
return !this.rendition?.location?.atStart
|
|
|
|
},
|
|
|
|
hasNext() {
|
|
|
|
return !this.rendition?.location?.atEnd
|
|
|
|
},
|
2023-03-22 11:16:01 +01:00
|
|
|
/** @returns {Array<ePub.NavItem>} */
|
2023-03-24 23:57:41 +01:00
|
|
|
chapters() {
|
2023-06-18 19:56:32 +02:00
|
|
|
return this.book?.navigation?.toc || []
|
2023-03-24 23:57:41 +01:00
|
|
|
},
|
2023-03-21 13:27:21 +01:00
|
|
|
userMediaProgress() {
|
|
|
|
if (!this.libraryItemId) return
|
|
|
|
return this.$store.getters['user/getUserMediaProgress'](this.libraryItemId)
|
2021-10-18 01:05:43 +02:00
|
|
|
},
|
2023-06-06 23:53:11 +02:00
|
|
|
savedEbookLocation() {
|
2023-06-10 19:46:57 +02:00
|
|
|
if (!this.keepProgress) return null
|
2023-06-06 23:53:11 +02:00
|
|
|
if (!this.userMediaProgress?.ebookLocation) return null
|
|
|
|
// Validate ebookLocation is an epubcfi
|
|
|
|
if (!String(this.userMediaProgress.ebookLocation).startsWith('epubcfi')) return null
|
|
|
|
return this.userMediaProgress.ebookLocation
|
|
|
|
},
|
2023-03-24 23:57:41 +01:00
|
|
|
localStorageLocationsKey() {
|
|
|
|
return `ebookLocations-${this.libraryItemId}`
|
2023-03-26 21:44:59 +02:00
|
|
|
},
|
|
|
|
readerWidth() {
|
|
|
|
if (this.windowWidth < 640) return this.windowWidth
|
|
|
|
return this.windowWidth - 200
|
2023-05-27 18:58:01 +02:00
|
|
|
},
|
|
|
|
readerHeight() {
|
|
|
|
if (this.windowHeight < 400 || !this.playerOpen) return this.windowHeight
|
|
|
|
return this.windowHeight - 164
|
2023-05-28 17:47:28 +02:00
|
|
|
},
|
2023-06-10 19:46:57 +02:00
|
|
|
ebookUrl() {
|
|
|
|
if (this.fileId) {
|
|
|
|
return `/api/items/${this.libraryItemId}/ebook/${this.fileId}`
|
|
|
|
}
|
2023-05-28 17:47:28 +02:00
|
|
|
return `/api/items/${this.libraryItemId}/ebook`
|
2023-06-15 00:30:08 +02:00
|
|
|
},
|
|
|
|
themeRules() {
|
|
|
|
const isDark = this.ereaderSettings.theme === 'dark'
|
|
|
|
const fontColor = isDark ? '#fff' : '#000'
|
|
|
|
const backgroundColor = isDark ? 'rgb(35 35 35)' : 'rgb(255, 255, 255)'
|
|
|
|
|
|
|
|
const lineSpacing = this.ereaderSettings.lineSpacing / 100
|
|
|
|
|
|
|
|
const fontScale = this.ereaderSettings.fontScale / 100
|
|
|
|
|
|
|
|
return {
|
|
|
|
'*': {
|
|
|
|
color: `${fontColor}!important`,
|
|
|
|
'background-color': `${backgroundColor}!important`,
|
|
|
|
'line-height': lineSpacing * fontScale + 'rem!important'
|
|
|
|
},
|
|
|
|
a: {
|
|
|
|
color: `${fontColor}!important`
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 23:57:41 +01:00
|
|
|
}
|
2023-03-21 13:27:21 +01:00
|
|
|
},
|
|
|
|
methods: {
|
2023-06-15 00:30:08 +02:00
|
|
|
updateSettings(settings) {
|
|
|
|
this.ereaderSettings = settings
|
|
|
|
|
|
|
|
if (!this.rendition) return
|
|
|
|
|
|
|
|
this.applyTheme()
|
|
|
|
|
|
|
|
const fontScale = settings.fontScale || 100
|
|
|
|
this.rendition.themes.fontSize(`${fontScale}%`)
|
|
|
|
this.rendition.spread(settings.spread || 'auto')
|
|
|
|
},
|
2023-03-24 23:57:41 +01:00
|
|
|
prev() {
|
|
|
|
return this.rendition?.prev()
|
|
|
|
},
|
|
|
|
next() {
|
|
|
|
return this.rendition?.next()
|
|
|
|
},
|
|
|
|
goToChapter(href) {
|
|
|
|
return this.rendition?.display(href)
|
|
|
|
},
|
2023-03-21 13:27:21 +01:00
|
|
|
keyUp(e) {
|
2023-03-22 11:16:01 +01:00
|
|
|
const rtl = this.book.package.metadata.direction === 'rtl'
|
2021-10-18 01:05:43 +02:00
|
|
|
if ((e.keyCode || e.which) == 37) {
|
2023-03-24 23:57:41 +01:00
|
|
|
return rtl ? this.next() : this.prev()
|
2021-10-18 01:05:43 +02:00
|
|
|
} else if ((e.keyCode || e.which) == 39) {
|
2023-03-24 23:57:41 +01:00
|
|
|
return rtl ? this.prev() : this.next()
|
2021-10-18 01:05:43 +02:00
|
|
|
}
|
|
|
|
},
|
2023-03-22 11:16:01 +01:00
|
|
|
/**
|
|
|
|
* @param {object} payload
|
|
|
|
* @param {string} payload.ebookLocation - CFI of the current location
|
2023-03-23 08:45:00 +01:00
|
|
|
* @param {string} payload.ebookProgress - eBook Progress Percentage
|
2023-03-22 11:16:01 +01:00
|
|
|
*/
|
|
|
|
updateProgress(payload) {
|
2023-06-10 19:46:57 +02:00
|
|
|
if (!this.keepProgress) return
|
2023-03-22 11:16:01 +01:00
|
|
|
this.$axios.$patch(`/api/me/progress/${this.libraryItemId}`, payload).catch((error) => {
|
|
|
|
console.error('EpubReader.updateProgress failed:', error)
|
|
|
|
})
|
|
|
|
},
|
2023-03-25 21:53:19 +01: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
|
|
|
|
}
|
|
|
|
},
|
2023-03-23 08:45:00 +01:00
|
|
|
/** @param {string} locationString */
|
2023-03-25 21:53:19 +01:00
|
|
|
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)
|
2023-03-23 08:45:00 +01:00
|
|
|
},
|
2023-03-25 21:53:19 +01:00
|
|
|
/** @param {string} locationString */
|
|
|
|
saveLocations(locationString) {
|
|
|
|
localStorage.setItem(
|
|
|
|
this.localStorageLocationsKey,
|
|
|
|
JSON.stringify({
|
|
|
|
lastAccessed: Date.now(),
|
|
|
|
locations: locationString
|
|
|
|
})
|
|
|
|
)
|
2023-03-23 08:45:00 +01:00
|
|
|
},
|
|
|
|
loadLocations() {
|
2023-03-25 21:53:19 +01:00
|
|
|
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
|
2023-03-23 08:45:00 +01:00
|
|
|
},
|
2023-03-22 11:16:01 +01:00
|
|
|
/** @param {string} location - CFI of the new location */
|
2023-03-21 13:27:21 +01:00
|
|
|
relocated(location) {
|
2023-06-06 23:53:11 +02:00
|
|
|
if (this.savedEbookLocation === location.start.cfi) {
|
2023-03-26 20:50:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-22 11:16:01 +01:00
|
|
|
if (location.end.percentage) {
|
|
|
|
this.updateProgress({
|
|
|
|
ebookLocation: location.start.cfi,
|
2023-03-24 23:57:41 +01:00
|
|
|
ebookProgress: location.end.percentage
|
|
|
|
})
|
2023-03-22 11:16:01 +01:00
|
|
|
} else {
|
|
|
|
this.updateProgress({
|
2023-03-24 23:57:41 +01:00
|
|
|
ebookLocation: location.start.cfi
|
|
|
|
})
|
2023-03-21 13:27:21 +01:00
|
|
|
}
|
|
|
|
},
|
2023-03-22 11:16:01 +01:00
|
|
|
initEpub() {
|
|
|
|
/** @type {EpubReader} */
|
2023-03-25 21:53:19 +01:00
|
|
|
const reader = this
|
2021-10-18 01:05:43 +02:00
|
|
|
|
2023-03-21 13:27:21 +01:00
|
|
|
/** @type {ePub.Book} */
|
2023-06-10 19:46:57 +02:00
|
|
|
reader.book = new ePub(reader.ebookUrl, {
|
2023-03-26 21:44:59 +02:00
|
|
|
width: this.readerWidth,
|
2023-05-28 17:47:28 +02:00
|
|
|
height: this.readerHeight - 50,
|
|
|
|
openAs: 'epub',
|
|
|
|
requestHeaders: {
|
|
|
|
Authorization: `Bearer ${this.userToken}`
|
|
|
|
}
|
2023-03-24 23:57:41 +01:00
|
|
|
})
|
2021-10-18 01:05:43 +02:00
|
|
|
|
2023-03-21 13:27:21 +01:00
|
|
|
/** @type {ePub.Rendition} */
|
2023-03-24 23:57:41 +01:00
|
|
|
reader.rendition = reader.book.renderTo('viewer', {
|
2023-03-26 21:44:59 +02:00
|
|
|
width: this.readerWidth,
|
2023-06-15 00:30:08 +02:00
|
|
|
height: this.readerHeight * 0.8,
|
2023-06-18 21:10:01 +02:00
|
|
|
spread: 'auto',
|
|
|
|
snap: true,
|
|
|
|
manager: 'continuous',
|
|
|
|
flow: 'paginated'
|
2023-03-24 23:57:41 +01:00
|
|
|
})
|
2021-10-18 01:05:43 +02:00
|
|
|
|
2023-03-22 11:16:01 +01:00
|
|
|
// load saved progress
|
2023-06-06 23:53:11 +02:00
|
|
|
reader.rendition.display(this.savedEbookLocation || reader.book.locations.start)
|
2023-03-22 11:16:01 +01:00
|
|
|
|
2023-06-15 00:30:08 +02:00
|
|
|
reader.rendition.on('rendered', () => {
|
|
|
|
this.applyTheme()
|
|
|
|
})
|
2023-03-22 11:16:01 +01:00
|
|
|
|
2023-03-21 13:27:21 +01:00
|
|
|
reader.book.ready.then(() => {
|
2023-03-22 11:16:01 +01:00
|
|
|
// set up event listeners
|
2023-03-24 23:57:41 +01:00
|
|
|
reader.rendition.on('relocated', reader.relocated)
|
2023-03-21 13:27:21 +01:00
|
|
|
reader.rendition.on('keydown', reader.keyUp)
|
|
|
|
|
2023-03-26 21:44:59 +02:00
|
|
|
reader.rendition.on('touchstart', (event) => {
|
2023-06-18 21:10:01 +02:00
|
|
|
this.$emit('touchstart', event)
|
2023-03-26 21:44:59 +02:00
|
|
|
})
|
|
|
|
reader.rendition.on('touchend', (event) => {
|
2023-06-18 21:10:01 +02:00
|
|
|
this.$emit('touchend', event)
|
2023-03-26 21:44:59 +02:00
|
|
|
})
|
|
|
|
|
2023-03-22 11:16:01 +01:00
|
|
|
// load ebook cfi locations
|
2023-03-25 21:53:19 +01:00
|
|
|
const savedLocations = this.loadLocations()
|
|
|
|
if (savedLocations) {
|
|
|
|
reader.book.locations.load(savedLocations)
|
2023-03-21 13:34:21 +01:00
|
|
|
} else {
|
|
|
|
reader.book.locations.generate().then(() => {
|
2023-03-25 21:53:19 +01:00
|
|
|
this.checkSaveLocations(reader.book.locations.save())
|
2023-03-24 23:57:41 +01:00
|
|
|
})
|
2023-03-21 13:34:21 +01:00
|
|
|
}
|
2023-03-24 23:57:41 +01:00
|
|
|
})
|
2023-03-26 21:44:59 +02:00
|
|
|
},
|
|
|
|
resize() {
|
|
|
|
this.windowWidth = window.innerWidth
|
2023-05-27 18:58:01 +02:00
|
|
|
this.windowHeight = window.innerHeight
|
|
|
|
this.rendition?.resize(this.readerWidth, this.readerHeight * 0.8)
|
2023-06-15 00:30:08 +02:00
|
|
|
},
|
|
|
|
applyTheme() {
|
|
|
|
if (!this.rendition) return
|
|
|
|
this.rendition.getContents().forEach((c) => {
|
|
|
|
c.addStylesheetRules(this.themeRules)
|
|
|
|
})
|
2023-03-24 23:57:41 +01:00
|
|
|
}
|
2021-10-18 01:05:43 +02:00
|
|
|
},
|
|
|
|
mounted() {
|
2023-03-26 21:44:59 +02:00
|
|
|
this.windowWidth = window.innerWidth
|
2023-05-27 18:58:01 +02:00
|
|
|
this.windowHeight = window.innerHeight
|
2023-03-26 21:44:59 +02:00
|
|
|
window.addEventListener('resize', this.resize)
|
2023-03-24 23:57:41 +01:00
|
|
|
this.initEpub()
|
2023-05-27 18:58:01 +02:00
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
window.removeEventListener('resize', this.resize)
|
|
|
|
this.book?.destroy()
|
2023-03-24 23:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-18 01:05:43 +02:00
|
|
|
</script>
|