mirror of
https://github.com/janeczku/calibre-web
synced 2025-12-12 03:08:05 +00:00
The code in epub-progress.js was causing the epub file to be loaded twice over the network, because it created a second instance of the epub.js library. I moved all the progress percentage display code into epub.js. Only the locationchange polyfill code was left in the separate file, renamed to locationchange-polyfill.js. I rewrote the percentage progress code to use epub.js events, giving more succinct and readable code. Also, I added localStorage caching of the epub location calculations, required for displaying percentage progress, which can take up to 60-90 seconds to calculate on longer ebooks. This localStorage caching approach is recommended by epub.js in the `locations.html` example in their repo. Signed-off-by: Ross Williams <ross@ross-williams.net>
111 lines
3.6 KiB
JavaScript
111 lines
3.6 KiB
JavaScript
/* global $, calibre, EPUBJS, ePubReader */
|
|
|
|
var reader;
|
|
|
|
(function() {
|
|
"use strict";
|
|
|
|
EPUBJS.filePath = calibre.filePath;
|
|
EPUBJS.cssPath = calibre.cssPath;
|
|
|
|
reader = ePubReader(calibre.bookUrl, {
|
|
restore: true,
|
|
bookmarks: calibre.bookmark ? [calibre.bookmark] : []
|
|
});
|
|
|
|
Object.keys(themes).forEach(function (theme) {
|
|
reader.rendition.themes.register(theme, themes[theme].css_path);
|
|
});
|
|
|
|
if (calibre.useBookmarks) {
|
|
reader.on("reader:bookmarked", updateBookmark.bind(reader, "add"));
|
|
reader.on("reader:unbookmarked", updateBookmark.bind(reader, "remove"));
|
|
} else {
|
|
$("#bookmark, #show-Bookmarks").remove();
|
|
}
|
|
|
|
// Enable swipe support
|
|
// I have no idea why swiperRight/swiperLeft from plugins is not working, events just don't get fired
|
|
var touchStart = 0;
|
|
var touchEnd = 0;
|
|
|
|
reader.rendition.on('touchstart', function(event) {
|
|
touchStart = event.changedTouches[0].screenX;
|
|
});
|
|
reader.rendition.on('touchend', function(event) {
|
|
touchEnd = event.changedTouches[0].screenX;
|
|
if (touchStart < touchEnd) {
|
|
if(reader.book.package.metadata.direction === "rtl") {
|
|
reader.rendition.next();
|
|
} else {
|
|
reader.rendition.prev();
|
|
}
|
|
// Swiped Right
|
|
}
|
|
if (touchStart > touchEnd) {
|
|
if(reader.book.package.metadata.direction === "rtl") {
|
|
reader.rendition.prev();
|
|
} else {
|
|
reader.rendition.next();
|
|
}
|
|
// Swiped Left
|
|
}
|
|
});
|
|
|
|
// Update progress percentage
|
|
let progressDiv = document.getElementById("progress");
|
|
reader.book.ready.then((()=>{
|
|
let locations_key = reader.book.key()+'-locations';
|
|
let stored_locations = localStorage.getItem(locations_key);
|
|
let make_locations, save_locations;
|
|
if (stored_locations) {
|
|
make_locations = Promise.resolve(reader.book.locations.load(stored_locations));
|
|
// No-op because locations are already saved
|
|
save_locations = ()=>{};
|
|
} else {
|
|
make_locations = reader.book.locations.generate();
|
|
save_locations = ()=>{
|
|
localStorage.setItem(locations_key, reader.book.locations.save());
|
|
};
|
|
}
|
|
make_locations.then(()=>{
|
|
reader.rendition.on('relocated', (location)=>{
|
|
let percentage = Math.round(location.end.percentage*100);
|
|
progressDiv.textContent=percentage+"%";
|
|
});
|
|
reader.rendition.reportLocation();
|
|
progressDiv.style.visibility = "visible";
|
|
}).then(save_locations);
|
|
}));
|
|
|
|
/**
|
|
* @param {string} action - Add or remove bookmark
|
|
* @param {string|int} location - Location or zero
|
|
*/
|
|
function updateBookmark(action, location) {
|
|
// Remove other bookmarks (there can only be one)
|
|
if (action === "add") {
|
|
this.settings.bookmarks.filter(function (bookmark) {
|
|
return bookmark && bookmark !== location;
|
|
}).map(function (bookmark) {
|
|
this.removeBookmark(bookmark);
|
|
}.bind(this));
|
|
}
|
|
|
|
var csrftoken = $("input[name='csrf_token']").val();
|
|
|
|
// Save to database
|
|
$.ajax(calibre.bookmarkUrl, {
|
|
method: "post",
|
|
data: { bookmark: location || "" },
|
|
headers: { "X-CSRFToken": csrftoken }
|
|
}).fail(function (xhr, status, error) {
|
|
alert(error);
|
|
});
|
|
}
|
|
|
|
// Default settings load
|
|
const theme = localStorage.getItem("calibre.reader.theme") ?? "lightTheme";
|
|
selectTheme(theme);
|
|
})();
|