mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-02 14:49:16 +02:00
Add locators parsing in Rendition
This commit is contained in:
parent
e714ac778e
commit
8c9c8250a1
4 changed files with 32 additions and 21 deletions
|
@ -35,7 +35,17 @@ class Manifest extends Publication {
|
|||
|
||||
const contentsUrl = this.contentsUrl;
|
||||
if (contentsUrl) {
|
||||
this.contents = await this.loadContents(contentsUrl);
|
||||
this.contents = await this.loadNavigation(contentsUrl, "toc");
|
||||
}
|
||||
|
||||
const landmarksUrl = this.landmarksUrl;
|
||||
if (landmarksUrl) {
|
||||
this.landmarks = await this.loadNavigation(landmarksUrl, "landmarks");
|
||||
}
|
||||
|
||||
const pagelistUrl = this.pagelistUrl;
|
||||
if (pagelistUrl) {
|
||||
this.pagelist = await this.loadNavigation(pagelistUrl, "pagelist");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -48,13 +58,13 @@ class Manifest extends Publication {
|
|||
return this.unpack(manifest);
|
||||
}
|
||||
|
||||
async loadContents(contentsUrl) {
|
||||
async loadNavigation(contentsUrl, role="toc") {
|
||||
const html = await this.load(contentsUrl, "html");
|
||||
let items = [];
|
||||
let url = createUrl(contentsUrl);
|
||||
|
||||
const title = html.querySelector("h1, h2, h3, h4, h5, h6");
|
||||
const toc = html.querySelector("*[role='doc-toc']");
|
||||
const toc = html.querySelector(`*[role='doc-${role}']`);
|
||||
if (toc) {
|
||||
const links = toc.querySelectorAll("ol > li > a, ul > li > a");
|
||||
for (const link of links) {
|
||||
|
|
|
@ -34,7 +34,7 @@ class Publication {
|
|||
resources: new ResourceList(),
|
||||
links: new ResourceList(),
|
||||
uniqueResources: new ResourceList(),
|
||||
displayOptions: new DisplayOptions()
|
||||
displayOptions: new DisplayOptions(),
|
||||
};
|
||||
|
||||
if (requestMethod) {
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import {qs, sprint, locationOf, defer} from "../utils/core.js";
|
||||
import Queue from "../utils/queue.js";
|
||||
import { locationOf } from "../utils/core.js";
|
||||
import EpubCFI from "../utils/epubcfi.js";
|
||||
import { EVENTS } from "../utils/constants.js";
|
||||
import EventEmitter from "../utils/eventemitter.js";
|
||||
|
||||
/**
|
||||
|
@ -9,19 +7,19 @@ import EventEmitter from "../utils/eventemitter.js";
|
|||
* @param {object} [manifest]
|
||||
*/
|
||||
class Locators {
|
||||
constructor(manifest) {
|
||||
if (manifest) {
|
||||
this.unpack(manifest);
|
||||
constructor(publication) {
|
||||
if (publication) {
|
||||
this.unpack(publication);
|
||||
}
|
||||
}
|
||||
|
||||
unpack(manifest) {
|
||||
if (manifest.locations) {
|
||||
this.unpackLocations(manifest.locations);
|
||||
unpack(publication) {
|
||||
if (publication.locations) {
|
||||
this.unpackLocations(publication.locations);
|
||||
}
|
||||
|
||||
if (manifest.pages) {
|
||||
this.unpackPages(manifest.page);
|
||||
if (publication.pagelist) {
|
||||
this.unpackPages(publication.pagelist);
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ import DefaultViewManager from "../managers/default/default.js";
|
|||
import ContinuousViewManager from "../managers/continuous/continuous.js";
|
||||
|
||||
import Publication from "../publication/publication.js";
|
||||
import Locators from "./locators.js";
|
||||
|
||||
/**
|
||||
* Displays an Epub as a series of Views for each Section.
|
||||
|
@ -253,6 +254,8 @@ class Rendition {
|
|||
});
|
||||
}
|
||||
|
||||
this.locators = new Locators(this.publication);
|
||||
|
||||
this.direction(this.publication.metadata.direction || this.settings.defaultDirection);
|
||||
|
||||
// Parse metadata to get layout props
|
||||
|
@ -846,22 +849,22 @@ class Rendition {
|
|||
};
|
||||
|
||||
if (this.publication.locations.length) {
|
||||
let locationStart = this.publication.locators.locationFromCfi(start.mapping.start);
|
||||
let locationEnd = this.publication.locators.locationFromCfi(end.mapping.end);
|
||||
let locationStart = this.locators.locationFromCfi(start.mapping.start);
|
||||
let locationEnd = this.locators.locationFromCfi(end.mapping.end);
|
||||
|
||||
if (locationStart != null) {
|
||||
located.start.location = locationStart;
|
||||
located.start.percentage = this.publication.locators.percentageFromLocation(locationStart);
|
||||
located.start.percentage = this.locators.percentageFromLocation(locationStart);
|
||||
}
|
||||
if (locationEnd != null) {
|
||||
located.end.location = locationEnd;
|
||||
located.end.percentage = this.publication.locators.percentageFromLocation(locationEnd);
|
||||
located.end.percentage = this.locators.percentageFromLocation(locationEnd);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.publication.pagelist.length) {
|
||||
let pageStart = this.publication.locators.pageFromCfi(start.mapping.start);
|
||||
let pageEnd = this.publication.locators.pageFromCfi(end.mapping.end);
|
||||
let pageStart = this.locators.pageFromCfi(start.mapping.start);
|
||||
let pageEnd = this.locators.pageFromCfi(end.mapping.end);
|
||||
|
||||
if (pageStart != -1) {
|
||||
located.start.page = pageStart;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue