1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-02 14:49:16 +02:00

Treat page numbers as strings, store page number to href mapping

This commit is contained in:
Martin Quiazon 2022-03-29 00:16:54 -07:00 committed by Martin Quiazon
parent 0963efe979
commit 9184ca53f2
2 changed files with 25 additions and 16 deletions

View file

@ -15,6 +15,7 @@ class PageList {
constructor(xml) { constructor(xml) {
this.pages = []; this.pages = [];
this.locations = []; this.locations = [];
this.hrefMap = {};
this.epubcfi = new EpubCFI(); this.epubcfi = new EpubCFI();
this.firstPage = 0; this.firstPage = 0;
@ -106,7 +107,7 @@ class PageList {
var content = qs(item, "content"); var content = qs(item, "content");
var href = content.getAttribute("src"); var href = content.getAttribute("src");
var page = parseInt(pageText, 10); var page = pageText;
return { return {
"href": href, "href": href,
@ -124,7 +125,7 @@ class PageList {
var content = qs(item, "a"), var content = qs(item, "a"),
href = content.getAttribute("href") || "", href = content.getAttribute("href") || "",
text = content.textContent || "", text = content.textContent || "",
page = parseInt(text), page = text,
isCfi = href.indexOf("epubcfi"), isCfi = href.indexOf("epubcfi"),
split, split,
packageUrl, packageUrl,
@ -156,19 +157,20 @@ class PageList {
process(pageList){ process(pageList){
pageList.forEach(function(item){ pageList.forEach(function(item){
this.pages.push(item.page); this.pages.push(item.page);
this.hrefMap[item.page] = item.href;
if (item.cfi) { if (item.cfi) {
this.locations.push(item.cfi); this.locations.push(item.cfi);
} }
}, this); }, this);
this.firstPage = parseInt(this.pages[0]); this.firstPage = this.pages[0];
this.lastPage = parseInt(this.pages[this.pages.length-1]); this.lastPage = this.pages[this.pages.length-1];
this.totalPages = this.lastPage - this.firstPage; this.totalPages = this.lastPage - this.firstPage;
} }
/** /**
* Get a PageList result from a EpubCFI * Get a PageList result from a EpubCFI
* @param {string} cfi EpubCFI String * @param {string} cfi EpubCFI String
* @return {number} page * @return {string} page
*/ */
pageFromCfi(cfi){ pageFromCfi(cfi){
var pg = -1; var pg = -1;
@ -205,15 +207,11 @@ class PageList {
/** /**
* Get an EpubCFI from a Page List Item * Get an EpubCFI from a Page List Item
* @param {string | number} pg * @param {string} pg
* @return {string} cfi * @return {string} cfi
*/ */
cfiFromPage(pg){ cfiFromPage(pg){
var cfi = -1; var cfi = -1;
// check that pg is an int
if(typeof pg != "number"){
pg = parseInt(pg);
}
// check if the cfi is in the page list // check if the cfi is in the page list
// Pages could be unsorted. // Pages could be unsorted.
@ -228,7 +226,7 @@ class PageList {
/** /**
* Get a Page from Book percentage * Get a Page from Book percentage
* @param {number} percent * @param {number} percent
* @return {number} page * @return {string} page
*/ */
pageFromPercentage(percent){ pageFromPercentage(percent){
var pg = Math.round(this.totalPages * percent); var pg = Math.round(this.totalPages * percent);
@ -237,7 +235,7 @@ class PageList {
/** /**
* Returns a value between 0 - 1 corresponding to the location of a page * Returns a value between 0 - 1 corresponding to the location of a page
* @param {number} pg the page * @param {string} pg the page
* @return {number} percentage * @return {number} percentage
*/ */
percentageFromPage(pg){ percentageFromPage(pg){
@ -256,6 +254,15 @@ class PageList {
return percentage; return percentage;
} }
/**
* Returns the href corresponding to a page
* @param {string} pg the page
* @return {string} href
*/
hrefFromPage(pg){
return this.hrefMap[pg];
}
/** /**
* Destroy * Destroy
*/ */

10
types/pagelist.d.ts vendored
View file

@ -10,13 +10,15 @@ export default class Pagelist {
parse(xml: XMLDocument): Array<PageListItem>; parse(xml: XMLDocument): Array<PageListItem>;
pageFromCfi(cfi: string): number; pageFromCfi(cfi: string): string;
cfiFromPage(pg: string | number): string; cfiFromPage(pg: string): string;
pageFromPercentage(percent: number): number; pageFromPercentage(percent: number): string;
percentageFromPage(pg: number): number; percentageFromPage(pg: string): number;
hrefFromPage(pg: string): string;
destroy(): void; destroy(): void;