Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/pagelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PageList {
constructor(xml) {
this.pages = [];
this.locations = [];
this.hrefMap = {};
this.epubcfi = new EpubCFI();

this.firstPage = 0;
Expand Down Expand Up @@ -106,7 +107,7 @@ class PageList {
var content = qs(item, "content");

var href = content.getAttribute("src");
var page = parseInt(pageText, 10);
var page = pageText;

return {
"href": href,
Expand All @@ -124,7 +125,7 @@ class PageList {
var content = qs(item, "a"),
href = content.getAttribute("href") || "",
text = content.textContent || "",
page = parseInt(text),
page = text,
isCfi = href.indexOf("epubcfi"),
split,
packageUrl,
Expand Down Expand Up @@ -156,19 +157,20 @@ class PageList {
process(pageList){
pageList.forEach(function(item){
this.pages.push(item.page);
this.hrefMap[item.page] = item.href;
if (item.cfi) {
this.locations.push(item.cfi);
}
}, this);
this.firstPage = parseInt(this.pages[0]);
this.lastPage = parseInt(this.pages[this.pages.length-1]);
this.firstPage = this.pages[0];
this.lastPage = this.pages[this.pages.length-1];
this.totalPages = this.lastPage - this.firstPage;
}

/**
* Get a PageList result from a EpubCFI
* @param {string} cfi EpubCFI String
* @return {number} page
* @return {string} page
*/
pageFromCfi(cfi){
var pg = -1;
Expand Down Expand Up @@ -205,15 +207,11 @@ class PageList {

/**
* Get an EpubCFI from a Page List Item
* @param {string | number} pg
* @param {string} pg
* @return {string} cfi
*/
cfiFromPage(pg){
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
// Pages could be unsorted.
Expand All @@ -228,7 +226,7 @@ class PageList {
/**
* Get a Page from Book percentage
* @param {number} percent
* @return {number} page
* @return {string} page
*/
pageFromPercentage(percent){
var pg = Math.round(this.totalPages * percent);
Expand All @@ -237,7 +235,7 @@ class PageList {

/**
* 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
*/
percentageFromPage(pg){
Expand All @@ -256,6 +254,15 @@ class PageList {
return percentage;
}

/**
* Returns the href corresponding to a page
* @param {string} pg the page
* @return {string} href
*/
hrefFromPage(pg){
return this.hrefMap[pg];
}

/**
* Destroy
*/
Expand Down
10 changes: 6 additions & 4 deletions types/pagelist.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export default class Pagelist {

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;

Expand Down