mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
Simplify iframe view expanding
This commit is contained in:
parent
8d2b9cc8d4
commit
eff216fd2d
5 changed files with 57 additions and 135 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "epubjs",
|
"name": "epubjs",
|
||||||
"version": "0.3.47",
|
"version": "0.3.48",
|
||||||
"description": "Parse and Render Epubs",
|
"description": "Parse and Render Epubs",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"module": "src/index.js",
|
"module": "src/index.js",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import EventEmitter from "event-emitter";
|
import EventEmitter from "event-emitter";
|
||||||
import {isNumber, prefixed} from "./utils/core";
|
import {isNumber, prefixed, borders} from "./utils/core";
|
||||||
import EpubCFI from "./epubcfi";
|
import EpubCFI from "./epubcfi";
|
||||||
import Mapping from "./mapping";
|
import Mapping from "./mapping";
|
||||||
import {replaceLinks} from "./utils/replacements";
|
import {replaceLinks} from "./utils/replacements";
|
||||||
|
@ -107,9 +107,10 @@ class Contents {
|
||||||
}
|
}
|
||||||
|
|
||||||
textWidth() {
|
textWidth() {
|
||||||
var width;
|
let width;
|
||||||
var range = this.document.createRange();
|
let range = this.document.createRange();
|
||||||
var content = this.content || this.document.body;
|
let content = this.content || this.document.body;
|
||||||
|
let border = borders(content);
|
||||||
|
|
||||||
// Select the contents of frame
|
// Select the contents of frame
|
||||||
range.selectNodeContents(content);
|
range.selectNodeContents(content);
|
||||||
|
@ -117,20 +118,28 @@ class Contents {
|
||||||
// get the width of the text content
|
// get the width of the text content
|
||||||
width = range.getBoundingClientRect().width;
|
width = range.getBoundingClientRect().width;
|
||||||
|
|
||||||
return width;
|
if (border && border.width) {
|
||||||
|
width += border.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.round(width);
|
||||||
}
|
}
|
||||||
|
|
||||||
textHeight() {
|
textHeight() {
|
||||||
var height;
|
let height;
|
||||||
var range = this.document.createRange();
|
let range = this.document.createRange();
|
||||||
var content = this.content || this.document.body;
|
let content = this.content || this.document.body;
|
||||||
|
let border = borders(content);
|
||||||
|
|
||||||
range.selectNodeContents(content);
|
range.selectNodeContents(content);
|
||||||
|
|
||||||
height = range.getBoundingClientRect().height;
|
height = range.getBoundingClientRect().height;
|
||||||
|
|
||||||
return height;
|
if (height && border.height) {
|
||||||
|
height += border.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.round(height);
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollWidth() {
|
scrollWidth() {
|
||||||
|
@ -323,7 +332,6 @@ class Contents {
|
||||||
resizeCheck() {
|
resizeCheck() {
|
||||||
let width = this.textWidth();
|
let width = this.textWidth();
|
||||||
let height = this.textHeight();
|
let height = this.textHeight();
|
||||||
|
|
||||||
if (width != this._size.width || height != this._size.height) {
|
if (width != this._size.width || height != this._size.height) {
|
||||||
|
|
||||||
this._size = {
|
this._size = {
|
||||||
|
@ -342,10 +350,7 @@ class Contents {
|
||||||
// Test size again
|
// Test size again
|
||||||
clearTimeout(this.expanding);
|
clearTimeout(this.expanding);
|
||||||
|
|
||||||
width = this.textWidth();
|
requestAnimationFrame(this.resizeCheck.bind(this));
|
||||||
height = this.textHeight();
|
|
||||||
|
|
||||||
this.resizeCheck();
|
|
||||||
|
|
||||||
this.expanding = setTimeout(this.resizeListeners.bind(this), 350);
|
this.expanding = setTimeout(this.resizeListeners.bind(this), 350);
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,8 +128,10 @@ class Layout {
|
||||||
// colWidth = (width - gap) / divisor;
|
// colWidth = (width - gap) / divisor;
|
||||||
// gap = gap / divisor;
|
// gap = gap / divisor;
|
||||||
colWidth = (width / divisor) - gap;
|
colWidth = (width / divisor) - gap;
|
||||||
|
pageWidth = colWidth + gap;
|
||||||
} else {
|
} else {
|
||||||
colWidth = width;
|
colWidth = width;
|
||||||
|
pageWidth = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.name === "pre-paginated" && divisor > 1) {
|
if (this.name === "pre-paginated" && divisor > 1) {
|
||||||
|
@ -137,7 +139,6 @@ class Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
spreadWidth = (colWidth * divisor) + gap;
|
spreadWidth = (colWidth * divisor) + gap;
|
||||||
pageWidth = colWidth + (gap / 2);
|
|
||||||
|
|
||||||
delta = width;
|
delta = width;
|
||||||
|
|
||||||
|
|
|
@ -529,7 +529,7 @@ class DefaultViewManager {
|
||||||
|
|
||||||
// Find mapping
|
// Find mapping
|
||||||
let start = left + container.left - position + used;
|
let start = left + container.left - position + used;
|
||||||
let end = start + this.layout.spreadWidth - used;
|
let end = start + this.layout.width - used;
|
||||||
|
|
||||||
let mapping = this.mapping.page(view.contents, view.section.cfiBase, start, end);
|
let mapping = this.mapping.page(view.contents, view.section.cfiBase, start, end);
|
||||||
|
|
||||||
|
|
|
@ -140,13 +140,10 @@ class IframeView {
|
||||||
// Listen for events that require an expansion of the iframe
|
// Listen for events that require an expansion of the iframe
|
||||||
this.addListeners();
|
this.addListeners();
|
||||||
|
|
||||||
// Wait for formating to apply
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
setTimeout(() => {
|
// Expand the iframe to the full size of the content
|
||||||
// Expand the iframe to the full size of the content
|
this.expand();
|
||||||
this.expand();
|
resolve();
|
||||||
resolve();
|
|
||||||
}, 1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
|
@ -221,7 +218,6 @@ class IframeView {
|
||||||
|
|
||||||
// this.contents.layout();
|
// this.contents.layout();
|
||||||
this.expand();
|
this.expand();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -241,149 +237,69 @@ class IframeView {
|
||||||
if(this.layout.name === "pre-paginated") return;
|
if(this.layout.name === "pre-paginated") return;
|
||||||
|
|
||||||
this._expanding = true;
|
this._expanding = true;
|
||||||
|
|
||||||
// Expand Horizontally
|
// Expand Horizontally
|
||||||
// if(height && !width) {
|
|
||||||
if(this.settings.axis === "horizontal") {
|
if(this.settings.axis === "horizontal") {
|
||||||
// Get the width of the text
|
// Get the width of the text
|
||||||
textWidth = this.contents.textWidth();
|
width = this.contents.textWidth();
|
||||||
width = this.contentWidth(textWidth);
|
|
||||||
|
|
||||||
// Check if the textWidth has changed
|
if (width % this.layout.pageWidth > 0) {
|
||||||
if(width != this._width){
|
width = Math.ceil(width / this.layout.pageWidth) * this.layout.pageWidth;
|
||||||
// Get the contentWidth by resizing the iframe
|
|
||||||
// Check with a min reset of the textWidth
|
|
||||||
|
|
||||||
// Add padding back
|
|
||||||
if (width % this.layout.width > 0) {
|
|
||||||
width += this.layout.gap / 2;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
columns = Math.ceil(width / this.settings.layout.delta);
|
|
||||||
if ( this.settings.layout.divisor > 1 &&
|
|
||||||
this.settings.layout.name === "reflowable" &&
|
|
||||||
(columns % 2 > 0)) {
|
|
||||||
// add a blank page
|
|
||||||
width += this.settings.layout.gap + this.settings.layout.columnWidth;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Save the textWdith
|
|
||||||
this._textWidth = textWidth;
|
|
||||||
|
|
||||||
// Save the contentWidth
|
|
||||||
this._contentWidth = width;
|
|
||||||
} else {
|
|
||||||
// Otherwise assume content height hasn't changed
|
|
||||||
width = this._contentWidth;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
columns = Math.ceil(width / this.settings.layout.delta);
|
||||||
|
if ( this.settings.layout.divisor > 1 &&
|
||||||
|
this.settings.layout.name === "reflowable" &&
|
||||||
|
(columns % 2 > 0)) {
|
||||||
|
// add a blank page
|
||||||
|
width += this.settings.layout.gap + this.settings.layout.columnWidth;
|
||||||
|
}
|
||||||
|
*/
|
||||||
} // Expand Vertically
|
} // Expand Vertically
|
||||||
else if(this.settings.axis === "vertical") {
|
else if(this.settings.axis === "vertical") {
|
||||||
textHeight = this.contents.textHeight();
|
height = this.contents.textHeight();
|
||||||
if(textHeight != this._textHeight){
|
|
||||||
height = this.contentHeight(textHeight);
|
|
||||||
this._textHeight = textHeight;
|
|
||||||
this._contentHeight = height;
|
|
||||||
} else {
|
|
||||||
height = this._contentHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only Resize if dimensions have changed or
|
// Only Resize if dimensions have changed or
|
||||||
// if Frame is still hidden, so needs reframing
|
// if Frame is still hidden, so needs reframing
|
||||||
if(this._needsReframe || width != this._width || height != this._height){
|
if(this._needsReframe || width != this._width || height != this._height){
|
||||||
this.resize(width, height);
|
this.reframe(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._expanding = false;
|
this._expanding = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
contentWidth(min) {
|
reframe(width, height) {
|
||||||
var prev;
|
var size;
|
||||||
var width;
|
|
||||||
|
|
||||||
// Save previous width
|
|
||||||
prev = this.iframe.style.width;
|
|
||||||
// Set the iframe size to min, width will only ever be greater
|
|
||||||
// Will preserve the aspect ratio
|
|
||||||
this.iframe.style.width = (min || 0) + "px";
|
|
||||||
// Get the scroll overflow width
|
|
||||||
width = this.contents.scrollWidth();
|
|
||||||
// Reset iframe size back
|
|
||||||
this.iframe.style.width = prev;
|
|
||||||
return width;
|
|
||||||
}
|
|
||||||
|
|
||||||
contentHeight(min) {
|
|
||||||
var prev;
|
|
||||||
var height;
|
|
||||||
|
|
||||||
prev = this.iframe.style.height;
|
|
||||||
this.iframe.style.height = (min || 0) + "px";
|
|
||||||
height = this.contents.scrollHeight();
|
|
||||||
|
|
||||||
this.iframe.style.height = prev;
|
|
||||||
return height;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
resize(width, height) {
|
|
||||||
|
|
||||||
if(!this.iframe) return;
|
|
||||||
|
|
||||||
if(isNumber(width)){
|
if(isNumber(width)){
|
||||||
|
this.element.style.width = width + "px";
|
||||||
this.iframe.style.width = width + "px";
|
this.iframe.style.width = width + "px";
|
||||||
this._width = width;
|
this._width = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isNumber(height)){
|
if(isNumber(height)){
|
||||||
|
this.element.style.height = height + "px";
|
||||||
this.iframe.style.height = height + "px";
|
this.iframe.style.height = height + "px";
|
||||||
this._height = height;
|
this._height = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.iframeBounds = bounds(this.iframe);
|
let widthDelta = this.prevBounds ? width - this.prevBounds.width : width;
|
||||||
|
let heightDelta = this.prevBounds ? height - this.prevBounds.height : height;
|
||||||
this.reframe(this.iframeBounds.width, this.iframeBounds.height);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
reframe(width, height) {
|
|
||||||
var size;
|
|
||||||
|
|
||||||
// if(!this.displayed) {
|
|
||||||
// this._needsReframe = true;
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
if(isNumber(width)){
|
|
||||||
this.element.style.width = width + "px";
|
|
||||||
}
|
|
||||||
|
|
||||||
if(isNumber(height)){
|
|
||||||
this.element.style.height = height + "px";
|
|
||||||
}
|
|
||||||
|
|
||||||
this.elementBounds = bounds(this.element);
|
|
||||||
|
|
||||||
let widthDelta = this.prevBounds ? this.elementBounds.width - this.prevBounds.width : this.elementBounds.width;
|
|
||||||
let heightDelta = this.prevBounds ? this.elementBounds.height - this.prevBounds.height : this.elementBounds.height;
|
|
||||||
|
|
||||||
size = {
|
size = {
|
||||||
width: this.elementBounds.width,
|
width: width,
|
||||||
height: this.elementBounds.height,
|
height: height,
|
||||||
widthDelta: widthDelta,
|
widthDelta: widthDelta,
|
||||||
heightDelta: heightDelta,
|
heightDelta: heightDelta,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.onResize(this, size);
|
this.onResize(this, size);
|
||||||
|
|
||||||
if (this.contents) {
|
|
||||||
this.settings.layout.format(this.contents);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.emit("resized", size);
|
this.emit("resized", size);
|
||||||
|
|
||||||
this.prevBounds = this.elementBounds;
|
this.prevBounds = size;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -445,12 +361,18 @@ class IframeView {
|
||||||
this.contents.on("expand", () => {
|
this.contents.on("expand", () => {
|
||||||
if(this.displayed && this.iframe) {
|
if(this.displayed && this.iframe) {
|
||||||
this.expand();
|
this.expand();
|
||||||
|
if (this.contents) {
|
||||||
|
this.settings.layout.format(this.contents);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.contents.on("resize", (e) => {
|
this.contents.on("resize", (e) => {
|
||||||
if(this.displayed && this.iframe) {
|
if(this.displayed && this.iframe) {
|
||||||
this.expand();
|
this.expand();
|
||||||
|
if (this.contents) {
|
||||||
|
this.settings.layout.format(this.contents);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -465,12 +387,6 @@ class IframeView {
|
||||||
this.settings.axis = axis;
|
this.settings.axis = axis;
|
||||||
}
|
}
|
||||||
|
|
||||||
resizeListenters() {
|
|
||||||
// Test size again
|
|
||||||
clearTimeout(this.expanding);
|
|
||||||
this.expanding = setTimeout(this.expand.bind(this), 350);
|
|
||||||
}
|
|
||||||
|
|
||||||
addListeners() {
|
addListeners() {
|
||||||
//TODO: Add content listeners for expanding
|
//TODO: Add content listeners for expanding
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue