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

Add navigateToNthPage function #459

Merged
merged 1 commit into from
Oct 23, 2018
Merged
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
33 changes: 33 additions & 0 deletions src/adapt/epub.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Copyright 2013 Google, Inc.
* Copyright 2015 Trim-marks Inc.
* Copyright 2018 Vivliostyle Foundation
*
* Vivliostyle.js is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -1576,6 +1577,38 @@ adapt.epub.OPFView.prototype.previousSpread = function(position) {
}
};

/**
* Move to the Nth page and render it.
* @param {number} nthPage
* @param {!adapt.epub.Position} position
* @return {!adapt.task.Result.<?adapt.epub.PageAndPosition>}
*/
adapt.epub.OPFView.prototype.navigateToNthPage = function(nthPage, position) {
if (nthPage < 1) {
return adapt.task.newResult(/** @type {?adapt.epub.PageAndPosition} */ (null));
}
let countPages = 0;
let pageIndex = -1;
let spineIndex = 0;
for (let item of this.spineItems) {
const findPageIndex = nthPage - countPages - 1;
if (findPageIndex < item.pages.length) {
pageIndex = findPageIndex;
break;
}
countPages += item.pages.length;
spineIndex++;
}
if (pageIndex === -1) {
return adapt.task.newResult(/** @type {?adapt.epub.PageAndPosition} */ (null));
}
return this.findPage({
spineIndex,
pageIndex,
offsetInItem: -1
});
};

/**
* Move to the epage specified by the given number (zero-based) and render it.
* @param {number} epage
Expand Down
4 changes: 4 additions & 0 deletions src/adapt/viewer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Copyright 2013 Google, Inc.
* Copyright 2015 Trim-marks Inc.
* Copyright 2018 Vivliostyle Foundation
*
* Vivliostyle.js is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -917,6 +918,9 @@ adapt.viewer.Viewer.prototype.moveTo = function(command) {
const m = method;
method = () => m.call(self.opfView, self.pagePosition);
}
} else if (typeof command["nthPage"] == "number") {
const nthPage = /** @type {number} */ (command["nthPage"]);
method = () => self.opfView.navigateToNthPage(nthPage, self.pagePosition);
} else if (typeof command["epage"] == "number") {
const epage = /** @type {number} */ (command["epage"]);
method = () => self.opfView.navigateToEPage(epage);
Expand Down
10 changes: 10 additions & 0 deletions src/vivliostyle/viewer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright 2015 Trim-marks Inc.
* Copyright 2018 Vivliostyle Foundation
*
* Vivliostyle.js is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -364,6 +365,14 @@ goog.scope(() => {
this.adaptViewer.sendCommand({"a": "moveTo", "where": this.resolveNavigation(nav)});
};

/**
* Navigate to the Nth page.
* @param {number} nthPage
*/
Viewer.prototype.navigateToNthPage = function(nthPage) {
this.adaptViewer.sendCommand({"a": "moveTo", "nthPage": nthPage});
};

/**
* Navigate to the specified internal URL.
* @param {string} url
Expand Down Expand Up @@ -407,6 +416,7 @@ goog.scope(() => {
goog.exportProperty(Viewer.prototype, "loadEPUB", Viewer.prototype.loadEPUB);
goog.exportProperty(Viewer.prototype, "getCurrentPageProgression", Viewer.prototype.getCurrentPageProgression);
goog.exportProperty(Viewer.prototype, "navigateToPage", Viewer.prototype.navigateToPage);
goog.exportProperty(Viewer.prototype, "navigateToNthPage", Viewer.prototype.navigateToNthPage);
goog.exportProperty(Viewer.prototype, "navigateToInternalUrl", Viewer.prototype.navigateToInternalUrl);
goog.exportProperty(Viewer.prototype, "queryZoomFactor", Viewer.prototype.queryZoomFactor);
goog.exportProperty(Viewer.prototype, "getPageSizes", Viewer.prototype.getPageSizes);
Expand Down