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

Fix locationOf with empty string #1129

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
14 changes: 7 additions & 7 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "epubjs",
"version": "0.3.88",
"name": "epubjs-myh",
"version": "0.3.98",
"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
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
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
22 changes: 17 additions & 5 deletions src/managers/default/index.js
Expand Up @@ -343,6 +343,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 +457,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 +494,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 +568,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 +587,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 @@ -920,12 +931,13 @@ class DefaultViewManager {
this._stageSize = this.stage.size();

if(!this.isPaginated) {
this.layout.calculate(this._stageSize.width, this._stageSize.height);
this.layout.calculate(this._stageSize.width, this._stageSize.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 Down