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

resource process method, types fixes

This commit is contained in:
Fred Chasen 2018-10-29 18:29:04 -07:00
parent 5bad36d709
commit 231496a047
11 changed files with 76 additions and 49 deletions

View file

@ -213,7 +213,7 @@ class Book {
// this.toc = undefined; // this.toc = undefined;
if (this.settings.store) { if (this.settings.store) {
this.store(); this.store(this.settings.store);
} }
if(url) { if(url) {
@ -430,33 +430,33 @@ class Book {
/** /**
* unpack the contents of the Books packageXml * unpack the contents of the Books packaging
* @private * @private
* @param {document} packageXml XML Document * @param {Packaging} packaging object
*/ */
unpack(opf) { unpack(packaging) {
this.package = opf; this.package = packaging; //TODO: deprecated this
this.spine.unpack(this.package, this.resolve.bind(this), this.canonical.bind(this)); this.spine.unpack(this.packaging, this.resolve.bind(this), this.canonical.bind(this));
this.resources = new Resources(this.package.manifest, { this.resources = new Resources(this.packaging.manifest, {
archive: this.archive, archive: this.archive,
resolver: this.resolve.bind(this), resolver: this.resolve.bind(this),
request: this.request.bind(this), request: this.request.bind(this),
replacements: this.settings.replacements || (this.archived ? "blobUrl" : "base64") replacements: this.settings.replacements || (this.archived ? "blobUrl" : "base64")
}); });
this.loadNavigation(this.package).then(() => { this.loadNavigation(this.packaging).then(() => {
// this.toc = this.navigation.toc; // this.toc = this.navigation.toc;
this.loading.navigation.resolve(this.navigation); this.loading.navigation.resolve(this.navigation);
}); });
if (this.package.coverPath) { if (this.packaging.coverPath) {
this.cover = this.resolve(this.package.coverPath); this.cover = this.resolve(this.packaging.coverPath);
} }
// Resolve promises // Resolve promises
this.loading.manifest.resolve(this.package.manifest); this.loading.manifest.resolve(this.packaging.manifest);
this.loading.metadata.resolve(this.package.metadata); this.loading.metadata.resolve(this.packaging.metadata);
this.loading.spine.resolve(this.spine); this.loading.spine.resolve(this.spine);
this.loading.cover.resolve(this.cover); this.loading.cover.resolve(this.cover);
this.loading.resources.resolve(this.resources); this.loading.resources.resolve(this.resources);
@ -481,19 +481,19 @@ class Book {
/** /**
* Load Navigation and PageList from package * Load Navigation and PageList from package
* @private * @private
* @param {document} opf XML Document * @param {Packaging} packaging
*/ */
loadNavigation(opf) { loadNavigation(packaging) {
let navPath = opf.navPath || opf.ncxPath; let navPath = packaging.navPath || packaging.ncxPath;
let toc = opf.toc; let toc = packaging.toc;
// From json manifest // From json manifest
if (toc) { if (toc) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.navigation = new Navigation(toc); this.navigation = new Navigation(toc);
if (opf.pageList) { if (packaging.pageList) {
this.pageList = new PageList(opf.pageList); // TODO: handle page lists from Manifest this.pageList = new PageList(packaging.pageList); // TODO: handle page lists from Manifest
} }
resolve(this.navigation); resolve(this.navigation);
@ -575,7 +575,7 @@ class Book {
* @param {string} [encoding] * @param {string} [encoding]
* @return {Store} * @return {Store}
*/ */
store() { store(name) {
// Use "blobUrl" or "base64" for replacements // Use "blobUrl" or "base64" for replacements
let replacementsSetting = this.settings.replacements && this.settings.replacements !== "none"; let replacementsSetting = this.settings.replacements && this.settings.replacements !== "none";
// Save original url // Save original url
@ -583,7 +583,7 @@ class Book {
// Save original request method // Save original request method
let requester = this.settings.requestMethod || request.bind(this); let requester = this.settings.requestMethod || request.bind(this);
// Create new Store // Create new Store
this.storage = new Store(this.settings.store, requester, this.resolve.bind(this)); this.storage = new Store(name, requester, this.resolve.bind(this));
// Replace request method to go through store // Replace request method to go through store
this.request = this.storage.request.bind(this.storage); this.request = this.storage.request.bind(this.storage);
@ -683,7 +683,7 @@ class Book {
* @return {string} key * @return {string} key
*/ */
key(identifier) { key(identifier) {
var ident = identifier || this.package.metadata.identifier || this.url.filename; var ident = identifier || this.packaging.metadata.identifier || this.url.filename;
return `epubjs:${EPUBJS_VERSION}:${ident}`; return `epubjs:${EPUBJS_VERSION}:${ident}`;
} }

View file

@ -1031,7 +1031,7 @@ class Rendition {
* @private * @private
*/ */
injectIdentifier(doc, section) { injectIdentifier(doc, section) {
let ident = this.book.package.metadata.identifier; let ident = this.book.packaging.metadata.identifier;
let meta = doc.createElement("meta"); let meta = doc.createElement("meta");
meta.setAttribute("name", "dc.relation.ispartof"); meta.setAttribute("name", "dc.relation.ispartof");
if (ident) { if (ident) {

View file

@ -22,6 +22,15 @@ class Resources {
resolver: (options && options.resolver), resolver: (options && options.resolver),
request: (options && options.request) request: (options && options.request)
}; };
this.process(manifest);
}
/**
* Process resources
* @param {Manifest} manifest
*/
process(manifest){
this.manifest = manifest; this.manifest = manifest;
this.resources = Object.keys(manifest). this.resources = Object.keys(manifest).
map(function (key){ map(function (key){

View file

@ -25,7 +25,7 @@ class Themes {
/** /**
* Add themes to be used by a rendition * Add themes to be used by a rendition
* @param {object | string} * @param {object | Array<object> | string}
* @example themes.register("light", "http://example.com/light.css") * @example themes.register("light", "http://example.com/light.css")
* @example themes.register("light", { "body": { "color": "purple"}}) * @example themes.register("light", { "body": { "color": "purple"}})
* @example themes.register({ "light" : {...}, "dark" : {...}}) * @example themes.register({ "light" : {...}, "dark" : {...}})

9
types/book.d.ts vendored
View file

@ -16,6 +16,8 @@ import Path from "./utils/path";
import Resources from "./resources"; import Resources from "./resources";
import Container from "./container"; import Container from "./container";
import Packaging from "./packaging"; import Packaging from "./packaging";
import Store from "./store";
export interface BookOptions { export interface BookOptions {
requestMethod?: (url: string, type: string, withCredentials: object, headers: object) => Promise<object>; requestMethod?: (url: string, type: string, withCredentials: object, headers: object) => Promise<object>;
requestCredentials?: object, requestCredentials?: object,
@ -33,7 +35,7 @@ export default class Book {
settings: BookOptions; settings: BookOptions;
opening: any; // should be core.defer opening: any; // should be core.defer
opened: Promise<void> opened: Promise<Book>;
isOpen: boolean; isOpen: boolean;
loaded: { loaded: {
metadata: Promise<PackagingMetadataObject>, metadata: Promise<PackagingMetadataObject>,
@ -58,6 +60,7 @@ export default class Book {
rendition: Rendition rendition: Rendition
container: Container; container: Container;
packaging: Packaging; packaging: Packaging;
storage: Store;
canonical(path: string): string; canonical(path: string): string;
@ -70,7 +73,7 @@ export default class Book {
getRange(cfiRange: string): Range; getRange(cfiRange: string): Range;
key(identifier: string): string; key(identifier?: string): string;
load(path: string): Promise<object>; load(path: string): Promise<object>;
@ -103,6 +106,8 @@ export default class Book {
unarchive(input: BinaryType, encoding?: string): Promise<Archive>; unarchive(input: BinaryType, encoding?: string): Promise<Archive>;
store(name: string): Store;
unpack(opf: XMLDocument): Promise<Book>; unpack(opf: XMLDocument): Promise<Book>;
// Event emitters // Event emitters

View file

@ -19,9 +19,9 @@ export default class Locations {
cfiFromPercentage(percentage: number): string; cfiFromPercentage(percentage: number): string;
load(locations: JSON): Array<string>; load(locations: string): Array<string>;
save(): JSON; save(): string;
currentLocation(): Location; currentLocation(): Location;
currentLocation(curr: string | number): void; currentLocation(curr: string | number): void;

11
types/navigation.d.ts vendored
View file

@ -1,7 +1,7 @@
export interface NavItem { export interface NavItem {
id?: string, id: string,
href?: string, href: string,
label?: string, label: string,
subitems?: Array<NavItem>, subitems?: Array<NavItem>,
parent?: NavItem parent?: NavItem
} }
@ -15,13 +15,16 @@ export interface LandmarkItem {
export default class Navigation { export default class Navigation {
constructor(xml: XMLDocument); constructor(xml: XMLDocument);
toc: Array<NavItem>;
landmarks: Array<LandmarkItem>;
parse(xml: XMLDocument): void; parse(xml: XMLDocument): void;
get(target: string) : NavItem; get(target: string) : NavItem;
landmark(type: string) : LandmarkItem; landmark(type: string) : LandmarkItem;
load(json: JSON): Array<NavItem>; load(json: string): Array<NavItem>;
forEach(fn: (item: NavItem) => {}): any; forEach(fn: (item: NavItem) => {}): any;

10
types/packaging.d.ts vendored
View file

@ -45,9 +45,17 @@ export interface PackagingManifestObject {
export default class Packaging { export default class Packaging {
constructor(packageDocument: XMLDocument); constructor(packageDocument: XMLDocument);
manifest: PackagingManifestObject;
navPath: string;
ncxPath: string;
coverPath: string;
spineNodeIndex: number;
spine: Array<PackagingSpineItem>;
metadata: PackagingMetadataObject;
parse(packageDocument: XMLDocument): PackagingObject; parse(packageDocument: XMLDocument): PackagingObject;
load(json: JSON): PackagingObject; load(json: string): PackagingObject;
destroy(): void; destroy(): void;

View file

@ -9,6 +9,8 @@ export default class Resources {
request?: Function request?: Function
}); });
process(manifest: PackagingManifestObject): void;
createUrl(url: string): Promise<string>; createUrl(url: string): Promise<string>;
replacements(): Promise<Array<string>>; replacements(): Promise<Array<string>>;

2
types/section.d.ts vendored
View file

@ -28,7 +28,7 @@ export default class Section {
constructor(item: SpineItem, hooks: HooksObject); constructor(item: SpineItem, hooks: HooksObject);
idref: string; idref: string;
linear: string; linear: boolean;
properties: Array<string>; properties: Array<string>;
index: number; index: number;
href: string; href: string;

2
types/themes.d.ts vendored
View file

@ -26,7 +26,7 @@ export default class Themes {
add( name: string, contents: Contents ): void; add( name: string, contents: Contents ): void;
override(name: string, value: string, priority: boolean): void; override(name: string, value: string, priority?: boolean): void;
overrides(contents: Contents): void; overrides(contents: Contents): void;