From b25c024b6646c716c540d6c4aec1a1bc1bad3218 Mon Sep 17 00:00:00 2001 From: quarz12 Date: Mon, 15 May 2023 17:10:07 +0200 Subject: [PATCH] added getCurrentFileProgress, TODO: resolve CFI to xml node --- cps/static/js/reading/epub-progress.js | 42 +++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/cps/static/js/reading/epub-progress.js b/cps/static/js/reading/epub-progress.js index 95938407..4141b485 100644 --- a/cps/static/js/reading/epub-progress.js +++ b/cps/static/js/reading/epub-progress.js @@ -3,15 +3,16 @@ class EpubParser { this.files = filesList; this.parser = new DOMParser(); this.opfXml = this.getOPFXml(); + this.encoder = new TextEncoder(); } - getTextByteLength() { + getTotalByteLength() { let size = 0; for (let key of Object.keys(this.files)) { let file = this.files[key]; if (file.name.endsWith("html")) { - console.log(file.name+" "+file._data.uncompressedSize) + console.log(file.name + " " + file._data.uncompressedSize) size += file._data.uncompressedSize; } } @@ -69,14 +70,47 @@ class EpubParser { let filepath = this.absPath(this.resolveIDref(file)); //ignore non text files if (filepath.endsWith("html")) { - console.log(filepath+" "+bytesize) + console.log(filepath + " " + bytesize) bytesize += this.files[filepath]._data.uncompressedSize; } } else { - break + break; } } return bytesize; } + cfiToXmlNode(file,cfi){ + } + /** + takes the node that the cfi points at and counts the bytes of all nodes before that + */ + getCurrentFileProgress(currentFile, CFI) { + let size = 0 + let startnode = this.cfiToXmlNode(currentFile,CFI); + let prev = startnode.previousElementSibling; + while (prev !== null) { + size += this.encoder.encode(prev.outerHTML).length; + prev = prev.previousElementSibling; + } + let parent = startnode.parentElement; + while (parent !== null) { + let parentPrev = parent.previousElementSibling; + while (parentPrev !== null) { + size += this.encoder.encode(parentPrev.outerHTML).length; + parentPrev = parentPrev.previousElementSibling; + } + parent=parent.parentElement; + } + return size; + } + + getProgress(currentFile, CFI) { + let percentage = this.getTotalByteLength() / (this.getPreviousFilesSize(currentFile) + this.getCurrentFileProgress(currentFile, CFI)); + if (percentage === Infinity) { + return 0; + } else { + return percentage; + } + } }