@readium/shared 2.2.0-alpha.2 → 2.2.0-beta.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@readium/shared",
3
- "version": "2.2.0-alpha.2",
3
+ "version": "2.2.0-beta.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",
@@ -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<Document> {
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
- this._link.mediaType.isHTML ? (this._link.mediaType.string as 'application/xhtml+xml' | 'text/html') : 'text/xml'
119
+ 'application/xml'
120
120
  );
121
121
  }
122
122
  }
@@ -27,11 +27,11 @@ export abstract class Resource {
27
27
  return JSON.parse(str);
28
28
  });
29
29
  }
30
- readAsXML(): Promise<Document | undefined> {
31
- return this.link().then(l => this.readAsString().then(str => {
30
+ readAsXML(): Promise<XMLDocument | undefined> {
31
+ return this.readAsString().then(str => {
32
32
  if (str === undefined) return str;
33
- return new DOMParser().parseFromString(str, l.mediaType.isHTML ? (l.mediaType.string as 'application/xhtml+xml' | 'text/html') : 'text/xml');
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?: 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 || new 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: Links.deserialize(json.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
- if (this.links?.items.length > 0) json.links = this.links.serialize();
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
- if(this.links) {
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
- if(this.links) {
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
- if(this.links) {
201
- links.push(this.links);
202
- }
200
+ links.push(this.links);
203
201
 
204
202
  const link = find(links);
205
203
 
@@ -12,6 +12,7 @@ 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";
15
16
 
16
17
  export type ServiceFactory = () => null;
17
18
 
@@ -79,6 +80,36 @@ export class Publication {
79
80
  return this.manifest.linkWithRel(rel);
80
81
  }
81
82
 
83
+ /**
84
+ * Gets the cover image for the publication.
85
+ * First looks for rel='cover' in links/resources/readingOrder,
86
+ * then falls back to any image (JPEG, PNG, GIF, AVIF, SVG) as bitmap fallback.
87
+ */
88
+ public getCover(): Link | undefined {
89
+ const locations = [
90
+ this.links,
91
+ this.resources,
92
+ this.readingOrder
93
+ ].filter(Boolean) as Links[];
94
+
95
+ // First, look for explicit cover relationship
96
+ for (const location of locations) {
97
+ const cover = location.items.find(link => link.rels?.has('cover'));
98
+ if (cover) return cover;
99
+ }
100
+
101
+ // Fallback to any image (including SVG which is not bitmap)
102
+ for (const location of locations) {
103
+ const image = location.items.find(link =>
104
+ link.mediaType.isBitmap ||
105
+ link.mediaType.matches(MediaType.SVG)
106
+ );
107
+ if (image) return image;
108
+ }
109
+
110
+ return undefined;
111
+ }
112
+
82
113
  public async positionsFromManifest(): Promise<Locator[]> {
83
114
  const positionListLink = this.manifest.links.findWithMediaType(
84
115
  '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
  }
@@ -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
  }
@@ -24,5 +24,5 @@ export declare class HttpResource implements Resource {
24
24
  length(): Promise<number>;
25
25
  readAsJSON(): Promise<unknown>;
26
26
  readAsString(): Promise<string>;
27
- readAsXML(): Promise<Document>;
27
+ readAsXML(): Promise<XMLDocument>;
28
28
  }
@@ -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<Document | undefined>;
13
+ readAsXML(): Promise<XMLDocument | undefined>;
14
14
  abstract close(): void;
15
15
  }
@@ -20,7 +20,7 @@ export declare class Manifest {
20
20
  constructor(values: {
21
21
  context?: Array<string>;
22
22
  metadata: Metadata;
23
- links?: Links;
23
+ links: Links;
24
24
  readingOrder: Links;
25
25
  resources?: Links;
26
26
  toc?: Links;
@@ -44,6 +44,12 @@ export declare class Publication {
44
44
  * Finds the first [Link] having the given [rel] in the publications's links.
45
45
  */
46
46
  linkWithRel(rel: string): Link | undefined;
47
+ /**
48
+ * Gets the cover image for the publication.
49
+ * First looks for rel='cover' in links/resources/readingOrder,
50
+ * then falls back to any image (JPEG, PNG, GIF, AVIF, SVG) as bitmap fallback.
51
+ */
52
+ getCover(): Link | undefined;
47
53
  positionsFromManifest(): Promise<Locator[]>;
48
54
  guideForLink(link: Link): Promise<GuidedNavigationDocument | undefined>;
49
55
  /**
@@ -41,11 +41,5 @@ declare module '../Locator' {
41
41
  * https://www.w3.org/TR/media-frags/
42
42
  */
43
43
  space(): [number, number, number, number] | undefined;
44
- /**
45
- * Text fragments, used by browsers to jump to/highlight text.
46
- *
47
- * https://developer.mozilla.org/en-US/docs/Web/URI/Fragment/Text_fragments
48
- */
49
- text(): string | undefined;
50
44
  }
51
45
  }
@@ -89,6 +89,7 @@ export declare class MediaType {
89
89
  static get ACSM(): MediaType;
90
90
  static get AIFF(): MediaType;
91
91
  static get AVI(): MediaType;
92
+ static get AVIF(): MediaType;
92
93
  static get BINARY(): MediaType;
93
94
  static get BMP(): MediaType;
94
95
  static get CBZ(): MediaType;
package/LICENSE DELETED
@@ -1,28 +0,0 @@
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.