1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-03 14:59:18 +02:00

Add locators parsing in Rendition

This commit is contained in:
Fred Chasen 2022-02-07 21:51:33 -08:00
parent e714ac778e
commit 8c9c8250a1
4 changed files with 32 additions and 21 deletions

View file

@ -35,7 +35,17 @@ class Manifest extends Publication {
const contentsUrl = this.contentsUrl; const contentsUrl = this.contentsUrl;
if (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); return this.unpack(manifest);
} }
async loadContents(contentsUrl) { async loadNavigation(contentsUrl, role="toc") {
const html = await this.load(contentsUrl, "html"); const html = await this.load(contentsUrl, "html");
let items = []; let items = [];
let url = createUrl(contentsUrl); let url = createUrl(contentsUrl);
const title = html.querySelector("h1, h2, h3, h4, h5, h6"); 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) { if (toc) {
const links = toc.querySelectorAll("ol > li > a, ul > li > a"); const links = toc.querySelectorAll("ol > li > a, ul > li > a");
for (const link of links) { for (const link of links) {

View file

@ -34,7 +34,7 @@ class Publication {
resources: new ResourceList(), resources: new ResourceList(),
links: new ResourceList(), links: new ResourceList(),
uniqueResources: new ResourceList(), uniqueResources: new ResourceList(),
displayOptions: new DisplayOptions() displayOptions: new DisplayOptions(),
}; };
if (requestMethod) { if (requestMethod) {

View file

@ -1,7 +1,5 @@
import {qs, sprint, locationOf, defer} from "../utils/core.js"; import { locationOf } from "../utils/core.js";
import Queue from "../utils/queue.js";
import EpubCFI from "../utils/epubcfi.js"; import EpubCFI from "../utils/epubcfi.js";
import { EVENTS } from "../utils/constants.js";
import EventEmitter from "../utils/eventemitter.js"; import EventEmitter from "../utils/eventemitter.js";
/** /**
@ -9,19 +7,19 @@ import EventEmitter from "../utils/eventemitter.js";
* @param {object} [manifest] * @param {object} [manifest]
*/ */
class Locators { class Locators {
constructor(manifest) { constructor(publication) {
if (manifest) { if (publication) {
this.unpack(manifest); this.unpack(publication);
} }
} }
unpack(manifest) { unpack(publication) {
if (manifest.locations) { if (publication.locations) {
this.unpackLocations(manifest.locations); this.unpackLocations(publication.locations);
} }
if (manifest.pages) { if (publication.pagelist) {
this.unpackPages(manifest.page); this.unpackPages(publication.pagelist);
} }
} }

View file

@ -19,6 +19,7 @@ import DefaultViewManager from "../managers/default/default.js";
import ContinuousViewManager from "../managers/continuous/continuous.js"; import ContinuousViewManager from "../managers/continuous/continuous.js";
import Publication from "../publication/publication.js"; import Publication from "../publication/publication.js";
import Locators from "./locators.js";
/** /**
* Displays an Epub as a series of Views for each Section. * 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); this.direction(this.publication.metadata.direction || this.settings.defaultDirection);
// Parse metadata to get layout props // Parse metadata to get layout props
@ -846,22 +849,22 @@ class Rendition {
}; };
if (this.publication.locations.length) { if (this.publication.locations.length) {
let locationStart = this.publication.locators.locationFromCfi(start.mapping.start); let locationStart = this.locators.locationFromCfi(start.mapping.start);
let locationEnd = this.publication.locators.locationFromCfi(end.mapping.end); let locationEnd = this.locators.locationFromCfi(end.mapping.end);
if (locationStart != null) { if (locationStart != null) {
located.start.location = locationStart; located.start.location = locationStart;
located.start.percentage = this.publication.locators.percentageFromLocation(locationStart); located.start.percentage = this.locators.percentageFromLocation(locationStart);
} }
if (locationEnd != null) { if (locationEnd != null) {
located.end.location = locationEnd; located.end.location = locationEnd;
located.end.percentage = this.publication.locators.percentageFromLocation(locationEnd); located.end.percentage = this.locators.percentageFromLocation(locationEnd);
} }
} }
if (this.publication.pagelist.length) { if (this.publication.pagelist.length) {
let pageStart = this.publication.locators.pageFromCfi(start.mapping.start); let pageStart = this.locators.pageFromCfi(start.mapping.start);
let pageEnd = this.publication.locators.pageFromCfi(end.mapping.end); let pageEnd = this.locators.pageFromCfi(end.mapping.end);
if (pageStart != -1) { if (pageStart != -1) {
located.start.page = pageStart; located.start.page = pageStart;