@readium/shared 2.3.0 → 2.3.1
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/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022, Readium Foundation
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getTime as g,getHtmlId as
|
|
1
|
+
import{getTime as g,getHtmlId as v}from"../../html/Locations.js";import{Profile as p}from"../../Profiles.js";import{isNptStartOfResource as k,formatNptTime as I,parseNptTime as T}from"../../../util/npt.js";class l{constructor(i,t,e=[],s=[],n=void 0){this._trimmedLinkMap=new Map,this._allItems=i,this.linkMap=t,this._conformsTo=e,this.tocLinks=s,this._tocDepth=n}static build(i,t={}){const e=i.toc?.items??[],s=i.readingOrder.items,{depth:n}=t,o=i.metadata?.conformsTo??[],r=new Map,c=[];for(let h=0;h<s.length;h++){const f=s[h],a=l.bareHref(f.href),d=f.title??l.findTitleInToc(e,a,n),u=l.collectChildrenFromToc(e,a,n,1,r),m={title:d,references:[f.href],children:u.length>0?u:void 0};r.set(m,f),c.push(m)}return new l(c,r,o,e,n)}augment(i){for(const t of this.flatAll){const e=this.linkFor(t);if(!e)continue;const s=i(t,e);s.position!==void 0&&(t.position=s.position),s.scroll!==void 0&&(t.scroll=s.scroll),s.role!==void 0&&(t.role=s.role)}}get depth(){return this._depth}set depth(i){this._depth!==i&&(this._depth=i,this._tocDepth=i,this._items=void 0,this._flat=void 0,this._toc=void 0)}get items(){return this._items||(this._depth!==void 0?(this._trimmedLinkMap=new Map,this._items=l.trimToDepth(this._allItems,this._depth,this.linkMap,this._trimmedLinkMap)):this._items=this._allItems),this._items}locate(i){const t=i.href.split("#")[0],e=g(i.locations),s=v(i.locations),n=i.locations.progression;if(e!==void 0){let o=-1/0,r;for(const c of this.flat){if(!this.itemMatchesHref(c,t))continue;const h=this.itemStartTime(c,t);h!==void 0&&h<=e&&h>o&&(o=h,r=c)}if(r)return r}if(s){const o=this.flat.find(r=>this.itemMatchesHref(r,t)?r.references.some(c=>c.split("#")[1]===s):!1);if(o)return o}if(n!==void 0){let o=-1/0,r;for(const h of this.flat){if(!this.itemMatchesHref(h,t))continue;const f=this.itemScrollPosition(h);f!==void 0&&f<=n&&f>o&&(o=f,r=h)}if(r)return r;const c=this.items.find(h=>this.itemMatchesHref(h,t));if(c?.children?.length&&!c.children.some(h=>h.scroll!==void 0)){const h=Math.min(Math.floor(n*c.children.length),c.children.length-1);return c.children[h]}}return this.flat.find(o=>this.itemMatchesHref(o,t))}adjacentTo(i){const t=this.flat.indexOf(i);return{previous:t>0?this.flat[t-1]:void 0,next:t>=0&&t<this.flat.length-1?this.flat[t+1]:void 0}}segmentsForHref(i){const t=i.split("#")[0],e=this.items.find(s=>this.itemMatchesHref(s,t));return e?e.children?.length?e.children:[e]:[]}ancestors(i){return this.ancestorPath(this.items,i)??[]}linkFor(i){return this.linkMap.get(i)??this._trimmedLinkMap.get(i)}get contextualizedToc(){return this._toc||(this._toc=this.tocLinks.length>0?this.buildTocEntries(this.tocLinks,this._tocDepth,1):this._allItems.map(i=>this.entryFor(this.linkFor(i),i))),this._toc}tocEntryFor(i){const t=this.linkFor(i);if(!t)return;const e=this.findTocEntryByLink(this.contextualizedToc,t);if(e)return e;const s=this.nearestTocEntryForResource(t.href,i);return s||this.previousResolvedTocEntry(t)}get flat(){return this._flat||(this._flat=this.flattenItems(this.items)),this._flat}get flatAll(){return this.flattenItems(this._allItems)}static findTitleInToc(i,t,e){const{atStart:s,fragments:n}=l.collectTocCandidates(i,t,e,1);if(s.length>0)return s[0].title;if(n.length===1)return n[0].title}static collectChildrenFromToc(i,t,e,s,n){if(e!==void 0&&s>e)return[];const o=[];for(const r of i){const c=l.bareHref(r.href);if((c===t||c==="")&&r.title&&!l.isStartOfResource(r.href)){const f=c===""?t+r.href:r.href,a={title:r.title,references:[f]};n.set(a,r),o.push(a)}r.children?.items?.length&&o.push(...l.collectChildrenFromToc(r.children.items,t,e,s+1,n))}return o}static collectTocCandidates(i,t,e,s){if(e!==void 0&&s>e)return{atStart:[],fragments:[]};const n=[],o=[];for(const r of i){const c=l.bareHref(r.href);if((c===t||c==="")&&r.title&&(l.isStartOfResource(r.href)?n.push(r):o.push(r)),r.children?.items?.length){const h=l.collectTocCandidates(r.children.items,t,e,s+1);n.push(...h.atStart),o.push(...h.fragments)}}return{atStart:n,fragments:o}}static isStartOfResource(i){const t=i.split("#")[1];if(!t)return!0;const e=t.match(/(?:^|&)t=([^&]+)/);return e!==null&&k(e[1])}get linkToItem(){if(!this._linkToItem){this._linkToItem=new Map;for(const[i,t]of this.linkMap)this._linkToItem.set(t,i)}return this._linkToItem}buildTocEntries(i,t,e){return t!==void 0&&e>t?[]:i.map(s=>{const n=this.linkToItem.get(s)??this.resourceStartItem(s.href),o=s.children?.items?.length?this.buildTocEntries(s.children.items,t,e+1):[];return{...this.entryFor(s,n),children:o.length>0?o:void 0}})}tocLinksFlat(i,t,e){if(t!==void 0&&e>t)return[];const s=[];for(const n of i)s.push(n),n.children?.items?.length&&s.push(...this.tocLinksFlat(n.children.items,t,e+1));return s}entryFor(i,t){const e=this._conformsTo.includes(p.AUDIOBOOK);return{link:i,position:!e&&t?.position!==void 0?String(t.position):void 0,timestamp:e&&t?.position!==void 0?I(t.position):void 0}}resourceStartItem(i){const t=l.bareHref(i);return this._allItems.find(e=>this.itemMatchesHref(e,t))}findTocEntryByLink(i,t){for(const e of i){if(e.link===t)return e;if(e.children){const s=this.findTocEntryByLink(e.children,t);if(s)return s}}}nearestTocEntryForResource(i,t){const e=l.bareHref(i),s=this.tocLinksFlat(this.tocLinks,this._tocDepth,1).filter(f=>l.bareHref(f.href)===e).map(f=>({link:f,item:this.linkToItem.get(f)??this.resourceStartItem(f.href)}));if(s.length===0)return;const n=this._conformsTo.includes(p.AUDIOBOOK),o=n?t.position??0:t.scroll??0;let r,c=-1/0;for(const f of s){const a=n?f.item?.position??0:f.item?.scroll??0;a<=o&&a>c&&(c=a,r=f)}const h=r??s[0];return this.findTocEntryByLink(this.contextualizedToc,h.link)}previousResolvedTocEntry(i){const t=this._allItems.findIndex(e=>this.linkFor(e)===i);if(t!==-1)for(let e=t-1;e>=0;e--){const s=this.linkFor(this._allItems[e]);if(!s)continue;const n=l.bareHref(s.href),{atStart:o,fragments:r}=l.collectTocCandidates(this.tocLinks,n,this._tocDepth,1),c=o.filter(d=>l.bareHref(d.href)===n),h=r.filter(d=>l.bareHref(d.href)===n),f=c[0]??(h.length>0?h[h.length-1]:void 0);if(!f)continue;const a=this.findTocEntryByLink(this.contextualizedToc,f);if(a)return a}}static trimToDepth(i,t,e,s){return i.map(n=>{const o={...n,children:t>1&&n.children?.length?l.trimToDepth(n.children,t-1,e,s):void 0},r=e.get(n);return r&&s.set(o,r),o})}flattenItems(i){const t=[];for(const e of i)t.push(e),e.children&&t.push(...this.flattenItems(e.children));return t}itemStartTime(i,t){for(const e of i.references){const s=e.indexOf("#"),n=s>=0?e.slice(0,s):e,o=s>=0?e.slice(s+1):void 0;if((n||t)!==t)continue;if(!o)return;const c=o.match(/(?:^|&)t=([^&]+)/);return c?T(c[1]):void 0}}itemMatchesHref(i,t){return i.references.some(e=>{const s=e.split("#")[0];return s===t||s===""})}ancestorPath(i,t){for(const e of i){if(e===t)return[];if(e.children){const s=this.ancestorPath(e.children,t);if(s!==null)return[e,...s]}}return null}itemScrollPosition(i){return i.scroll}static bareHref(i){return i.split("#")[0]}}export{l as Timeline};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@readium/shared",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Shared models to be used across other Readium projects and implementations in Typescript",
|
|
6
6
|
"author": "readium",
|
|
@@ -63,14 +63,6 @@
|
|
|
63
63
|
"engines": {
|
|
64
64
|
"node": ">=18"
|
|
65
65
|
},
|
|
66
|
-
"scripts": {
|
|
67
|
-
"dev": "vite",
|
|
68
|
-
"clean": "rimraf types dist",
|
|
69
|
-
"build": "pnpm clean && tsc && vite build",
|
|
70
|
-
"test": "jest",
|
|
71
|
-
"check-locales": "node scripts/check-locales.mjs",
|
|
72
|
-
"update-locales": "node scripts/update-locales.mjs && node scripts/check-locales.mjs"
|
|
73
|
-
},
|
|
74
66
|
"devDependencies": {
|
|
75
67
|
"@babel/core": "^7.28.6",
|
|
76
68
|
"@babel/preset-env": "^7.28.6",
|
|
@@ -84,5 +76,13 @@
|
|
|
84
76
|
"tslib": "^2.8.1",
|
|
85
77
|
"typescript": "^5.9.3",
|
|
86
78
|
"vite": "^7.3.1"
|
|
79
|
+
},
|
|
80
|
+
"scripts": {
|
|
81
|
+
"dev": "vite",
|
|
82
|
+
"clean": "rimraf types dist",
|
|
83
|
+
"build": "pnpm clean && tsc && vite build",
|
|
84
|
+
"test": "jest",
|
|
85
|
+
"check-locales": "node scripts/check-locales.mjs",
|
|
86
|
+
"update-locales": "node scripts/update-locales.mjs && node scripts/check-locales.mjs"
|
|
87
87
|
}
|
|
88
|
-
}
|
|
88
|
+
}
|
|
@@ -267,10 +267,12 @@ export class Timeline {
|
|
|
267
267
|
}
|
|
268
268
|
|
|
269
269
|
/**
|
|
270
|
-
* Maps a `TimelineItem` (typically from `locate()`) to its `ContextualizedTocEntry
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
270
|
+
* Maps a `TimelineItem` (typically from `locate()`) to its `ContextualizedTocEntry`,
|
|
271
|
+
* trying three fallbacks in order:
|
|
272
|
+
* 1. A direct match on `current`'s own link.
|
|
273
|
+
* 2. The nearest preceding toc entry within the same resource.
|
|
274
|
+
* 3. When no toc entry references the resource at all, the nearest
|
|
275
|
+
* preceding resource's toc entry.
|
|
274
276
|
*/
|
|
275
277
|
tocEntryFor(current: TimelineItem): ContextualizedTocEntry | undefined {
|
|
276
278
|
const link = this.linkFor(current);
|
|
@@ -279,7 +281,10 @@ export class Timeline {
|
|
|
279
281
|
const direct = this.findTocEntryByLink(this.contextualizedToc, link);
|
|
280
282
|
if (direct) return direct;
|
|
281
283
|
|
|
282
|
-
|
|
284
|
+
const nearest = this.nearestTocEntryForResource(link.href, current);
|
|
285
|
+
if (nearest) return nearest;
|
|
286
|
+
|
|
287
|
+
return this.previousResolvedTocEntry(link);
|
|
283
288
|
}
|
|
284
289
|
|
|
285
290
|
private get flat(): TimelineItem[] {
|
|
@@ -470,6 +475,11 @@ export class Timeline {
|
|
|
470
475
|
return undefined;
|
|
471
476
|
}
|
|
472
477
|
|
|
478
|
+
/**
|
|
479
|
+
* Tier-2 fallback for `tocEntryFor`: among the toc entries that reference
|
|
480
|
+
* `href`'s own resource (e.g. chapter markers within one audio file),
|
|
481
|
+
* return the nearest one at or before `current`'s position/scroll.
|
|
482
|
+
*/
|
|
473
483
|
private nearestTocEntryForResource(href: string, current: TimelineItem): ContextualizedTocEntry | undefined {
|
|
474
484
|
const bare = Timeline.bareHref(href);
|
|
475
485
|
// Re-walk with raw items (not the display-formatted TocEntry tree) so we can compare
|
|
@@ -491,6 +501,33 @@ export class Timeline {
|
|
|
491
501
|
return this.findTocEntryByLink(this.contextualizedToc, chosen.link);
|
|
492
502
|
}
|
|
493
503
|
|
|
504
|
+
/**
|
|
505
|
+
* Tier-3 fallback for `tocEntryFor`: when no toc entry references
|
|
506
|
+
* `href`'s resource at all, walk backward through the reading order and
|
|
507
|
+
* return the nearest preceding resource's toc entry.
|
|
508
|
+
*/
|
|
509
|
+
private previousResolvedTocEntry(link: Link): ContextualizedTocEntry | undefined {
|
|
510
|
+
const index = this._allItems.findIndex(item => this.linkFor(item) === link);
|
|
511
|
+
if (index === -1) return undefined;
|
|
512
|
+
|
|
513
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
514
|
+
const precedingLink = this.linkFor(this._allItems[i]);
|
|
515
|
+
if (!precedingLink) continue;
|
|
516
|
+
const precedingBare = Timeline.bareHref(precedingLink.href);
|
|
517
|
+
const { atStart, fragments } = Timeline.collectTocCandidates(this.tocLinks, precedingBare, this._tocDepth, 1);
|
|
518
|
+
// Exclude fragment-only entries (bareHref "") — collectTocCandidates
|
|
519
|
+
// treats them as matching any bare href for single-track audio, but
|
|
520
|
+
// here they must belong to this specific preceding resource.
|
|
521
|
+
const ownAtStart = atStart.filter(l => Timeline.bareHref(l.href) === precedingBare);
|
|
522
|
+
const ownFragments = fragments.filter(l => Timeline.bareHref(l.href) === precedingBare);
|
|
523
|
+
const chosen = ownAtStart[0] ?? (ownFragments.length > 0 ? ownFragments[ownFragments.length - 1] : undefined);
|
|
524
|
+
if (!chosen) continue;
|
|
525
|
+
const entry = this.findTocEntryByLink(this.contextualizedToc, chosen);
|
|
526
|
+
if (entry) return entry;
|
|
527
|
+
}
|
|
528
|
+
return undefined;
|
|
529
|
+
}
|
|
530
|
+
|
|
494
531
|
// -------------------------------------------------------------------------
|
|
495
532
|
// Shared utilities
|
|
496
533
|
// -------------------------------------------------------------------------
|
|
@@ -103,10 +103,12 @@ export declare class Timeline {
|
|
|
103
103
|
*/
|
|
104
104
|
get contextualizedToc(): ContextualizedTocEntry[];
|
|
105
105
|
/**
|
|
106
|
-
* Maps a `TimelineItem` (typically from `locate()`) to its `ContextualizedTocEntry
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
106
|
+
* Maps a `TimelineItem` (typically from `locate()`) to its `ContextualizedTocEntry`,
|
|
107
|
+
* trying three fallbacks in order:
|
|
108
|
+
* 1. A direct match on `current`'s own link.
|
|
109
|
+
* 2. The nearest preceding toc entry within the same resource.
|
|
110
|
+
* 3. When no toc entry references the resource at all, the nearest
|
|
111
|
+
* preceding resource's toc entry.
|
|
110
112
|
*/
|
|
111
113
|
tocEntryFor(current: TimelineItem): ContextualizedTocEntry | undefined;
|
|
112
114
|
private get flat();
|
|
@@ -146,7 +148,18 @@ export declare class Timeline {
|
|
|
146
148
|
private entryFor;
|
|
147
149
|
private resourceStartItem;
|
|
148
150
|
private findTocEntryByLink;
|
|
151
|
+
/**
|
|
152
|
+
* Tier-2 fallback for `tocEntryFor`: among the toc entries that reference
|
|
153
|
+
* `href`'s own resource (e.g. chapter markers within one audio file),
|
|
154
|
+
* return the nearest one at or before `current`'s position/scroll.
|
|
155
|
+
*/
|
|
149
156
|
private nearestTocEntryForResource;
|
|
157
|
+
/**
|
|
158
|
+
* Tier-3 fallback for `tocEntryFor`: when no toc entry references
|
|
159
|
+
* `href`'s resource at all, walk backward through the reading order and
|
|
160
|
+
* return the nearest preceding resource's toc entry.
|
|
161
|
+
*/
|
|
162
|
+
private previousResolvedTocEntry;
|
|
150
163
|
private static trimToDepth;
|
|
151
164
|
private flattenItems;
|
|
152
165
|
private itemStartTime;
|