@readium/shared 2.2.0-alpha.2 → 2.2.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1032 -794
- package/dist/index.umd.cjs +3 -3
- package/package.json +11 -11
- package/src/fetcher/HttpFetcher.ts +2 -2
- package/src/fetcher/Resource.ts +4 -4
- package/src/publication/Manifest.ts +11 -13
- package/src/publication/Publication.ts +43 -0
- package/src/publication/html/Locations.ts +0 -14
- package/src/publication/services/index.ts +2 -1
- package/src/publication/services/timeline/Timeline.ts +349 -0
- package/src/publication/services/timeline/TimelineItem.ts +24 -0
- package/src/publication/services/timeline/index.ts +2 -0
- package/src/util/mediatype/MediaType.ts +4 -0
- package/types/src/fetcher/HttpFetcher.d.ts +1 -1
- package/types/src/fetcher/Resource.d.ts +1 -1
- package/types/src/publication/Manifest.d.ts +1 -1
- package/types/src/publication/Publication.d.ts +14 -0
- package/types/src/publication/html/Locations.d.ts +0 -6
- package/types/src/publication/services/index.d.ts +1 -0
- package/types/src/publication/services/timeline/Timeline.d.ts +94 -0
- package/types/src/publication/services/timeline/TimelineItem.d.ts +24 -0
- package/types/src/publication/services/timeline/index.d.ts +2 -0
- package/types/src/util/mediatype/MediaType.d.ts +1 -0
- package/types/test/AltIdentifier.test.d.ts +1 -0
- package/types/test/Contributor.test.d.ts +1 -0
- package/types/test/GuidedNavigation.test.d.ts +1 -0
- package/types/test/Link.test.d.ts +1 -0
- package/types/test/LocalizedString.test.d.ts +1 -0
- package/types/test/Locator.test.d.ts +1 -0
- package/types/test/LocatorCollection.test.d.ts +1 -0
- package/types/test/Manifest.test.d.ts +1 -0
- package/types/test/Metadata.test.d.ts +1 -0
- package/types/test/Properties.test.d.ts +1 -0
- package/types/test/Publication.test.d.ts +1 -0
- package/types/test/PublicationCollection.test.d.ts +1 -0
- package/types/test/Subject.test.d.ts +1 -0
- package/types/test/TDM.test.d.ts +1 -0
- package/types/test/Timeline.test.d.ts +1 -0
- package/types/test/accessibility/Accessibility.test.d.ts +1 -0
- package/types/test/accessibility/AccessibilityMetadataDisplayGuide.test.d.ts +1 -0
- package/types/test/accessibility/Localization.test.d.ts +1 -0
- package/types/test/encryption/Encryption.test.d.ts +1 -0
- package/types/test/encryption/Properties.test.d.ts +1 -0
- package/types/test/epub/MediaOverlay.test.d.ts +1 -0
- package/types/test/epub/Metadata.test.d.ts +1 -0
- package/types/test/epub/PropertiesTest.test.d.ts +1 -0
- package/types/test/epub/PublicationTest.test.d.ts +1 -0
- package/types/test/html/DomRange.test.d.ts +1 -0
- package/types/test/html/Locator.test.d.ts +1 -0
- package/types/test/opds/PropertiesTest.test.d.ts +1 -0
- package/types/test/opds/PublicationTest.test.d.ts +1 -0
- package/types/test/util/URITemplate.test.d.ts +1 -0
- package/types/test/util/mediatype/MediaType.test.d.ts +1 -0
- package/LICENSE +0 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@readium/shared",
|
|
3
|
-
"version": "2.2.0-
|
|
3
|
+
"version": "2.2.0-beta.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Shared models to be used across other Readium projects and implementations in Typescript",
|
|
6
6
|
"author": "readium",
|
|
@@ -44,6 +44,15 @@
|
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=18"
|
|
46
46
|
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"dev": "vite",
|
|
49
|
+
"clean": "rimraf types dist",
|
|
50
|
+
"build": "pnpm clean && tsc && vite build",
|
|
51
|
+
"test": "jest",
|
|
52
|
+
"size": "size-limit",
|
|
53
|
+
"check-locales": "node scripts/check-locales.mjs",
|
|
54
|
+
"update-locales": "pnpm remove @edrlab/thorium-locales && pnpm add @edrlab/thorium-locales@github:edrlab/thorium-locales && pnpm check-locales"
|
|
55
|
+
},
|
|
47
56
|
"size-limit": [
|
|
48
57
|
{
|
|
49
58
|
"path": "dist/index.js",
|
|
@@ -68,14 +77,5 @@
|
|
|
68
77
|
"tslib": "^2.8.1",
|
|
69
78
|
"typescript": "^5.9.3",
|
|
70
79
|
"vite": "^7.3.1"
|
|
71
|
-
},
|
|
72
|
-
"scripts": {
|
|
73
|
-
"dev": "vite",
|
|
74
|
-
"clean": "rimraf types dist",
|
|
75
|
-
"build": "pnpm clean && tsc && vite build",
|
|
76
|
-
"test": "jest",
|
|
77
|
-
"size": "size-limit",
|
|
78
|
-
"check-locales": "node scripts/check-locales.mjs",
|
|
79
|
-
"update-locales": "pnpm remove @edrlab/thorium-locales && pnpm add @edrlab/thorium-locales@github:edrlab/thorium-locales && pnpm check-locales"
|
|
80
80
|
}
|
|
81
|
-
}
|
|
81
|
+
}
|
|
@@ -108,7 +108,7 @@ export class HttpResource implements Resource {
|
|
|
108
108
|
return await resp.text();
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
async readAsXML(): Promise<
|
|
111
|
+
async readAsXML(): Promise<XMLDocument> {
|
|
112
112
|
const resp = await this.client(this.url);
|
|
113
113
|
if (!resp.ok)
|
|
114
114
|
throw new Error(
|
|
@@ -116,7 +116,7 @@ export class HttpResource implements Resource {
|
|
|
116
116
|
); // TODO
|
|
117
117
|
return new DOMParser().parseFromString(
|
|
118
118
|
await resp.text(),
|
|
119
|
-
|
|
119
|
+
'application/xml'
|
|
120
120
|
);
|
|
121
121
|
}
|
|
122
122
|
}
|
package/src/fetcher/Resource.ts
CHANGED
|
@@ -27,11 +27,11 @@ export abstract class Resource {
|
|
|
27
27
|
return JSON.parse(str);
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
-
readAsXML(): Promise<
|
|
31
|
-
return this.
|
|
30
|
+
readAsXML(): Promise<XMLDocument | undefined> {
|
|
31
|
+
return this.readAsString().then(str => {
|
|
32
32
|
if (str === undefined) return str;
|
|
33
|
-
return new DOMParser().parseFromString(str,
|
|
34
|
-
})
|
|
33
|
+
return new DOMParser().parseFromString(str, 'text/xml');
|
|
34
|
+
});
|
|
35
35
|
}
|
|
36
36
|
abstract close(): void;
|
|
37
37
|
}
|
|
@@ -35,7 +35,7 @@ export class Manifest {
|
|
|
35
35
|
constructor(values: {
|
|
36
36
|
context?: Array<string>;
|
|
37
37
|
metadata: Metadata;
|
|
38
|
-
links
|
|
38
|
+
links: Links;
|
|
39
39
|
readingOrder: Links;
|
|
40
40
|
resources?: Links;
|
|
41
41
|
toc?: Links;
|
|
@@ -43,7 +43,7 @@ export class Manifest {
|
|
|
43
43
|
}) {
|
|
44
44
|
this.context = values.context;
|
|
45
45
|
this.metadata = values.metadata;
|
|
46
|
-
this.links = values.links
|
|
46
|
+
this.links = values.links;
|
|
47
47
|
this.readingOrder = values.readingOrder;
|
|
48
48
|
this.resources = values.resources;
|
|
49
49
|
this.toc = values.toc;
|
|
@@ -63,6 +63,10 @@ export class Manifest {
|
|
|
63
63
|
|
|
64
64
|
if (!metadata) return;
|
|
65
65
|
|
|
66
|
+
const links = Links.deserialize(json.links);
|
|
67
|
+
|
|
68
|
+
if (!links) return;
|
|
69
|
+
|
|
66
70
|
const readingOrder = Links.deserialize(
|
|
67
71
|
json.readingOrder ? json.readingOrder : json.spine
|
|
68
72
|
);
|
|
@@ -72,7 +76,7 @@ export class Manifest {
|
|
|
72
76
|
return new Manifest({
|
|
73
77
|
context: arrayfromJSONorString(json['@context']),
|
|
74
78
|
metadata,
|
|
75
|
-
links
|
|
79
|
+
links,
|
|
76
80
|
readingOrder,
|
|
77
81
|
resources: Links.deserialize(json.resources),
|
|
78
82
|
toc: Links.deserialize(json.toc),
|
|
@@ -89,7 +93,7 @@ export class Manifest {
|
|
|
89
93
|
const json: any = {};
|
|
90
94
|
if (this.context !== undefined) json['@context'] = this.context;
|
|
91
95
|
json.metadata = this.metadata.serialize();
|
|
92
|
-
|
|
96
|
+
json.links = this.links.serialize();
|
|
93
97
|
json.readingOrder = this.readingOrder.serialize();
|
|
94
98
|
if (this.resources) json.resources = this.resources.serialize();
|
|
95
99
|
if (this.toc) json.toc = this.toc.serialize();
|
|
@@ -104,9 +108,7 @@ export class Manifest {
|
|
|
104
108
|
if (this.resources) {
|
|
105
109
|
links.push(this.resources);
|
|
106
110
|
}
|
|
107
|
-
|
|
108
|
-
links.push(this.links);
|
|
109
|
-
}
|
|
111
|
+
links.push(this.links);
|
|
110
112
|
|
|
111
113
|
let result: Link | undefined;
|
|
112
114
|
|
|
@@ -127,9 +129,7 @@ export class Manifest {
|
|
|
127
129
|
if (this.resources) {
|
|
128
130
|
result.push(this.resources.filterByRel(rel));
|
|
129
131
|
}
|
|
130
|
-
|
|
131
|
-
result.push(this.links.filterByRel(rel));
|
|
132
|
-
}
|
|
132
|
+
result.push(this.links.filterByRel(rel));
|
|
133
133
|
|
|
134
134
|
return result.reduce((acc, val) => acc.concat(val), []);
|
|
135
135
|
}
|
|
@@ -197,9 +197,7 @@ export class Manifest {
|
|
|
197
197
|
if (this.resources) {
|
|
198
198
|
links.push(this.resources);
|
|
199
199
|
}
|
|
200
|
-
|
|
201
|
-
links.push(this.links);
|
|
202
|
-
}
|
|
200
|
+
links.push(this.links);
|
|
203
201
|
|
|
204
202
|
const link = find(links);
|
|
205
203
|
|
|
@@ -12,6 +12,8 @@ import { PublicationCollection } from './PublicationCollection';
|
|
|
12
12
|
import { Resource } from '../fetcher/Resource';
|
|
13
13
|
import { GuidedNavigationDocument } from "./GuidedNavigation";
|
|
14
14
|
import { URITemplate } from "../util";
|
|
15
|
+
import { MediaType } from "../util/mediatype/MediaType";
|
|
16
|
+
import { Timeline } from './services/timeline/Timeline';
|
|
15
17
|
|
|
16
18
|
export type ServiceFactory = () => null;
|
|
17
19
|
|
|
@@ -20,6 +22,7 @@ export class Publication {
|
|
|
20
22
|
/** The manifest holding the publication metadata extracted from the publication file */
|
|
21
23
|
public manifest: Manifest;
|
|
22
24
|
private readonly fetcher: Fetcher = new EmptyFetcher();
|
|
25
|
+
private _timeline: Timeline | undefined;
|
|
23
26
|
|
|
24
27
|
// Shortcuts to manifest properties
|
|
25
28
|
public readonly context?: Array<string>;
|
|
@@ -46,6 +49,16 @@ export class Publication {
|
|
|
46
49
|
this.subcollections = values.manifest.subcollections;
|
|
47
50
|
}
|
|
48
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Returns the publication's timeline, built from its reading order and table of contents.
|
|
54
|
+
* Called with no arguments, the result is cached. Pass options to build a fresh timeline
|
|
55
|
+
* with a specific configuration (e.g. a depth limit).
|
|
56
|
+
*/
|
|
57
|
+
public get timeline(): Timeline {
|
|
58
|
+
if (!this._timeline) this._timeline = Timeline.build(this);
|
|
59
|
+
return this._timeline;
|
|
60
|
+
}
|
|
61
|
+
|
|
49
62
|
/** The URL where this publication is served, computed from the `Link` with `self` relation.
|
|
50
63
|
* e.g. https://provider.com/pub1293/manifest.json gives https://provider.com/pub1293/
|
|
51
64
|
*/
|
|
@@ -79,6 +92,36 @@ export class Publication {
|
|
|
79
92
|
return this.manifest.linkWithRel(rel);
|
|
80
93
|
}
|
|
81
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Gets the cover image for the publication.
|
|
97
|
+
* First looks for rel='cover' in links/resources/readingOrder,
|
|
98
|
+
* then falls back to any image (JPEG, PNG, GIF, AVIF, SVG) as bitmap fallback.
|
|
99
|
+
*/
|
|
100
|
+
public getCover(): Link | undefined {
|
|
101
|
+
const locations = [
|
|
102
|
+
this.links,
|
|
103
|
+
this.resources,
|
|
104
|
+
this.readingOrder
|
|
105
|
+
].filter(Boolean) as Links[];
|
|
106
|
+
|
|
107
|
+
// First, look for explicit cover relationship
|
|
108
|
+
for (const location of locations) {
|
|
109
|
+
const cover = location.items.find(link => link.rels?.has('cover'));
|
|
110
|
+
if (cover) return cover;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Fallback to any image (including SVG which is not bitmap)
|
|
114
|
+
for (const location of locations) {
|
|
115
|
+
const image = location.items.find(link =>
|
|
116
|
+
link.mediaType.isBitmap ||
|
|
117
|
+
link.mediaType.matches(MediaType.SVG)
|
|
118
|
+
);
|
|
119
|
+
if (image) return image;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
82
125
|
public async positionsFromManifest(): Promise<Locator[]> {
|
|
83
126
|
const positionListLink = this.manifest.links.findWithMediaType(
|
|
84
127
|
'application/vnd.readium.position-list+json'
|
|
@@ -53,13 +53,6 @@ declare module '../Locator' {
|
|
|
53
53
|
* https://www.w3.org/TR/media-frags/
|
|
54
54
|
*/
|
|
55
55
|
space(): [number, number, number, number] | undefined;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Text fragments, used by browsers to jump to/highlight text.
|
|
59
|
-
*
|
|
60
|
-
* https://developer.mozilla.org/en-US/docs/Web/URI/Fragment/Text_fragments
|
|
61
|
-
*/
|
|
62
|
-
text(): string | undefined;
|
|
63
56
|
}
|
|
64
57
|
}
|
|
65
58
|
|
|
@@ -133,11 +126,4 @@ LocatorLocations.prototype.space = function(): [number, number, number, number]
|
|
|
133
126
|
if(xywh.length !== 4) return; // Must have four parts
|
|
134
127
|
if(xywh.some(isNaN)) return; // All parts must be numbers
|
|
135
128
|
return xywh as [number, number, number, number];
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
LocatorLocations.prototype.text = function(): string | undefined {
|
|
139
|
-
const fp = this.fragmentParameters();
|
|
140
|
-
console.log(fp);
|
|
141
|
-
// :~:
|
|
142
|
-
return undefined;
|
|
143
129
|
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from './content';
|
|
1
|
+
export * from './content';
|
|
2
|
+
export * from './timeline';
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { Link, Links } from "../../Link";
|
|
2
|
+
import { Locator } from "../../Locator";
|
|
3
|
+
import { TimelineItem } from "./TimelineItem";
|
|
4
|
+
|
|
5
|
+
interface PublicationLike {
|
|
6
|
+
toc?: Links;
|
|
7
|
+
readingOrder: Links;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A publication's timeline, built once from its reading order and table of contents.
|
|
12
|
+
*
|
|
13
|
+
* **Source of truth**: the reading order. Every reading order item becomes
|
|
14
|
+
* exactly one top-level `TimelineItem`. The TOC is consulted to:
|
|
15
|
+
* 1. Derive a display title when the reading order item has none.
|
|
16
|
+
* 2. Populate flat children — all TOC fragment entries that reference the
|
|
17
|
+
* resource, collected depth-first in TOC declaration order.
|
|
18
|
+
*
|
|
19
|
+
* No TOC hierarchy is reconstructed; that requires role context and is not yet
|
|
20
|
+
* implemented. TOC entries whose href does not match any reading order item
|
|
21
|
+
* are ignored.
|
|
22
|
+
*
|
|
23
|
+
* The `depth` build option limits how many levels deep into the TOC tree both
|
|
24
|
+
* title resolution and child collection may look. Level 1 = top-level TOC
|
|
25
|
+
* entries; level 2 = their children; etc. `undefined` means no limit.
|
|
26
|
+
*/
|
|
27
|
+
export class Timeline {
|
|
28
|
+
private readonly _allItems: TimelineItem[];
|
|
29
|
+
private readonly linkMap: Map<TimelineItem, Link>;
|
|
30
|
+
private _depth: number | undefined;
|
|
31
|
+
private _items: TimelineItem[] | undefined;
|
|
32
|
+
private _flat: TimelineItem[] | undefined;
|
|
33
|
+
|
|
34
|
+
constructor(items: TimelineItem[], linkMap: Map<TimelineItem, Link>) {
|
|
35
|
+
this._allItems = items;
|
|
36
|
+
this.linkMap = linkMap;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static build(publication: PublicationLike, options: { depth?: number } = {}): Timeline {
|
|
40
|
+
const tocLinks = publication.toc?.items ?? [];
|
|
41
|
+
const roLinks = publication.readingOrder.items;
|
|
42
|
+
const { depth } = options;
|
|
43
|
+
const linkMap = new Map<TimelineItem, Link>();
|
|
44
|
+
const items: TimelineItem[] = [];
|
|
45
|
+
|
|
46
|
+
for (let i = 0; i < roLinks.length; i++) {
|
|
47
|
+
const ro = roLinks[i];
|
|
48
|
+
const bare = Timeline.bareHref(ro.href);
|
|
49
|
+
|
|
50
|
+
const title =
|
|
51
|
+
ro.title ??
|
|
52
|
+
Timeline.findTitleInToc(tocLinks, bare, depth) ??
|
|
53
|
+
`Resource ${i + 1}`;
|
|
54
|
+
|
|
55
|
+
const tocChildren = Timeline.collectChildrenFromToc(tocLinks, bare, depth, 1, linkMap);
|
|
56
|
+
|
|
57
|
+
const item: TimelineItem = {
|
|
58
|
+
title,
|
|
59
|
+
references: [ro.href],
|
|
60
|
+
children: tocChildren.length > 0 ? tocChildren : undefined,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const tocLink = Timeline.findTocLink(tocLinks, bare, depth);
|
|
64
|
+
linkMap.set(item, tocLink ?? ro);
|
|
65
|
+
items.push(item);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return new Timeline(items, linkMap);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The active depth limit for TOC traversal (title resolution and child
|
|
73
|
+
* collection). `undefined` means unlimited. Setting the same value again
|
|
74
|
+
* is a no-op; a new value invalidates the cached items and flat list.
|
|
75
|
+
*/
|
|
76
|
+
get depth(): number | undefined {
|
|
77
|
+
return this._depth;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
set depth(value: number | undefined) {
|
|
81
|
+
if (this._depth === value) return;
|
|
82
|
+
this._depth = value;
|
|
83
|
+
this._items = undefined;
|
|
84
|
+
this._flat = undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Top-level timeline items. Cached; invalidated when `depth` changes. */
|
|
88
|
+
get items(): TimelineItem[] {
|
|
89
|
+
if (!this._items) {
|
|
90
|
+
this._items = this._depth !== undefined
|
|
91
|
+
? Timeline.trimToDepth(this._allItems, this._depth)
|
|
92
|
+
: this._allItems;
|
|
93
|
+
}
|
|
94
|
+
return this._items;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
locate(locator: Locator): TimelineItem | undefined {
|
|
98
|
+
const href = locator.href.split("#")[0];
|
|
99
|
+
const time = locator.locations?.time();
|
|
100
|
+
|
|
101
|
+
let match: TimelineItem | undefined;
|
|
102
|
+
|
|
103
|
+
if (time !== undefined) {
|
|
104
|
+
for (const item of this.flat) {
|
|
105
|
+
if (this.itemMatchesTime(item, href, time)) match = item;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!match) {
|
|
110
|
+
match = this.flat.find(item => this.bareHrefFromItem(item) === href);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return match;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
adjacentTo(item: TimelineItem): { previous: TimelineItem | undefined; next: TimelineItem | undefined } {
|
|
117
|
+
const index = this.flat.indexOf(item);
|
|
118
|
+
return {
|
|
119
|
+
previous: index > 0 ? this.flat[index - 1] : undefined,
|
|
120
|
+
next: index >= 0 && index < this.flat.length - 1 ? this.flat[index + 1] : undefined,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
segmentsForHref(href: string): TimelineItem[] {
|
|
125
|
+
const bare = href.split("#")[0];
|
|
126
|
+
const item = this.items.find(i => this.bareHrefFromItem(i) === bare);
|
|
127
|
+
if (!item) return [];
|
|
128
|
+
return item.children?.length ? item.children : [item];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
itemAtProgression(href: string, progression: number, duration?: number): TimelineItem | undefined {
|
|
132
|
+
const bare = href.split("#")[0];
|
|
133
|
+
const item = this.items.find(i => this.bareHrefFromItem(i) === bare);
|
|
134
|
+
if (!item) return undefined;
|
|
135
|
+
if (!item.children?.length) return item;
|
|
136
|
+
|
|
137
|
+
if (duration !== undefined) {
|
|
138
|
+
const time = progression * duration;
|
|
139
|
+
let match: TimelineItem = item;
|
|
140
|
+
for (const child of item.children) {
|
|
141
|
+
const t = this.timeFromItem(child);
|
|
142
|
+
if (t === undefined || t > time) break;
|
|
143
|
+
match = child;
|
|
144
|
+
}
|
|
145
|
+
return match;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const index = Math.min(Math.floor(progression * item.children.length), item.children.length - 1);
|
|
149
|
+
return item.children[index];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
ancestors(item: TimelineItem): TimelineItem[] {
|
|
153
|
+
return this.ancestorPath(this.items, item) ?? [];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
linkFor(item: TimelineItem): Link | undefined {
|
|
157
|
+
return this.linkMap.get(item);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private get flat(): TimelineItem[] {
|
|
161
|
+
if (!this._flat) this._flat = this.flattenItems(this.items);
|
|
162
|
+
return this._flat;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// -------------------------------------------------------------------------
|
|
166
|
+
// TOC title resolution
|
|
167
|
+
// -------------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Resolve a display title for `bare` from the TOC, looking no deeper than
|
|
171
|
+
* `maxDepth` levels (1 = top-level entries only). Priority:
|
|
172
|
+
* 1. A start-of-resource entry: bare href or `#t=0` (audio).
|
|
173
|
+
* 2. Exactly one fragment entry referencing this resource — unambiguous.
|
|
174
|
+
* 3. Multiple fragment entries — ambiguous, returns `undefined`.
|
|
175
|
+
*/
|
|
176
|
+
private static findTitleInToc(
|
|
177
|
+
tocLinks: Link[],
|
|
178
|
+
bare: string,
|
|
179
|
+
maxDepth: number | undefined,
|
|
180
|
+
): string | undefined {
|
|
181
|
+
const { atStart, fragments } = Timeline.collectTocCandidates(tocLinks, bare, maxDepth, 1);
|
|
182
|
+
if (atStart.length > 0) return atStart[0].title!;
|
|
183
|
+
if (fragments.length === 1) return fragments[0].title!;
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Return the most relevant TOC Link for `bare` (used for `linkFor`).
|
|
189
|
+
* Prefers a start-of-resource entry; falls back to the first fragment entry.
|
|
190
|
+
*/
|
|
191
|
+
private static findTocLink(
|
|
192
|
+
tocLinks: Link[],
|
|
193
|
+
bare: string,
|
|
194
|
+
maxDepth: number | undefined,
|
|
195
|
+
): Link | undefined {
|
|
196
|
+
const { atStart, fragments } = Timeline.collectTocCandidates(tocLinks, bare, maxDepth, 1);
|
|
197
|
+
return atStart[0] ?? fragments[0];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Collect all TOC entries that reference `bare` as flat children, walking
|
|
202
|
+
* up to `maxDepth` levels deep. Start-of-resource entries (#t=0 or no
|
|
203
|
+
* fragment) are excluded — they contribute to title resolution, not children.
|
|
204
|
+
* Order follows TOC declaration (depth-first).
|
|
205
|
+
*/
|
|
206
|
+
private static collectChildrenFromToc(
|
|
207
|
+
tocLinks: Link[],
|
|
208
|
+
bare: string,
|
|
209
|
+
maxDepth: number | undefined,
|
|
210
|
+
currentDepth: number,
|
|
211
|
+
linkMap: Map<TimelineItem, Link>,
|
|
212
|
+
): TimelineItem[] {
|
|
213
|
+
if (maxDepth !== undefined && currentDepth > maxDepth) return [];
|
|
214
|
+
|
|
215
|
+
const result: TimelineItem[] = [];
|
|
216
|
+
|
|
217
|
+
for (const link of tocLinks) {
|
|
218
|
+
if (Timeline.bareHref(link.href) === bare && link.title && !Timeline.isStartOfResource(link.href)) {
|
|
219
|
+
const child: TimelineItem = { title: link.title, references: [link.href] };
|
|
220
|
+
linkMap.set(child, link);
|
|
221
|
+
result.push(child);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (link.children?.items?.length) {
|
|
225
|
+
result.push(...Timeline.collectChildrenFromToc(
|
|
226
|
+
link.children.items, bare, maxDepth, currentDepth + 1, linkMap,
|
|
227
|
+
));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Walk the TOC tree up to `maxDepth` levels and collect all entries whose
|
|
236
|
+
* bare href matches `bare`, split into start-of-resource entries and
|
|
237
|
+
* fragment entries.
|
|
238
|
+
*/
|
|
239
|
+
private static collectTocCandidates(
|
|
240
|
+
tocLinks: Link[],
|
|
241
|
+
bare: string,
|
|
242
|
+
maxDepth: number | undefined,
|
|
243
|
+
currentDepth: number,
|
|
244
|
+
): { atStart: Link[]; fragments: Link[] } {
|
|
245
|
+
if (maxDepth !== undefined && currentDepth > maxDepth) {
|
|
246
|
+
return { atStart: [], fragments: [] };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const atStart: Link[] = [];
|
|
250
|
+
const fragments: Link[] = [];
|
|
251
|
+
|
|
252
|
+
for (const link of tocLinks) {
|
|
253
|
+
if (Timeline.bareHref(link.href) === bare && link.title) {
|
|
254
|
+
if (Timeline.isStartOfResource(link.href)) {
|
|
255
|
+
atStart.push(link);
|
|
256
|
+
} else {
|
|
257
|
+
fragments.push(link);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (link.children?.items?.length) {
|
|
262
|
+
const child = Timeline.collectTocCandidates(
|
|
263
|
+
link.children.items, bare, maxDepth, currentDepth + 1,
|
|
264
|
+
);
|
|
265
|
+
atStart.push(...child.atStart);
|
|
266
|
+
fragments.push(...child.fragments);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return { atStart, fragments };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* A TOC href points to the start of its resource when it has no fragment,
|
|
275
|
+
* or when the fragment is `t=0` (audio: explicit beginning of the file).
|
|
276
|
+
*/
|
|
277
|
+
private static isStartOfResource(href: string): boolean {
|
|
278
|
+
const fragment = href.split("#")[1];
|
|
279
|
+
if (!fragment) return true;
|
|
280
|
+
return fragment === "t=0";
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// -------------------------------------------------------------------------
|
|
284
|
+
// Shared utilities
|
|
285
|
+
// -------------------------------------------------------------------------
|
|
286
|
+
|
|
287
|
+
private static trimToDepth(items: TimelineItem[], remaining: number): TimelineItem[] {
|
|
288
|
+
return items.map(item => ({
|
|
289
|
+
...item,
|
|
290
|
+
children: remaining > 1 && item.children?.length
|
|
291
|
+
? Timeline.trimToDepth(item.children, remaining - 1)
|
|
292
|
+
: undefined,
|
|
293
|
+
}));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private flattenItems(items: TimelineItem[]): TimelineItem[] {
|
|
297
|
+
const result: TimelineItem[] = [];
|
|
298
|
+
for (const item of items) {
|
|
299
|
+
result.push(item);
|
|
300
|
+
if (item.children) result.push(...this.flattenItems(item.children));
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private itemMatchesTime(item: TimelineItem, href: string, time: number): boolean {
|
|
306
|
+
for (const ref of item.references) {
|
|
307
|
+
const hashIndex = ref.indexOf("#");
|
|
308
|
+
const refHref = hashIndex >= 0 ? ref.slice(0, hashIndex) : ref;
|
|
309
|
+
const refFragment = hashIndex >= 0 ? ref.slice(hashIndex + 1) : undefined;
|
|
310
|
+
if ((refHref || href) !== href) continue;
|
|
311
|
+
if (time >= Timeline.parseTimeFragment(refFragment)) return true;
|
|
312
|
+
}
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private static parseTimeFragment(fragment: string | undefined): number {
|
|
317
|
+
if (!fragment) return 0;
|
|
318
|
+
const match = fragment.match(/(?:^|&)t=(\d+(?:\.\d+)?)/);
|
|
319
|
+
return match ? parseFloat(match[1]) : 0;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private bareHrefFromItem(item: TimelineItem): string {
|
|
323
|
+
return (item.references[0] ?? "").split("#")[0];
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private timeFromItem(item: TimelineItem): number | undefined {
|
|
327
|
+
const ref = item.references[0];
|
|
328
|
+
if (!ref) return undefined;
|
|
329
|
+
const fragment = ref.split("#")[1];
|
|
330
|
+
if (!fragment) return undefined;
|
|
331
|
+
const match = fragment.match(/(?:^|&)t=(\d+(?:\.\d+)?)/);
|
|
332
|
+
return match ? parseFloat(match[1]) : undefined;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
private ancestorPath(items: TimelineItem[], target: TimelineItem): TimelineItem[] | null {
|
|
336
|
+
for (const item of items) {
|
|
337
|
+
if (item === target) return [];
|
|
338
|
+
if (item.children) {
|
|
339
|
+
const path = this.ancestorPath(item.children, target);
|
|
340
|
+
if (path !== null) return [item, ...path];
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private static bareHref(href: string): string {
|
|
347
|
+
return href.split("#")[0];
|
|
348
|
+
}
|
|
349
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a single entry in a publication's timeline.
|
|
3
|
+
*
|
|
4
|
+
* The timeline contextualizes the reading order and table of contents so that
|
|
5
|
+
* consuming apps can display the current chapter title, build a progress bar,
|
|
6
|
+
* show previous/next as chapter titles, group search results by chapter, etc.
|
|
7
|
+
*/
|
|
8
|
+
export interface TimelineItem {
|
|
9
|
+
/** Display title of this entry. */
|
|
10
|
+
title: string;
|
|
11
|
+
/**
|
|
12
|
+
* References as hrefs with optional fragments.
|
|
13
|
+
* e.g. ["track1.mp3#t=60"] for audio, ["chapter1.html"] for EPUB, ["#page=6"] for PDF.
|
|
14
|
+
*/
|
|
15
|
+
references: string[];
|
|
16
|
+
/** Structural roles of this entry, e.g. ["chapter"], ["part"]. */
|
|
17
|
+
role?: string[];
|
|
18
|
+
/** Position number in the reading order context. */
|
|
19
|
+
position?: number;
|
|
20
|
+
/** Scroll progression within the resource (0 to 1), for entries that start mid-way in a resource. */
|
|
21
|
+
scroll?: number;
|
|
22
|
+
/** Nested entries. */
|
|
23
|
+
children?: TimelineItem[];
|
|
24
|
+
}
|
|
@@ -224,6 +224,7 @@ export class MediaType {
|
|
|
224
224
|
/** Returns whether this media type is of a bitmap image, so excluding vectorial formats. */
|
|
225
225
|
public get isBitmap(): boolean {
|
|
226
226
|
return this.matchesAny(
|
|
227
|
+
MediaType.AVIF,
|
|
227
228
|
MediaType.BMP,
|
|
228
229
|
MediaType.GIF,
|
|
229
230
|
MediaType.JPEG,
|
|
@@ -292,6 +293,9 @@ export class MediaType {
|
|
|
292
293
|
fileExtension: 'avi',
|
|
293
294
|
});
|
|
294
295
|
}
|
|
296
|
+
public static get AVIF(): MediaType {
|
|
297
|
+
return MediaType.parse({ mediaType: 'image/avif', fileExtension: 'avif' });
|
|
298
|
+
}
|
|
295
299
|
public static get BINARY(): MediaType {
|
|
296
300
|
return MediaType.parse({ mediaType: 'application/octet-stream' });
|
|
297
301
|
}
|
|
@@ -10,6 +10,6 @@ export declare abstract class Resource {
|
|
|
10
10
|
abstract read(range?: NumberRange): Promise<Uint8Array | undefined>;
|
|
11
11
|
readAsString(): Promise<string | undefined>;
|
|
12
12
|
readAsJSON(): Promise<unknown | undefined>;
|
|
13
|
-
readAsXML(): Promise<
|
|
13
|
+
readAsXML(): Promise<XMLDocument | undefined>;
|
|
14
14
|
abstract close(): void;
|
|
15
15
|
}
|