mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-04 15:09:16 +02:00
Moved replacements to replacment.js
This commit is contained in:
parent
55813bfe69
commit
d8edee0793
10 changed files with 385 additions and 434 deletions
369
build/epub.js
369
build/epub.js
|
@ -423,7 +423,8 @@ EPUBJS.Book.prototype.removeSavedContents = function() {
|
||||||
|
|
||||||
//-- Takes a string or a element
|
//-- Takes a string or a element
|
||||||
EPUBJS.Book.prototype.renderTo = function(elem){
|
EPUBJS.Book.prototype.renderTo = function(elem){
|
||||||
var book = this;
|
var book = this,
|
||||||
|
rendered;
|
||||||
|
|
||||||
if(_.isElement(elem)) {
|
if(_.isElement(elem)) {
|
||||||
this.element = elem;
|
this.element = elem;
|
||||||
|
@ -434,12 +435,16 @@ EPUBJS.Book.prototype.renderTo = function(elem){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.opened.
|
rendered = this.opened.
|
||||||
then(function(){
|
then(function(){
|
||||||
book.render = new EPUBJS.Renderer(book);
|
book.render = new EPUBJS.Renderer(book);
|
||||||
|
|
||||||
return book.startDisplay();
|
return book.startDisplay();
|
||||||
})
|
}, function(error) { console.error(error) });
|
||||||
|
|
||||||
|
rendered.then(null, function(error) { console.error(error) });
|
||||||
|
|
||||||
|
return rendered;
|
||||||
}
|
}
|
||||||
|
|
||||||
EPUBJS.Book.prototype.startDisplay = function(){
|
EPUBJS.Book.prototype.startDisplay = function(){
|
||||||
|
@ -763,7 +768,6 @@ EPUBJS.Book.prototype.triggerHooks = function(type, callback, passed){
|
||||||
hooks = this.hooks[type];
|
hooks = this.hooks[type];
|
||||||
|
|
||||||
count = hooks.length;
|
count = hooks.length;
|
||||||
|
|
||||||
function countdown(){
|
function countdown(){
|
||||||
count--;
|
count--;
|
||||||
if(count <= 0 && callback) callback();
|
if(count <= 0 && callback) callback();
|
||||||
|
@ -1614,13 +1618,8 @@ EPUBJS.Renderer = function(book) {
|
||||||
this.el = book.element;
|
this.el = book.element;
|
||||||
this.book = book;
|
this.book = book;
|
||||||
|
|
||||||
this.settings = book.settings;
|
// this.settings = book.settings;
|
||||||
|
this.caches = {};
|
||||||
book.registerHook("beforeChapterDisplay",
|
|
||||||
[this.replaceLinks.bind(this),
|
|
||||||
this.replaceResources.bind(this),
|
|
||||||
this.replaceHead.bind(this)], true);
|
|
||||||
|
|
||||||
|
|
||||||
this.crossBrowserColumnCss();
|
this.crossBrowserColumnCss();
|
||||||
|
|
||||||
|
@ -1642,8 +1641,8 @@ EPUBJS.Renderer.prototype.initialize = function(){
|
||||||
this.iframe = document.createElement('iframe');
|
this.iframe = document.createElement('iframe');
|
||||||
//this.iframe.id = "epubjs-iframe";
|
//this.iframe.id = "epubjs-iframe";
|
||||||
|
|
||||||
if(this.settings.width || this.settings.height){
|
if(this.book.settings.width || this.book.settings.height){
|
||||||
this.resizeIframe(false, this.settings.width || this.el.clientWidth, this.settings.height || this.el.clientHeight);
|
this.resizeIframe(false, this.book.settings.width || this.el.clientWidth, this.book.settings.height || this.el.clientHeight);
|
||||||
} else {
|
} else {
|
||||||
this.resizeIframe(false, this.el.clientWidth, this.el.clientHeight);
|
this.resizeIframe(false, this.el.clientWidth, this.el.clientHeight);
|
||||||
|
|
||||||
|
@ -1663,6 +1662,18 @@ EPUBJS.Renderer.prototype.listeners = function(){
|
||||||
|
|
||||||
// window.addEventListener("hashchange", book.route.bind(this), false);
|
// window.addEventListener("hashchange", book.route.bind(this), false);
|
||||||
|
|
||||||
|
this.book.registerHook("beforeChapterDisplay", this.replaceLinks.bind(this), true);
|
||||||
|
|
||||||
|
if(this.determineStore()) {
|
||||||
|
|
||||||
|
this.book.registerHook("beforeChapterDisplay", [
|
||||||
|
EPUBJS.replace.head,
|
||||||
|
EPUBJS.replace.resources,
|
||||||
|
EPUBJS.replace.svg
|
||||||
|
], true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.chapter = function(chapter){
|
EPUBJS.Renderer.prototype.chapter = function(chapter){
|
||||||
|
@ -2019,25 +2030,6 @@ EPUBJS.Renderer.prototype.setLeft = function(leftPos){
|
||||||
this.doc.defaultView.scrollTo(leftPos, 0);
|
this.doc.defaultView.scrollTo(leftPos, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.replace = function(query, func, callback){
|
|
||||||
var items, resources, count;
|
|
||||||
|
|
||||||
items = this.doc.querySelectorAll(query);
|
|
||||||
resources = Array.prototype.slice.call(items);
|
|
||||||
count = resources.length;
|
|
||||||
|
|
||||||
resources.forEach(function(item){
|
|
||||||
|
|
||||||
func(item, function(){
|
|
||||||
count--;
|
|
||||||
if(count <= 0 && callback) callback();
|
|
||||||
});
|
|
||||||
|
|
||||||
}.bind(this));
|
|
||||||
|
|
||||||
if(count === 0) callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.determineStore = function(callback){
|
EPUBJS.Renderer.prototype.determineStore = function(callback){
|
||||||
if(this.book.fromStorage) {
|
if(this.book.fromStorage) {
|
||||||
|
|
||||||
|
@ -2059,162 +2051,88 @@ EPUBJS.Renderer.prototype.determineStore = function(callback){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-- Replaces links in head, such as stylesheets
|
EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
|
||||||
EPUBJS.Renderer.prototype.replaceHead = function(callback){
|
var items = this.doc.querySelectorAll(query),
|
||||||
var srcs, resources, count, oldUrls,
|
resources = Array.prototype.slice.call(items),
|
||||||
newUrls = {},
|
count = resources.length,
|
||||||
unarchiver = this,
|
after = function(result){
|
||||||
store = this.determineStore(),
|
|
||||||
replaceUrl = function(link, url){
|
|
||||||
link.setAttribute("href", url);
|
|
||||||
link.onload = function(){
|
|
||||||
count--;
|
count--;
|
||||||
if(count <= 0) finished();
|
if(progress) progress(result, count);
|
||||||
}
|
if(count <= 0 && finished) finished(true);
|
||||||
},
|
|
||||||
finished = function() {
|
|
||||||
|
|
||||||
if(callback) callback();
|
|
||||||
|
|
||||||
_.each(oldUrls, function(url){
|
|
||||||
store.revokeUrl(url);
|
|
||||||
});
|
|
||||||
|
|
||||||
unarchiver.urlCache = newUrls;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if(!store) {
|
if(count === 0) {
|
||||||
if(callback) callback();
|
finished(false);
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
srcs = this.doc.head.querySelectorAll('[href]');
|
resources.forEach(function(item){
|
||||||
resources = Array.prototype.slice.call(srcs);
|
|
||||||
count = resources.length;
|
|
||||||
|
|
||||||
if(!this.urlCache) this.urlCache = {};
|
|
||||||
oldUrls = _.clone(this.urlCache);
|
|
||||||
|
|
||||||
resources.forEach(function(link){
|
|
||||||
var src = link.getAttribute("href"),
|
|
||||||
full = this.book.settings.contentsPath + src;
|
|
||||||
|
|
||||||
|
|
||||||
if(full in oldUrls){
|
|
||||||
replaceUrl(link, oldUrls[full]);
|
|
||||||
newUrls[full] = oldUrls[full];
|
|
||||||
delete oldUrls[full];
|
|
||||||
}else{
|
|
||||||
|
|
||||||
//-- Handle replacing urls in CSS
|
|
||||||
if(link.getAttribute("rel") === "stylesheet") {
|
|
||||||
store.getText(full).then(function(text){
|
|
||||||
var url;
|
|
||||||
|
|
||||||
unarchiver.replaceUrlsInCss(full, text).then(function(newText){
|
|
||||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
|
||||||
|
|
||||||
var blob = new Blob([newText], { "type" : "text\/css" }),
|
|
||||||
url = _URL.createObjectURL(blob);
|
|
||||||
|
|
||||||
replaceUrl(link, url);
|
|
||||||
newUrls[full] = url;
|
|
||||||
|
|
||||||
}, function(e) {console.error(e)});
|
|
||||||
|
|
||||||
});
|
|
||||||
}else{
|
|
||||||
store.getUrl(full).then(function(url){
|
|
||||||
replaceUrl(link, url);
|
|
||||||
newUrls[full] = url;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func(item, after);
|
||||||
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
if(count === 0) finished();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.replaceUrlsInCss = function(base, text){
|
EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callback) {
|
||||||
var promise = new RSVP.Promise(),
|
var _oldUrls,
|
||||||
promises = [],
|
_newUrls = {},
|
||||||
store = this.determineStore(),
|
_store = this.determineStore(),
|
||||||
matches = text.match(/url\(\'?\"?([^\'|^\"]*)\'?\"?\)/g);
|
_cache = this.caches[query],
|
||||||
|
_contentsPath = this.book.settings.contentsPath,
|
||||||
|
_attr = attr,
|
||||||
|
progress = function(url, full, count) {
|
||||||
|
_newUrls[full] = url;
|
||||||
|
},
|
||||||
|
finished = function(notempty) {
|
||||||
|
|
||||||
if(!matches){
|
if(callback) callback();
|
||||||
promise.resolve(text);
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
matches.forEach(function(str){
|
_.each(_oldUrls, function(url){
|
||||||
var full = EPUBJS.core.resolveUrl(base, str.replace(/url\(|[|\)|\'|\"]/g, ''));
|
_store.revokeUrl(url);
|
||||||
replaced = store.getUrl(full).then(function(url){
|
|
||||||
text = text.replace(str, 'url("'+url+'")');
|
|
||||||
}, function(e) {console.error(e)} );
|
|
||||||
|
|
||||||
promises.push(replaced);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
RSVP.all(promises).then(function(){
|
_cache = _newUrls;
|
||||||
promise.resolve(text);
|
};
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
if(!_store) return;
|
||||||
|
|
||||||
|
if(!_cache) _cache = {};
|
||||||
|
_oldUrls = _.clone(_cache);
|
||||||
|
|
||||||
|
this.replace(query, function(link, done){
|
||||||
|
|
||||||
|
var src = link.getAttribute(_attr),
|
||||||
|
full = EPUBJS.core.resolveUrl(_contentsPath, src),
|
||||||
|
replaceUrl = function(url) {
|
||||||
|
link.setAttribute(_attr, url);
|
||||||
|
link.onload = function(){
|
||||||
|
done(url, full);
|
||||||
}
|
}
|
||||||
/*
|
};
|
||||||
//-- Replaces links in head, such as stylesheets
|
|
||||||
EPUBJS.Renderer.prototype.replaceCss = function(callback){
|
|
||||||
var styleSheets = this.doc.styleSheets,
|
|
||||||
store = this.determineStore(),
|
|
||||||
rules = [];
|
|
||||||
|
|
||||||
_.each(styleSheets, function(sheet){
|
|
||||||
_.each(sheet.cssRules, function(rule, index){
|
|
||||||
if(rule.type == 5) {
|
|
||||||
//url("../fonts/STIXGeneral.otf")
|
|
||||||
// if()
|
|
||||||
var urlString = rule.cssText.match(/url\(\'?\"?([^\'|^\"]*)\'?\"?\)/),
|
|
||||||
full;
|
|
||||||
// rule.cssText = ""
|
|
||||||
// console.log(rule.style.src, rule.style[3])
|
|
||||||
// console.log(urlString)
|
|
||||||
|
|
||||||
if(urlString[1]){
|
if(full in _oldUrls){
|
||||||
full = "OPS/" + urlString[1].slice(3);
|
replaceUrl(_oldUrls[full]);
|
||||||
store.getUrl(full).then(function(url){
|
_newUrls[full] = _oldUrls[full];
|
||||||
var newRule = rule.cssText.replace(/url\(\'?\"?([^\'|^\"]*)\'?\"?\)/, 'url("'+url+'")');
|
delete _oldUrls[full];
|
||||||
sheet.deleteRule(index)
|
}else{
|
||||||
sheet.insertRule(newRule, false);
|
func(_store, full, replaceUrl, link);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}, finished, progress);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
//-- Replaces the relative links within the book to use our internal page changer
|
//-- Replaces the relative links within the book to use our internal page changer
|
||||||
EPUBJS.Renderer.prototype.replaceLinks = function(callback){
|
EPUBJS.Renderer.prototype.replaceLinks = function(callback){
|
||||||
var hrefs = this.doc.querySelectorAll('a'),
|
|
||||||
links = Array.prototype.slice.call(hrefs),
|
|
||||||
that = this;
|
|
||||||
|
|
||||||
links.forEach(function(link){
|
var renderer = this;
|
||||||
var path,
|
|
||||||
href = link.getAttribute("href"),
|
|
||||||
relative,
|
|
||||||
fragment;
|
|
||||||
|
|
||||||
if(!href) return;
|
this.replace("a[href]", function(link, done){
|
||||||
|
|
||||||
|
var href = link.getAttribute("href"),
|
||||||
relative = href.search("://"),
|
relative = href.search("://"),
|
||||||
fragment = href[0] == "#";
|
fragment = href[0] == "#";
|
||||||
|
|
||||||
|
@ -2225,49 +2143,17 @@ EPUBJS.Renderer.prototype.replaceLinks = function(callback){
|
||||||
}else{
|
}else{
|
||||||
|
|
||||||
link.onclick = function(){
|
link.onclick = function(){
|
||||||
that.book.goto(href);
|
renderer.book.goto(href);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
done();
|
||||||
|
|
||||||
|
}, callback);
|
||||||
|
|
||||||
if(callback) callback();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-- Replaces assets src's to point to stored version if browser is offline
|
|
||||||
EPUBJS.Renderer.prototype.replaceResources = function(callback){
|
|
||||||
var srcs, resources, count;
|
|
||||||
var store = this.determineStore();
|
|
||||||
|
|
||||||
if(!store) {
|
|
||||||
if(callback) callback();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
srcs = this.doc.querySelectorAll('[src]');
|
|
||||||
resources = Array.prototype.slice.call(srcs);
|
|
||||||
count = resources.length;
|
|
||||||
|
|
||||||
resources.forEach(function(link){
|
|
||||||
var src = link.getAttribute("src"),
|
|
||||||
full = this.book.settings.contentsPath + src;
|
|
||||||
|
|
||||||
store.getUrl(full).then(function(url){
|
|
||||||
// link.setAttribute("src", url);
|
|
||||||
link.src = url;
|
|
||||||
|
|
||||||
link.onload = function(){
|
|
||||||
count--;
|
|
||||||
if(count <= 0 && callback) callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}.bind(this));
|
|
||||||
|
|
||||||
if(count === 0) callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.page = function(pg){
|
EPUBJS.Renderer.prototype.page = function(pg){
|
||||||
if(pg >= 1 && pg <= this.displayedPages){
|
if(pg >= 1 && pg <= this.displayedPages){
|
||||||
|
@ -2432,6 +2318,101 @@ EPUBJS.Renderer.prototype.remove = function() {
|
||||||
|
|
||||||
//-- Enable binding events to parser
|
//-- Enable binding events to parser
|
||||||
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);
|
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);
|
||||||
|
var EPUBJS = EPUBJS || {};
|
||||||
|
EPUBJS.replace = {};
|
||||||
|
|
||||||
|
EPUBJS.replace.head = function(callback, renderer) {
|
||||||
|
|
||||||
|
renderer.replaceWithStored("link[href]", "href", EPUBJS.replace.links, callback);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-- Replaces assets src's to point to stored version if browser is offline
|
||||||
|
EPUBJS.replace.resources = function(callback, renderer){
|
||||||
|
//srcs = this.doc.querySelectorAll('[src]');
|
||||||
|
renderer.replaceWithStored("[src]", "src", EPUBJS.replace.srcs, callback);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
EPUBJS.replace.svg = function(callback, renderer) {
|
||||||
|
|
||||||
|
renderer.replaceWithStored("image", "xlink:href", function(_store, full, done){
|
||||||
|
_store.getUrl(full).then(done);
|
||||||
|
}, callback);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
EPUBJS.replace.srcs = function(_store, full, done){
|
||||||
|
|
||||||
|
_store.getUrl(full).then(done);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//-- Replaces links in head, such as stylesheets - link[href]
|
||||||
|
EPUBJS.replace.links = function(_store, full, done, link){
|
||||||
|
|
||||||
|
//-- Handle replacing urls in CSS
|
||||||
|
if(link.getAttribute("rel") === "stylesheet") {
|
||||||
|
EPUBJS.replace.stylesheets(_store, full).then(done);
|
||||||
|
}else{
|
||||||
|
_store.getUrl(full).then(done);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
EPUBJS.replace.stylesheets = function(_store, full) {
|
||||||
|
var promise = new RSVP.Promise();
|
||||||
|
|
||||||
|
if(!_store) return;
|
||||||
|
|
||||||
|
_store.getText(full).then(function(text){
|
||||||
|
var url;
|
||||||
|
|
||||||
|
EPUBJS.replace.cssUrls(_store, full, text).then(function(newText){
|
||||||
|
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||||
|
|
||||||
|
var blob = new Blob([newText], { "type" : "text\/css" }),
|
||||||
|
url = _URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
promise.resolve(url);
|
||||||
|
|
||||||
|
}, function(e) {console.error(e)});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
EPUBJS.replace.cssUrls = function(_store, base, text){
|
||||||
|
var promise = new RSVP.Promise(),
|
||||||
|
promises = [],
|
||||||
|
matches = text.match(/url\(\'?\"?([^\'|^\"]*)\'?\"?\)/g);
|
||||||
|
|
||||||
|
if(!_store) return;
|
||||||
|
|
||||||
|
if(!matches){
|
||||||
|
promise.resolve(text);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
matches.forEach(function(str){
|
||||||
|
var full = EPUBJS.core.resolveUrl(base, str.replace(/url\(|[|\)|\'|\"]/g, ''));
|
||||||
|
replaced = _store.getUrl(full).then(function(url){
|
||||||
|
text = text.replace(str, 'url("'+url+'")');
|
||||||
|
}, function(e) {console.error(e)} );
|
||||||
|
|
||||||
|
promises.push(replaced);
|
||||||
|
});
|
||||||
|
|
||||||
|
RSVP.all(promises).then(function(){
|
||||||
|
promise.resolve(text);
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
EPUBJS.Unarchiver = function(url){
|
EPUBJS.Unarchiver = function(url){
|
||||||
|
|
||||||
|
|
4
build/epub.min.js
vendored
4
build/epub.min.js
vendored
File diff suppressed because one or more lines are too long
4
demo/js/epub.min.js
vendored
4
demo/js/epub.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -32,12 +32,20 @@
|
||||||
<script src="../src/chapter.js"></script>
|
<script src="../src/chapter.js"></script>
|
||||||
<script src="../src/renderer.js"></script>
|
<script src="../src/renderer.js"></script>
|
||||||
<script src="../src/epubcfi.js"></script>
|
<script src="../src/epubcfi.js"></script>
|
||||||
|
<script src="../src/replace.js"></script>
|
||||||
|
|
||||||
<!-- Hooks -->
|
<!-- Hooks -->
|
||||||
<!-- <script src="../hooks/default/transculsions.js"></script> -->
|
<!-- <script src="../hooks/default/transculsions.js"></script> -->
|
||||||
<!-- <script src="../hooks/default/endnotes.js"></script> -->
|
<!-- <script src="../hooks/default/endnotes.js"></script> -->
|
||||||
<!-- <script src="../hooks/default/smartimages.js"></script> -->
|
<!-- <script src="../hooks/default/smartimages.js"></script> -->
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
EPUBJS.filePath = "../demo/js/libs/";
|
||||||
|
EPUBJS.cssPath = "../demo/css/";
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
|
|
||||||
body {
|
body {
|
||||||
|
@ -95,7 +103,7 @@
|
||||||
<script>
|
<script>
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var Book = ePub("../demo/moby-dick/");
|
var Book = ePub("../demo/moby-dick/", {restore: true});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
@ -110,6 +118,7 @@
|
||||||
|
|
||||||
Book.renderTo("area");
|
Book.renderTo("area");
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -15,10 +15,17 @@
|
||||||
<!-- Zip JS -->
|
<!-- Zip JS -->
|
||||||
<script src="/demo/js/libs/zip.min.js"></script>
|
<script src="/demo/js/libs/zip.min.js"></script>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
EPUBJS.filePath = "../demo/js/libs/";
|
||||||
|
EPUBJS.cssPath = "../demo/css/";
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var Book = ePub("../demo/moby-dick/");
|
var Book = ePub("../demo/moby-dick.epub");
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus SVG 1.1//EN"
|
||||||
<html class="no-js">
|
"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
||||||
|
@ -10,6 +11,11 @@
|
||||||
|
|
||||||
<!-- EPUBJS Renderer -->
|
<!-- EPUBJS Renderer -->
|
||||||
<script src="../build/epub.min.js"></script>
|
<script src="../build/epub.min.js"></script>
|
||||||
|
<script>
|
||||||
|
var qs = document.querySelector;
|
||||||
|
document.querySelector = null;
|
||||||
|
document.querySelectorAll = null;
|
||||||
|
</script>
|
||||||
<script src="../hooks/extensions/hypothesis.js"></script>
|
<script src="../hooks/extensions/hypothesis.js"></script>
|
||||||
|
|
||||||
<link rel="stylesheet" href="basic.css">
|
<link rel="stylesheet" href="basic.css">
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
background: #6b6b6b;
|
background: #6b6b6b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
epub-viewer {
|
epub-viewer {
|
||||||
width: 80%;
|
width: 80%;
|
||||||
height: 80%;
|
height: 80%;
|
||||||
|
@ -68,6 +69,28 @@
|
||||||
padding: 0 6px;
|
padding: 0 6px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media only screen and (min-width: 901px) and (max-width: 1040px) {
|
||||||
|
epub-viewer {
|
||||||
|
width: 50%;
|
||||||
|
margin-left: 25%;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@media only screen and (min-width: 551px) and (max-width: 900px) {
|
||||||
|
epub-viewer {
|
||||||
|
width: 60%;
|
||||||
|
margin-left: 20%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media only screen and (max-width: 550px) {
|
||||||
|
epub-viewer {
|
||||||
|
width: 80%;
|
||||||
|
margin-left: 10%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<epub-sidebar id="sidebar"
|
<epub-sidebar id="sidebar"
|
||||||
|
|
|
@ -67,7 +67,46 @@
|
||||||
margin: -33px 0 0 -33px;
|
margin: -33px 0 0 -33px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media only screen and (max-width: 1040px) {
|
||||||
|
|
||||||
|
#divider,
|
||||||
|
#divider.show {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@media only screen and (min-width: 551px) and (max-width: 900px) {
|
||||||
|
|
||||||
|
#prev {
|
||||||
|
left: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#next {
|
||||||
|
right: 20px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media only screen and (max-width: 550px) {
|
||||||
|
|
||||||
|
#prev {
|
||||||
|
left: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#next {
|
||||||
|
right: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.arrow {
|
||||||
|
height: 100%;
|
||||||
|
top: 45px;
|
||||||
|
width: 10%;
|
||||||
|
text-indent: -10000px;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* and (orientation : landscape) */
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
<div id="divider"></div>
|
<div id="divider"></div>
|
||||||
|
@ -106,10 +145,10 @@
|
||||||
this.$.loader.style.display = "none";
|
this.$.loader.style.display = "none";
|
||||||
},
|
},
|
||||||
bookDisplayed: function() {
|
bookDisplayed: function() {
|
||||||
this.$.divider.style.display = "block";
|
this.$.divider.classList.add('show');
|
||||||
},
|
},
|
||||||
bookUnloaded: function() {
|
bookUnloaded: function() {
|
||||||
this.$.divider.style.display = "none";
|
this.$.divider.classList.remove('show');
|
||||||
},
|
},
|
||||||
srcChanged: function() {
|
srcChanged: function() {
|
||||||
|
|
||||||
|
|
12
src/book.js
12
src/book.js
|
@ -358,7 +358,8 @@ EPUBJS.Book.prototype.removeSavedContents = function() {
|
||||||
|
|
||||||
//-- Takes a string or a element
|
//-- Takes a string or a element
|
||||||
EPUBJS.Book.prototype.renderTo = function(elem){
|
EPUBJS.Book.prototype.renderTo = function(elem){
|
||||||
var book = this;
|
var book = this,
|
||||||
|
rendered;
|
||||||
|
|
||||||
if(_.isElement(elem)) {
|
if(_.isElement(elem)) {
|
||||||
this.element = elem;
|
this.element = elem;
|
||||||
|
@ -369,12 +370,16 @@ EPUBJS.Book.prototype.renderTo = function(elem){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.opened.
|
rendered = this.opened.
|
||||||
then(function(){
|
then(function(){
|
||||||
book.render = new EPUBJS.Renderer(book);
|
book.render = new EPUBJS.Renderer(book);
|
||||||
|
|
||||||
return book.startDisplay();
|
return book.startDisplay();
|
||||||
})
|
}, function(error) { console.error(error) });
|
||||||
|
|
||||||
|
rendered.then(null, function(error) { console.error(error) });
|
||||||
|
|
||||||
|
return rendered;
|
||||||
}
|
}
|
||||||
|
|
||||||
EPUBJS.Book.prototype.startDisplay = function(){
|
EPUBJS.Book.prototype.startDisplay = function(){
|
||||||
|
@ -698,7 +703,6 @@ EPUBJS.Book.prototype.triggerHooks = function(type, callback, passed){
|
||||||
hooks = this.hooks[type];
|
hooks = this.hooks[type];
|
||||||
|
|
||||||
count = hooks.length;
|
count = hooks.length;
|
||||||
|
|
||||||
function countdown(){
|
function countdown(){
|
||||||
count--;
|
count--;
|
||||||
if(count <= 0 && callback) callback();
|
if(count <= 0 && callback) callback();
|
||||||
|
|
262
src/renderer.js
262
src/renderer.js
|
@ -2,13 +2,8 @@ EPUBJS.Renderer = function(book) {
|
||||||
this.el = book.element;
|
this.el = book.element;
|
||||||
this.book = book;
|
this.book = book;
|
||||||
|
|
||||||
this.settings = book.settings;
|
// this.settings = book.settings;
|
||||||
|
this.caches = {};
|
||||||
book.registerHook("beforeChapterDisplay",
|
|
||||||
[this.replaceLinks.bind(this),
|
|
||||||
this.replaceResources.bind(this),
|
|
||||||
this.replaceHead.bind(this)], true);
|
|
||||||
|
|
||||||
|
|
||||||
this.crossBrowserColumnCss();
|
this.crossBrowserColumnCss();
|
||||||
|
|
||||||
|
@ -30,8 +25,8 @@ EPUBJS.Renderer.prototype.initialize = function(){
|
||||||
this.iframe = document.createElement('iframe');
|
this.iframe = document.createElement('iframe');
|
||||||
//this.iframe.id = "epubjs-iframe";
|
//this.iframe.id = "epubjs-iframe";
|
||||||
|
|
||||||
if(this.settings.width || this.settings.height){
|
if(this.book.settings.width || this.book.settings.height){
|
||||||
this.resizeIframe(false, this.settings.width || this.el.clientWidth, this.settings.height || this.el.clientHeight);
|
this.resizeIframe(false, this.book.settings.width || this.el.clientWidth, this.book.settings.height || this.el.clientHeight);
|
||||||
} else {
|
} else {
|
||||||
this.resizeIframe(false, this.el.clientWidth, this.el.clientHeight);
|
this.resizeIframe(false, this.el.clientWidth, this.el.clientHeight);
|
||||||
|
|
||||||
|
@ -51,6 +46,18 @@ EPUBJS.Renderer.prototype.listeners = function(){
|
||||||
|
|
||||||
// window.addEventListener("hashchange", book.route.bind(this), false);
|
// window.addEventListener("hashchange", book.route.bind(this), false);
|
||||||
|
|
||||||
|
this.book.registerHook("beforeChapterDisplay", this.replaceLinks.bind(this), true);
|
||||||
|
|
||||||
|
if(this.determineStore()) {
|
||||||
|
|
||||||
|
this.book.registerHook("beforeChapterDisplay", [
|
||||||
|
EPUBJS.replace.head,
|
||||||
|
EPUBJS.replace.resources,
|
||||||
|
EPUBJS.replace.svg
|
||||||
|
], true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.chapter = function(chapter){
|
EPUBJS.Renderer.prototype.chapter = function(chapter){
|
||||||
|
@ -407,25 +414,6 @@ EPUBJS.Renderer.prototype.setLeft = function(leftPos){
|
||||||
this.doc.defaultView.scrollTo(leftPos, 0);
|
this.doc.defaultView.scrollTo(leftPos, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.replace = function(query, func, callback){
|
|
||||||
var items, resources, count;
|
|
||||||
|
|
||||||
items = this.doc.querySelectorAll(query);
|
|
||||||
resources = Array.prototype.slice.call(items);
|
|
||||||
count = resources.length;
|
|
||||||
|
|
||||||
resources.forEach(function(item){
|
|
||||||
|
|
||||||
func(item, function(){
|
|
||||||
count--;
|
|
||||||
if(count <= 0 && callback) callback();
|
|
||||||
});
|
|
||||||
|
|
||||||
}.bind(this));
|
|
||||||
|
|
||||||
if(count === 0) callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.determineStore = function(callback){
|
EPUBJS.Renderer.prototype.determineStore = function(callback){
|
||||||
if(this.book.fromStorage) {
|
if(this.book.fromStorage) {
|
||||||
|
|
||||||
|
@ -447,162 +435,88 @@ EPUBJS.Renderer.prototype.determineStore = function(callback){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-- Replaces links in head, such as stylesheets
|
EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
|
||||||
EPUBJS.Renderer.prototype.replaceHead = function(callback){
|
var items = this.doc.querySelectorAll(query),
|
||||||
var srcs, resources, count, oldUrls,
|
resources = Array.prototype.slice.call(items),
|
||||||
newUrls = {},
|
count = resources.length,
|
||||||
unarchiver = this,
|
after = function(result){
|
||||||
store = this.determineStore(),
|
|
||||||
replaceUrl = function(link, url){
|
|
||||||
link.setAttribute("href", url);
|
|
||||||
link.onload = function(){
|
|
||||||
count--;
|
count--;
|
||||||
if(count <= 0) finished();
|
if(progress) progress(result, count);
|
||||||
}
|
if(count <= 0 && finished) finished(true);
|
||||||
},
|
|
||||||
finished = function() {
|
|
||||||
|
|
||||||
if(callback) callback();
|
|
||||||
|
|
||||||
_.each(oldUrls, function(url){
|
|
||||||
store.revokeUrl(url);
|
|
||||||
});
|
|
||||||
|
|
||||||
unarchiver.urlCache = newUrls;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if(!store) {
|
if(count === 0) {
|
||||||
if(callback) callback();
|
finished(false);
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
srcs = this.doc.head.querySelectorAll('[href]');
|
resources.forEach(function(item){
|
||||||
resources = Array.prototype.slice.call(srcs);
|
|
||||||
count = resources.length;
|
|
||||||
|
|
||||||
if(!this.urlCache) this.urlCache = {};
|
|
||||||
oldUrls = _.clone(this.urlCache);
|
|
||||||
|
|
||||||
resources.forEach(function(link){
|
|
||||||
var src = link.getAttribute("href"),
|
|
||||||
full = this.book.settings.contentsPath + src;
|
|
||||||
|
|
||||||
|
|
||||||
if(full in oldUrls){
|
|
||||||
replaceUrl(link, oldUrls[full]);
|
|
||||||
newUrls[full] = oldUrls[full];
|
|
||||||
delete oldUrls[full];
|
|
||||||
}else{
|
|
||||||
|
|
||||||
//-- Handle replacing urls in CSS
|
|
||||||
if(link.getAttribute("rel") === "stylesheet") {
|
|
||||||
store.getText(full).then(function(text){
|
|
||||||
var url;
|
|
||||||
|
|
||||||
unarchiver.replaceUrlsInCss(full, text).then(function(newText){
|
|
||||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
|
||||||
|
|
||||||
var blob = new Blob([newText], { "type" : "text\/css" }),
|
|
||||||
url = _URL.createObjectURL(blob);
|
|
||||||
|
|
||||||
replaceUrl(link, url);
|
|
||||||
newUrls[full] = url;
|
|
||||||
|
|
||||||
}, function(e) {console.error(e)});
|
|
||||||
|
|
||||||
});
|
|
||||||
}else{
|
|
||||||
store.getUrl(full).then(function(url){
|
|
||||||
replaceUrl(link, url);
|
|
||||||
newUrls[full] = url;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func(item, after);
|
||||||
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
if(count === 0) finished();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.replaceUrlsInCss = function(base, text){
|
EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callback) {
|
||||||
var promise = new RSVP.Promise(),
|
var _oldUrls,
|
||||||
promises = [],
|
_newUrls = {},
|
||||||
store = this.determineStore(),
|
_store = this.determineStore(),
|
||||||
matches = text.match(/url\(\'?\"?([^\'|^\"]*)\'?\"?\)/g);
|
_cache = this.caches[query],
|
||||||
|
_contentsPath = this.book.settings.contentsPath,
|
||||||
|
_attr = attr,
|
||||||
|
progress = function(url, full, count) {
|
||||||
|
_newUrls[full] = url;
|
||||||
|
},
|
||||||
|
finished = function(notempty) {
|
||||||
|
|
||||||
if(!matches){
|
if(callback) callback();
|
||||||
promise.resolve(text);
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
matches.forEach(function(str){
|
_.each(_oldUrls, function(url){
|
||||||
var full = EPUBJS.core.resolveUrl(base, str.replace(/url\(|[|\)|\'|\"]/g, ''));
|
_store.revokeUrl(url);
|
||||||
replaced = store.getUrl(full).then(function(url){
|
|
||||||
text = text.replace(str, 'url("'+url+'")');
|
|
||||||
}, function(e) {console.error(e)} );
|
|
||||||
|
|
||||||
promises.push(replaced);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
RSVP.all(promises).then(function(){
|
_cache = _newUrls;
|
||||||
promise.resolve(text);
|
};
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
if(!_store) return;
|
||||||
|
|
||||||
|
if(!_cache) _cache = {};
|
||||||
|
_oldUrls = _.clone(_cache);
|
||||||
|
|
||||||
|
this.replace(query, function(link, done){
|
||||||
|
|
||||||
|
var src = link.getAttribute(_attr),
|
||||||
|
full = EPUBJS.core.resolveUrl(_contentsPath, src),
|
||||||
|
replaceUrl = function(url) {
|
||||||
|
link.setAttribute(_attr, url);
|
||||||
|
link.onload = function(){
|
||||||
|
done(url, full);
|
||||||
}
|
}
|
||||||
/*
|
};
|
||||||
//-- Replaces links in head, such as stylesheets
|
|
||||||
EPUBJS.Renderer.prototype.replaceCss = function(callback){
|
|
||||||
var styleSheets = this.doc.styleSheets,
|
|
||||||
store = this.determineStore(),
|
|
||||||
rules = [];
|
|
||||||
|
|
||||||
_.each(styleSheets, function(sheet){
|
|
||||||
_.each(sheet.cssRules, function(rule, index){
|
|
||||||
if(rule.type == 5) {
|
|
||||||
//url("../fonts/STIXGeneral.otf")
|
|
||||||
// if()
|
|
||||||
var urlString = rule.cssText.match(/url\(\'?\"?([^\'|^\"]*)\'?\"?\)/),
|
|
||||||
full;
|
|
||||||
// rule.cssText = ""
|
|
||||||
// console.log(rule.style.src, rule.style[3])
|
|
||||||
// console.log(urlString)
|
|
||||||
|
|
||||||
if(urlString[1]){
|
if(full in _oldUrls){
|
||||||
full = "OPS/" + urlString[1].slice(3);
|
replaceUrl(_oldUrls[full]);
|
||||||
store.getUrl(full).then(function(url){
|
_newUrls[full] = _oldUrls[full];
|
||||||
var newRule = rule.cssText.replace(/url\(\'?\"?([^\'|^\"]*)\'?\"?\)/, 'url("'+url+'")');
|
delete _oldUrls[full];
|
||||||
sheet.deleteRule(index)
|
}else{
|
||||||
sheet.insertRule(newRule, false);
|
func(_store, full, replaceUrl, link);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}, finished, progress);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
//-- Replaces the relative links within the book to use our internal page changer
|
//-- Replaces the relative links within the book to use our internal page changer
|
||||||
EPUBJS.Renderer.prototype.replaceLinks = function(callback){
|
EPUBJS.Renderer.prototype.replaceLinks = function(callback){
|
||||||
var hrefs = this.doc.querySelectorAll('a'),
|
|
||||||
links = Array.prototype.slice.call(hrefs),
|
|
||||||
that = this;
|
|
||||||
|
|
||||||
links.forEach(function(link){
|
var renderer = this;
|
||||||
var path,
|
|
||||||
href = link.getAttribute("href"),
|
|
||||||
relative,
|
|
||||||
fragment;
|
|
||||||
|
|
||||||
if(!href) return;
|
this.replace("a[href]", function(link, done){
|
||||||
|
|
||||||
|
var href = link.getAttribute("href"),
|
||||||
relative = href.search("://"),
|
relative = href.search("://"),
|
||||||
fragment = href[0] == "#";
|
fragment = href[0] == "#";
|
||||||
|
|
||||||
|
@ -613,49 +527,17 @@ EPUBJS.Renderer.prototype.replaceLinks = function(callback){
|
||||||
}else{
|
}else{
|
||||||
|
|
||||||
link.onclick = function(){
|
link.onclick = function(){
|
||||||
that.book.goto(href);
|
renderer.book.goto(href);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
done();
|
||||||
|
|
||||||
|
}, callback);
|
||||||
|
|
||||||
if(callback) callback();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-- Replaces assets src's to point to stored version if browser is offline
|
|
||||||
EPUBJS.Renderer.prototype.replaceResources = function(callback){
|
|
||||||
var srcs, resources, count;
|
|
||||||
var store = this.determineStore();
|
|
||||||
|
|
||||||
if(!store) {
|
|
||||||
if(callback) callback();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
srcs = this.doc.querySelectorAll('[src]');
|
|
||||||
resources = Array.prototype.slice.call(srcs);
|
|
||||||
count = resources.length;
|
|
||||||
|
|
||||||
resources.forEach(function(link){
|
|
||||||
var src = link.getAttribute("src"),
|
|
||||||
full = this.book.settings.contentsPath + src;
|
|
||||||
|
|
||||||
store.getUrl(full).then(function(url){
|
|
||||||
// link.setAttribute("src", url);
|
|
||||||
link.src = url;
|
|
||||||
|
|
||||||
link.onload = function(){
|
|
||||||
count--;
|
|
||||||
if(count <= 0 && callback) callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}.bind(this));
|
|
||||||
|
|
||||||
if(count === 0) callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.page = function(pg){
|
EPUBJS.Renderer.prototype.page = function(pg){
|
||||||
if(pg >= 1 && pg <= this.displayedPages){
|
if(pg >= 1 && pg <= this.displayedPages){
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue