[fix] server word count, [add] client word count
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-10-26 20:20:58 -04:00
parent b9b9ad2098
commit 1b8b5060f1
3 changed files with 26 additions and 30 deletions

View File

@@ -45,9 +45,15 @@ class EBookReader {
* Load progress and generate locations
**/
async setupReader() {
// Get Word Count (If Needed)
if (this.bookState.words == 0)
this.bookState.words = await this.countWords();
// Load Progress
let { cfi } = await this.getCFIFromXPath(this.bookState.progress);
if (!cfi) this.bookState.currentWord = 0;
this.bookState.currentWord = cfi
? this.bookState.percentage * (this.bookState.words / 100)
: 0;
let getStats = function () {
// Start Timer
@@ -1038,6 +1044,21 @@ class EBookReader {
);
}
/**
* Count the words of the book. Useful for keeping a more accurate track
* of progress percentage. Implementation returns the same number as the
* server side implementation.
**/
countWords() {
// Iterate over each item in the spine, render, and count words.
return this.book.spine.spineItems.reduce(async (totalCount, item) => {
let currentCount = await totalCount;
let newDoc = await item.load(this.book.load.bind(this.book));
let itemCount = newDoc.innerText.trim().split(/\s+/).length;
return currentCount + itemCount;
}, 0);
}
/**
* Save settings to localStorage
**/