added getCurrentFileProgress,

TODO: resolve CFI to xml node
This commit is contained in:
quarz12 2023-05-15 17:10:07 +02:00
parent bc65180c32
commit b25c024b66

View File

@ -3,10 +3,11 @@ class EpubParser {
this.files = filesList; this.files = filesList;
this.parser = new DOMParser(); this.parser = new DOMParser();
this.opfXml = this.getOPFXml(); this.opfXml = this.getOPFXml();
this.encoder = new TextEncoder();
} }
getTextByteLength() { getTotalByteLength() {
let size = 0; let size = 0;
for (let key of Object.keys(this.files)) { for (let key of Object.keys(this.files)) {
let file = this.files[key]; let file = this.files[key];
@ -73,10 +74,43 @@ class EpubParser {
bytesize += this.files[filepath]._data.uncompressedSize; bytesize += this.files[filepath]._data.uncompressedSize;
} }
} else { } else {
break break;
} }
} }
return bytesize; 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;
}
}
}