cozy-iiif 0.1.6 → 0.2.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/dist/types.d.ts CHANGED
@@ -43,7 +43,7 @@ export interface CozyManifest {
43
43
  readonly canvases: CozyCanvas[];
44
44
  readonly structure: CozyRange[];
45
45
  getLabel(locale?: string): string | undefined;
46
- getTableOfContents(): CozyTOCNode[];
46
+ getTableOfContents(): CozyTOC;
47
47
  getMetadata(locale?: string): CozyMetadata[];
48
48
  }
49
49
  export interface CozyRange {
@@ -69,13 +69,20 @@ export interface CozyMetadata {
69
69
  readonly label: string;
70
70
  readonly value: string;
71
71
  }
72
+ export interface CozyTOC {
73
+ root: CozyTOCNode[];
74
+ getNode(id: string): CozyTOCNode | undefined;
75
+ }
72
76
  export interface CozyTOCNode {
73
77
  readonly id: string;
74
78
  readonly type: 'range' | 'canvas';
79
+ readonly source: CozyRange | CozyCanvas;
80
+ readonly children: CozyTOCNode[];
81
+ readonly navItems: CozyCanvas[];
82
+ readonly navSections: CozyRange[];
83
+ readonly parent?: CozyTOCNode;
84
+ readonly level: number;
75
85
  getLabel(locale?: string): string | undefined;
76
- children: CozyTOCNode[];
77
- parent?: CozyTOCNode;
78
- level: number;
79
86
  }
80
87
  export type CozyImageResource = StaticImageResource | ImageServiceResource;
81
88
  export type ImageServiceResource = DynamicImageServiceResource | Level0ImageServiceResource;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-iiif",
3
- "version": "0.1.6",
3
+ "version": "0.2.1",
4
4
  "description": "A developer-friendly collection of abstractions and utilities built on top of @iiif/presentation-3 and @iiif/parser",
5
5
  "license": "MIT",
6
6
  "author": "Rainer Simon",
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "exports": {
26
26
  ".": "./dist/index.js",
27
+ "./helpers": "./dist/helpers/index.js",
27
28
  "./level-0": "./dist/level-0/index.js"
28
29
  },
29
30
  "devDependencies": {
package/src/Cozy.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type { Canvas, Collection, Manifest, Range } from '@iiif/presentation-3';
2
2
  import { convertPresentation2 } from '@iiif/parser/presentation-2';
3
3
  import { Traverse } from '@iiif/parser';
4
- import * as Helpers from './helpers';
5
4
  import {
6
5
  getImages,
7
6
  getLabel,
@@ -261,4 +260,4 @@ const parseImageResource = (resource: any) => {
261
260
  }
262
261
  }
263
262
 
264
- export const Cozy = { parse, parseURL, Helpers };
263
+ export const Cozy = { parse, parseURL };
@@ -1,38 +1,75 @@
1
- import type { CozyRange, CozyTOCNode } from '../types';
1
+ import type { CozyCanvas, CozyRange, CozyTOC, CozyTOCNode } from '../types';
2
2
 
3
- export const getTableOfContents = (ranges: CozyRange[]) => () => {
3
+ export const getTableOfContents = (ranges: CozyRange[]) => (): CozyTOC => {
4
+
5
+ const index = new Map<string, CozyTOCNode>();
4
6
 
5
7
  const buildTree = (range: CozyRange, parent: CozyTOCNode | undefined, level: number = 0): CozyTOCNode => {
6
8
  const node: CozyTOCNode = {
7
9
  id: range.id,
8
10
  type: 'range',
9
- getLabel: range.getLabel,
11
+ source: range,
10
12
  children: [],
13
+ navItems: [],
14
+ navSections: [],
11
15
  parent,
12
- level
16
+ level,
17
+ getLabel: range.getLabel,
13
18
  };
14
19
 
15
20
  if (range.items && range.items.length > 0) {
16
21
  range.items.forEach(item => {
17
22
  if (item.source.type === 'Range') {
18
- const childNode = buildTree(item as CozyRange, node, level + 1);
23
+ const r = item as CozyRange;
24
+ const childNode = buildTree(r, node, level + 1);
19
25
  node.children.push(childNode);
20
26
  } else {
27
+ // This child is Canvas, i.e. a TOCNode with
28
+ // no further children.
21
29
  node.children.push({
22
30
  id: item.id,
23
31
  type: 'canvas',
24
- getLabel: item.getLabel,
32
+ source: item as CozyCanvas,
25
33
  children: [],
34
+ navItems: [],
35
+ navSections: [],
26
36
  parent: node,
27
- level: level + 1
37
+ level: level + 1,
38
+ getLabel: item.getLabel
28
39
  });
29
40
  }
30
41
  });
31
42
  }
43
+
44
+ // From the actual child ToC Nodes, infer the "logical" child navItems
45
+ // (canvases) and navSections (ranges).
46
+ const navChildren = node.children.map(n => {
47
+ if (n.type === 'canvas') {
48
+ // An actual leaf node
49
+ return n.source as CozyCanvas;
50
+ } else if (n.children.length === 1 && n.children[0].type === 'canvas') {
51
+ // A range with a single canvas child - logical leaf node!
52
+ return n.children[0].source as CozyCanvas;
53
+ } else {
54
+ return n.source as CozyRange;
55
+ }
56
+ });
57
+
58
+ const navItems = navChildren.filter(c => c.source.type === 'Canvas') as CozyCanvas[];
59
+ const navSections = navChildren.filter(c => c.source.type === 'Range') as CozyRange[];
32
60
 
61
+ node.navItems.push(...navItems);
62
+ node.navSections.push(...navSections);
63
+
64
+ index.set(node.id, node);
65
+
33
66
  return node;
34
67
  };
35
68
 
36
69
  const topRanges = ranges.filter(range => range.source.behavior?.includes('top'));
37
- return topRanges.map(range => buildTree(range, undefined));
70
+ const root = topRanges.map(range => buildTree(range, undefined));
71
+
72
+ const getNode = (id: string) => index.get(id);
73
+
74
+ return { root, getNode };
38
75
  }
package/src/types.ts CHANGED
@@ -1,4 +1,13 @@
1
- import type { Manifest, Canvas, ImageService2, ImageService3, IIIFExternalWebResource, Collection, Range, Annotation, AnnotationPage } from '@iiif/presentation-3';
1
+ import type {
2
+ Manifest,
3
+ Canvas,
4
+ ImageService2,
5
+ ImageService3,
6
+ IIIFExternalWebResource,
7
+ Collection,
8
+ Range,
9
+ AnnotationPage
10
+ } from '@iiif/presentation-3';
2
11
 
3
12
  export type CozyParseResult =
4
13
  | { type: 'collection', url: string, resource: CozyCollection }
@@ -54,7 +63,7 @@ export interface CozyManifest {
54
63
 
55
64
  getLabel(locale?: string): string | undefined;
56
65
 
57
- getTableOfContents(): CozyTOCNode[];
66
+ getTableOfContents(): CozyTOC;
58
67
 
59
68
  getMetadata(locale?: string): CozyMetadata[];
60
69
 
@@ -106,19 +115,33 @@ export interface CozyMetadata {
106
115
 
107
116
  }
108
117
 
118
+ export interface CozyTOC {
119
+
120
+ root: CozyTOCNode[];
121
+
122
+ getNode(id: string): CozyTOCNode | undefined;
123
+
124
+ }
125
+
109
126
  export interface CozyTOCNode {
110
127
 
111
128
  readonly id: string;
112
129
 
113
130
  readonly type: 'range' | 'canvas';
114
131
 
115
- getLabel(locale?: string): string | undefined;
132
+ readonly source: CozyRange | CozyCanvas;
133
+
134
+ readonly children: CozyTOCNode[];
135
+
136
+ readonly navItems: CozyCanvas[];
116
137
 
117
- children: CozyTOCNode[];
138
+ readonly navSections: CozyRange[];
118
139
 
119
- parent?: CozyTOCNode;
140
+ readonly parent?: CozyTOCNode;
120
141
 
121
- level: number;
142
+ readonly level: number;
143
+
144
+ getLabel(locale?: string): string | undefined;
122
145
 
123
146
  }
124
147
 
package/vite.config.ts CHANGED
@@ -8,6 +8,7 @@ export default defineConfig({
8
8
  lib: {
9
9
  entry: {
10
10
  'index': resolve(__dirname, 'src/index.ts'),
11
+ 'helpers/index': resolve(__dirname, 'src/helpers/index.ts'),
11
12
  'level-0/index': resolve(__dirname, 'src/level-0/index.ts'),
12
13
  },
13
14
  formats: ['es']