mirror of
				https://github.com/janeczku/calibre-web
				synced 2025-11-04 09:13:02 +00:00 
			
		
		
		
	Refactored epub-reader progress
Progress in epubs now visible also in dark mode(s)
This commit is contained in:
		@@ -1,54 +0,0 @@
 | 
				
			|||||||
/**
 | 
					 | 
				
			||||||
 * waits until queue is finished, meaning the book is done loading
 | 
					 | 
				
			||||||
 * @param callback
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
function qFinished(callback){
 | 
					 | 
				
			||||||
    let timeout=setInterval(()=>{
 | 
					 | 
				
			||||||
        if(reader.rendition.q.running===undefined)
 | 
					 | 
				
			||||||
            clearInterval(timeout);
 | 
					 | 
				
			||||||
            callback();
 | 
					 | 
				
			||||||
        },300
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function calculateProgress(){
 | 
					 | 
				
			||||||
    let data=reader.rendition.location.end;
 | 
					 | 
				
			||||||
    return Math.round(epub.locations.percentageFromCfi(data.cfi)*100);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// register new event emitter locationchange that fires on urlchange
 | 
					 | 
				
			||||||
// source: https://stackoverflow.com/a/52809105/21941129
 | 
					 | 
				
			||||||
(() => {
 | 
					 | 
				
			||||||
    let oldPushState = history.pushState;
 | 
					 | 
				
			||||||
    history.pushState = function pushState() {
 | 
					 | 
				
			||||||
        let ret = oldPushState.apply(this, arguments);
 | 
					 | 
				
			||||||
        window.dispatchEvent(new Event('locationchange'));
 | 
					 | 
				
			||||||
        return ret;
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let oldReplaceState = history.replaceState;
 | 
					 | 
				
			||||||
    history.replaceState = function replaceState() {
 | 
					 | 
				
			||||||
        let ret = oldReplaceState.apply(this, arguments);
 | 
					 | 
				
			||||||
        window.dispatchEvent(new Event('locationchange'));
 | 
					 | 
				
			||||||
        return ret;
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    window.addEventListener('popstate', () => {
 | 
					 | 
				
			||||||
        window.dispatchEvent(new Event('locationchange'));
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
})();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
window.addEventListener('locationchange',()=>{
 | 
					 | 
				
			||||||
    let newPos=calculateProgress();
 | 
					 | 
				
			||||||
    progressDiv.textContent=newPos+"%";
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var epub=ePub(calibre.bookUrl)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
let progressDiv=document.getElementById("progress");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
qFinished(()=>{
 | 
					 | 
				
			||||||
    epub.locations.generate().then(()=> {
 | 
					 | 
				
			||||||
    window.dispatchEvent(new Event('locationchange'))
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
})
 | 
					 | 
				
			||||||
@@ -52,6 +52,32 @@ var reader;
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 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} action - Add or remove bookmark
 | 
				
			||||||
     * @param {string|int} location - Location or zero
 | 
					     * @param {string|int} location - Location or zero
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										21
									
								
								cps/static/js/reading/locationchange-polyfill.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								cps/static/js/reading/locationchange-polyfill.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					// register new event emitter locationchange that fires on urlchange
 | 
				
			||||||
 | 
					// source: https://stackoverflow.com/a/52809105/21941129
 | 
				
			||||||
 | 
					(() => {
 | 
				
			||||||
 | 
					    let oldPushState = history.pushState;
 | 
				
			||||||
 | 
					    history.pushState = function pushState() {
 | 
				
			||||||
 | 
					        let ret = oldPushState.apply(this, arguments);
 | 
				
			||||||
 | 
					        window.dispatchEvent(new Event('locationchange'));
 | 
				
			||||||
 | 
					        return ret;
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let oldReplaceState = history.replaceState;
 | 
				
			||||||
 | 
					    history.replaceState = function replaceState() {
 | 
				
			||||||
 | 
					        let ret = oldReplaceState.apply(this, arguments);
 | 
				
			||||||
 | 
					        window.dispatchEvent(new Event('locationchange'));
 | 
				
			||||||
 | 
					        return ret;
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    window.addEventListener('popstate', () => {
 | 
				
			||||||
 | 
					        window.dispatchEvent(new Event('locationchange'));
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					})();
 | 
				
			||||||
@@ -175,6 +175,7 @@
 | 
				
			|||||||
            // Apply theme to rest of the page. 
 | 
					            // Apply theme to rest of the page. 
 | 
				
			||||||
            document.getElementById("main").style.backgroundColor = themes[id]["bgColor"];
 | 
					            document.getElementById("main").style.backgroundColor = themes[id]["bgColor"];
 | 
				
			||||||
            document.getElementById("titlebar").style.color = themes[id]["title-color"] || "#fff";
 | 
					            document.getElementById("titlebar").style.color = themes[id]["title-color"] || "#fff";
 | 
				
			||||||
 | 
					            document.getElementById("progress").style.color = themes[id]["title-color"] || "#fff";
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // font size settings logic
 | 
					        // font size settings logic
 | 
				
			||||||
@@ -215,6 +216,6 @@
 | 
				
			|||||||
      <script src="{{ url_for('static', filename='js/libs/screenfull.min.js') }}"></script>
 | 
					      <script src="{{ url_for('static', filename='js/libs/screenfull.min.js') }}"></script>
 | 
				
			||||||
      <script src="{{ url_for('static', filename='js/libs/reader.min.js') }}"></script>
 | 
					      <script src="{{ url_for('static', filename='js/libs/reader.min.js') }}"></script>
 | 
				
			||||||
      <script src="{{ url_for('static', filename='js/reading/epub.js') }}"></script>
 | 
					      <script src="{{ url_for('static', filename='js/reading/epub.js') }}"></script>
 | 
				
			||||||
      <script src="{{ url_for('static', filename='js/reading/epub-progress.js') }}"></script>      
 | 
					      <!--script src="{{ url_for('static', filename='js/reading/locationchange-polyfill.js') }}"></script-->
 | 
				
			||||||
    </body>
 | 
					    </body>
 | 
				
			||||||
</html>
 | 
					</html>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user