1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

updated to new JSZip api

This commit is contained in:
Fred Chasen 2016-08-19 11:50:48 +02:00
parent 874cabe510
commit 45885fc039
10 changed files with 94 additions and 60 deletions

View file

@ -5,7 +5,7 @@ Epub.js v0.3
Epub.js is a JavaScript library for rendering ePub documents in the browser, across many devices.
Epub.js provides an interface for common ebook functions (such as rendering, persistence and pagination) without the need to develop a dedicated application or plugin. Importantly, it has an incredibly permissive [Free BSD](http://en.wikipedia.org/wiki/BSD_licenses) license.
Epub.js provides an interface for common ebook functions (such as rendering, persistence and pagination) without the need to develop a dedicated application or plugin. Importantly, it has an incredibly permissive [Free BSD](http://en.wikipedia.org/wiki/BSD_licenses) license.
[Try it while reading Moby Dick](http://futurepress.github.com/epub.js/reader/)
@ -30,6 +30,12 @@ Get the minified code from the build folder:
<script src="../dist/epub.min.js"></script>
```
If using archived `.epub` files include JSZip:
```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.1/jszip.min.js"></script>
```
Setup a element to render to:
```html
@ -164,5 +170,5 @@ Follow us on twitter: @Epubjs
Other
-------------------------
EPUB is a registered trademark of the [IDPF](http://idpf.org/).
EPUB is a registered trademark of the [IDPF](http://idpf.org/).

66
dist/epub.js vendored
View file

@ -11779,7 +11779,7 @@ Rendition.prototype.replaceCss = function(href, urls, replacementUrls){
var fileUri = URI(href);
var absolute = fileUri.absoluteTo(this.book.baseUrl).toString();
// Get the text of the css file from the archive
var text = this.book.archive.getText(absolute);
var textResponse = this.book.archive.getText(absolute);
// Get asset links relative to css file
var relUrls = urls.
map(function(assetHref) {
@ -11788,17 +11788,20 @@ Rendition.prototype.replaceCss = function(href, urls, replacementUrls){
return relative;
}.bind(this));
// Replacements in the css text
text = replace.substitute(text, relUrls, replacementUrls);
return textResponse.then(function (text) {
// Replacements in the css text
text = replace.substitute(text, relUrls, replacementUrls);
// Get the new url
newUrl = core.createBlobUrl(text, 'text/css');
// Get the new url
newUrl = core.createBlobUrl(text, 'text/css');
// switch the url in the replacementUrls
indexInUrls = urls.indexOf(href);
if (indexInUrls > -1) {
replacementUrls[indexInUrls] = newUrl;
}
});
// switch the url in the replacementUrls
indexInUrls = urls.indexOf(href);
if (indexInUrls > -1) {
replacementUrls[indexInUrls] = newUrl;
}
};
Rendition.prototype.replaceAssets = function(section, urls, replacementUrls){
@ -12657,15 +12660,17 @@ Unarchive.prototype.checkRequirements = function(callback){
Unarchive.prototype.open = function(zipUrl){
if (zipUrl instanceof ArrayBuffer) {
return new RSVP.Promise(function(resolve, reject) {
this.zip = new JSZip(zipUrl);
resolve(this.zip);
});
// return new RSVP.Promise(function(resolve, reject) {
// this.zip = new JSZip(zipUrl);
// resolve(this.zip);
// });
return this.zip.loadAsync(zipUrl);
} else {
return request(zipUrl, "binary")
.then(function(data){
this.zip = new JSZip(data);
return this.zip;
// this.zip = new JSZip(data);
// return this.zip;
return this.zip.loadAsync(data);
}.bind(this));
}
};
@ -12688,8 +12693,10 @@ Unarchive.prototype.request = function(url, type){
}
if (response) {
r = this.handleResponse(response, type);
deferred.resolve(r);
response.then(function (r) {
result = this.handleResponse(r, type);
deferred.resolve(result);
}.bind(this));
} else {
deferred.reject({
message : "File not found in the epub: " + url,
@ -12730,7 +12737,9 @@ Unarchive.prototype.getBlob = function(url, _mimeType){
if(entry) {
mimeType = _mimeType || mime.lookup(entry.name);
return new Blob([entry.asUint8Array()], {type : mimeType});
return entry.async("uint8array").then(function(uint8array) {
return new Blob([uint8array], {type : mimeType});
});
}
};
@ -12739,7 +12748,9 @@ Unarchive.prototype.getText = function(url, encoding){
var entry = this.zip.file(decodededUrl);
if(entry) {
return entry.asText();
return entry.async("string").then(function(text) {
return text;
});
}
};
@ -12747,19 +12758,22 @@ Unarchive.prototype.createUrl = function(url, mime){
var deferred = new RSVP.defer();
var _URL = window.URL || window.webkitURL || window.mozURL;
var tempUrl;
var blob;
var blob;
var response;
if(url in this.urlCache) {
deferred.resolve(this.urlCache[url]);
return deferred.promise;
}
blob = this.getBlob(url);
response = this.getBlob(url);
if (blob) {
tempUrl = _URL.createObjectURL(blob);
deferred.resolve(tempUrl);
this.urlCache[url] = tempUrl;
if (response) {
response.then(function(blob) {
tempUrl = _URL.createObjectURL(blob);
deferred.resolve(tempUrl);
this.urlCache[url] = tempUrl;
}.bind(this));
} else {
deferred.reject({
message : "File not found in the epub: " + url,

2
dist/epub.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>EPUB.js Annotator Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.1/jszip.min.js"></script>
<script src="../dist/epub.js"></script>
<!-- <script type="text/javascript" src="https://cdn.jsdelivr.net/annotator/1.2.9/annotator.min.js"></script> -->

View file

@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>EPUB.js Pagination Example</title>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.1/jszip.min.js"></script>
<script src="../libs/jszip/jszip.js"></script>
<script src="../dist/epub.js"></script>

View file

@ -7,7 +7,7 @@
<script src="../dist/epub.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.1/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/rangy/1.3.0/rangy-core.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/rangy/1.3.0/rangy-classapplier.js"></script>

View file

@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>EPUB.js + Hypothes.is Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.1/jszip.min.js"></script>
<script src="../dist/epub.js"></script>
<script>

View file

@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>EPUB.js Pagination Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.1/jszip.min.js"></script>
<script src="../dist/epub.js"></script>
<style type="text/css">

View file

@ -468,7 +468,7 @@ Rendition.prototype.replaceCss = function(href, urls, replacementUrls){
var fileUri = URI(href);
var absolute = fileUri.absoluteTo(this.book.baseUrl).toString();
// Get the text of the css file from the archive
var text = this.book.archive.getText(absolute);
var textResponse = this.book.archive.getText(absolute);
// Get asset links relative to css file
var relUrls = urls.
map(function(assetHref) {
@ -477,17 +477,20 @@ Rendition.prototype.replaceCss = function(href, urls, replacementUrls){
return relative;
}.bind(this));
// Replacements in the css text
text = replace.substitute(text, relUrls, replacementUrls);
return textResponse.then(function (text) {
// Replacements in the css text
text = replace.substitute(text, relUrls, replacementUrls);
// Get the new url
newUrl = core.createBlobUrl(text, 'text/css');
// Get the new url
newUrl = core.createBlobUrl(text, 'text/css');
// switch the url in the replacementUrls
indexInUrls = urls.indexOf(href);
if (indexInUrls > -1) {
replacementUrls[indexInUrls] = newUrl;
}
});
// switch the url in the replacementUrls
indexInUrls = urls.indexOf(href);
if (indexInUrls > -1) {
replacementUrls[indexInUrls] = newUrl;
}
};
Rendition.prototype.replaceAssets = function(section, urls, replacementUrls){

View file

@ -26,15 +26,17 @@ Unarchive.prototype.checkRequirements = function(callback){
Unarchive.prototype.open = function(zipUrl){
if (zipUrl instanceof ArrayBuffer) {
return new RSVP.Promise(function(resolve, reject) {
this.zip = new JSZip(zipUrl);
resolve(this.zip);
});
// return new RSVP.Promise(function(resolve, reject) {
// this.zip = new JSZip(zipUrl);
// resolve(this.zip);
// });
return this.zip.loadAsync(zipUrl);
} else {
return request(zipUrl, "binary")
.then(function(data){
this.zip = new JSZip(data);
return this.zip;
// this.zip = new JSZip(data);
// return this.zip;
return this.zip.loadAsync(data);
}.bind(this));
}
};
@ -57,8 +59,10 @@ Unarchive.prototype.request = function(url, type){
}
if (response) {
r = this.handleResponse(response, type);
deferred.resolve(r);
response.then(function (r) {
result = this.handleResponse(r, type);
deferred.resolve(result);
}.bind(this));
} else {
deferred.reject({
message : "File not found in the epub: " + url,
@ -99,7 +103,9 @@ Unarchive.prototype.getBlob = function(url, _mimeType){
if(entry) {
mimeType = _mimeType || mime.lookup(entry.name);
return new Blob([entry.asUint8Array()], {type : mimeType});
return entry.async("uint8array").then(function(uint8array) {
return new Blob([uint8array], {type : mimeType});
});
}
};
@ -108,7 +114,9 @@ Unarchive.prototype.getText = function(url, encoding){
var entry = this.zip.file(decodededUrl);
if(entry) {
return entry.asText();
return entry.async("string").then(function(text) {
return text;
});
}
};
@ -116,19 +124,22 @@ Unarchive.prototype.createUrl = function(url, mime){
var deferred = new RSVP.defer();
var _URL = window.URL || window.webkitURL || window.mozURL;
var tempUrl;
var blob;
var blob;
var response;
if(url in this.urlCache) {
deferred.resolve(this.urlCache[url]);
return deferred.promise;
}
blob = this.getBlob(url);
response = this.getBlob(url);
if (blob) {
tempUrl = _URL.createObjectURL(blob);
deferred.resolve(tempUrl);
this.urlCache[url] = tempUrl;
if (response) {
response.then(function(blob) {
tempUrl = _URL.createObjectURL(blob);
deferred.resolve(tempUrl);
this.urlCache[url] = tempUrl;
}.bind(this));
} else {
deferred.reject({
message : "File not found in the epub: " + url,