mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
Add Destroy to view managers
This commit is contained in:
parent
db727b00a9
commit
d95ec97f7f
10 changed files with 118 additions and 59 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "epubjs",
|
"name": "epubjs",
|
||||||
"version": "0.3.11",
|
"version": "0.3.12",
|
||||||
"description": "Parse and Render Epubs",
|
"description": "Parse and Render Epubs",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"jsnext:main": "src/index.js",
|
"jsnext:main": "src/index.js",
|
||||||
|
|
|
@ -247,8 +247,9 @@ class Archive {
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
this.urlCache.forEach((fromCache) => _URL.revokeObjectURL(fromCache));
|
for (let fromCache in this.urlCache) {
|
||||||
|
_URL.revokeObjectURL(fromCache);
|
||||||
|
}
|
||||||
this.zip = undefined;
|
this.zip = undefined;
|
||||||
this.urlCache = {};
|
this.urlCache = {};
|
||||||
}
|
}
|
||||||
|
|
12
src/book.js
12
src/book.js
|
@ -27,7 +27,7 @@ const EPUBJS_VERSION = "0.3";
|
||||||
* @param {boolean} [options.requestCredentials=undefined] send the xhr request withCredentials
|
* @param {boolean} [options.requestCredentials=undefined] send the xhr request withCredentials
|
||||||
* @param {object} [options.requestHeaders=undefined] send the xhr request headers
|
* @param {object} [options.requestHeaders=undefined] send the xhr request headers
|
||||||
* @param {string} [options.encoding=binary] optional to pass 'binary' or base64' for archived Epubs
|
* @param {string} [options.encoding=binary] optional to pass 'binary' or base64' for archived Epubs
|
||||||
* @param {string} [options.replacements=base64] use base64, blobUrl, or none for replacing assets in archived Epubs
|
* @param {string} [options.replacements=none] use base64, blobUrl, or none for replacing assets in archived Epubs
|
||||||
* @returns {Book}
|
* @returns {Book}
|
||||||
* @example new Book("/path/to/book.epub", {})
|
* @example new Book("/path/to/book.epub", {})
|
||||||
* @example new Book({ replacements: "blobUrl" })
|
* @example new Book({ replacements: "blobUrl" })
|
||||||
|
@ -46,7 +46,7 @@ class Book {
|
||||||
requestCredentials: undefined,
|
requestCredentials: undefined,
|
||||||
requestHeaders: undefined,
|
requestHeaders: undefined,
|
||||||
encoding: undefined,
|
encoding: undefined,
|
||||||
replacements: "base64"
|
replacements: undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
extend(this.settings, options);
|
extend(this.settings, options);
|
||||||
|
@ -351,7 +351,8 @@ class Book {
|
||||||
this.resources = new Resources(this.package.manifest, {
|
this.resources = new Resources(this.package.manifest, {
|
||||||
archive: this.archive,
|
archive: this.archive,
|
||||||
resolver: this.resolve.bind(this),
|
resolver: this.resolve.bind(this),
|
||||||
replacements: this.settings.replacements
|
request: this.request.bind(this),
|
||||||
|
replacements: this.settings.replacements || "base64"
|
||||||
});
|
});
|
||||||
|
|
||||||
this.loadNavigation(this.package).then(() => {
|
this.loadNavigation(this.package).then(() => {
|
||||||
|
@ -372,9 +373,12 @@ class Book {
|
||||||
|
|
||||||
this.isOpen = true;
|
this.isOpen = true;
|
||||||
|
|
||||||
if(this.archived || this.settings.replacements) {
|
if(this.archived || this.settings.replacements && this.settings.replacements != "none") {
|
||||||
this.replacements().then(() => {
|
this.replacements().then(() => {
|
||||||
this.opening.resolve(this);
|
this.opening.resolve(this);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Resolve book opened promise
|
// Resolve book opened promise
|
||||||
|
|
|
@ -85,9 +85,11 @@ class DefaultViewManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy(){
|
destroy(){
|
||||||
// this.views.each(function(view){
|
this.views.each(function(view){
|
||||||
// view.destroy();
|
view.destroy();
|
||||||
// });
|
});
|
||||||
|
|
||||||
|
this.stage.destroy();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -108,7 +110,6 @@ class DefaultViewManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
resize(width, height){
|
resize(width, height){
|
||||||
|
|
||||||
// Clear the queue
|
// Clear the queue
|
||||||
this.q.clear();
|
this.q.clear();
|
||||||
|
|
||||||
|
@ -119,12 +120,13 @@ class DefaultViewManager {
|
||||||
this.viewSettings.width = this._stageSize.width;
|
this.viewSettings.width = this._stageSize.width;
|
||||||
this.viewSettings.height = this._stageSize.height;
|
this.viewSettings.height = this._stageSize.height;
|
||||||
|
|
||||||
|
this.updateLayout();
|
||||||
|
|
||||||
// Update for existing views
|
// Update for existing views
|
||||||
this.views.each(function(view) {
|
this.views.each(function(view) {
|
||||||
view.size(this._stageSize.width, this._stageSize.height);
|
view.size(this._stageSize.width, this._stageSize.height);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
this.updateLayout();
|
|
||||||
|
|
||||||
this.emit("resized", {
|
this.emit("resized", {
|
||||||
width: this.stage.width,
|
width: this.stage.width,
|
||||||
|
|
|
@ -225,6 +225,23 @@ class Stage {
|
||||||
|
|
||||||
this.sheet.insertRule(scope + selector + " {" + rules + "}", 0);
|
this.sheet.insertRule(scope + selector + " {" + rules + "}", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
destroy() {
|
||||||
|
var base;
|
||||||
|
|
||||||
|
if (this.element) {
|
||||||
|
|
||||||
|
if(this.settings.hidden) {
|
||||||
|
base = this.wrapper;
|
||||||
|
} else {
|
||||||
|
base = this.container;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.element.contains(this.container)) {
|
||||||
|
this.element.removeChild(this.container);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Stage;
|
export default Stage;
|
||||||
|
|
|
@ -237,17 +237,19 @@ 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(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();
|
textWidth = this.contents.textWidth();
|
||||||
|
width = this.contentWidth(textWidth);
|
||||||
|
|
||||||
// Check if the textWidth has changed
|
// Check if the textWidth has changed
|
||||||
if(textWidth != this._textWidth){
|
if(width != this._width){
|
||||||
// Get the contentWidth by resizing the iframe
|
// Get the contentWidth by resizing the iframe
|
||||||
// Check with a min reset of the textWidth
|
// Check with a min reset of the textWidth
|
||||||
width = this.contentWidth(textWidth);
|
|
||||||
|
// width = this.contentWidth(textWidth);
|
||||||
|
|
||||||
columns = Math.ceil(width / (this.settings.layout.columnWidth + this.settings.layout.gap));
|
columns = Math.ceil(width / (this.settings.layout.columnWidth + this.settings.layout.gap));
|
||||||
|
|
||||||
|
@ -260,6 +262,7 @@ class IframeView {
|
||||||
|
|
||||||
// Save the textWdith
|
// Save the textWdith
|
||||||
this._textWidth = textWidth;
|
this._textWidth = textWidth;
|
||||||
|
|
||||||
// Save the contentWidth
|
// Save the contentWidth
|
||||||
this._contentWidth = width;
|
this._contentWidth = width;
|
||||||
} else {
|
} else {
|
||||||
|
@ -344,6 +347,7 @@ class IframeView {
|
||||||
// this._needsReframe = true;
|
// this._needsReframe = true;
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if(isNumber(width)){
|
if(isNumber(width)){
|
||||||
this.element.style.width = width + "px";
|
this.element.style.width = width + "px";
|
||||||
}
|
}
|
||||||
|
|
|
@ -221,6 +221,9 @@ class Rendition {
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
_display(target){
|
_display(target){
|
||||||
|
if (!this.book) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
var isCfiString = this.epubcfi.isCfiString(target);
|
var isCfiString = this.epubcfi.isCfiString(target);
|
||||||
var displaying = new defer();
|
var displaying = new defer();
|
||||||
var displayed = displaying.promise;
|
var displayed = displaying.promise;
|
||||||
|
@ -513,30 +516,32 @@ class Rendition {
|
||||||
*/
|
*/
|
||||||
destroy(){
|
destroy(){
|
||||||
// Clear the queue
|
// Clear the queue
|
||||||
this.q.clear();
|
// this.q.clear();
|
||||||
this.q = undefined;
|
// this.q = undefined;
|
||||||
|
|
||||||
this.book = book;
|
this.manager && this.manager.destroy();
|
||||||
|
|
||||||
|
this.book = undefined;
|
||||||
|
|
||||||
this.views = null;
|
this.views = null;
|
||||||
|
|
||||||
this.hooks.display.clear();
|
// this.hooks.display.clear();
|
||||||
this.hooks.serialize.clear();
|
// this.hooks.serialize.clear();
|
||||||
this.hooks.content.clear();
|
// this.hooks.content.clear();
|
||||||
this.hooks.layout.clear();
|
// this.hooks.layout.clear();
|
||||||
this.hooks.render.clear();
|
// this.hooks.render.clear();
|
||||||
this.hooks.show.clear();
|
// this.hooks.show.clear();
|
||||||
this.hooks = {};
|
// this.hooks = {};
|
||||||
|
|
||||||
this.themes.destroy();
|
// this.themes.destroy();
|
||||||
this.themes = undefined;
|
// this.themes = undefined;
|
||||||
|
|
||||||
this.epubcfi = undefined;
|
// this.epubcfi = undefined;
|
||||||
|
|
||||||
|
// this.starting = undefined;
|
||||||
|
// this.started = undefined;
|
||||||
|
|
||||||
this.starting = undefined;
|
|
||||||
this.started = undefined;
|
|
||||||
|
|
||||||
this.manager && this.manager.destroy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import {substitute} from "./utils/replacements";
|
import {substitute} from "./utils/replacements";
|
||||||
import {createBase64Url, createBlobUrl} from "./utils/core";
|
import {createBase64Url, createBlobUrl, blob2base64} from "./utils/core";
|
||||||
|
import Url from "./utils/url";
|
||||||
|
import mime from "../libs/mime/mime";
|
||||||
import Path from "./utils/path";
|
import Path from "./utils/path";
|
||||||
import path from "path-webpack";
|
import path from "path-webpack";
|
||||||
|
|
||||||
|
@ -17,7 +19,8 @@ class Resources {
|
||||||
this.settings = {
|
this.settings = {
|
||||||
replacements: (options && options.replacements) || "base64",
|
replacements: (options && options.replacements) || "base64",
|
||||||
archive: (options && options.archive),
|
archive: (options && options.archive),
|
||||||
resolver: (options && options.resolver)
|
resolver: (options && options.resolver),
|
||||||
|
request: (options && options.request)
|
||||||
};
|
};
|
||||||
this.manifest = manifest;
|
this.manifest = manifest;
|
||||||
this.resources = Object.keys(manifest).
|
this.resources = Object.keys(manifest).
|
||||||
|
@ -90,16 +93,34 @@ class Resources {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createUrl (url) {
|
||||||
|
var parsedUrl = new Url(url);
|
||||||
|
var mimeType = mime.lookup(parsedUrl.filename);
|
||||||
|
|
||||||
|
if (this.settings.archive) {
|
||||||
|
return this.settings.archive.createUrl(url, {"base64": (this.settings.replacements === "base64")});
|
||||||
|
} else {
|
||||||
|
if (this.settings.replacements === "base64") {
|
||||||
|
return this.settings.request(url, 'blob')
|
||||||
|
.then((blob) => {
|
||||||
|
return blob2base64(blob);
|
||||||
|
})
|
||||||
|
.then((blob) => {
|
||||||
|
return createBase64Url(blob, mimeType);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return this.settings.request(url, 'blob').then((blob) => {
|
||||||
|
return createBlobUrl(blob, mimeType);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create blob urls for all the assets
|
* Create blob urls for all the assets
|
||||||
* @param {Archive} archive
|
|
||||||
* @param {resolver} resolver Url resolver
|
|
||||||
* @return {Promise} returns replacement urls
|
* @return {Promise} returns replacement urls
|
||||||
*/
|
*/
|
||||||
replacements(archive, resolver){
|
replacements(){
|
||||||
archive = archive || this.settings.archive;
|
|
||||||
resolver = resolver || this.settings.resolver;
|
|
||||||
|
|
||||||
if (this.settings.replacements === "none") {
|
if (this.settings.replacements === "none") {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
resolve(this.urls);
|
resolve(this.urls);
|
||||||
|
@ -108,10 +129,11 @@ class Resources {
|
||||||
|
|
||||||
var replacements = this.urls.
|
var replacements = this.urls.
|
||||||
map( (url) => {
|
map( (url) => {
|
||||||
var absolute = resolver(url);
|
var absolute = this.settings.resolver(url);
|
||||||
|
|
||||||
return archive.createUrl(absolute, {"base64": (this.settings.replacements === "base64")}).
|
return this.createUrl(absolute).
|
||||||
catch((err) => {
|
catch((err) => {
|
||||||
|
console.error(err);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -156,14 +178,10 @@ class Resources {
|
||||||
* Create a new CSS file with the replaced URLs
|
* Create a new CSS file with the replaced URLs
|
||||||
* @private
|
* @private
|
||||||
* @param {string} href the original css file
|
* @param {string} href the original css file
|
||||||
* @param {[Archive]} archive
|
|
||||||
* @param {[method]} resolver
|
|
||||||
* @return {Promise} returns a BlobUrl to the new CSS file or a data url
|
* @return {Promise} returns a BlobUrl to the new CSS file or a data url
|
||||||
*/
|
*/
|
||||||
createCssFile(href, archive, resolver){
|
createCssFile(href){
|
||||||
var newUrl;
|
var newUrl;
|
||||||
archive = archive || this.settings.archive;
|
|
||||||
resolver = resolver || this.settings.resolver;
|
|
||||||
|
|
||||||
if (path.isAbsolute(href)) {
|
if (path.isAbsolute(href)) {
|
||||||
return new Promise(function(resolve){
|
return new Promise(function(resolve){
|
||||||
|
@ -171,14 +189,20 @@ class Resources {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var absolute = resolver(href);
|
var absolute = this.settings.resolver(href);
|
||||||
|
|
||||||
// Get the text of the css file from the archive
|
// Get the text of the css file from the archive
|
||||||
var textResponse = archive.getText(absolute);
|
var textResponse;
|
||||||
|
|
||||||
|
if (this.settings.archive) {
|
||||||
|
textResponse = this.settings.archive.getText(absolute);
|
||||||
|
} else {
|
||||||
|
textResponse = this.settings.request(absolute, "text");
|
||||||
|
}
|
||||||
|
|
||||||
// Get asset links relative to css file
|
// Get asset links relative to css file
|
||||||
var relUrls = this.urls.map( (assetHref) => {
|
var relUrls = this.urls.map( (assetHref) => {
|
||||||
var resolved = resolver(assetHref);
|
var resolved = this.settings.resolver(assetHref);
|
||||||
var relative = new Path(absolute).relative(resolved);
|
var relative = new Path(absolute).relative(resolved);
|
||||||
|
|
||||||
return relative;
|
return relative;
|
||||||
|
@ -245,8 +269,7 @@ class Resources {
|
||||||
resolve(this.replacementUrls[indexInUrls]);
|
resolve(this.replacementUrls[indexInUrls]);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
} else {
|
} else {
|
||||||
return this.archive.createUrl(path,
|
return this.createUrl(path);
|
||||||
{"base64": (this.settings.replacements === "base64")});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -407,12 +407,14 @@ export function walk(node,callback){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function blob2base64(blob, cb) {
|
export function blob2base64(blob) {
|
||||||
var reader = new FileReader();
|
return new Promise(function(resolve, reject) {
|
||||||
reader.readAsDataURL(blob);
|
var reader = new FileReader();
|
||||||
reader.onloadend = function() {
|
reader.readAsDataURL(blob);
|
||||||
cb(reader.result);
|
reader.onloadend = function() {
|
||||||
};
|
resolve(reader.result);
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// From: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred#backwards_forwards_compatible
|
// From: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred#backwards_forwards_compatible
|
||||||
|
|
|
@ -72,7 +72,7 @@ class Queue {
|
||||||
dequeue(){
|
dequeue(){
|
||||||
var inwait, task, result;
|
var inwait, task, result;
|
||||||
|
|
||||||
if(this._q.length) {
|
if(this._q.length && !this.paused) {
|
||||||
inwait = this._q.shift();
|
inwait = this._q.shift();
|
||||||
task = inwait.task;
|
task = inwait.task;
|
||||||
if(task){
|
if(task){
|
||||||
|
@ -178,6 +178,7 @@ class Queue {
|
||||||
clear(){
|
clear(){
|
||||||
this._q = [];
|
this._q = [];
|
||||||
this.running = false;
|
this.running = false;
|
||||||
|
this.paused = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue