react-iiif-vault 0.9.6 → 0.9.9

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/cjs/index.js CHANGED
@@ -2,29 +2,223 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var React = require('react');
5
+ var React$1 = require('react');
6
+ var atlas = require('@atlas-viewer/atlas');
6
7
  var vault = require('@iiif/vault');
7
- var iiifImageApi = require('@atlas-viewer/iiif-image-api');
8
- var vaultHelpers = require('@iiif/vault-helpers');
9
8
  var typesafeActions = require('typesafe-actions');
9
+ var require$$0 = require('react-dom');
10
+ var vaultHelpers = require('@iiif/vault-helpers');
11
+ var iiifImageApi = require('@atlas-viewer/iiif-image-api');
10
12
 
11
13
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
14
 
13
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
15
+ var React__default = /*#__PURE__*/_interopDefaultLegacy(React$1);
16
+ var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
14
17
 
15
- var __defProp$5 = Object.defineProperty;
16
- var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
17
- var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
18
- var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
19
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
20
- var __spreadValues$5 = (a, b) => {
18
+ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
19
+
20
+ var reactErrorBoundary_umd = {exports: {}};
21
+
22
+ (function (module, exports) {
23
+ (function (global, factory) {
24
+ factory(exports, React__default["default"]) ;
25
+ })(commonjsGlobal, (function (exports, React) {
26
+ function _interopNamespace(e) {
27
+ if (e && e.__esModule) return e;
28
+ var n = Object.create(null);
29
+ if (e) {
30
+ Object.keys(e).forEach(function (k) {
31
+ if (k !== 'default') {
32
+ var d = Object.getOwnPropertyDescriptor(e, k);
33
+ Object.defineProperty(n, k, d.get ? d : {
34
+ enumerable: true,
35
+ get: function () { return e[k]; }
36
+ });
37
+ }
38
+ });
39
+ }
40
+ n["default"] = e;
41
+ return Object.freeze(n);
42
+ }
43
+
44
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
45
+
46
+ function _setPrototypeOf(o, p) {
47
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
48
+ o.__proto__ = p;
49
+ return o;
50
+ };
51
+
52
+ return _setPrototypeOf(o, p);
53
+ }
54
+
55
+ function _inheritsLoose(subClass, superClass) {
56
+ subClass.prototype = Object.create(superClass.prototype);
57
+ subClass.prototype.constructor = subClass;
58
+ _setPrototypeOf(subClass, superClass);
59
+ }
60
+
61
+ var changedArray = function changedArray(a, b) {
62
+ if (a === void 0) {
63
+ a = [];
64
+ }
65
+
66
+ if (b === void 0) {
67
+ b = [];
68
+ }
69
+
70
+ return a.length !== b.length || a.some(function (item, index) {
71
+ return !Object.is(item, b[index]);
72
+ });
73
+ };
74
+
75
+ var initialState = {
76
+ error: null
77
+ };
78
+
79
+ var ErrorBoundary = /*#__PURE__*/function (_React$Component) {
80
+ _inheritsLoose(ErrorBoundary, _React$Component);
81
+
82
+ function ErrorBoundary() {
83
+ var _this;
84
+
85
+ for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
86
+ _args[_key] = arguments[_key];
87
+ }
88
+
89
+ _this = _React$Component.call.apply(_React$Component, [this].concat(_args)) || this;
90
+ _this.state = initialState;
91
+
92
+ _this.resetErrorBoundary = function () {
93
+ var _this$props;
94
+
95
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
96
+ args[_key2] = arguments[_key2];
97
+ }
98
+
99
+ _this.props.onReset == null ? void 0 : (_this$props = _this.props).onReset.apply(_this$props, args);
100
+
101
+ _this.reset();
102
+ };
103
+
104
+ return _this;
105
+ }
106
+
107
+ ErrorBoundary.getDerivedStateFromError = function getDerivedStateFromError(error) {
108
+ return {
109
+ error: error
110
+ };
111
+ };
112
+
113
+ var _proto = ErrorBoundary.prototype;
114
+
115
+ _proto.reset = function reset() {
116
+ this.setState(initialState);
117
+ };
118
+
119
+ _proto.componentDidCatch = function componentDidCatch(error, info) {
120
+ var _this$props$onError, _this$props2;
121
+
122
+ (_this$props$onError = (_this$props2 = this.props).onError) == null ? void 0 : _this$props$onError.call(_this$props2, error, info);
123
+ };
124
+
125
+ _proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
126
+ var error = this.state.error;
127
+ var resetKeys = this.props.resetKeys; // There's an edge case where if the thing that triggered the error
128
+ // happens to *also* be in the resetKeys array, we'd end up resetting
129
+ // the error boundary immediately. This would likely trigger a second
130
+ // error to be thrown.
131
+ // So we make sure that we don't check the resetKeys on the first call
132
+ // of cDU after the error is set
133
+
134
+ if (error !== null && prevState.error !== null && changedArray(prevProps.resetKeys, resetKeys)) {
135
+ var _this$props$onResetKe, _this$props3;
136
+
137
+ (_this$props$onResetKe = (_this$props3 = this.props).onResetKeysChange) == null ? void 0 : _this$props$onResetKe.call(_this$props3, prevProps.resetKeys, resetKeys);
138
+ this.reset();
139
+ }
140
+ };
141
+
142
+ _proto.render = function render() {
143
+ var error = this.state.error;
144
+ var _this$props4 = this.props,
145
+ fallbackRender = _this$props4.fallbackRender,
146
+ FallbackComponent = _this$props4.FallbackComponent,
147
+ fallback = _this$props4.fallback;
148
+
149
+ if (error !== null) {
150
+ var _props = {
151
+ error: error,
152
+ resetErrorBoundary: this.resetErrorBoundary
153
+ };
154
+
155
+ if ( /*#__PURE__*/React__namespace.isValidElement(fallback)) {
156
+ return fallback;
157
+ } else if (typeof fallbackRender === 'function') {
158
+ return fallbackRender(_props);
159
+ } else if (FallbackComponent) {
160
+ return /*#__PURE__*/React__namespace.createElement(FallbackComponent, _props);
161
+ } else {
162
+ throw new Error('react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop');
163
+ }
164
+ }
165
+
166
+ return this.props.children;
167
+ };
168
+
169
+ return ErrorBoundary;
170
+ }(React__namespace.Component);
171
+
172
+ function withErrorBoundary(Component, errorBoundaryProps) {
173
+ var Wrapped = function Wrapped(props) {
174
+ return /*#__PURE__*/React__namespace.createElement(ErrorBoundary, errorBoundaryProps, /*#__PURE__*/React__namespace.createElement(Component, props));
175
+ }; // Format for display in DevTools
176
+
177
+
178
+ var name = Component.displayName || Component.name || 'Unknown';
179
+ Wrapped.displayName = "withErrorBoundary(" + name + ")";
180
+ return Wrapped;
181
+ }
182
+
183
+ function useErrorHandler(givenError) {
184
+ var _React$useState = React__namespace.useState(null),
185
+ error = _React$useState[0],
186
+ setError = _React$useState[1];
187
+
188
+ if (givenError != null) throw givenError;
189
+ if (error != null) throw error;
190
+ return setError;
191
+ }
192
+ /*
193
+ eslint
194
+ @typescript-eslint/sort-type-union-intersection-members: "off",
195
+ @typescript-eslint/no-throw-literal: "off",
196
+ @typescript-eslint/prefer-nullish-coalescing: "off"
197
+ */
198
+
199
+ exports.ErrorBoundary = ErrorBoundary;
200
+ exports.useErrorHandler = useErrorHandler;
201
+ exports.withErrorBoundary = withErrorBoundary;
202
+
203
+ Object.defineProperty(exports, '__esModule', { value: true });
204
+
205
+ }));
206
+
207
+ }(reactErrorBoundary_umd, reactErrorBoundary_umd.exports));
208
+
209
+ var __defProp$a = Object.defineProperty;
210
+ var __getOwnPropSymbols$a = Object.getOwnPropertySymbols;
211
+ var __hasOwnProp$a = Object.prototype.hasOwnProperty;
212
+ var __propIsEnum$a = Object.prototype.propertyIsEnumerable;
213
+ var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
214
+ var __spreadValues$a = (a, b) => {
21
215
  for (var prop in b || (b = {}))
22
- if (__hasOwnProp$5.call(b, prop))
23
- __defNormalProp$5(a, prop, b[prop]);
24
- if (__getOwnPropSymbols$5)
25
- for (var prop of __getOwnPropSymbols$5(b)) {
26
- if (__propIsEnum$5.call(b, prop))
27
- __defNormalProp$5(a, prop, b[prop]);
216
+ if (__hasOwnProp$a.call(b, prop))
217
+ __defNormalProp$a(a, prop, b[prop]);
218
+ if (__getOwnPropSymbols$a)
219
+ for (var prop of __getOwnPropSymbols$a(b)) {
220
+ if (__propIsEnum$a.call(b, prop))
221
+ __defNormalProp$a(a, prop, b[prop]);
28
222
  }
29
223
  return a;
30
224
  };
@@ -37,55 +231,31 @@ const defaultResourceContext = {
37
231
  };
38
232
  const ResourceReactContext = React__default["default"].createContext(defaultResourceContext);
39
233
  const useResourceContext = () => {
40
- return React.useContext(ResourceReactContext);
234
+ return React$1.useContext(ResourceReactContext);
41
235
  };
42
- const ResourceProvider = ({ value, children }) => {
236
+ function ResourceProvider({ value, children }) {
43
237
  const parentContext = useResourceContext();
44
- const newContext = React.useMemo(() => {
45
- return __spreadValues$5(__spreadValues$5({}, parentContext), value);
238
+ const newContext = React$1.useMemo(() => {
239
+ return __spreadValues$a(__spreadValues$a({}, parentContext), value);
46
240
  }, [value, parentContext]);
47
241
  return /* @__PURE__ */ React__default["default"].createElement(ResourceReactContext.Provider, {
48
242
  value: newContext
49
243
  }, children);
50
- };
51
-
52
- const AnnotationContext = ({ annotation, children }) => {
53
- return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
54
- value: { annotation }
55
- }, children);
56
- };
57
-
58
- const CanvasContext = ({ canvas, children }) => {
59
- return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
60
- value: { canvas }
61
- }, children);
62
- };
63
-
64
- const CollectionContext = ({ collection, children }) => {
65
- return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
66
- value: { collection }
67
- }, children);
68
- };
69
-
70
- const ManifestContext = ({ manifest, children }) => {
71
- return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
72
- value: { manifest }
73
- }, children);
74
- };
75
-
76
- const RangeContext = ({ range, children }) => {
77
- return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
78
- value: { range }
79
- }, children);
80
- };
244
+ }
81
245
 
82
246
  const ReactVaultContext = React__default["default"].createContext({
83
247
  vault: null,
84
248
  setVaultInstance: (vault) => {
85
249
  }
86
250
  });
87
- const VaultProvider = ({ vault: vault$1, vaultOptions, useGlobal, resources, children }) => {
88
- const [vaultInstance, setVaultInstance] = React.useState(() => {
251
+ function VaultProvider({
252
+ vault: vault$1,
253
+ vaultOptions,
254
+ useGlobal,
255
+ resources,
256
+ children
257
+ }) {
258
+ const [vaultInstance, setVaultInstance] = React$1.useState(() => {
89
259
  if (vault$1) {
90
260
  return vault$1;
91
261
  }
@@ -102,10 +272,10 @@ const VaultProvider = ({ vault: vault$1, vaultOptions, useGlobal, resources, chi
102
272
  }, /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
103
273
  value: resources || {}
104
274
  }, children));
105
- };
275
+ }
106
276
 
107
277
  const useVault = () => {
108
- const { vault } = React.useContext(ReactVaultContext);
278
+ const { vault } = React$1.useContext(ReactVaultContext);
109
279
  if (vault === null) {
110
280
  throw new Error("Vault not found. Ensure you have your provider set up correctly.");
111
281
  }
@@ -135,13 +305,13 @@ var __async$1 = (__this, __arguments, generator) => {
135
305
  function useExternalResource(idOrRef, { noCache = false } = {}) {
136
306
  const id = typeof idOrRef === "string" ? idOrRef : idOrRef.id;
137
307
  const vault = useVault();
138
- const [realId, setRealId] = React.useState(id);
139
- const [error, setError] = React.useState(void 0);
140
- const initialData = React.useMemo(() => {
308
+ const [realId, setRealId] = React$1.useState(id);
309
+ const [error, setError] = React$1.useState(void 0);
310
+ const initialData = React$1.useMemo(() => {
141
311
  return vault.get(id, { skipSelfReturn: true }) || void 0;
142
312
  }, [id, vault]);
143
- const [resource, setResource] = React.useState(initialData);
144
- React.useEffect(() => {
313
+ const [resource, setResource] = React$1.useState(initialData);
314
+ React$1.useEffect(() => {
145
315
  (() => __async$1(this, null, function* () {
146
316
  try {
147
317
  const fetchedResource = initialData && !noCache ? initialData : yield vault.load(id);
@@ -170,12 +340,24 @@ function useExternalManifest(idOrRef, options) {
170
340
  return { id, isLoaded, error, manifest: resource, requestId, cached };
171
341
  }
172
342
 
343
+ function ManifestContext({ manifest, children }) {
344
+ return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
345
+ value: { manifest }
346
+ }, children);
347
+ }
348
+
349
+ function CanvasContext({ canvas, children }) {
350
+ return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
351
+ value: { canvas }
352
+ }, children);
353
+ }
354
+
173
355
  function useVaultSelector(selector, deps = []) {
174
356
  const vault = useVault();
175
- const [selectedState, setSelectedState] = React.useState(() => selector(vault.getState(), vault));
176
- React.useEffect(() => {
177
- return vault.subscribe((s) => selector(s, vault), (state) => {
178
- setSelectedState(state);
357
+ const [selectedState, setSelectedState] = React$1.useState(() => selector(vault.getState(), vault));
358
+ React$1.useEffect(() => {
359
+ return vault.subscribe((s) => selector(s, vault), (s) => {
360
+ setSelectedState(s);
179
361
  }, false);
180
362
  }, deps);
181
363
  return selectedState;
@@ -183,7 +365,7 @@ function useVaultSelector(selector, deps = []) {
183
365
 
184
366
  const VisibleCanvasReactContext = React__default["default"].createContext([]);
185
367
  function useVisibleCanvases() {
186
- const ids = React.useContext(VisibleCanvasReactContext);
368
+ const ids = React$1.useContext(VisibleCanvasReactContext);
187
369
  return useVaultSelector((state) => {
188
370
  return ids.map((id) => state.iiif.entities.Canvas[id]).filter(Boolean);
189
371
  }, [ids]);
@@ -191,7 +373,7 @@ function useVisibleCanvases() {
191
373
 
192
374
  const noop = () => {
193
375
  };
194
- const SimpleViewerReactContext = React.createContext({
376
+ const SimpleViewerReactContext = React$1.createContext({
195
377
  setCurrentCanvasId: noop,
196
378
  setCurrentCanvasIndex: noop,
197
379
  nextCanvas: noop,
@@ -200,24 +382,24 @@ const SimpleViewerReactContext = React.createContext({
200
382
  totalCanvases: 0,
201
383
  pagingView: true
202
384
  });
203
- const SimpleViewerProvider = (props) => {
385
+ function SimpleViewerProvider(props) {
204
386
  const manifest = useExternalManifest(props.manifest);
205
- const [currentCanvasId, setCurrentCanvasId] = React.useState("");
206
- const [visible, setVisible] = React.useState([]);
387
+ const [currentCanvasId, setCurrentCanvasId] = React$1.useState("");
388
+ const [visible, setVisible] = React$1.useState([]);
207
389
  const pagingView = (typeof props.pagingEnabled === "undefined" || props.pagingEnabled) && manifest.manifest && manifest.manifest.behavior && manifest.manifest.behavior.includes("paged");
208
- React.useEffect(() => {
390
+ React$1.useEffect(() => {
209
391
  var _a, _b;
210
392
  if (manifest.manifest) {
211
393
  setCurrentCanvasId((_a = manifest.manifest.items[0]) == null ? void 0 : _a.id);
212
394
  setVisible([(_b = manifest.manifest.items[0]) == null ? void 0 : _b.id]);
213
395
  }
214
396
  }, [manifest.manifest, props.manifest]);
215
- const canvasList = React.useMemo(() => {
397
+ const canvasList = React$1.useMemo(() => {
216
398
  var _a;
217
399
  return ((_a = manifest.manifest) == null ? void 0 : _a.items.map((c) => c.id)) || [];
218
400
  }, [manifest.manifest, props.manifest]);
219
- const currentCanvasIndex = React.useMemo(() => canvasList.indexOf(currentCanvasId), [canvasList, currentCanvasId]);
220
- const nextCanvas = React.useCallback(() => {
401
+ const currentCanvasIndex = React$1.useMemo(() => canvasList.indexOf(currentCanvasId), [canvasList, currentCanvasId]);
402
+ const nextCanvas = React$1.useCallback(() => {
221
403
  if (canvasList.length && currentCanvasId) {
222
404
  if (currentCanvasIndex === -1) {
223
405
  return;
@@ -233,7 +415,7 @@ const SimpleViewerProvider = (props) => {
233
415
  }
234
416
  }
235
417
  }, [pagingView, canvasList, currentCanvasId, currentCanvasIndex]);
236
- const previousCanvas = React.useCallback(() => {
418
+ const previousCanvas = React$1.useCallback(() => {
237
419
  if (canvasList.length && currentCanvasId) {
238
420
  if (currentCanvasIndex === 0 || currentCanvasIndex === -1) {
239
421
  return;
@@ -246,7 +428,7 @@ const SimpleViewerProvider = (props) => {
246
428
  }
247
429
  }
248
430
  }, [pagingView, canvasList, currentCanvasId, currentCanvasIndex]);
249
- const setCurrentCanvasIndex = React.useCallback((idx) => {
431
+ const setCurrentCanvasIndex = React$1.useCallback((idx) => {
250
432
  const realIdx = pagingView && idx % 2 === 1 ? idx - 1 : idx;
251
433
  const newId = canvasList[realIdx];
252
434
  const newNextId = pagingView && realIdx !== 0 ? canvasList[realIdx + 1] : null;
@@ -266,13 +448,13 @@ const SimpleViewerProvider = (props) => {
266
448
  });
267
449
  }
268
450
  }, [pagingView, canvasList]);
269
- const internalSetCurrentCanvasId = React.useCallback((nextId) => {
451
+ const internalSetCurrentCanvasId = React$1.useCallback((nextId) => {
270
452
  const idx = canvasList.indexOf(nextId);
271
453
  if (idx !== -1) {
272
454
  setCurrentCanvasIndex(idx);
273
455
  }
274
456
  }, [canvasList, setCurrentCanvasIndex]);
275
- const ctx = React.useMemo(() => ({
457
+ const ctx = React$1.useMemo(() => ({
276
458
  setCurrentCanvasId: internalSetCurrentCanvasId,
277
459
  nextCanvas,
278
460
  previousCanvas,
@@ -297,20 +479,20 @@ const SimpleViewerProvider = (props) => {
297
479
  }, /* @__PURE__ */ React__default["default"].createElement(CanvasContext, {
298
480
  canvas: currentCanvasId
299
481
  }, props.children))));
300
- };
482
+ }
301
483
  function useSimpleViewer() {
302
- return React.useContext(SimpleViewerReactContext);
484
+ return React$1.useContext(SimpleViewerReactContext);
303
485
  }
304
486
 
305
487
  function useContextBridge() {
306
488
  return {
307
- VaultContext: React.useContext(ReactVaultContext),
308
- ResourceContext: React.useContext(ResourceReactContext),
309
- SimpleViewerReactContext: React.useContext(SimpleViewerReactContext),
310
- VisibleCanvasReactContext: React.useContext(VisibleCanvasReactContext)
489
+ VaultContext: React$1.useContext(ReactVaultContext),
490
+ ResourceContext: React$1.useContext(ResourceReactContext),
491
+ SimpleViewerReactContext: React$1.useContext(SimpleViewerReactContext),
492
+ VisibleCanvasReactContext: React$1.useContext(VisibleCanvasReactContext)
311
493
  };
312
494
  }
313
- const ContextBridge = (props) => {
495
+ function ContextBridge(props) {
314
496
  return /* @__PURE__ */ React__default["default"].createElement(VaultProvider, {
315
497
  vault: props.bridge.VaultContext.vault || void 0,
316
498
  resources: props.bridge.ResourceContext
@@ -319,62 +501,393 @@ const ContextBridge = (props) => {
319
501
  }, /* @__PURE__ */ React__default["default"].createElement(SimpleViewerReactContext.Provider, {
320
502
  value: props.bridge.SimpleViewerReactContext
321
503
  }, props.children)));
322
- };
504
+ }
323
505
 
324
- const ImageServiceLoaderContext = React__default["default"].createContext(new iiifImageApi.ImageServiceLoader());
325
- function useImageServiceLoader() {
326
- return React.useContext(ImageServiceLoaderContext);
506
+ const IMPORT_ENTITIES = "@iiif/IMPORT_ENTITIES";
507
+ const MODIFY_ENTITY_FIELD = "@iiif/MODIFY_ENTITY_FIELD";
508
+ const REORDER_ENTITY_FIELD = "@iiif/REORDER_ENTITY_FIELD";
509
+ const ADD_REFERENCE = "@iiif/ADD_REFERENCE";
510
+ const REMOVE_REFERENCE = "@iiif/REMOVE_REFERENCE";
511
+ const importEntities = typesafeActions.createAction(IMPORT_ENTITIES)();
512
+ const modifyEntityField = typesafeActions.createAction(MODIFY_ENTITY_FIELD)();
513
+ const reorderEntityField = typesafeActions.createAction(REORDER_ENTITY_FIELD)();
514
+ const addReference = typesafeActions.createAction(ADD_REFERENCE)();
515
+ const removeReference = typesafeActions.createAction(REMOVE_REFERENCE)();
516
+ const entityActions = { importEntities, modifyEntityField, reorderEntityField, addReference, removeReference };
517
+
518
+ const ADD_MAPPING = "@iiif/ADD_MAPPING";
519
+ const ADD_MAPPINGS = "@iiif/ADD_MAPPINGS";
520
+ typesafeActions.createAction(ADD_MAPPING)();
521
+ typesafeActions.createAction(ADD_MAPPINGS)();
522
+
523
+ const SET_META_VALUE = "@iiif/SET_META_VALUE";
524
+ const SET_META_VALUE_DYNAMIC = "@iiif/SET_META_VALUE_DYNAMIC";
525
+ const UNSET_META_VALUE = "@iiif/UNSET_META_VALUE";
526
+ typesafeActions.createAction(SET_META_VALUE)();
527
+ typesafeActions.createAction(SET_META_VALUE_DYNAMIC)();
528
+ typesafeActions.createAction(UNSET_META_VALUE)();
529
+ const REQUEST_RESOURCE = "@iiif/REQUEST_RESOURCE";
530
+ const REQUEST_ERROR = "@iiif/REQUEST_ERROR";
531
+ const REQUEST_MISMATCH = "@iiif/REQUEST_MISMATCH";
532
+ const REQUEST_COMPLETE = "@iiif/REQUEST_COMPLETE";
533
+ const REQUEST_OFFLINE_RESOURCE = "@iiif/REQUEST_OFFLINE_RESOURCE";
534
+ typesafeActions.createAction(REQUEST_RESOURCE)();
535
+ typesafeActions.createAction(REQUEST_ERROR)();
536
+ typesafeActions.createAction(REQUEST_MISMATCH)();
537
+ typesafeActions.createAction(REQUEST_COMPLETE)();
538
+ typesafeActions.createAction(REQUEST_OFFLINE_RESOURCE)();
539
+
540
+ const BATCH_ACTIONS = "@iiif/BATCH";
541
+ const BATCH_IMPORT = "@iiif/BATCH_IMPORT";
542
+ typesafeActions.createAction(BATCH_ACTIONS)();
543
+ typesafeActions.createAction(BATCH_IMPORT)();
544
+
545
+ function useDispatch() {
546
+ const vault = useVault();
547
+ const store = vault.getStore();
548
+ return React$1.useMemo(() => {
549
+ return (action) => store.dispatch(action);
550
+ }, [store]);
327
551
  }
328
552
 
329
- const BOX_SELECTOR = /&?(xywh=)?(pixel:|percent:)?([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)/;
330
- const TEMPORAL_SELECTOR = /&?(t=)(npt:)?([0-9]+(.[0-9]+)?)?(,([0-9]+(.[0-9]+)?))?/;
331
- function parseSelector(source) {
332
- if (Array.isArray(source)) {
333
- return source.reduce((data, nextSource) => {
334
- const { selector, selectors } = parseSelector(nextSource);
335
- if (selector) {
336
- if (!data.selector) {
337
- data.selector = selector;
338
- }
339
- data.selectors.push(...selectors);
340
- }
341
- return data;
342
- }, {
343
- selector: null,
344
- selectors: []
345
- });
346
- }
347
- if (!source) {
348
- return {
349
- selector: null,
350
- selectors: []
553
+ function isVaultActivated(obj) {
554
+ return typeof obj !== "string" && obj && obj.bindToVault;
555
+ }
556
+ function useVirtualAnnotationPage() {
557
+ const vault = useVault();
558
+ const sources = React$1.useRef([]);
559
+ const dispatch = useDispatch();
560
+ const virtualId = React$1.useMemo(() => {
561
+ return `vault://annotation-page/${new Date().getTime()}/${Math.round(Math.random() * 1e9).toString(16)}`;
562
+ }, []);
563
+ React$1.useLayoutEffect(() => {
564
+ const page = {
565
+ id: virtualId,
566
+ type: "AnnotationPage",
567
+ behavior: [],
568
+ motivation: null,
569
+ label: null,
570
+ thumbnail: [],
571
+ summary: null,
572
+ requiredStatement: null,
573
+ metadata: [],
574
+ rights: null,
575
+ provider: [],
576
+ items: [],
577
+ seeAlso: [],
578
+ homepage: [],
579
+ logo: [],
580
+ rendering: [],
581
+ service: []
351
582
  };
352
- }
353
- if (typeof source === "string") {
354
- const [id, fragment] = source.split("#");
355
- if (!fragment) {
356
- return {
357
- selector: null,
358
- selectors: []
359
- };
360
- }
361
- return parseSelector({ type: "FragmentSelector", value: fragment });
362
- }
363
- if (source.type === "PointSelector" && (source.t || source.t === 0)) {
364
- const selector = {
365
- type: "TemporalSelector",
366
- temporal: {
367
- startTime: source.t
583
+ dispatch(entityActions.importEntities({
584
+ entities: {
585
+ AnnotationPage: {
586
+ [page.id]: page
587
+ }
368
588
  }
369
- };
370
- return {
371
- selector,
372
- selectors: [selector]
373
- };
374
- }
375
- if (source.type === "FragmentSelector") {
376
- const matchBoxSelector = BOX_SELECTOR.exec(source.value);
377
- if (matchBoxSelector) {
589
+ }));
590
+ }, [virtualId]);
591
+ const fullPage = useVaultSelector((state) => virtualId ? state.iiif.entities.AnnotationPage[virtualId] : null, [virtualId]);
592
+ const addAnnotation = React$1.useCallback((id, atIndex) => {
593
+ if (virtualId) {
594
+ if (isVaultActivated(id)) {
595
+ const display = id;
596
+ if (!display.__vault) {
597
+ display.bindToVault(vault);
598
+ }
599
+ id = typeof display.source === "string" ? display.source : display.source.id;
600
+ sources.current[id] = display;
601
+ } else if (typeof id !== "string") {
602
+ id = id.id;
603
+ }
604
+ const full = vault.get({ id: virtualId, type: "AnnotationPage" });
605
+ const annotation = vault.get({ id, type: "Annotation" });
606
+ if (full && annotation) {
607
+ if (!full.items.find((r) => r.id === annotation.id)) {
608
+ dispatch(entityActions.addReference({
609
+ id: virtualId,
610
+ type: "AnnotationPage",
611
+ key: "items",
612
+ reference: {
613
+ id,
614
+ type: "Annotation"
615
+ },
616
+ index: atIndex
617
+ }));
618
+ }
619
+ }
620
+ }
621
+ }, [virtualId]);
622
+ const removeAnnotation = React$1.useCallback((id) => {
623
+ if (virtualId) {
624
+ if (isVaultActivated(id)) {
625
+ id = typeof id.source === "string" ? id.source : id.source.id;
626
+ } else if (typeof id !== "string") {
627
+ id = id.id;
628
+ }
629
+ if (sources.current[id]) {
630
+ sources.current[id].beforeRemove();
631
+ }
632
+ const full = vault.get({ id: virtualId, type: "AnnotationPage" });
633
+ if (full) {
634
+ dispatch(entityActions.removeReference({
635
+ id: virtualId,
636
+ type: "AnnotationPage",
637
+ key: "items",
638
+ reference: {
639
+ id,
640
+ type: "Annotation"
641
+ }
642
+ }));
643
+ }
644
+ }
645
+ }, [virtualId]);
646
+ return [
647
+ fullPage,
648
+ {
649
+ addAnnotation,
650
+ removeAnnotation
651
+ }
652
+ ];
653
+ }
654
+
655
+ const VirtualAnnotationPageContext = React$1.createContext(null);
656
+ function useVirtualAnnotationPageContext() {
657
+ const ctx = React$1.useContext(VirtualAnnotationPageContext);
658
+ return [
659
+ ctx.fullPage,
660
+ {
661
+ addAnnotation: ctx.addAnnotation,
662
+ removeAnnotation: ctx.removeAnnotation
663
+ }
664
+ ];
665
+ }
666
+ function VirtualAnnotationProvider({ children }) {
667
+ const [fullPage, { addAnnotation, removeAnnotation }] = useVirtualAnnotationPage();
668
+ return /* @__PURE__ */ React__default["default"].createElement(VirtualAnnotationPageContext.Provider, {
669
+ value: React$1.useMemo(() => ({ fullPage, addAnnotation, removeAnnotation }), [fullPage])
670
+ }, children);
671
+ }
672
+
673
+ var __defProp$9 = Object.defineProperty;
674
+ var __defProps$7 = Object.defineProperties;
675
+ var __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors;
676
+ var __getOwnPropSymbols$9 = Object.getOwnPropertySymbols;
677
+ var __hasOwnProp$9 = Object.prototype.hasOwnProperty;
678
+ var __propIsEnum$9 = Object.prototype.propertyIsEnumerable;
679
+ var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
680
+ var __spreadValues$9 = (a, b) => {
681
+ for (var prop in b || (b = {}))
682
+ if (__hasOwnProp$9.call(b, prop))
683
+ __defNormalProp$9(a, prop, b[prop]);
684
+ if (__getOwnPropSymbols$9)
685
+ for (var prop of __getOwnPropSymbols$9(b)) {
686
+ if (__propIsEnum$9.call(b, prop))
687
+ __defNormalProp$9(a, prop, b[prop]);
688
+ }
689
+ return a;
690
+ };
691
+ var __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
692
+ function DefaultCanvasFallback({ width, style, height }) {
693
+ return /* @__PURE__ */ React__default["default"].createElement("div", {
694
+ style: __spreadProps$7(__spreadValues$9({ width, height, minHeight: 500 }, style || {}), { background: "#f9f9f9" })
695
+ }, "Unknown error");
696
+ }
697
+
698
+ var createRoot;
699
+
700
+ var m = require$$0__default["default"];
701
+ {
702
+ var i = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
703
+ createRoot = function(c, o) {
704
+ i.usingClientEntryPoint = true;
705
+ try {
706
+ return m.createRoot(c, o);
707
+ } finally {
708
+ i.usingClientEntryPoint = false;
709
+ }
710
+ };
711
+ }
712
+
713
+ const ViewerPresetContext = React$1.createContext(null);
714
+ function useViewerPreset() {
715
+ return React$1.useContext(ViewerPresetContext);
716
+ }
717
+
718
+ const PortalContext = React__default["default"].createContext(null);
719
+ const OverlayPortalContext = React__default["default"].createContext(null);
720
+ function CanvasPortal({ children, overlay }) {
721
+ const htmlElement = React$1.useContext(overlay ? OverlayPortalContext : PortalContext);
722
+ const root = React$1.useMemo(() => htmlElement ? createRoot(htmlElement) : null, [htmlElement]);
723
+ const preset = useViewerPreset();
724
+ React$1.useLayoutEffect(() => {
725
+ return () => root == null ? void 0 : root.unmount();
726
+ }, [root]);
727
+ React$1.useLayoutEffect(() => {
728
+ if (root) {
729
+ root.render(/* @__PURE__ */ React__default["default"].createElement(ViewerPresetContext.Provider, {
730
+ value: preset
731
+ }, children));
732
+ }
733
+ }, [children, preset]);
734
+ return null;
735
+ }
736
+
737
+ var __defProp$8 = Object.defineProperty;
738
+ var __defProps$6 = Object.defineProperties;
739
+ var __getOwnPropDescs$6 = Object.getOwnPropertyDescriptors;
740
+ var __getOwnPropSymbols$8 = Object.getOwnPropertySymbols;
741
+ var __hasOwnProp$8 = Object.prototype.hasOwnProperty;
742
+ var __propIsEnum$8 = Object.prototype.propertyIsEnumerable;
743
+ var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
744
+ var __spreadValues$8 = (a, b) => {
745
+ for (var prop in b || (b = {}))
746
+ if (__hasOwnProp$8.call(b, prop))
747
+ __defNormalProp$8(a, prop, b[prop]);
748
+ if (__getOwnPropSymbols$8)
749
+ for (var prop of __getOwnPropSymbols$8(b)) {
750
+ if (__propIsEnum$8.call(b, prop))
751
+ __defNormalProp$8(a, prop, b[prop]);
752
+ }
753
+ return a;
754
+ };
755
+ var __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
756
+ var __objRest = (source, exclude) => {
757
+ var target = {};
758
+ for (var prop in source)
759
+ if (__hasOwnProp$8.call(source, prop) && exclude.indexOf(prop) < 0)
760
+ target[prop] = source[prop];
761
+ if (source != null && __getOwnPropSymbols$8)
762
+ for (var prop of __getOwnPropSymbols$8(source)) {
763
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$8.call(source, prop))
764
+ target[prop] = source[prop];
765
+ }
766
+ return target;
767
+ };
768
+ const AtlasAuto = atlas.AtlasAuto;
769
+ function Viewer(_a) {
770
+ var _b = _a, {
771
+ children,
772
+ errorFallback,
773
+ outerContainerProps = {}
774
+ } = _b, props = __objRest(_b, [
775
+ "children",
776
+ "errorFallback",
777
+ "outerContainerProps"
778
+ ]);
779
+ const portal = React$1.useRef(null);
780
+ const [portalElement, setPortalElement] = React$1.useState();
781
+ const [viewerPreset, setViewerPreset] = React$1.useState();
782
+ const overlayPortal = React$1.useRef(null);
783
+ const [overlayPortalElement, setOverlayPortalElement] = React$1.useState();
784
+ const bridge = useContextBridge();
785
+ const ErrorFallback = errorFallback || DefaultCanvasFallback;
786
+ React$1.useLayoutEffect(() => {
787
+ setPortalElement(portal.current);
788
+ setOverlayPortalElement(overlayPortal.current);
789
+ }, []);
790
+ return /* @__PURE__ */ React__default["default"].createElement(reactErrorBoundary_umd.exports.ErrorBoundary, {
791
+ fallbackRender: () => /* @__PURE__ */ React__default["default"].createElement(ErrorFallback, __spreadValues$8({}, props))
792
+ }, /* @__PURE__ */ React__default["default"].createElement("div", __spreadValues$8({
793
+ style: { position: "relative" }
794
+ }, outerContainerProps), /* @__PURE__ */ React__default["default"].createElement(AtlasAuto, __spreadProps$6(__spreadValues$8({}, props), {
795
+ onCreated: (preset) => {
796
+ setViewerPreset(preset);
797
+ if (props.onCreated) {
798
+ props.onCreated(preset);
799
+ }
800
+ }
801
+ }), /* @__PURE__ */ React__default["default"].createElement(ViewerPresetContext.Provider, {
802
+ value: viewerPreset
803
+ }, /* @__PURE__ */ React__default["default"].createElement(PortalContext.Provider, {
804
+ value: portalElement
805
+ }, /* @__PURE__ */ React__default["default"].createElement(OverlayPortalContext.Provider, {
806
+ value: overlayPortalElement
807
+ }, /* @__PURE__ */ React__default["default"].createElement(ContextBridge, {
808
+ bridge
809
+ }, /* @__PURE__ */ React__default["default"].createElement(VirtualAnnotationProvider, null, children)))))), /* @__PURE__ */ React__default["default"].createElement("div", {
810
+ ref: overlayPortal
811
+ })), /* @__PURE__ */ React__default["default"].createElement("div", {
812
+ ref: portal
813
+ }));
814
+ }
815
+
816
+ function useResourceEvents(resource, scope) {
817
+ const vault = useVault();
818
+ const helper = React$1.useMemo(() => vaultHelpers.createEventsHelper(vault), [vault]);
819
+ const hooks = useVaultSelector(() => {
820
+ if (resource && resource.id) {
821
+ return vault.getResourceMeta(resource.id, "eventManager");
822
+ }
823
+ return null;
824
+ }, [resource]);
825
+ return React$1.useMemo(() => {
826
+ return resource ? helper.getListenersAsProps(resource, scope) : {};
827
+ }, [hooks, resource, vault, scope]);
828
+ }
829
+
830
+ function useStyles(resource, scope) {
831
+ const vault = useVault();
832
+ const helper = React$1.useMemo(() => vaultHelpers.createStylesHelper(vault), [vault]);
833
+ return useVaultSelector(() => {
834
+ if (!resource) {
835
+ return null;
836
+ }
837
+ const styles = helper.getAppliedStyles(resource.id);
838
+ return styles ? scope ? styles[scope] : styles : void 0;
839
+ }, [resource, scope]);
840
+ }
841
+
842
+ const BOX_SELECTOR = /&?(xywh=)?(pixel:|percent:)?([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)/;
843
+ const TEMPORAL_SELECTOR = /&?(t=)(npt:)?([0-9]+(.[0-9]+)?)?(,([0-9]+(.[0-9]+)?))?/;
844
+ function parseSelector(source) {
845
+ if (Array.isArray(source)) {
846
+ return source.reduce((data, nextSource) => {
847
+ const { selector, selectors } = parseSelector(nextSource);
848
+ if (selector) {
849
+ if (!data.selector) {
850
+ data.selector = selector;
851
+ }
852
+ data.selectors.push(...selectors);
853
+ }
854
+ return data;
855
+ }, {
856
+ selector: null,
857
+ selectors: []
858
+ });
859
+ }
860
+ if (!source) {
861
+ return {
862
+ selector: null,
863
+ selectors: []
864
+ };
865
+ }
866
+ if (typeof source === "string") {
867
+ const [id, fragment] = source.split("#");
868
+ if (!fragment) {
869
+ return {
870
+ selector: null,
871
+ selectors: []
872
+ };
873
+ }
874
+ return parseSelector({ type: "FragmentSelector", value: fragment });
875
+ }
876
+ if (source.type === "PointSelector" && (source.t || source.t === 0)) {
877
+ const selector = {
878
+ type: "TemporalSelector",
879
+ temporal: {
880
+ startTime: source.t
881
+ }
882
+ };
883
+ return {
884
+ selector,
885
+ selectors: [selector]
886
+ };
887
+ }
888
+ if (source.type === "FragmentSelector") {
889
+ const matchBoxSelector = BOX_SELECTOR.exec(source.value);
890
+ if (matchBoxSelector) {
378
891
  const selector = {
379
892
  type: "BoxSelector",
380
893
  spatial: {
@@ -415,25 +928,25 @@ function parseSelector(source) {
415
928
  };
416
929
  }
417
930
 
418
- var __defProp$4 = Object.defineProperty;
419
- var __defProps$4 = Object.defineProperties;
420
- var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
421
- var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
422
- var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
423
- var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
424
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
425
- var __spreadValues$4 = (a, b) => {
931
+ var __defProp$7 = Object.defineProperty;
932
+ var __defProps$5 = Object.defineProperties;
933
+ var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
934
+ var __getOwnPropSymbols$7 = Object.getOwnPropertySymbols;
935
+ var __hasOwnProp$7 = Object.prototype.hasOwnProperty;
936
+ var __propIsEnum$7 = Object.prototype.propertyIsEnumerable;
937
+ var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
938
+ var __spreadValues$7 = (a, b) => {
426
939
  for (var prop in b || (b = {}))
427
- if (__hasOwnProp$4.call(b, prop))
428
- __defNormalProp$4(a, prop, b[prop]);
429
- if (__getOwnPropSymbols$4)
430
- for (var prop of __getOwnPropSymbols$4(b)) {
431
- if (__propIsEnum$4.call(b, prop))
432
- __defNormalProp$4(a, prop, b[prop]);
940
+ if (__hasOwnProp$7.call(b, prop))
941
+ __defNormalProp$7(a, prop, b[prop]);
942
+ if (__getOwnPropSymbols$7)
943
+ for (var prop of __getOwnPropSymbols$7(b)) {
944
+ if (__propIsEnum$7.call(b, prop))
945
+ __defNormalProp$7(a, prop, b[prop]);
433
946
  }
434
947
  return a;
435
948
  };
436
- var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
949
+ var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
437
950
  function expandTarget(target, options = {}) {
438
951
  if (Array.isArray(target)) {
439
952
  return expandTarget(target[0]);
@@ -490,7 +1003,7 @@ function expandTarget(target, options = {}) {
490
1003
  if (!fragment) {
491
1004
  return {
492
1005
  type: "SpecificResource",
493
- source: __spreadProps$4(__spreadValues$4({}, target), {
1006
+ source: __spreadProps$5(__spreadValues$7({}, target), {
494
1007
  id
495
1008
  }),
496
1009
  selector: null,
@@ -499,7 +1012,7 @@ function expandTarget(target, options = {}) {
499
1012
  }
500
1013
  return expandTarget({
501
1014
  type: "SpecificResource",
502
- source: __spreadProps$4(__spreadValues$4({}, target), {
1015
+ source: __spreadProps$5(__spreadValues$7({}, target), {
503
1016
  id
504
1017
  }),
505
1018
  selector: {
@@ -516,155 +1029,25 @@ function expandTarget(target, options = {}) {
516
1029
  };
517
1030
  }
518
1031
 
519
- function parseSpecificResource(resource) {
520
- if (resource.type === "SpecificResource") {
521
- return [resource.source, { selector: resource.selector }];
522
- }
523
- return [resource, { selector: null }];
524
- }
525
- function getPaintables(vault, paintingAnnotations, enabledChoices) {
526
- const types = [];
527
- let choice = null;
528
- const items = [];
529
- for (const annotation of paintingAnnotations) {
530
- const bodies = vault.get(annotation.body);
531
- for (const unknownBody of bodies) {
532
- const [body, { selector }] = parseSpecificResource(unknownBody);
533
- const type = (body.type || "unknown").toLowerCase();
534
- if (type === "choice") {
535
- const nestedBodies = vault.get(body.items);
536
- const selected = enabledChoices.length ? enabledChoices.map((cid) => nestedBodies.find((b) => b.id === cid)).filter(Boolean) : [nestedBodies[0]];
537
- if (selected.length === 0) {
538
- selected.push(nestedBodies[0]);
539
- }
540
- choice = {
541
- type: "single-choice",
542
- items: nestedBodies.map((b) => ({
543
- id: b.id,
544
- label: b.label,
545
- selected: selected.indexOf(b) !== -1
546
- })),
547
- label: unknownBody.label
548
- };
549
- bodies.push(...selected);
550
- continue;
551
- }
552
- if (types.indexOf(type) === -1) {
553
- types.push(type);
554
- }
555
- items.push({
556
- type,
557
- resource: body,
558
- target: annotation.target,
559
- selector
560
- });
1032
+ var __defProp$6 = Object.defineProperty;
1033
+ var __defProps$4 = Object.defineProperties;
1034
+ var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
1035
+ var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
1036
+ var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
1037
+ var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
1038
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1039
+ var __spreadValues$6 = (a, b) => {
1040
+ for (var prop in b || (b = {}))
1041
+ if (__hasOwnProp$6.call(b, prop))
1042
+ __defNormalProp$6(a, prop, b[prop]);
1043
+ if (__getOwnPropSymbols$6)
1044
+ for (var prop of __getOwnPropSymbols$6(b)) {
1045
+ if (__propIsEnum$6.call(b, prop))
1046
+ __defNormalProp$6(a, prop, b[prop]);
561
1047
  }
562
- }
563
- return {
564
- types,
565
- items,
566
- choice
567
- };
568
- }
569
- const emptyActions = {
570
- makeChoice: () => {
571
- }
572
- };
573
- const unknownResponse = { type: "unknown" };
574
- const unsupportedStrategy = (reason) => {
575
- return { type: "unknown", reason, annotations: { pages: [] } };
1048
+ return a;
576
1049
  };
577
-
578
- function getImageStrategy(canvas, paintables, loadImageService) {
579
- const imageTypes = [];
580
- for (const singleImage of paintables.items) {
581
- const resource = singleImage.resource && singleImage.resource.type === "SpecificResource" ? singleImage.resource.source : singleImage.resource;
582
- if (!resource.id) {
583
- return unsupportedStrategy("No resource Identifier");
584
- }
585
- let imageService = void 0;
586
- if (resource.service) {
587
- const imageServices = iiifImageApi.getImageServices(resource);
588
- if (imageServices[0]) {
589
- imageService = loadImageService(imageServices[0], canvas);
590
- }
591
- }
592
- const { selector: imageTarget, source } = expandTarget(singleImage.target);
593
- if (source.id !== canvas.id) {
594
- continue;
595
- }
596
- const defaultTarget = {
597
- type: "BoxSelector",
598
- spatial: {
599
- x: 0,
600
- y: 0,
601
- width: canvas.width,
602
- height: canvas.height
603
- }
604
- };
605
- const target = imageTarget ? imageTarget.type === "TemporalSelector" ? {
606
- type: "TemporalBoxSelector",
607
- temporal: imageTarget.temporal,
608
- spatial: defaultTarget.spatial
609
- } : imageTarget : null;
610
- const defaultImageSelector = {
611
- type: "BoxSelector",
612
- spatial: {
613
- x: 0,
614
- y: 0,
615
- width: canvas.width,
616
- height: canvas.height
617
- }
618
- };
619
- const imageSelector = singleImage.resource.type === "SpecificResource" ? expandTarget(singleImage.resource) : null;
620
- const selector = imageSelector && imageSelector.selector && (imageSelector.selector.type === "BoxSelector" || imageSelector.selector.type === "TemporalBoxSelector") ? {
621
- type: "BoxSelector",
622
- spatial: {
623
- x: imageSelector.selector.spatial.x,
624
- y: imageSelector.selector.spatial.y,
625
- width: imageSelector.selector.spatial.width,
626
- height: imageSelector.selector.spatial.height
627
- }
628
- } : defaultImageSelector;
629
- const imageType = {
630
- id: resource.id,
631
- type: "Image",
632
- width: target ? resource.width : canvas.width,
633
- height: target ? resource.height : canvas.height,
634
- service: imageService,
635
- sizes: imageService && imageService.sizes ? imageService.sizes : resource.width && resource.height ? [{ width: resource.width, height: resource.height }] : [],
636
- target: target && target.type !== "PointSelector" ? target : defaultTarget,
637
- selector
638
- };
639
- imageTypes.push(imageType);
640
- }
641
- return {
642
- type: "images",
643
- image: imageTypes[0],
644
- images: imageTypes,
645
- choice: paintables.choice
646
- };
647
- }
648
-
649
- var __defProp$3 = Object.defineProperty;
650
- var __defProps$3 = Object.defineProperties;
651
- var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
652
- var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
653
- var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
654
- var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
655
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
656
- var __spreadValues$3 = (a, b) => {
657
- for (var prop in b || (b = {}))
658
- if (__hasOwnProp$3.call(b, prop))
659
- __defNormalProp$3(a, prop, b[prop]);
660
- if (__getOwnPropSymbols$3)
661
- for (var prop of __getOwnPropSymbols$3(b)) {
662
- if (__propIsEnum$3.call(b, prop))
663
- __defNormalProp$3(a, prop, b[prop]);
664
- }
665
- return a;
666
- };
667
- var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
1050
+ var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
668
1051
  function useAnnotation(options = {}, deps = []) {
669
1052
  const { id, selector } = options;
670
1053
  const ctx = useResourceContext();
@@ -672,11 +1055,11 @@ function useAnnotation(options = {}, deps = []) {
672
1055
  const annotationId = id ? id : ctx.annotation;
673
1056
  const annotation = useVaultSelector((s) => annotationId ? s.iiif.entities.Annotation[annotationId] : void 0, [annotationId]);
674
1057
  const body = useVaultSelector((s) => annotation && annotation.body ? annotation.body.map((singleBody) => singleBody ? s.iiif.entities[singleBody.type][singleBody.id] : null).filter(Boolean) : [], [annotation]);
675
- return React.useMemo(() => {
1058
+ return React$1.useMemo(() => {
676
1059
  if (!annotation) {
677
1060
  return void 0;
678
1061
  }
679
- const newAnnotation = __spreadProps$3(__spreadValues$3({}, annotation), {
1062
+ const newAnnotation = __spreadProps$4(__spreadValues$6({}, annotation), {
680
1063
  body,
681
1064
  target: expandTarget(annotation.target, { typeMap: vault.getState().iiif.mapping })
682
1065
  });
@@ -687,37 +1070,202 @@ function useAnnotation(options = {}, deps = []) {
687
1070
  }, [annotation, selector, body, ...deps]);
688
1071
  }
689
1072
 
690
- function useManifest(options = {}, deps = []) {
1073
+ function useCanvas(options = {}, deps = []) {
691
1074
  const { id, selector } = options;
692
1075
  const ctx = useResourceContext();
693
- useVault();
694
- const manifestId = id ? id : ctx.manifest;
695
- const manifest = useVaultSelector((s) => manifestId ? s.iiif.entities.Manifest[manifestId] : void 0, [manifestId]);
696
- return React.useMemo(() => {
697
- if (!manifest) {
1076
+ const canvasId = id ? id : ctx.canvas;
1077
+ const canvas = useVaultSelector((s) => canvasId ? s.iiif.entities.Canvas[canvasId] : void 0, [canvasId]);
1078
+ return React$1.useMemo(() => {
1079
+ if (!canvas) {
698
1080
  return void 0;
699
1081
  }
700
1082
  if (selector) {
701
- return selector(manifest);
1083
+ return selector(canvas);
702
1084
  }
703
- return manifest;
704
- }, [manifest, selector, ...deps]);
1085
+ return canvas;
1086
+ }, [canvas, selector, ...deps]);
705
1087
  }
706
1088
 
707
- function useCanvas(options = {}, deps = []) {
1089
+ var __defProp$5 = Object.defineProperty;
1090
+ var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
1091
+ var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
1092
+ var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
1093
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1094
+ var __spreadValues$5 = (a, b) => {
1095
+ for (var prop in b || (b = {}))
1096
+ if (__hasOwnProp$5.call(b, prop))
1097
+ __defNormalProp$5(a, prop, b[prop]);
1098
+ if (__getOwnPropSymbols$5)
1099
+ for (var prop of __getOwnPropSymbols$5(b)) {
1100
+ if (__propIsEnum$5.call(b, prop))
1101
+ __defNormalProp$5(a, prop, b[prop]);
1102
+ }
1103
+ return a;
1104
+ };
1105
+ const RenderAnnotation = ({
1106
+ id,
1107
+ style: defaultStyle,
1108
+ className,
1109
+ interactive
1110
+ }) => {
1111
+ const annotation = useAnnotation({ id });
1112
+ const style = useStyles(annotation, "atlas");
1113
+ const html = useStyles(annotation, "html");
1114
+ const events = useResourceEvents(annotation, ["atlas"]);
1115
+ const canvas = useCanvas();
1116
+ const allStyles = React$1.useMemo(() => {
1117
+ return atlas.mergeStyles(defaultStyle, style);
1118
+ }, [defaultStyle, style]);
1119
+ const isValid = canvas && annotation && annotation.target && annotation.target.selector && annotation.target.selector.type === "BoxSelector" && annotation.target.source && annotation.target.source.id === canvas.id;
1120
+ if (!isValid) {
1121
+ return null;
1122
+ }
1123
+ return /* @__PURE__ */ React__default["default"].createElement(atlas.RegionHighlight, __spreadValues$5({
1124
+ id: annotation.id,
1125
+ isEditing: true,
1126
+ region: annotation.target.selector.spatial,
1127
+ style: allStyles,
1128
+ className: (html == null ? void 0 : html.className) || className,
1129
+ interactive: !!((html == null ? void 0 : html.href) || interactive),
1130
+ href: (html == null ? void 0 : html.href) || null,
1131
+ title: (html == null ? void 0 : html.title) || null,
1132
+ hrefTarget: (html == null ? void 0 : html.target) || null
1133
+ }, events));
1134
+ };
1135
+
1136
+ const RenderAnnotationPage = ({
1137
+ className,
1138
+ page
1139
+ }) => {
1140
+ var _a;
1141
+ const style = useStyles(page, "atlas");
1142
+ const html = useStyles(page, "html");
1143
+ useVaultSelector((state) => page.id ? state.iiif.entities.AnnotationPage[page.id] : null, []);
1144
+ return /* @__PURE__ */ React__default["default"].createElement(React$1.Fragment, null, (_a = page.items) == null ? void 0 : _a.map((annotation) => {
1145
+ return /* @__PURE__ */ React__default["default"].createElement(RenderAnnotation, {
1146
+ key: annotation.id,
1147
+ id: annotation.id,
1148
+ style,
1149
+ className: (html == null ? void 0 : html.className) || className
1150
+ });
1151
+ }));
1152
+ };
1153
+
1154
+ const TileSet = atlas.TileSet;
1155
+ function RenderImage({
1156
+ id,
1157
+ image,
1158
+ thumbnail,
1159
+ isStatic,
1160
+ x = 0,
1161
+ y = 0,
1162
+ annotations,
1163
+ children
1164
+ }) {
1165
+ var _a, _b, _c, _d;
1166
+ return /* @__PURE__ */ React__default["default"].createElement(React$1.Fragment, {
1167
+ key: id
1168
+ }, !image.service ? /* @__PURE__ */ React__default["default"].createElement(React$1.Fragment, {
1169
+ key: "no-service"
1170
+ }, /* @__PURE__ */ React__default["default"].createElement("world-image", {
1171
+ uri: image.id,
1172
+ target: image.target.spatial,
1173
+ display: image.width && image.height ? {
1174
+ width: image.width,
1175
+ height: image.height
1176
+ } : void 0
1177
+ }), annotations, children) : /* @__PURE__ */ React__default["default"].createElement(React$1.Fragment, {
1178
+ key: "service"
1179
+ }, /* @__PURE__ */ React__default["default"].createElement(TileSet, {
1180
+ viewport: isStatic,
1181
+ tiles: {
1182
+ id: image.service.id || image.service["@id"],
1183
+ height: image.height,
1184
+ width: image.width,
1185
+ imageService: image.service,
1186
+ thumbnail: thumbnail && thumbnail.type === "fixed" ? thumbnail : void 0
1187
+ },
1188
+ x: ((_a = image.target) == null ? void 0 : _a.spatial.x) + x,
1189
+ y: ((_b = image.target) == null ? void 0 : _b.spatial.y) + y,
1190
+ width: (_c = image.target) == null ? void 0 : _c.spatial.width,
1191
+ height: (_d = image.target) == null ? void 0 : _d.spatial.height
1192
+ }), annotations, children));
1193
+ }
1194
+
1195
+ function parseSpecificResource(resource) {
1196
+ if (resource.type === "SpecificResource") {
1197
+ return [resource.source, { selector: resource.selector }];
1198
+ }
1199
+ return [resource, { selector: null }];
1200
+ }
1201
+ function getPaintables(vault, paintingAnnotations, enabledChoices) {
1202
+ const types = [];
1203
+ let choice = null;
1204
+ const items = [];
1205
+ for (const annotation of paintingAnnotations) {
1206
+ const bodies = vault.get(annotation.body);
1207
+ for (const unknownBody of bodies) {
1208
+ const [body, { selector }] = parseSpecificResource(unknownBody);
1209
+ const type = (body.type || "unknown").toLowerCase();
1210
+ if (type === "choice") {
1211
+ const nestedBodies = vault.get(body.items);
1212
+ const selected = enabledChoices.length ? enabledChoices.map((cid) => nestedBodies.find((b) => b.id === cid)).filter(Boolean) : [nestedBodies[0]];
1213
+ if (selected.length === 0) {
1214
+ selected.push(nestedBodies[0]);
1215
+ }
1216
+ choice = {
1217
+ type: "single-choice",
1218
+ items: nestedBodies.map((b) => ({
1219
+ id: b.id,
1220
+ label: b.label,
1221
+ selected: selected.indexOf(b) !== -1
1222
+ })),
1223
+ label: unknownBody.label
1224
+ };
1225
+ bodies.push(...selected);
1226
+ continue;
1227
+ }
1228
+ if (types.indexOf(type) === -1) {
1229
+ types.push(type);
1230
+ }
1231
+ items.push({
1232
+ type,
1233
+ resource: body,
1234
+ target: annotation.target,
1235
+ selector
1236
+ });
1237
+ }
1238
+ }
1239
+ return {
1240
+ types,
1241
+ items,
1242
+ choice
1243
+ };
1244
+ }
1245
+ const emptyActions = {
1246
+ makeChoice: () => {
1247
+ }
1248
+ };
1249
+ const unknownResponse = { type: "unknown" };
1250
+ const unsupportedStrategy = (reason) => {
1251
+ return { type: "unknown", reason, annotations: { pages: [] } };
1252
+ };
1253
+
1254
+ function useManifest(options = {}, deps = []) {
708
1255
  const { id, selector } = options;
709
1256
  const ctx = useResourceContext();
710
- const canvasId = id ? id : ctx.canvas;
711
- const canvas = useVaultSelector((s) => canvasId ? s.iiif.entities.Canvas[canvasId] : void 0, [canvasId]);
712
- return React.useMemo(() => {
713
- if (!canvas) {
1257
+ useVault();
1258
+ const manifestId = id ? id : ctx.manifest;
1259
+ const manifest = useVaultSelector((s) => manifestId ? s.iiif.entities.Manifest[manifestId] : void 0, [manifestId]);
1260
+ return React$1.useMemo(() => {
1261
+ if (!manifest) {
714
1262
  return void 0;
715
1263
  }
716
1264
  if (selector) {
717
- return selector(canvas);
1265
+ return selector(manifest);
718
1266
  }
719
- return canvas;
720
- }, [canvas, selector, ...deps]);
1267
+ return manifest;
1268
+ }, [manifest, selector, ...deps]);
721
1269
  }
722
1270
 
723
1271
  function getMeta$1(state, resourceId) {
@@ -781,25 +1329,25 @@ function flattenAnnotationPageIds({
781
1329
  return foundIds;
782
1330
  }
783
1331
 
784
- var __defProp$2 = Object.defineProperty;
785
- var __defProps$2 = Object.defineProperties;
786
- var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
787
- var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
788
- var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
789
- var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
790
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
791
- var __spreadValues$2 = (a, b) => {
1332
+ var __defProp$4 = Object.defineProperty;
1333
+ var __defProps$3 = Object.defineProperties;
1334
+ var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
1335
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
1336
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
1337
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
1338
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1339
+ var __spreadValues$4 = (a, b) => {
792
1340
  for (var prop in b || (b = {}))
793
- if (__hasOwnProp$2.call(b, prop))
794
- __defNormalProp$2(a, prop, b[prop]);
795
- if (__getOwnPropSymbols$2)
796
- for (var prop of __getOwnPropSymbols$2(b)) {
797
- if (__propIsEnum$2.call(b, prop))
798
- __defNormalProp$2(a, prop, b[prop]);
1341
+ if (__hasOwnProp$4.call(b, prop))
1342
+ __defNormalProp$4(a, prop, b[prop]);
1343
+ if (__getOwnPropSymbols$4)
1344
+ for (var prop of __getOwnPropSymbols$4(b)) {
1345
+ if (__propIsEnum$4.call(b, prop))
1346
+ __defNormalProp$4(a, prop, b[prop]);
799
1347
  }
800
1348
  return a;
801
1349
  };
802
- var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
1350
+ var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
803
1351
  function getMeta(state, resourceId) {
804
1352
  var _a;
805
1353
  const resourceMeta = (_a = state == null ? void 0 : state.iiif) == null ? void 0 : _a.meta[resourceId];
@@ -813,7 +1361,7 @@ function useAnnotationPageManager(resourceId, options = {}) {
813
1361
  const manifest = useManifest();
814
1362
  const canvas = useCanvas();
815
1363
  const canvases = useVisibleCanvases();
816
- const availablePageIds = React.useMemo(() => {
1364
+ const availablePageIds = React$1.useMemo(() => {
817
1365
  return flattenAnnotationPageIds({
818
1366
  all: options.all,
819
1367
  manifest,
@@ -822,7 +1370,7 @@ function useAnnotationPageManager(resourceId, options = {}) {
822
1370
  });
823
1371
  }, [options.all, canvas, canvases, manifest]);
824
1372
  const enabledPageIds = useEnabledAnnotationPageIds(resourceId, options.all ? void 0 : availablePageIds);
825
- const setPageDisabled = React.useCallback((deselectId) => {
1373
+ const setPageDisabled = React$1.useCallback((deselectId) => {
826
1374
  if (!resourceId) {
827
1375
  return;
828
1376
  }
@@ -830,12 +1378,12 @@ function useAnnotationPageManager(resourceId, options = {}) {
830
1378
  if (existingResources && !existingResources[resourceId]) {
831
1379
  return existingResources;
832
1380
  }
833
- return __spreadProps$2(__spreadValues$2({}, existingResources || {}), {
1381
+ return __spreadProps$3(__spreadValues$4({}, existingResources || {}), {
834
1382
  [resourceId]: false
835
1383
  });
836
1384
  });
837
1385
  }, [resourceId, vault]);
838
- const setPageEnabled = React.useCallback((id, opt = {}) => {
1386
+ const setPageEnabled = React$1.useCallback((id, opt = {}) => {
839
1387
  if (!resourceId) {
840
1388
  return;
841
1389
  }
@@ -857,7 +1405,7 @@ function useAnnotationPageManager(resourceId, options = {}) {
857
1405
  if (existingResources && existingResources[resourceId]) {
858
1406
  return existingResources;
859
1407
  }
860
- return __spreadProps$2(__spreadValues$2({}, existingResources || {}), {
1408
+ return __spreadProps$3(__spreadValues$4({}, existingResources || {}), {
861
1409
  [resourceId]: true
862
1410
  });
863
1411
  });
@@ -870,194 +1418,103 @@ function useAnnotationPageManager(resourceId, options = {}) {
870
1418
  };
871
1419
  }
872
1420
 
873
- function usePaintingAnnotations(options = {}) {
874
- const canvas = useCanvas(options.canvasId ? { id: options.canvasId } : void 0);
875
- return useVaultSelector((state, vault) => {
876
- if (!canvas) {
877
- return [];
878
- }
879
- const annotationPages = vault.get(canvas.items);
880
- const flatAnnotations = [];
881
- for (const page of annotationPages) {
882
- flatAnnotations.push(...vault.get(page.items));
883
- }
884
- return flatAnnotations;
885
- }, [canvas]);
886
- }
887
-
888
- function useAnnotationsAtTime(time, options = {}) {
889
- const allAnnotations = usePaintingAnnotations(options);
890
- return allAnnotations;
1421
+ function useResources(ids, type) {
1422
+ return useVaultSelector((state, vault) => vault.get(ids.map((id) => ({ id, type }))), [ids, type]);
891
1423
  }
892
1424
 
893
- function useCanvasClock(canvasId, autoplay = false) {
1425
+ const ImageServiceLoaderContext = React__default["default"].createContext(new iiifImageApi.ImageServiceLoader());
1426
+ function useImageServiceLoader() {
1427
+ return React$1.useContext(ImageServiceLoaderContext);
894
1428
  }
895
1429
 
896
- function useCollection(options, deps = []) {
897
- const { id, selector } = options;
898
- const ctx = useResourceContext();
899
- const collectionId = id ? id : ctx.collection;
900
- const collection = useVaultSelector((s) => collectionId ? s.iiif.entities.Collection[collectionId] : void 0, [collectionId]);
901
- return React.useMemo(() => {
902
- if (!collection) {
903
- return void 0;
904
- }
905
- if (selector) {
906
- return selector(collection);
907
- }
908
- return collection;
909
- }, [collection, selector, ...deps]);
910
- }
911
-
912
- function useDispatch() {
913
- const vault = useVault();
914
- const store = vault.getStore();
915
- return React.useMemo(() => {
916
- return (action) => store.dispatch(action);
917
- }, [store]);
918
- }
919
-
920
- function useEventListener(resource, name, listener, scope, deps = []) {
921
- const vault = useVault();
922
- const helper = React.useMemo(() => vaultHelpers.createEventsHelper(vault), [vault]);
923
- React.useEffect(() => {
924
- const currentResource = resource;
925
- if (!currentResource) {
926
- return () => {
927
- };
1430
+ var __defProp$3 = Object.defineProperty;
1431
+ var __defProps$2 = Object.defineProperties;
1432
+ var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
1433
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
1434
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
1435
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
1436
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1437
+ var __spreadValues$3 = (a, b) => {
1438
+ for (var prop in b || (b = {}))
1439
+ if (__hasOwnProp$3.call(b, prop))
1440
+ __defNormalProp$3(a, prop, b[prop]);
1441
+ if (__getOwnPropSymbols$3)
1442
+ for (var prop of __getOwnPropSymbols$3(b)) {
1443
+ if (__propIsEnum$3.call(b, prop))
1444
+ __defNormalProp$3(a, prop, b[prop]);
928
1445
  }
929
- helper.addEventListener(currentResource, name, listener, scope);
1446
+ return a;
1447
+ };
1448
+ var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
1449
+ function useLoadImageService() {
1450
+ const loader = useImageServiceLoader();
1451
+ const [imageServiceStatus, setImageServiceStatus] = React$1.useState({});
1452
+ const didUnmount = React$1.useRef(false);
1453
+ React$1.useEffect(() => {
930
1454
  return () => {
931
- helper.removeEventListener(currentResource, name, listener);
932
- };
933
- }, [helper, resource, name, ...deps]);
934
- }
935
-
936
- function useExistingVault() {
937
- const oldContext = React.useContext(ReactVaultContext);
938
- return oldContext && oldContext.vault ? oldContext.vault : vault.globalVault();
939
- }
940
-
941
- function useExternalCollection(idOrRef, options) {
942
- const { id, isLoaded, error, resource, requestId, cached } = useExternalResource(idOrRef, options);
943
- return { id, isLoaded, error, manifest: resource, requestId, cached };
944
- }
945
-
946
- var __async = (__this, __arguments, generator) => {
947
- return new Promise((resolve, reject) => {
948
- var fulfilled = (value) => {
949
- try {
950
- step(generator.next(value));
951
- } catch (e) {
952
- reject(e);
953
- }
954
- };
955
- var rejected = (value) => {
956
- try {
957
- step(generator.throw(value));
958
- } catch (e) {
959
- reject(e);
960
- }
1455
+ didUnmount.current = true;
961
1456
  };
962
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
963
- step((generator = generator.apply(__this, __arguments)).next());
964
- });
965
- };
966
- function useImageService({ cacheKey } = {}) {
967
- const canvas = useCanvas();
968
- const annotations = usePaintingAnnotations();
969
- const vault = useVault();
970
- const imageService = useImageServiceLoader();
971
- const [_data, setData] = React.useState(void 0);
972
- const [isFetching, setIsFetching] = React.useState(false);
973
- const [status, setStatus] = React.useState("idle");
974
- const [error, setError] = React.useState(void 0);
975
- const key = canvas ? canvas.id : "undefined";
976
- const initialData = React.useMemo(() => {
977
- try {
978
- if (canvas && annotations.length) {
979
- const annotation = annotations[0];
980
- const resource = vault.get(annotation.body[0]);
981
- const imageServices = iiifImageApi.getImageServices(resource);
982
- const firstImageService = imageServices[0];
983
- if (!firstImageService) {
984
- return void 0;
1457
+ }, []);
1458
+ const loadImageService = React$1.useCallback((imageService, { height, width }) => {
1459
+ if (imageService) {
1460
+ const imageServiceId = imageService.id || imageService["@id"];
1461
+ const syncLoaded = loader.loadServiceSync({
1462
+ id: imageServiceId,
1463
+ width: imageService.width || width,
1464
+ height: imageService.height || height,
1465
+ source: imageService
1466
+ });
1467
+ if (syncLoaded) {
1468
+ imageService = syncLoaded;
1469
+ } else if (!imageServiceStatus[imageServiceId]) {
1470
+ if (!didUnmount.current) {
1471
+ setImageServiceStatus((r) => {
1472
+ return __spreadProps$2(__spreadValues$3({}, r), {
1473
+ [imageServiceId]: "loading"
1474
+ });
1475
+ });
985
1476
  }
986
- return imageService.loadServiceSync({
987
- id: firstImageService.id || firstImageService["@id"],
988
- width: firstImageService.width || canvas.width,
989
- height: firstImageService.height || canvas.height
990
- }) || void 0;
991
- }
992
- } catch (e) {
993
- console.error(e);
994
- }
995
- return void 0;
996
- }, [key, cacheKey, canvas]);
997
- const data = status === "success" && _data ? _data : initialData;
998
- React.useEffect(() => {
999
- (() => __async(this, null, function* () {
1000
- try {
1001
- if (canvas && annotations.length) {
1002
- const annotation = annotations[0];
1003
- const resource = vault.get(annotation.body[0]);
1004
- const imageServices = iiifImageApi.getImageServices(resource);
1005
- const firstImageService = imageServices[0];
1006
- if (!firstImageService) {
1007
- return;
1008
- }
1009
- setIsFetching(true);
1010
- setStatus("loading");
1011
- try {
1012
- const loadedService = (yield imageService.loadService({
1013
- id: firstImageService.id || firstImageService["@id"],
1014
- width: firstImageService.width || canvas.width,
1015
- height: firstImageService.height || canvas.height
1016
- })) || void 0;
1017
- setData(loadedService);
1018
- setStatus("success");
1019
- setIsFetching(false);
1020
- } catch (err) {
1021
- setStatus("error");
1022
- setError(err);
1477
+ loader.loadService({
1478
+ id: imageServiceId,
1479
+ width: imageService.width || width,
1480
+ height: imageService.height || height
1481
+ }).then(() => {
1482
+ if (!didUnmount.current) {
1483
+ setImageServiceStatus((r) => {
1484
+ return __spreadProps$2(__spreadValues$3({}, r), {
1485
+ [imageServiceId]: "done"
1486
+ });
1487
+ });
1023
1488
  }
1024
- }
1025
- } catch (err) {
1026
- setStatus("error");
1027
- setError(err);
1489
+ });
1028
1490
  }
1029
- }))();
1030
- }, [key, cacheKey]);
1031
- return React.useMemo(() => {
1032
- return {
1033
- data,
1034
- isFetching,
1035
- status,
1036
- error
1037
- };
1038
- }, [data, isFetching, status, error]);
1491
+ }
1492
+ return imageService;
1493
+ }, [loader, imageServiceStatus]);
1494
+ return [loadImageService, imageServiceStatus];
1039
1495
  }
1040
1496
 
1041
- function useImageTile() {
1042
- const imageService = useImageService();
1043
- return {
1044
- isLoading: imageService.isFetching,
1045
- tile: imageService.data ? {
1046
- id: imageService.data.id || imageService.data["@id"],
1047
- width: imageService.data.width,
1048
- height: imageService.data.height,
1049
- imageService: imageService.data,
1050
- thumbnail: void 0
1051
- } : null
1052
- };
1497
+ function usePaintingAnnotations(options = {}) {
1498
+ const canvas = useCanvas(options.canvasId ? { id: options.canvasId } : void 0);
1499
+ return useVaultSelector((state, vault) => {
1500
+ if (!canvas) {
1501
+ return [];
1502
+ }
1503
+ const annotationPages = vault.get(canvas.items);
1504
+ const flatAnnotations = [];
1505
+ for (const page of annotationPages) {
1506
+ flatAnnotations.push(...vault.get(page.items));
1507
+ }
1508
+ return flatAnnotations;
1509
+ }, [canvas]);
1053
1510
  }
1054
1511
 
1055
1512
  function usePaintables(options, deps = []) {
1056
1513
  const vault = useVault();
1057
1514
  const paintingAnnotations = usePaintingAnnotations();
1058
- const [enabledChoices, setEnabledChoices] = React.useState((options == null ? void 0 : options.defaultChoices) || []);
1059
- const paintables = React.useMemo(() => getPaintables(vault, paintingAnnotations, enabledChoices), [vault, paintingAnnotations, enabledChoices, ...deps]);
1060
- const makeChoice = React.useCallback((id, { deselectOthers = true, deselect = false } = {}) => {
1515
+ const [enabledChoices, setEnabledChoices] = React$1.useState((options == null ? void 0 : options.defaultChoices) || []);
1516
+ const paintables = React$1.useMemo(() => getPaintables(vault, paintingAnnotations, enabledChoices), [vault, paintingAnnotations, enabledChoices, ...deps]);
1517
+ const makeChoice = React$1.useCallback((id, { deselectOthers = true, deselect = false } = {}) => {
1061
1518
  if (paintables.choice) {
1062
1519
  if (paintables.choice.type !== "single-choice") {
1063
1520
  throw new Error("Complex choice not supported yet");
@@ -1096,65 +1553,271 @@ function usePaintables(options, deps = []) {
1096
1553
  return [paintables, actions];
1097
1554
  }
1098
1555
 
1099
- function useRange(options = {}, deps = []) {
1100
- const { id, selector } = options;
1101
- const ctx = useResourceContext();
1102
- const rangeId = id ? id : ctx.range;
1103
- const range = useVaultSelector((s) => rangeId ? s.iiif.entities.Range[rangeId] : void 0, [rangeId]);
1104
- return React.useMemo(() => {
1105
- if (!range) {
1106
- return void 0;
1107
- }
1108
- if (selector) {
1109
- return selector(range);
1556
+ function getImageStrategy(canvas, paintables, loadImageService) {
1557
+ const imageTypes = [];
1558
+ for (const singleImage of paintables.items) {
1559
+ const resource = singleImage.resource && singleImage.resource.type === "SpecificResource" ? singleImage.resource.source : singleImage.resource;
1560
+ if (!resource.id) {
1561
+ return unsupportedStrategy("No resource Identifier");
1110
1562
  }
1111
- return range;
1112
- }, [range, selector, ...deps]);
1113
- }
1114
-
1115
- function useResourceEvents(resource, scope) {
1116
- const vault = useVault();
1117
- const helper = React.useMemo(() => vaultHelpers.createEventsHelper(vault), [vault]);
1118
- const hooks = useVaultSelector(() => {
1119
- if (resource && resource.id) {
1120
- return vault.getResourceMeta(resource.id, "eventManager");
1563
+ let imageService = void 0;
1564
+ if (resource.service) {
1565
+ const imageServices = iiifImageApi.getImageServices(resource);
1566
+ if (imageServices[0]) {
1567
+ imageService = loadImageService(imageServices[0], canvas);
1568
+ }
1121
1569
  }
1122
- return null;
1123
- }, [resource]);
1124
- return React.useMemo(() => {
1125
- return resource ? helper.getListenersAsProps(resource, scope) : {};
1126
- }, [hooks, resource, vault, scope]);
1127
- }
1128
-
1129
- function useResources(ids, type) {
1130
- return useVaultSelector((state, vault) => vault.get(ids.map((id) => ({ id, type }))), [ids, type]);
1131
- }
1132
-
1133
- function useSearchService() {
1134
- const manifest = useManifest();
1135
- return manifest ? manifest.service.find((service) => service.profile === "SearchService1" || service.profile === "http://iiif.io/api/search/1/search") : void 0;
1136
- }
1137
-
1138
- function useStyleHelper() {
1139
- const vault = useVault();
1140
- return React.useMemo(() => vaultHelpers.createStylesHelper(vault), [vault]);
1141
- }
1142
-
1143
- function useStyles(resource, scope) {
1144
- const vault = useVault();
1145
- const helper = React.useMemo(() => vaultHelpers.createStylesHelper(vault), [vault]);
1146
- return useVaultSelector(() => {
1147
- if (!resource) {
1148
- return null;
1570
+ const { selector: imageTarget, source } = expandTarget(singleImage.target);
1571
+ if (source.id !== canvas.id) {
1572
+ continue;
1149
1573
  }
1150
- const styles = helper.getAppliedStyles(resource.id);
1151
- return styles ? scope ? styles[scope] : styles : void 0;
1152
- }, [resource, scope]);
1574
+ const defaultTarget = {
1575
+ type: "BoxSelector",
1576
+ spatial: {
1577
+ x: 0,
1578
+ y: 0,
1579
+ width: canvas.width,
1580
+ height: canvas.height
1581
+ }
1582
+ };
1583
+ const target = imageTarget ? imageTarget.type === "TemporalSelector" ? {
1584
+ type: "TemporalBoxSelector",
1585
+ temporal: imageTarget.temporal,
1586
+ spatial: defaultTarget.spatial
1587
+ } : imageTarget : null;
1588
+ const defaultImageSelector = {
1589
+ type: "BoxSelector",
1590
+ spatial: {
1591
+ x: 0,
1592
+ y: 0,
1593
+ width: canvas.width,
1594
+ height: canvas.height
1595
+ }
1596
+ };
1597
+ const imageSelector = singleImage.resource.type === "SpecificResource" ? expandTarget(singleImage.resource) : null;
1598
+ const selector = imageSelector && imageSelector.selector && (imageSelector.selector.type === "BoxSelector" || imageSelector.selector.type === "TemporalBoxSelector") ? {
1599
+ type: "BoxSelector",
1600
+ spatial: {
1601
+ x: imageSelector.selector.spatial.x,
1602
+ y: imageSelector.selector.spatial.y,
1603
+ width: imageSelector.selector.spatial.width,
1604
+ height: imageSelector.selector.spatial.height
1605
+ }
1606
+ } : defaultImageSelector;
1607
+ if (imageService && !imageService.id) {
1608
+ imageService.id = imageService["@id"];
1609
+ }
1610
+ const imageType = {
1611
+ id: resource.id,
1612
+ type: "Image",
1613
+ width: target ? resource.width : canvas.width,
1614
+ height: target ? resource.height : canvas.height,
1615
+ service: imageService,
1616
+ sizes: imageService && imageService.sizes ? imageService.sizes : resource.width && resource.height ? [{ width: resource.width, height: resource.height }] : [],
1617
+ target: target && target.type !== "PointSelector" ? target : defaultTarget,
1618
+ selector
1619
+ };
1620
+ imageTypes.push(imageType);
1621
+ }
1622
+ return {
1623
+ type: "images",
1624
+ image: imageTypes[0],
1625
+ images: imageTypes,
1626
+ choice: paintables.choice
1627
+ };
1628
+ }
1629
+
1630
+ const supportedFormats = ["model/gltf-binary"];
1631
+ function get3dStrategy(canvas, paintables) {
1632
+ const first = paintables.items[0];
1633
+ const resource = first.resource;
1634
+ if (!resource.format) {
1635
+ return unsupportedStrategy("Unknown format");
1636
+ }
1637
+ if (supportedFormats.indexOf(resource.format) === -1) {
1638
+ return unsupportedStrategy(`3D format: ${resource.format} is unsupported`);
1639
+ }
1640
+ return {
1641
+ type: "3d-model",
1642
+ model: resource
1643
+ };
1644
+ }
1645
+
1646
+ function getAudioStrategy(canvas, paintables) {
1647
+ var _a;
1648
+ if (!canvas.duration) {
1649
+ return unsupportedStrategy("No duration on canvas");
1650
+ }
1651
+ if (paintables.items.length > 1) {
1652
+ return unsupportedStrategy("Only one audio source supported");
1653
+ }
1654
+ const audioResource = (_a = paintables.items[0]) == null ? void 0 : _a.resource;
1655
+ if (!audioResource) {
1656
+ return unsupportedStrategy("Unknown audio");
1657
+ }
1658
+ if (!audioResource.format) {
1659
+ return unsupportedStrategy("Audio does not have format");
1660
+ }
1661
+ return {
1662
+ type: "media",
1663
+ media: {
1664
+ duration: canvas.duration,
1665
+ url: audioResource.id,
1666
+ type: "Sound",
1667
+ target: {
1668
+ type: "TemporalSelector",
1669
+ temporal: {
1670
+ startTime: 0,
1671
+ endTime: canvas.duration
1672
+ }
1673
+ },
1674
+ format: audioResource.format,
1675
+ selector: {
1676
+ type: "TemporalSelector",
1677
+ temporal: {
1678
+ startTime: 0,
1679
+ endTime: canvas.duration
1680
+ }
1681
+ }
1682
+ },
1683
+ annotations: {
1684
+ pages: []
1685
+ }
1686
+ };
1687
+ }
1688
+
1689
+ function getVideoStrategy(canvas, paintables) {
1690
+ var _a;
1691
+ const videoPaintables = paintables.items.filter((t) => t.type === "video");
1692
+ if (!canvas.duration) {
1693
+ return unsupportedStrategy("No duration on canvas");
1694
+ }
1695
+ if (videoPaintables.length > 1) {
1696
+ return unsupportedStrategy("Only one video source supported");
1697
+ }
1698
+ const audioResource = (_a = videoPaintables[0]) == null ? void 0 : _a.resource;
1699
+ if (!audioResource) {
1700
+ return unsupportedStrategy("Unknown video");
1701
+ }
1702
+ if (!audioResource.format) {
1703
+ return unsupportedStrategy("Video does not have format");
1704
+ }
1705
+ return {
1706
+ type: "media",
1707
+ media: {
1708
+ duration: canvas.duration,
1709
+ url: audioResource.id,
1710
+ type: "Video",
1711
+ items: [],
1712
+ target: {
1713
+ type: "TemporalSelector",
1714
+ temporal: {
1715
+ startTime: 0,
1716
+ endTime: canvas.duration
1717
+ }
1718
+ },
1719
+ format: audioResource.format,
1720
+ selector: {
1721
+ type: "TemporalSelector",
1722
+ temporal: {
1723
+ startTime: 0,
1724
+ endTime: canvas.duration
1725
+ }
1726
+ }
1727
+ },
1728
+ annotations: {
1729
+ pages: []
1730
+ }
1731
+ };
1732
+ }
1733
+
1734
+ var __defProp$2 = Object.defineProperty;
1735
+ var __defProps$1 = Object.defineProperties;
1736
+ var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
1737
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
1738
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
1739
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
1740
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1741
+ var __spreadValues$2 = (a, b) => {
1742
+ for (var prop in b || (b = {}))
1743
+ if (__hasOwnProp$2.call(b, prop))
1744
+ __defNormalProp$2(a, prop, b[prop]);
1745
+ if (__getOwnPropSymbols$2)
1746
+ for (var prop of __getOwnPropSymbols$2(b)) {
1747
+ if (__propIsEnum$2.call(b, prop))
1748
+ __defNormalProp$2(a, prop, b[prop]);
1749
+ }
1750
+ return a;
1751
+ };
1752
+ var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
1753
+ function useRenderingStrategy(options) {
1754
+ const manifest = useManifest();
1755
+ const canvas = useCanvas();
1756
+ const vault = useVault();
1757
+ const [loadImageService, imageServiceStatus] = useLoadImageService();
1758
+ const { enabledPageIds } = useAnnotationPageManager((options == null ? void 0 : options.annotationPageManagerId) || (manifest == null ? void 0 : manifest.id) || (canvas == null ? void 0 : canvas.id), {
1759
+ all: false
1760
+ });
1761
+ const enabledPages = useResources(enabledPageIds, "AnnotationPage");
1762
+ const supports = (options == null ? void 0 : options.strategies) || ["images", "media", "complex-timeline"];
1763
+ const [paintables, actions] = usePaintables(options, [imageServiceStatus]);
1764
+ const strategy = React$1.useMemo(() => {
1765
+ if (!canvas || paintables.types.length === 0) {
1766
+ return unknownResponse;
1767
+ }
1768
+ if (paintables.types.length !== 1) {
1769
+ if (paintables.types.length === 2 && paintables.types.indexOf("text") !== -1) {
1770
+ paintables.types = paintables.types.filter((t) => t !== "text");
1771
+ } else {
1772
+ if (supports.indexOf("complex-timeline") === -1) {
1773
+ return unsupportedStrategy("Complex timeline not supported");
1774
+ }
1775
+ return unsupportedStrategy("ComplexTimelineStrategy not yet supported");
1776
+ }
1777
+ }
1778
+ const mainType = paintables.types[0];
1779
+ if (mainType === "image") {
1780
+ if (supports.indexOf("images") === -1) {
1781
+ return unsupportedStrategy("Image not supported");
1782
+ }
1783
+ return getImageStrategy(canvas, paintables, loadImageService);
1784
+ }
1785
+ if (mainType === "Model" || mainType === "model") {
1786
+ if (supports.indexOf("3d-model") === -1) {
1787
+ return unsupportedStrategy("3D not supported");
1788
+ }
1789
+ return get3dStrategy(canvas, paintables);
1790
+ }
1791
+ if (mainType === "sound" || mainType === "audio") {
1792
+ if (supports.indexOf("media") === -1) {
1793
+ return unsupportedStrategy("Media not supported");
1794
+ }
1795
+ return getAudioStrategy(canvas, paintables);
1796
+ }
1797
+ if (mainType === "video") {
1798
+ if (supports.indexOf("media") === -1) {
1799
+ return unsupportedStrategy("Media not supported");
1800
+ }
1801
+ return getVideoStrategy(canvas, paintables);
1802
+ }
1803
+ return unknownResponse;
1804
+ }, [canvas, paintables, vault, actions.makeChoice]);
1805
+ return React$1.useMemo(() => {
1806
+ if (strategy.type === "unknown") {
1807
+ return [strategy, emptyActions];
1808
+ }
1809
+ return [
1810
+ __spreadProps$1(__spreadValues$2({}, strategy), {
1811
+ annotations: { pages: enabledPages }
1812
+ }),
1813
+ actions
1814
+ ];
1815
+ }, [strategy, enabledPages]);
1153
1816
  }
1154
1817
 
1155
1818
  const useVaultEffect = (callback, deps = []) => {
1156
1819
  const vault = useVault();
1157
- React.useEffect(() => {
1820
+ React$1.useEffect(() => {
1158
1821
  callback(vault);
1159
1822
  }, [vault, ...deps]);
1160
1823
  };
@@ -1162,13 +1825,13 @@ const useVaultEffect = (callback, deps = []) => {
1162
1825
  function useThumbnail(request, dereference, { canvasId, manifestId } = {}) {
1163
1826
  const vault = useVault();
1164
1827
  const loader = useImageServiceLoader();
1165
- const helper = React.useMemo(() => vaultHelpers.createThumbnailHelper(vault, { imageServiceLoader: loader }), [vault, loader]);
1166
- const [thumbnail, setThumbnail] = React.useState();
1828
+ const helper = React$1.useMemo(() => vaultHelpers.createThumbnailHelper(vault, { imageServiceLoader: loader }), [vault, loader]);
1829
+ const [thumbnail, setThumbnail] = React$1.useState();
1167
1830
  const manifest = useManifest(manifestId ? { id: manifestId } : void 0);
1168
1831
  const canvas = useCanvas(canvasId ? { id: canvasId } : void 0);
1169
1832
  const subject = canvas ? canvas : manifest;
1170
- const didUnmount = React.useRef(false);
1171
- React.useEffect(() => {
1833
+ const didUnmount = React$1.useRef(false);
1834
+ React$1.useEffect(() => {
1172
1835
  return () => {
1173
1836
  didUnmount.current = true;
1174
1837
  };
@@ -1187,8 +1850,8 @@ function useThumbnail(request, dereference, { canvasId, manifestId } = {}) {
1187
1850
  }
1188
1851
 
1189
1852
  var __defProp$1 = Object.defineProperty;
1190
- var __defProps$1 = Object.defineProperties;
1191
- var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
1853
+ var __defProps = Object.defineProperties;
1854
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
1192
1855
  var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
1193
1856
  var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
1194
1857
  var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
@@ -1204,198 +1867,266 @@ var __spreadValues$1 = (a, b) => {
1204
1867
  }
1205
1868
  return a;
1206
1869
  };
1207
- var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
1208
- function useLoadImageService() {
1209
- const loader = useImageServiceLoader();
1210
- const [imageServiceStatus, setImageServiceStatus] = React.useState({});
1211
- const didUnmount = React.useRef(false);
1212
- React.useEffect(() => {
1213
- return () => {
1214
- didUnmount.current = true;
1215
- };
1216
- }, []);
1217
- const loadImageService = React.useCallback((imageService, { height, width }) => {
1218
- if (imageService) {
1219
- const imageServiceId = imageService.id || imageService["@id"];
1220
- const syncLoaded = loader.loadServiceSync({
1221
- id: imageServiceId,
1222
- width: imageService.width || width,
1223
- height: imageService.height || height
1870
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
1871
+ function getDefaultState(duration) {
1872
+ return { isMuted: false, playRequested: false, isPlaying: false, isFinished: false, volume: 100, duration };
1873
+ }
1874
+ function reducer(state, action) {
1875
+ switch (action.type) {
1876
+ case "FINISHED":
1877
+ return __spreadProps(__spreadValues$1({}, state), { isFinished: true, isPlaying: false, playRequested: false });
1878
+ case "PLAY_PAUSE":
1879
+ return __spreadProps(__spreadValues$1({}, state), { isFinished: false, isPlaying: !state.isPlaying });
1880
+ case "PLAY_REQUESTED":
1881
+ return __spreadProps(__spreadValues$1({}, state), { isFinished: false, playRequested: true });
1882
+ case "PAUSE":
1883
+ return __spreadProps(__spreadValues$1({}, state), { isPlaying: false });
1884
+ case "PLAY":
1885
+ return __spreadProps(__spreadValues$1({}, state), { isFinished: false, playRequested: false, isPlaying: true });
1886
+ case "MUTE":
1887
+ return __spreadProps(__spreadValues$1({}, state), { isMuted: true });
1888
+ case "SET_VOLUME":
1889
+ return __spreadProps(__spreadValues$1({}, state), { volume: action.volume, isMuted: action.volume === 0 });
1890
+ case "TOGGLE_MUTE":
1891
+ return __spreadProps(__spreadValues$1({}, state), { isMuted: !state.isMuted });
1892
+ case "UNMUTE":
1893
+ return __spreadProps(__spreadValues$1({}, state), { isMuted: false });
1894
+ }
1895
+ return state;
1896
+ }
1897
+ function formatTime(time) {
1898
+ const seconds = Math.round(time);
1899
+ return `${Math.floor(seconds / 60)}:${`${seconds % 60}`.padStart(2, "0")}`;
1900
+ }
1901
+ function useSimpleMediaPlayer(props) {
1902
+ const [state, dispatch] = React$1.useReducer(reducer, getDefaultState(props.duration));
1903
+ const media = React$1.useRef(null);
1904
+ const currentTime = React$1.useRef(null);
1905
+ const progress = React$1.useRef(null);
1906
+ const _isMuted = React$1.useRef(false);
1907
+ const _updateCurrentTime = React$1.useCallback(() => {
1908
+ if (currentTime.current && media.current) {
1909
+ currentTime.current.innerHTML = formatTime(media.current.currentTime);
1910
+ if (progress.current) {
1911
+ progress.current.style.width = `${media.current.currentTime / props.duration * 100}%`;
1912
+ }
1913
+ if (_isMuted.current !== media.current.muted) {
1914
+ _isMuted.current = media.current.muted;
1915
+ dispatch(media.current.muted ? { type: "MUTE" } : { type: "UNMUTE" });
1916
+ }
1917
+ }
1918
+ }, [props.duration]);
1919
+ const play = React$1.useCallback(() => {
1920
+ if (media.current) {
1921
+ dispatch({ type: "PLAY_REQUESTED" });
1922
+ media.current.play().then(() => {
1923
+ dispatch({ type: "PLAY" });
1224
1924
  });
1225
- if (syncLoaded) {
1226
- imageService = syncLoaded;
1227
- } else if (!imageServiceStatus[imageServiceId]) {
1228
- if (!didUnmount.current) {
1229
- setImageServiceStatus((r) => {
1230
- return __spreadProps$1(__spreadValues$1({}, r), {
1231
- [imageServiceId]: "loading"
1232
- });
1233
- });
1234
- }
1235
- loader.loadService({
1236
- id: imageServiceId,
1237
- width: imageService.width || width,
1238
- height: imageService.height || height
1239
- }).then(() => {
1240
- if (!didUnmount.current) {
1241
- setImageServiceStatus((r) => {
1242
- return __spreadProps$1(__spreadValues$1({}, r), {
1243
- [imageServiceId]: "done"
1244
- });
1245
- });
1246
- }
1247
- });
1925
+ _updateCurrentTime();
1926
+ }
1927
+ }, [_updateCurrentTime]);
1928
+ const playPause = React$1.useCallback(() => {
1929
+ if (media.current) {
1930
+ if (media.current.duration > 0 && media.current.paused) {
1931
+ play();
1932
+ } else {
1933
+ pause();
1248
1934
  }
1249
1935
  }
1250
- return imageService;
1251
- }, [loader, imageServiceStatus]);
1252
- return [loadImageService, imageServiceStatus];
1936
+ }, [_updateCurrentTime]);
1937
+ const pause = React$1.useCallback(() => {
1938
+ if (media.current) {
1939
+ media.current.pause();
1940
+ dispatch({ type: "PAUSE" });
1941
+ _updateCurrentTime();
1942
+ }
1943
+ }, [_updateCurrentTime]);
1944
+ const toggleMute = React$1.useCallback(() => {
1945
+ if (media.current) {
1946
+ media.current.muted = !media.current.muted;
1947
+ dispatch(media.current.muted ? { type: "MUTE" } : { type: "UNMUTE" });
1948
+ }
1949
+ }, []);
1950
+ const mute = React$1.useCallback(() => {
1951
+ if (media.current) {
1952
+ media.current.muted = true;
1953
+ dispatch({ type: "MUTE" });
1954
+ }
1955
+ }, []);
1956
+ const unmute = React$1.useCallback(() => {
1957
+ if (media.current) {
1958
+ media.current.muted = false;
1959
+ dispatch({ type: "UNMUTE" });
1960
+ }
1961
+ }, []);
1962
+ const setVolume = React$1.useCallback((newVolume) => {
1963
+ if (media.current) {
1964
+ media.current.muted = false;
1965
+ media.current.volume = newVolume / 100;
1966
+ dispatch({ type: "SET_VOLUME", volume: newVolume });
1967
+ }
1968
+ }, []);
1969
+ const setDurationPercent = React$1.useCallback((percent) => {
1970
+ if (media.current) {
1971
+ media.current.currentTime = Math.max(0, Math.min(percent * props.duration, props.duration));
1972
+ _updateCurrentTime();
1973
+ }
1974
+ }, []);
1975
+ const setTime = React$1.useCallback((time) => {
1976
+ if (media.current) {
1977
+ media.current.currentTime = Math.max(0, Math.min(time, props.duration));
1978
+ _updateCurrentTime();
1979
+ }
1980
+ }, []);
1981
+ React$1.useEffect(() => {
1982
+ const interval = setInterval(() => {
1983
+ _updateCurrentTime();
1984
+ }, 350);
1985
+ return () => clearInterval(interval);
1986
+ }, [_updateCurrentTime, props.duration]);
1987
+ React$1.useEffect(() => {
1988
+ const ended = () => {
1989
+ dispatch({ type: "FINISHED" });
1990
+ };
1991
+ const _media = media.current;
1992
+ _media == null ? void 0 : _media.addEventListener("ended", ended);
1993
+ return () => _media == null ? void 0 : _media.removeEventListener("ended", ended);
1994
+ }, []);
1995
+ return [
1996
+ { element: media, currentTime, progress },
1997
+ state,
1998
+ {
1999
+ play,
2000
+ pause,
2001
+ playPause,
2002
+ mute,
2003
+ unmute,
2004
+ toggleMute,
2005
+ setVolume,
2006
+ setDurationPercent,
2007
+ setTime
2008
+ }
2009
+ ];
1253
2010
  }
1254
2011
 
1255
- const IMPORT_ENTITIES = "@iiif/IMPORT_ENTITIES";
1256
- const MODIFY_ENTITY_FIELD = "@iiif/MODIFY_ENTITY_FIELD";
1257
- const REORDER_ENTITY_FIELD = "@iiif/REORDER_ENTITY_FIELD";
1258
- const ADD_REFERENCE = "@iiif/ADD_REFERENCE";
1259
- const REMOVE_REFERENCE = "@iiif/REMOVE_REFERENCE";
1260
- const importEntities = typesafeActions.createAction(IMPORT_ENTITIES)();
1261
- const modifyEntityField = typesafeActions.createAction(MODIFY_ENTITY_FIELD)();
1262
- const reorderEntityField = typesafeActions.createAction(REORDER_ENTITY_FIELD)();
1263
- const addReference = typesafeActions.createAction(ADD_REFERENCE)();
1264
- const removeReference = typesafeActions.createAction(REMOVE_REFERENCE)();
1265
- const entityActions = { importEntities, modifyEntityField, reorderEntityField, addReference, removeReference };
2012
+ const MediaReactContextState = React$1.createContext(null);
2013
+ const MediaReactContextActions = React$1.createContext(null);
2014
+ const MediaReactContextElements = React$1.createContext(null);
2015
+ function useMediaState() {
2016
+ const ctx = React$1.useContext(MediaReactContextState);
2017
+ if (!ctx) {
2018
+ throw new Error("Ctx not found");
2019
+ }
2020
+ return ctx;
2021
+ }
2022
+ function useMediaActions() {
2023
+ const ctx = React$1.useContext(MediaReactContextActions);
2024
+ if (!ctx) {
2025
+ throw new Error("Ctx not found");
2026
+ }
2027
+ return ctx;
2028
+ }
2029
+ function useMediaElements() {
2030
+ const ctx = React$1.useContext(MediaReactContextElements);
2031
+ if (!ctx) {
2032
+ throw new Error("Ctx not found");
2033
+ }
2034
+ return ctx;
2035
+ }
2036
+ function MediaPlayerProvider({
2037
+ actions,
2038
+ state,
2039
+ children,
2040
+ currentTime,
2041
+ progress,
2042
+ element
2043
+ }) {
2044
+ return /* @__PURE__ */ React.createElement(MediaReactContextElements.Provider, {
2045
+ value: { currentTime, progress, element }
2046
+ }, /* @__PURE__ */ React.createElement(MediaReactContextActions.Provider, {
2047
+ value: actions
2048
+ }, /* @__PURE__ */ React.createElement(MediaReactContextState.Provider, {
2049
+ value: state
2050
+ }, children)));
2051
+ }
1266
2052
 
1267
- const ADD_MAPPING = "@iiif/ADD_MAPPING";
1268
- const ADD_MAPPINGS = "@iiif/ADD_MAPPINGS";
1269
- typesafeActions.createAction(ADD_MAPPING)();
1270
- typesafeActions.createAction(ADD_MAPPINGS)();
1271
-
1272
- const SET_META_VALUE = "@iiif/SET_META_VALUE";
1273
- const SET_META_VALUE_DYNAMIC = "@iiif/SET_META_VALUE_DYNAMIC";
1274
- const UNSET_META_VALUE = "@iiif/UNSET_META_VALUE";
1275
- typesafeActions.createAction(SET_META_VALUE)();
1276
- typesafeActions.createAction(SET_META_VALUE_DYNAMIC)();
1277
- typesafeActions.createAction(UNSET_META_VALUE)();
1278
- const REQUEST_RESOURCE = "@iiif/REQUEST_RESOURCE";
1279
- const REQUEST_ERROR = "@iiif/REQUEST_ERROR";
1280
- const REQUEST_MISMATCH = "@iiif/REQUEST_MISMATCH";
1281
- const REQUEST_COMPLETE = "@iiif/REQUEST_COMPLETE";
1282
- const REQUEST_OFFLINE_RESOURCE = "@iiif/REQUEST_OFFLINE_RESOURCE";
1283
- typesafeActions.createAction(REQUEST_RESOURCE)();
1284
- typesafeActions.createAction(REQUEST_ERROR)();
1285
- typesafeActions.createAction(REQUEST_MISMATCH)();
1286
- typesafeActions.createAction(REQUEST_COMPLETE)();
1287
- typesafeActions.createAction(REQUEST_OFFLINE_RESOURCE)();
1288
-
1289
- const BATCH_ACTIONS = "@iiif/BATCH";
1290
- const BATCH_IMPORT = "@iiif/BATCH_IMPORT";
1291
- typesafeActions.createAction(BATCH_ACTIONS)();
1292
- typesafeActions.createAction(BATCH_IMPORT)();
2053
+ function Audio({ media, children }) {
2054
+ const [{ element, currentTime, progress }, state, actions] = useSimpleMediaPlayer({ duration: media.duration });
2055
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(CanvasPortal, null, /* @__PURE__ */ React.createElement(MediaPlayerProvider, {
2056
+ state,
2057
+ actions,
2058
+ currentTime,
2059
+ progress,
2060
+ element
2061
+ }, /* @__PURE__ */ React.createElement("audio", {
2062
+ ref: element,
2063
+ src: media.url
2064
+ }), children)));
2065
+ }
1293
2066
 
1294
- function isVaultActivated(obj) {
1295
- return typeof obj !== "string" && obj && obj.bindToVault;
2067
+ function Video({ media, children }) {
2068
+ const [{ element, currentTime, progress }, state, actions] = useSimpleMediaPlayer({ duration: media.duration });
2069
+ const Component = "div";
2070
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(CanvasPortal, {
2071
+ overlay: true
2072
+ }, /* @__PURE__ */ React.createElement("style", null, `
2073
+ .video-container {
2074
+ position: absolute;
2075
+ top: 0;
2076
+ bottom: 0;
2077
+ left: 0;
2078
+ right: 0;
2079
+ background: #000;
2080
+ z-index: 13;
2081
+ display: flex;
2082
+ justify-content: center;
2083
+ pointer-events: visible;
2084
+ }
2085
+ `), /* @__PURE__ */ React.createElement(Component, {
2086
+ className: "video-container",
2087
+ part: "video-container",
2088
+ onClick: () => actions.playPause()
2089
+ }, /* @__PURE__ */ React.createElement("video", {
2090
+ ref: element,
2091
+ src: media.url,
2092
+ style: { width: "100%", objectFit: "contain" }
2093
+ }))), /* @__PURE__ */ React.createElement(CanvasPortal, null, /* @__PURE__ */ React.createElement(MediaPlayerProvider, {
2094
+ state,
2095
+ actions,
2096
+ currentTime,
2097
+ progress,
2098
+ element
2099
+ }, children)));
1296
2100
  }
1297
- function useVirtualAnnotationPage() {
1298
- const vault = useVault();
1299
- const sources = React.useRef([]);
1300
- const dispatch = useDispatch();
1301
- const virtualId = React.useMemo(() => {
1302
- return `vault://annotation-page/${new Date().getTime()}/${Math.round(Math.random() * 1e9).toString(16)}`;
1303
- }, []);
1304
- React.useLayoutEffect(() => {
1305
- const page = {
1306
- id: virtualId,
1307
- type: "AnnotationPage",
1308
- behavior: [],
1309
- motivation: null,
1310
- label: null,
1311
- thumbnail: [],
1312
- summary: null,
1313
- requiredStatement: null,
1314
- metadata: [],
1315
- rights: null,
1316
- provider: [],
1317
- items: [],
1318
- seeAlso: [],
1319
- homepage: [],
1320
- logo: [],
1321
- rendering: [],
1322
- service: []
1323
- };
1324
- dispatch(entityActions.importEntities({
1325
- entities: {
1326
- AnnotationPage: {
1327
- [page.id]: page
1328
- }
1329
- }
1330
- }));
1331
- }, [virtualId]);
1332
- const fullPage = useVaultSelector((state) => virtualId ? state.iiif.entities.AnnotationPage[virtualId] : null, [virtualId]);
1333
- const addAnnotation = React.useCallback((id, atIndex) => {
1334
- if (virtualId) {
1335
- if (isVaultActivated(id)) {
1336
- const display = id;
1337
- if (!display.__vault) {
1338
- display.bindToVault(vault);
1339
- }
1340
- id = typeof display.source === "string" ? display.source : display.source.id;
1341
- sources.current[id] = display;
1342
- } else if (typeof id !== "string") {
1343
- id = id.id;
1344
- }
1345
- const full = vault.get({ id: virtualId, type: "AnnotationPage" });
1346
- const annotation = vault.get({ id, type: "Annotation" });
1347
- if (full && annotation) {
1348
- if (!full.items.find((r) => r.id === annotation.id)) {
1349
- dispatch(entityActions.addReference({
1350
- id: virtualId,
1351
- type: "AnnotationPage",
1352
- key: "items",
1353
- reference: {
1354
- id,
1355
- type: "Annotation"
1356
- },
1357
- index: atIndex
1358
- }));
1359
- }
1360
- }
1361
- }
1362
- }, [virtualId]);
1363
- const removeAnnotation = React.useCallback((id) => {
1364
- if (virtualId) {
1365
- if (isVaultActivated(id)) {
1366
- id = typeof id.source === "string" ? id.source : id.source.id;
1367
- } else if (typeof id !== "string") {
1368
- id = id.id;
1369
- }
1370
- if (sources.current[id]) {
1371
- sources.current[id].beforeRemove();
1372
- }
1373
- const full = vault.get({ id: virtualId, type: "AnnotationPage" });
1374
- if (full) {
1375
- dispatch(entityActions.removeReference({
1376
- id: virtualId,
1377
- type: "AnnotationPage",
1378
- key: "items",
1379
- reference: {
1380
- id,
1381
- type: "Annotation"
1382
- }
1383
- }));
1384
- }
1385
- }
1386
- }, [virtualId]);
1387
- return [
1388
- fullPage,
1389
- {
1390
- addAnnotation,
1391
- removeAnnotation
1392
- }
1393
- ];
2101
+
2102
+ function Model({ model }) {
2103
+ return /* @__PURE__ */ React.createElement(CanvasPortal, {
2104
+ overlay: true
2105
+ }, /* @__PURE__ */ React.createElement("style", null, `
2106
+ .model-container {
2107
+ position: absolute;
2108
+ top: 0;
2109
+ bottom: 0;
2110
+ left: 0;
2111
+ right: 0;
2112
+ background: #000;
2113
+ z-index: 13;
2114
+ display: flex;
2115
+ justify-content: center;
2116
+ pointer-events: visible;
2117
+ }
2118
+ `), /* @__PURE__ */ React.createElement("div", {
2119
+ className: "model-container"
2120
+ }, /* @__PURE__ */ React.createElement("model-viewer", {
2121
+ "interaction-prompt": "none",
2122
+ style: { width: "100%", height: "100%" },
2123
+ "camera-controls": "",
2124
+ "ar-status": "not-presenting",
2125
+ src: model.id
2126
+ })));
1394
2127
  }
1395
2128
 
1396
2129
  var __defProp = Object.defineProperty;
1397
- var __defProps = Object.defineProperties;
1398
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
1399
2130
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
1400
2131
  var __hasOwnProp = Object.prototype.hasOwnProperty;
1401
2132
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -1411,68 +2142,331 @@ var __spreadValues = (a, b) => {
1411
2142
  }
1412
2143
  return a;
1413
2144
  };
1414
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
1415
- function useRenderingStrategy(options) {
1416
- const manifest = useManifest();
2145
+ function RenderCanvas({
2146
+ x,
2147
+ y,
2148
+ onChoiceChange,
2149
+ registerActions,
2150
+ defaultChoices,
2151
+ isStatic,
2152
+ renderViewerControls,
2153
+ renderMediaControls,
2154
+ strategies,
2155
+ children
2156
+ }) {
1417
2157
  const canvas = useCanvas();
2158
+ const elementProps = useResourceEvents(canvas, ["deep-zoom"]);
2159
+ const [virtualPage] = useVirtualAnnotationPageContext();
1418
2160
  const vault = useVault();
1419
- const [loadImageService, imageServiceStatus] = useLoadImageService();
1420
- const { enabledPageIds } = useAnnotationPageManager((options == null ? void 0 : options.annotationPageManagerId) || (manifest == null ? void 0 : manifest.id) || (canvas == null ? void 0 : canvas.id), {
1421
- all: false
2161
+ const helper = React$1.useMemo(() => vaultHelpers.createStylesHelper(vault), [vault]);
2162
+ const [strategy, actions] = useRenderingStrategy({
2163
+ strategies: strategies || ["images"],
2164
+ defaultChoices: defaultChoices == null ? void 0 : defaultChoices.map(({ id }) => id)
1422
2165
  });
1423
- const enabledPages = useResources(enabledPageIds, "AnnotationPage");
1424
- const supports = (options == null ? void 0 : options.strategies) || ["images", "media", "complex-timeline"];
1425
- const [paintables, actions] = usePaintables(options, [imageServiceStatus]);
1426
- const strategy = React.useMemo(() => {
1427
- if (!canvas || paintables.types.length === 0) {
1428
- return unknownResponse;
2166
+ const choice = strategy.type === "images" ? strategy.choice : void 0;
2167
+ React$1.useEffect(() => {
2168
+ if (registerActions) {
2169
+ registerActions(actions);
1429
2170
  }
1430
- if (paintables.types.length !== 1) {
1431
- if (supports.indexOf("complex-timeline") === -1) {
1432
- return unsupportedStrategy("Complex timeline not supported");
2171
+ }, [strategy.annotations]);
2172
+ React$1.useEffect(() => {
2173
+ if (defaultChoices) {
2174
+ for (const choice2 of defaultChoices) {
2175
+ if (typeof choice2.opacity !== "undefined") {
2176
+ helper.applyStyles({ id: choice2.id }, "atlas", {
2177
+ opacity: choice2.opacity
2178
+ });
2179
+ }
1433
2180
  }
1434
- return unsupportedStrategy("ComplexTimelineStrategy not yet supported");
1435
2181
  }
1436
- const mainType = paintables.types[0];
1437
- if (mainType === "image") {
1438
- if (supports.indexOf("images") === -1) {
1439
- return unsupportedStrategy("Image not supported");
1440
- }
1441
- return getImageStrategy(canvas, paintables, loadImageService);
2182
+ }, [defaultChoices]);
2183
+ React$1.useLayoutEffect(() => {
2184
+ if (onChoiceChange) {
2185
+ onChoiceChange(choice);
1442
2186
  }
1443
- if (mainType === "audio") {
1444
- if (supports.indexOf("media") === -1) {
1445
- return unsupportedStrategy("Media not supported");
2187
+ }, [choice]);
2188
+ const thumbnail = useThumbnail({ maxWidth: 256, maxHeight: 256 });
2189
+ if (!canvas) {
2190
+ return null;
2191
+ }
2192
+ const accompanyingCanvas = canvas.accompanyingCanvas;
2193
+ const thumbnailFallbackImage = thumbnail && thumbnail.type === "fixed" ? /* @__PURE__ */ React__default["default"].createElement("world-object", {
2194
+ height: canvas.height,
2195
+ width: canvas.width,
2196
+ x,
2197
+ y
2198
+ }, /* @__PURE__ */ React__default["default"].createElement("world-image", {
2199
+ uri: thumbnail.id,
2200
+ target: { x: 0, y: 0, width: canvas.width, height: canvas.height },
2201
+ display: thumbnail.width && thumbnail.height ? {
2202
+ width: thumbnail.width,
2203
+ height: thumbnail.height
2204
+ } : void 0
2205
+ })) : null;
2206
+ if (strategy.type === "unknown") {
2207
+ if (thumbnailFallbackImage) {
2208
+ return thumbnailFallbackImage;
2209
+ }
2210
+ throw new Error(strategy.reason || "Unknown image strategy");
2211
+ }
2212
+ const annotations = /* @__PURE__ */ React__default["default"].createElement(React$1.Fragment, null, virtualPage ? /* @__PURE__ */ React__default["default"].createElement(RenderAnnotationPage, {
2213
+ page: virtualPage
2214
+ }) : null, strategy.annotations && strategy.annotations.pages ? strategy.annotations.pages.map((page) => {
2215
+ return /* @__PURE__ */ React__default["default"].createElement(RenderAnnotationPage, {
2216
+ key: page.id,
2217
+ page
2218
+ });
2219
+ }) : null, children);
2220
+ return /* @__PURE__ */ React__default["default"].createElement(React__default["default"].Fragment, null, /* @__PURE__ */ React__default["default"].createElement("world-object", __spreadValues({
2221
+ key: strategy.type,
2222
+ height: canvas.height,
2223
+ width: canvas.width,
2224
+ x,
2225
+ y
2226
+ }, elementProps), strategy.type === "images" ? /* @__PURE__ */ React__default["default"].createElement(React__default["default"].Fragment, null, strategy.images.map((image, idx) => {
2227
+ return /* @__PURE__ */ React__default["default"].createElement(RenderImage, {
2228
+ isStatic,
2229
+ key: image.id,
2230
+ image,
2231
+ id: image.id,
2232
+ thumbnail: idx === 0 ? thumbnail : void 0,
2233
+ annotations
2234
+ });
2235
+ }), renderViewerControls ? /* @__PURE__ */ React__default["default"].createElement(CanvasPortal, {
2236
+ overlay: true
2237
+ }, renderViewerControls(strategy)) : null) : null, strategy.type === "3d-model" ? /* @__PURE__ */ React__default["default"].createElement(Model, {
2238
+ model: strategy.model
2239
+ }) : null, strategy.type === "media" ? /* @__PURE__ */ React__default["default"].createElement(React__default["default"].Fragment, null, strategy.media.type === "Sound" ? /* @__PURE__ */ React__default["default"].createElement(Audio, {
2240
+ media: strategy.media
2241
+ }, thumbnailFallbackImage, renderMediaControls ? renderMediaControls(strategy) : null) : strategy.media.type === "Video" ? /* @__PURE__ */ React__default["default"].createElement(Video, {
2242
+ media: strategy.media
2243
+ }, thumbnailFallbackImage, renderMediaControls ? renderMediaControls(strategy) : null) : null) : null), strategy.type === "media" && strategy.media.type === "Sound" && accompanyingCanvas ? /* @__PURE__ */ React__default["default"].createElement(CanvasContext, {
2244
+ canvas: accompanyingCanvas.id
2245
+ }, /* @__PURE__ */ React__default["default"].createElement(RenderCanvas, null)) : null);
2246
+ }
2247
+
2248
+ const CanvasPanel = {
2249
+ RenderImage,
2250
+ RenderCanvas,
2251
+ RenderAnnotationPage,
2252
+ RenderAnnotation,
2253
+ Viewer
2254
+ };
2255
+
2256
+ function AnnotationContext({ annotation, children }) {
2257
+ return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
2258
+ value: { annotation }
2259
+ }, children);
2260
+ }
2261
+
2262
+ function CollectionContext({ collection, children }) {
2263
+ return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
2264
+ value: { collection }
2265
+ }, children);
2266
+ }
2267
+
2268
+ function RangeContext({ range, children }) {
2269
+ return /* @__PURE__ */ React__default["default"].createElement(ResourceProvider, {
2270
+ value: { range }
2271
+ }, children);
2272
+ }
2273
+
2274
+ function useAnnotationsAtTime(time, options = {}) {
2275
+ const allAnnotations = usePaintingAnnotations(options);
2276
+ return allAnnotations;
2277
+ }
2278
+
2279
+ function useCanvasClock(canvasId, autoplay = false) {
2280
+ }
2281
+
2282
+ function useCollection(options, deps = []) {
2283
+ const { id, selector } = options;
2284
+ const ctx = useResourceContext();
2285
+ const collectionId = id ? id : ctx.collection;
2286
+ const collection = useVaultSelector((s) => collectionId ? s.iiif.entities.Collection[collectionId] : void 0, [collectionId]);
2287
+ return React$1.useMemo(() => {
2288
+ if (!collection) {
2289
+ return void 0;
2290
+ }
2291
+ if (selector) {
2292
+ return selector(collection);
2293
+ }
2294
+ return collection;
2295
+ }, [collection, selector, ...deps]);
2296
+ }
2297
+
2298
+ function useEventListener(resource, name, listener, scope, deps = []) {
2299
+ const vault = useVault();
2300
+ const helper = React$1.useMemo(() => vaultHelpers.createEventsHelper(vault), [vault]);
2301
+ React$1.useEffect(() => {
2302
+ const currentResource = resource;
2303
+ if (!currentResource) {
2304
+ return () => {
2305
+ };
2306
+ }
2307
+ helper.addEventListener(currentResource, name, listener, scope);
2308
+ return () => {
2309
+ helper.removeEventListener(currentResource, name, listener);
2310
+ };
2311
+ }, [helper, resource, name, ...deps]);
2312
+ }
2313
+
2314
+ function useExistingVault() {
2315
+ const oldContext = React$1.useContext(ReactVaultContext);
2316
+ return oldContext && oldContext.vault ? oldContext.vault : vault.globalVault();
2317
+ }
2318
+
2319
+ function useExternalCollection(idOrRef, options) {
2320
+ const { id, isLoaded, error, resource, requestId, cached } = useExternalResource(idOrRef, options);
2321
+ return { id, isLoaded, error, manifest: resource, requestId, cached };
2322
+ }
2323
+
2324
+ var __async = (__this, __arguments, generator) => {
2325
+ return new Promise((resolve, reject) => {
2326
+ var fulfilled = (value) => {
2327
+ try {
2328
+ step(generator.next(value));
2329
+ } catch (e) {
2330
+ reject(e);
2331
+ }
2332
+ };
2333
+ var rejected = (value) => {
2334
+ try {
2335
+ step(generator.throw(value));
2336
+ } catch (e) {
2337
+ reject(e);
1446
2338
  }
1447
- return unsupportedStrategy("Audio strategy not yet supported");
2339
+ };
2340
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
2341
+ step((generator = generator.apply(__this, __arguments)).next());
2342
+ });
2343
+ };
2344
+ function useImageService({ cacheKey } = {}) {
2345
+ const canvas = useCanvas();
2346
+ const annotations = usePaintingAnnotations();
2347
+ const vault = useVault();
2348
+ const imageService = useImageServiceLoader();
2349
+ const [_data, setData] = React$1.useState(void 0);
2350
+ const [isFetching, setIsFetching] = React$1.useState(false);
2351
+ const [status, setStatus] = React$1.useState("idle");
2352
+ const [error, setError] = React$1.useState(void 0);
2353
+ const key = canvas ? canvas.id : "undefined";
2354
+ const initialData = React$1.useMemo(() => {
2355
+ try {
2356
+ if (canvas && annotations.length) {
2357
+ const annotation = annotations[0];
2358
+ const resource = vault.get(annotation.body[0]);
2359
+ const imageServices = iiifImageApi.getImageServices(resource);
2360
+ const firstImageService = imageServices[0];
2361
+ if (!firstImageService) {
2362
+ return void 0;
2363
+ }
2364
+ return imageService.loadServiceSync({
2365
+ id: firstImageService.id || firstImageService["@id"],
2366
+ width: firstImageService.width || canvas.width,
2367
+ height: firstImageService.height || canvas.height
2368
+ }) || void 0;
2369
+ }
2370
+ } catch (e) {
2371
+ console.error(e);
1448
2372
  }
1449
- if (mainType === "video") {
1450
- if (supports.indexOf("media") === -1) {
1451
- return unsupportedStrategy("Media not supported");
2373
+ return void 0;
2374
+ }, [key, cacheKey, canvas]);
2375
+ const data = status === "success" && _data ? _data : initialData;
2376
+ React$1.useEffect(() => {
2377
+ (() => __async(this, null, function* () {
2378
+ try {
2379
+ if (canvas && annotations.length) {
2380
+ const annotation = annotations[0];
2381
+ const resource = vault.get(annotation.body[0]);
2382
+ const imageServices = iiifImageApi.getImageServices(resource);
2383
+ const firstImageService = imageServices[0];
2384
+ if (!firstImageService) {
2385
+ return;
2386
+ }
2387
+ setIsFetching(true);
2388
+ setStatus("loading");
2389
+ try {
2390
+ const loadedService = (yield imageService.loadService({
2391
+ id: firstImageService.id || firstImageService["@id"],
2392
+ width: firstImageService.width || canvas.width,
2393
+ height: firstImageService.height || canvas.height
2394
+ })) || void 0;
2395
+ setData(loadedService);
2396
+ setStatus("success");
2397
+ setIsFetching(false);
2398
+ } catch (err) {
2399
+ setStatus("error");
2400
+ setError(err);
2401
+ }
2402
+ }
2403
+ } catch (err) {
2404
+ setStatus("error");
2405
+ setError(err);
1452
2406
  }
1453
- return unsupportedStrategy("Video strategy not yet supported");
2407
+ }))();
2408
+ }, [key, cacheKey]);
2409
+ return React$1.useMemo(() => {
2410
+ return {
2411
+ data,
2412
+ isFetching,
2413
+ status,
2414
+ error
2415
+ };
2416
+ }, [data, isFetching, status, error]);
2417
+ }
2418
+
2419
+ function useImageTile() {
2420
+ const imageService = useImageService();
2421
+ return {
2422
+ isLoading: imageService.isFetching,
2423
+ tile: imageService.data ? {
2424
+ id: imageService.data.id || imageService.data["@id"],
2425
+ width: imageService.data.width,
2426
+ height: imageService.data.height,
2427
+ imageService: imageService.data,
2428
+ thumbnail: void 0
2429
+ } : null
2430
+ };
2431
+ }
2432
+
2433
+ function useRange(options = {}, deps = []) {
2434
+ const { id, selector } = options;
2435
+ const ctx = useResourceContext();
2436
+ const rangeId = id ? id : ctx.range;
2437
+ const range = useVaultSelector((s) => rangeId ? s.iiif.entities.Range[rangeId] : void 0, [rangeId]);
2438
+ return React$1.useMemo(() => {
2439
+ if (!range) {
2440
+ return void 0;
1454
2441
  }
1455
- return unknownResponse;
1456
- }, [canvas, paintables, vault, actions.makeChoice]);
1457
- return React.useMemo(() => {
1458
- if (strategy.type === "unknown") {
1459
- return [strategy, emptyActions];
2442
+ if (selector) {
2443
+ return selector(range);
1460
2444
  }
1461
- return [
1462
- __spreadProps(__spreadValues({}, strategy), {
1463
- annotations: { pages: enabledPages }
1464
- }),
1465
- actions
1466
- ];
1467
- }, [strategy, enabledPages]);
2445
+ return range;
2446
+ }, [range, selector, ...deps]);
2447
+ }
2448
+
2449
+ function useSearchService() {
2450
+ const manifest = useManifest();
2451
+ return manifest ? manifest.service.find((service) => service.profile === "SearchService1" || service.profile === "http://iiif.io/api/search/1/search") : void 0;
2452
+ }
2453
+
2454
+ function useStyleHelper() {
2455
+ const vault = useVault();
2456
+ return React$1.useMemo(() => vaultHelpers.createStylesHelper(vault), [vault]);
1468
2457
  }
1469
2458
 
1470
2459
  exports.AnnotationContext = AnnotationContext;
1471
2460
  exports.CanvasContext = CanvasContext;
2461
+ exports.CanvasPanel = CanvasPanel;
2462
+ exports.CanvasPortal = CanvasPortal;
1472
2463
  exports.CollectionContext = CollectionContext;
1473
2464
  exports.ContextBridge = ContextBridge;
1474
2465
  exports.ImageServiceLoaderContext = ImageServiceLoaderContext;
1475
2466
  exports.ManifestContext = ManifestContext;
2467
+ exports.MediaPlayerProvider = MediaPlayerProvider;
2468
+ exports.OverlayPortalContext = OverlayPortalContext;
2469
+ exports.PortalContext = PortalContext;
1476
2470
  exports.RangeContext = RangeContext;
1477
2471
  exports.ReactVaultContext = ReactVaultContext;
1478
2472
  exports.ResourceProvider = ResourceProvider;
@@ -1480,10 +2474,12 @@ exports.ResourceReactContext = ResourceReactContext;
1480
2474
  exports.SimpleViewerProvider = SimpleViewerProvider;
1481
2475
  exports.SimpleViewerReactContext = SimpleViewerReactContext;
1482
2476
  exports.VaultProvider = VaultProvider;
2477
+ exports.VirtualAnnotationProvider = VirtualAnnotationProvider;
1483
2478
  exports.VisibleCanvasReactContext = VisibleCanvasReactContext;
1484
2479
  exports.emptyActions = emptyActions;
1485
2480
  exports.expandTarget = expandTarget;
1486
2481
  exports.flattenAnnotationPageIds = flattenAnnotationPageIds;
2482
+ exports.formatTime = formatTime;
1487
2483
  exports.getImageStrategy = getImageStrategy;
1488
2484
  exports.getPaintables = getPaintables;
1489
2485
  exports.parseSelector = parseSelector;
@@ -1508,6 +2504,9 @@ exports.useImageServiceLoader = useImageServiceLoader;
1508
2504
  exports.useImageTile = useImageTile;
1509
2505
  exports.useLoadImageService = useLoadImageService;
1510
2506
  exports.useManifest = useManifest;
2507
+ exports.useMediaActions = useMediaActions;
2508
+ exports.useMediaElements = useMediaElements;
2509
+ exports.useMediaState = useMediaState;
1511
2510
  exports.usePaintables = usePaintables;
1512
2511
  exports.usePaintingAnnotations = usePaintingAnnotations;
1513
2512
  exports.useRange = useRange;
@@ -1516,6 +2515,7 @@ exports.useResourceContext = useResourceContext;
1516
2515
  exports.useResourceEvents = useResourceEvents;
1517
2516
  exports.useResources = useResources;
1518
2517
  exports.useSearchService = useSearchService;
2518
+ exports.useSimpleMediaPlayer = useSimpleMediaPlayer;
1519
2519
  exports.useSimpleViewer = useSimpleViewer;
1520
2520
  exports.useStyleHelper = useStyleHelper;
1521
2521
  exports.useStyles = useStyles;
@@ -1524,4 +2524,5 @@ exports.useVault = useVault;
1524
2524
  exports.useVaultEffect = useVaultEffect;
1525
2525
  exports.useVaultSelector = useVaultSelector;
1526
2526
  exports.useVirtualAnnotationPage = useVirtualAnnotationPage;
2527
+ exports.useVirtualAnnotationPageContext = useVirtualAnnotationPageContext;
1527
2528
  exports.useVisibleCanvases = useVisibleCanvases;