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

@ -30,6 +30,12 @@ Get the minified code from the build folder:
<script src="../dist/epub.min.js"></script> <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: Setup a element to render to:
```html ```html

66
dist/epub.js vendored
View file

@ -11779,7 +11779,7 @@ Rendition.prototype.replaceCss = function(href, urls, replacementUrls){
var fileUri = URI(href); var fileUri = URI(href);
var absolute = fileUri.absoluteTo(this.book.baseUrl).toString(); var absolute = fileUri.absoluteTo(this.book.baseUrl).toString();
// Get the text of the css file from the archive // 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 // Get asset links relative to css file
var relUrls = urls. var relUrls = urls.
map(function(assetHref) { map(function(assetHref) {
@ -11788,17 +11788,20 @@ Rendition.prototype.replaceCss = function(href, urls, replacementUrls){
return relative; return relative;
}.bind(this)); }.bind(this));
// Replacements in the css text return textResponse.then(function (text) {
text = replace.substitute(text, relUrls, replacementUrls); // Replacements in the css text
text = replace.substitute(text, relUrls, replacementUrls);
// Get the new url // Get the new url
newUrl = core.createBlobUrl(text, 'text/css'); 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){ Rendition.prototype.replaceAssets = function(section, urls, replacementUrls){
@ -12657,15 +12660,17 @@ Unarchive.prototype.checkRequirements = function(callback){
Unarchive.prototype.open = function(zipUrl){ Unarchive.prototype.open = function(zipUrl){
if (zipUrl instanceof ArrayBuffer) { if (zipUrl instanceof ArrayBuffer) {
return new RSVP.Promise(function(resolve, reject) { // return new RSVP.Promise(function(resolve, reject) {
this.zip = new JSZip(zipUrl); // this.zip = new JSZip(zipUrl);
resolve(this.zip); // resolve(this.zip);
}); // });
return this.zip.loadAsync(zipUrl);
} else { } else {
return request(zipUrl, "binary") return request(zipUrl, "binary")
.then(function(data){ .then(function(data){
this.zip = new JSZip(data); // this.zip = new JSZip(data);
return this.zip; // return this.zip;
return this.zip.loadAsync(data);
}.bind(this)); }.bind(this));
} }
}; };
@ -12688,8 +12693,10 @@ Unarchive.prototype.request = function(url, type){
} }
if (response) { if (response) {
r = this.handleResponse(response, type); response.then(function (r) {
deferred.resolve(r); result = this.handleResponse(r, type);
deferred.resolve(result);
}.bind(this));
} else { } else {
deferred.reject({ deferred.reject({
message : "File not found in the epub: " + url, message : "File not found in the epub: " + url,
@ -12730,7 +12737,9 @@ Unarchive.prototype.getBlob = function(url, _mimeType){
if(entry) { if(entry) {
mimeType = _mimeType || mime.lookup(entry.name); 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); var entry = this.zip.file(decodededUrl);
if(entry) { 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 deferred = new RSVP.defer();
var _URL = window.URL || window.webkitURL || window.mozURL; var _URL = window.URL || window.webkitURL || window.mozURL;
var tempUrl; var tempUrl;
var blob; var blob;
var response;
if(url in this.urlCache) { if(url in this.urlCache) {
deferred.resolve(this.urlCache[url]); deferred.resolve(this.urlCache[url]);
return deferred.promise; return deferred.promise;
} }
blob = this.getBlob(url); response = this.getBlob(url);
if (blob) { if (response) {
tempUrl = _URL.createObjectURL(blob); response.then(function(blob) {
deferred.resolve(tempUrl); tempUrl = _URL.createObjectURL(blob);
this.urlCache[url] = tempUrl; deferred.resolve(tempUrl);
this.urlCache[url] = tempUrl;
}.bind(this));
} else { } else {
deferred.reject({ deferred.reject({
message : "File not found in the epub: " + url, 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"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>EPUB.js Annotator Example</title> <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 src="../dist/epub.js"></script>
<!-- <script type="text/javascript" src="https://cdn.jsdelivr.net/annotator/1.2.9/annotator.min.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"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>EPUB.js Pagination Example</title> <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="../libs/jszip/jszip.js"></script>
<script src="../dist/epub.js"></script> <script src="../dist/epub.js"></script>

View file

@ -7,7 +7,7 @@
<script src="../dist/epub.js"></script> <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-core.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/rangy/1.3.0/rangy-classapplier.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"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>EPUB.js + Hypothes.is Example</title> <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 src="../dist/epub.js"></script>
<script> <script>

View file

@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>EPUB.js Pagination Example</title> <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> <script src="../dist/epub.js"></script>
<style type="text/css"> <style type="text/css">

View file

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

View file

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