diff options
Diffstat (limited to 'js/reader.js')
-rw-r--r-- | js/reader.js | 131 |
1 files changed, 75 insertions, 56 deletions
diff --git a/js/reader.js b/js/reader.js index 4540664..ca95607 100644 --- a/js/reader.js +++ b/js/reader.js @@ -29,7 +29,6 @@ Promise.allSettled = Promise.allSettled || ((promises) => Promise.all( const Reader = { csrf_token: "", last_stored_cfi: "", - prevent_lastread_update: false, hyphenate: {}, init: function() { this.csrf_token = Cookie.get('epube_csrf_token'); @@ -316,7 +315,7 @@ const Reader = { contents.on("linkClicked", (href) => { console.log('linkClicked', href); - Reader.prevent_lastread_update = true; + Reader.Page.blockLastReadUpdate(); if (href.indexOf("://") == -1) { $(".prev_location_btn") @@ -409,58 +408,7 @@ const Reader = { } }); - rendition.on('relocated', async (location) => { - - // locations not generated yet - if (book.locations.length() == 0) - return; - - const column_hacks = await localforage.getItem("epube.enable-column-hacks"); - - if (column_hacks && Reader.Page._moved_next >= 20) { - console.log('forcing re-render because of column hacks'); - rendition.onResized($("#reader").width()); - Reader.Page._moved_next = 0; - } - - const currentCfi = location.start.cfi; - const currentPct = parseInt(book.locations.percentageFromCfi(currentCfi) * 100); - - $("#cur_page").text(Math.floor(location.start.location / LOCATION_DIVISOR)); - $("#total_pages").text(Math.floor(book.locations.length() / LOCATION_DIVISOR)); - $("#page_pct").text(parseInt(book.locations.percentageFromCfi(currentCfi)*100) + '%'); - - Reader.updateTocBarPosition(book, location); - - const displayed = location.start.displayed; - - if (displayed) { - $("#chapter_cur_page").text(displayed.page); - $("#chapter_total_pages").text(displayed.total); - - if (displayed.total > 0) - $("#chapter_pct").text(parseInt(displayed.page / displayed.total * 100) + '%') - } - - if (Reader.last_stored_cfi != currentCfi && !Reader.prevent_lastread_update) { - const lastread_timestamp = new Date().getTime(); - - console.log("storing lastread", currentPct, currentCfi, lastread_timestamp); - - await localforage.setItem(Reader.cacheId("lastread"), - {cfi: currentCfi, page: currentPct, total: 100, timestamp: lastread_timestamp}); - - if (App.isOnline()) { - console.log("updating remote lastread", currentCfi); - Reader.last_stored_cfi = currentCfi; - - $.post("backend.php", { op: "storelastread", id: $.urlParam("id"), page: currentPct, - cfi: currentCfi, timestamp: lastread_timestamp }); - } - } - - Reader.prevent_lastread_update = false; - }); + rendition.on('relocated', (location) => Reader.Page.onRelocated(book, location)); }, initModals: function() { $('#settings-modal').on('shown.bs.modal', async function() { @@ -1265,6 +1213,8 @@ const Reader = { }, Page: { _moved_next: 0, + _prevent_lastread_timeout: false, + _prevent_lastread: false, next: function() { window.book.rendition.next(); @@ -1295,6 +1245,71 @@ const Reader = { $(elem).fadeOut(); }, + blockLastReadUpdate: function() { + console.log('blocking last read updates...'); + + if (Reader.Page._prevent_lastread_timeout) + window.clearTimeout(Reader.Page._prevent_lastread_timeout); + + Reader.Page._prevent_lastread_timeout = window.setTimeout(() => { + Reader.Page._prevent_lastread = false; + Reader.Page._prevent_lastread_timeout = false; + console.log('unblocked last read updates'); + }, 250); + + Reader.Page._prevent_lastread = true; + }, + onRelocated: async function (book, location) { + const rendition = book.rendition; + + // locations not generated yet + if (book.locations.length() == 0) + return; + + const column_hacks = await localforage.getItem("epube.enable-column-hacks"); + + if (column_hacks && Reader.Page._moved_next >= 20) { + console.log('forcing re-render because of column hacks'); + rendition.onResized($("#reader").width()); + Reader.Page._moved_next = 0; + } + + const currentCfi = location.start.cfi; + const currentPct = parseInt(book.locations.percentageFromCfi(currentCfi) * 100); + + $("#cur_page").text(Math.floor(location.start.location / LOCATION_DIVISOR)); + $("#total_pages").text(Math.floor(book.locations.length() / LOCATION_DIVISOR)); + $("#page_pct").text(parseInt(book.locations.percentageFromCfi(currentCfi)*100) + '%'); + + Reader.updateTocBarPosition(book, location); + + const displayed = location.start.displayed; + + if (displayed) { + $("#chapter_cur_page").text(displayed.page); + $("#chapter_total_pages").text(displayed.total); + + if (displayed.total > 0) + $("#chapter_pct").text(parseInt(displayed.page / displayed.total * 100) + '%') + } + + if (Reader.last_stored_cfi != currentCfi && !Reader.Page._prevent_lastread) { + const lastread_timestamp = new Date().getTime(); + + console.log("storing lastread", currentPct, currentCfi, lastread_timestamp); + + await localforage.setItem(Reader.cacheId("lastread"), + {cfi: currentCfi, page: currentPct, total: 100, timestamp: lastread_timestamp}); + + if (App.isOnline()) { + console.log("updating remote lastread", currentCfi); + Reader.last_stored_cfi = currentCfi; + + $.post("backend.php", { op: "storelastread", id: $.urlParam("id"), page: currentPct, + cfi: currentCfi, timestamp: lastread_timestamp }); + } + } + }, clearLastRead: async function() { if (confirm("Clear stored last read location?")) { const total = window.book.locations.length(); @@ -1327,6 +1342,8 @@ const Reader = { if (lr_local && lr_local.cfi) { console.log('using local lastread cfi', lr_local.cfi); try { + Reader.Page.blockLastReadUpdate(); + await rendition.display(lr_local.cfi); location_found = true; @@ -1339,11 +1356,13 @@ const Reader = { try { const lr_remote = await $.post("backend.php", { op: "getlastread", id: $.urlParam("id") }); - console.log('got remote lastread', lr_remote, lr_local); + console.log('got remote lastread', lr_remote); if (lr_remote) { if (lr_remote.cfi && lr_local.cfi != lr_remote.cfi && (!lr_local.timestamp || lr_remote.timestamp <= lr_local.timestamp)) { - console.log('using remote lastread (timestamp is newer or local timestamp is missing)'); + console.log(`using remote lastread cfi ${lr_remote.cfi} (timestamp is newer or local timestamp is missing)`); + + Reader.Page.blockLastReadUpdate(); await localforage.setItem(Reader.cacheId("lastread"), {cfi: lr_remote.cfi, page: lr_remote.page, total: lr_remote.total, timestamp: lr_remote.timestamp}); |