audiobookshelf/client/components/readers/EpubReader.vue

125 lines
3.7 KiB
Vue
Raw Normal View History

<template>
<div class="h-full w-full">
<div class="h-full flex items-center">
<div style="width: 100px; max-width: 100px" class="h-full flex items-center overflow-x-hidden justify-center">
2023-03-21 13:27:21 +01:00
<span id="prev"
class="material-icons text-white text-opacity-50 hover:text-opacity-80 cursor-pointer text-6xl"
@mousedown.prevent @click="prev">chevron_left</span>
</div>
2023-03-21 13:27:21 +01:00
<div id="frame" class="w-full" style="height: 90%">
<div id="viewer" class="border border-gray-100 bg-white shadow-md"></div>
</div>
<div style="width: 100px; max-width: 100px" class="h-full flex items-center justify-center overflow-x-hidden">
2023-03-21 13:27:21 +01:00
<span id="next" class="material-icons text-white text-opacity-50 hover:text-opacity-80 cursor-pointer text-6xl"
@mousedown.prevent @click="next">chevron_right</span>
</div>
</div>
</div>
</template>
<script>
2023-03-21 13:27:21 +01:00
import ePub from "epubjs";
2023-03-21 13:27:21 +01:00
/**
* @typedef {EpubReader}
* @property {ePub.Book} book
* @property {ePub.Rendition} rendition
*/
export default {
props: {
2023-03-21 13:27:21 +01:00
url: String,
libraryItem: {
type: Object,
default: () => {}
}
},
data() {
return {
2023-03-21 13:27:21 +01:00
/** @type {ePub.Book} */
book: null,
2023-03-21 13:27:21 +01:00
/** @type {ePub.Rendition} */
rendition: null,
2023-03-21 13:27:21 +01:00
};
},
2023-03-21 13:27:21 +01:00
computed: {
libraryItemId() { return this.libraryItem ? this.libraryItem.id : null },
hasPrev() { return !this.rendition?.location.atStart },
hasNext() { return !this.rendition?.location.atEnd },
chapters() { return this.book ? this.book.navigation.toc : [] },
title() { return this.book ? this.book.metadata.title : "" },
author() { return this.book ? this.book.metadata.creator : "" },
userMediaProgress() {
if (!this.libraryItemId) return
return this.$store.getters['user/getUserMediaProgress'](this.libraryItemId)
},
2023-03-21 13:27:21 +01:00
},
methods: {
changedChapter() { this.rendition?.display(this.selectedChapter) },
prev() { this.rendition?.prev() },
next() { this.rendition?.next() },
keyUp(e) {
if ((e.keyCode || e.which) == 37) {
2023-03-21 13:27:21 +01:00
this.prev();
} else if ((e.keyCode || e.which) == 39) {
2023-03-21 13:27:21 +01:00
this.next();
}
},
2023-03-21 13:27:21 +01:00
relocated(location) {
var cfi = location.start.cfi;
var cfiFragment = "#" + cfi;
2023-03-21 13:27:21 +01:00
if(window.location.hash != cfiFragment) {
const url = new URL(window.location);
url.hash = cfiFragment;
history.pushState({}, '', url);
var updatePayload = {
currentTime: cfi,
}
2023-03-21 13:27:21 +01:00
var percentage = this.book.locations.percentageFromCfi(cfi);
if (percentage) {
updatePayload.progress = percentage
}
2023-03-21 13:27:21 +01:00
this.$axios.$patch(`/api/me/progress/${this.libraryItemId}`, updatePayload).catch((error) => {
console.error('Failed', error)
})
2023-03-21 13:27:21 +01:00
}
},
initEpub(cfi) {
var reader = this;
2023-03-21 13:27:21 +01:00
/** @type {ePub.Book} */
reader.book = new ePub(reader.url, {
storage: false,
worker: false,
manager: "continuous",
flow: "scrolled",
spreads: false,
width: window.innerWidth - 200,
height: window.innerHeight - 50,
});
2023-03-21 13:27:21 +01:00
/** @type {ePub.Rendition} */
reader.rendition = reader.book.renderTo("viewer", {
width: window.innerWidth - 200,
height: window.innerHeight * 0.9
});
2023-03-21 13:27:21 +01:00
reader.rendition.display(cfi);
reader.book.ready.then(() => {
reader.rendition.on('relocated', reader.relocated);
reader.rendition.on('keydown', reader.keyUp)
document.addEventListener('keydown', reader.keyUp, false);
reader.book.locations.generate();
});
},
},
mounted() {
2023-03-21 13:27:21 +01:00
this.initEpub(this.userMediaProgress?.currentTime);
},
};
</script>