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 scrollbar support for scroll flow #1130

Open
wants to merge 16 commits 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
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "epubjs",
"version": "0.3.88",
"name": "epubjs-myh",
"version": "0.3.101",
"description": "Parse and Render Epubs",
"main": "lib/index.js",
"module": "src/index.js",
Expand All @@ -16,15 +16,15 @@
"docs:md": "documentation build src/epub.js -f md -o documentation/md/API.md",
"lint": "eslint -c .eslintrc.js src; exit 0",
"start": "webpack-dev-server --inline --d",
"build": "NODE_ENV=production webpack --progress",
"minify": "NODE_ENV=production MINIMIZE=true webpack --progress",
"legacy": "NODE_ENV=production LEGACY=true webpack --progress",
"productionLegacy": "NODE_ENV=production MINIMIZE=true LEGACY=true webpack --progress",
"build": "set NODE_ENV=production && webpack --progress",
"minify": "set NODE_ENV=production && set MINIMIZE=true && webpack --progress",
"legacy": "set NODE_ENV=production && set LEGACY=true && webpack --progress",
"productionLegacy": "set NODE_ENV=production && set MINIMIZE=true && set LEGACY=true && webpack --progress",
"compile": "babel -d lib/ src/",
"watch": "babel --watch -d lib/ src/",
"prepare": "npm run compile && npm run build && npm run minify && npm run legacy && npm run productionLegacy"
},
"author": "fchasen@gmail.com",
"author": "myh@live.com",
"license": "BSD-2-Clause",
"devDependencies": {
"@babel/cli": "^7.10.1",
Expand Down
4 changes: 3 additions & 1 deletion src/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,9 @@ class Contents {
console.error(e, e.stack);
}
} else {
position = range.getBoundingClientRect();
// range.toString() might be an empty string and range.getBoundingClientRect() returns a all-zeros position.
// Thus, we use the parent element containing the range to get a valid bounding rect.
position = range.startContainer.parentElement.getBoundingClientRect();
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ class Layout {
calculate(_width, _height, _gap){

var divisor = 1;
var gap = _gap || 0;
var gap = _gap || 20;

//-- Check the width and create even width columns
// var fullWidth = Math.floor(_width);
var width = _width;
var height = _height;

var section = Math.floor(width / 12);
//var section = Math.floor(width / 12);

var columnWidth;
var spreadWidth;
Expand All @@ -123,9 +123,11 @@ class Layout {
divisor = 1;
}

/*
if (this.name === "reflowable" && this._flow === "paginated" && !(_gap >= 0)) {
gap = ((section % 2 === 0) ? section : section - 1);
}
*/

if (this.name === "pre-paginated" ) {
gap = 0;
Expand Down
41 changes: 32 additions & 9 deletions src/managers/default/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class DefaultViewManager {
this._bounds = this.bounds();
this._stageSize = this.stage.size();


// Set the dimensions for views
this.viewSettings.width = this._stageSize.width;
this.viewSettings.height = this._stageSize.height;
this.updateLayout();

// Function to handle a resize event.
// Will only attach if width and height are both fixed.
Expand Down Expand Up @@ -220,9 +220,11 @@ class DefaultViewManager {
// Clear current views
this.clear();

/* Redundant codes? this.updateLayout() will update them!?
// Update for new views
this.viewSettings.width = this._stageSize.width;
this.viewSettings.height = this._stageSize.height;
*/

this.updateLayout();

Expand Down Expand Up @@ -343,6 +345,12 @@ class DefaultViewManager {
if (distX + this.layout.delta > this.container.scrollWidth) {
distX = this.container.scrollWidth - this.layout.delta;
}

distY = Math.floor(offset.top / this.layout.delta) * this.layout.delta;

if (distY + this.layout.delta > this.container.scrollHeight) {
distY = this.container.scrollHeight - this.layout.delta;
}
}
this.scrollTo(distX, distY, true);
}
Expand Down Expand Up @@ -451,7 +459,9 @@ class DefaultViewManager {
this.scrollLeft = this.container.scrollLeft;

if (this.settings.rtlScrollType === "default"){
left = this.container.scrollLeft;
this.scrollLeft = Math.floor(this.container.scrollLeft);
// this.container.scrollLeft could has fractional part.
left = Math.floor(this.container.scrollLeft);

if (left > 0) {
this.scrollBy(this.layout.delta, 0, true);
Expand Down Expand Up @@ -486,6 +496,7 @@ class DefaultViewManager {

if(next) {
this.clear();
this.updateLayout();

let forceRight = false;
if (this.layout.name === "pre-paginated" && this.layout.divisor === 2 && next.properties.includes("page-spread-right")) {
Expand Down Expand Up @@ -559,9 +570,10 @@ class DefaultViewManager {

} else if (this.isPaginated && this.settings.axis === "vertical") {

this.scrollTop = this.container.scrollTop;
this.scrollTop = Math.floor(this.container.scrollTop);

let top = this.container.scrollTop;
// this.container.scrollTop could has fractional part.
let top = Math.floor(this.container.scrollTop);

if(top > 0) {
this.scrollBy(0, -(this.layout.height), true);
Expand All @@ -577,6 +589,7 @@ class DefaultViewManager {

if(prev) {
this.clear();
this.updateLayout();

let forceRight = false;
if (this.layout.name === "pre-paginated" && this.layout.divisor === 2 && typeof prev.prev() !== "object") {
Expand Down Expand Up @@ -919,13 +932,23 @@ class DefaultViewManager {

this._stageSize = this.stage.size();

if(!this.isPaginated) {
this.layout.calculate(this._stageSize.width, this._stageSize.height);
if (!this.isPaginated) {
let width;
let height;
if (this.settings.axis === "vertical") {
width = this._stageSize.width - (this.settings.scrollbarWidth | 0);
height = this._stageSize.height;
} else {
width = this._stageSize.width;
height = this._stageSize.height - (this.settings.scrollbarWidth | 0);
}
this.layout.calculate(width, height, undefined, this.settings.axis);
} else {
this.layout.calculate(
this._stageSize.width,
this._stageSize.height,
this.settings.gap
this.settings.gap,
this.settings.axis
);

// Set the look ahead offset for what is visible
Expand All @@ -942,7 +965,7 @@ class DefaultViewManager {
this.setLayout(this.layout);
}

setLayout(layout){
setLayout(layout) {

this.viewSettings.layout = layout;

Expand Down
10 changes: 9 additions & 1 deletion src/managers/views/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,15 @@ class IframeView {
}

let m = new Highlight(range, className, data, attributes);
let h = this.pane.addMark(m);
let h;
try {
h = this.pane.addMark(m);
} catch (err) {
// Print error only for invalid highlights.
// Throwing an error here will cause ePub display stops after navigate out and then in a section with an invalid highlight.
console.error(err);
return;
}

this.highlights[cfiRange] = { "mark": h, "element": h.element, "listeners": [emitter, cb] };

Expand Down
1 change: 1 addition & 0 deletions types/rendition.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface RenditionOptions {
manager?: string | Function | object,
view?: string | Function | object,
flow?: string,
scrollbarWidth?: number,
layout?: string,
spread?: string,
minSpreadWidth?: number,
Expand Down