@vitessce/vit-s 4.0.7 → 4.0.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.
Files changed (42) hide show
  1. package/dist/index.js +560 -65
  2. package/dist-tsc/VitS.d.ts +3 -0
  3. package/dist-tsc/VitS.d.ts.map +1 -1
  4. package/dist-tsc/VitS.js +4 -2
  5. package/dist-tsc/VitS.js.map +1 -1
  6. package/dist-tsc/VitessceGrid.d.ts.map +1 -1
  7. package/dist-tsc/VitessceGrid.js +2 -2
  8. package/dist-tsc/VitessceGrid.js.map +1 -1
  9. package/dist-tsc/data-hooks.d.ts +13 -0
  10. package/dist-tsc/data-hooks.d.ts.map +1 -1
  11. package/dist-tsc/data-hooks.js +15 -0
  12. package/dist-tsc/data-hooks.js.map +1 -1
  13. package/dist-tsc/hooks.d.ts +6 -1
  14. package/dist-tsc/hooks.d.ts.map +1 -1
  15. package/dist-tsc/hooks.js +52 -22
  16. package/dist-tsc/hooks.js.map +1 -1
  17. package/dist-tsc/hooks.test.js +100 -3
  18. package/dist-tsc/hooks.test.js.map +1 -1
  19. package/dist-tsc/index.d.ts +2 -2
  20. package/dist-tsc/index.js +2 -2
  21. package/dist-tsc/index.js.map +1 -1
  22. package/dist-tsc/state/annotation-frames.test.d.ts +2 -0
  23. package/dist-tsc/state/annotation-frames.test.d.ts.map +1 -0
  24. package/dist-tsc/state/annotation-frames.test.js +171 -0
  25. package/dist-tsc/state/annotation-frames.test.js.map +1 -0
  26. package/dist-tsc/state/hooks.d.ts +101 -4
  27. package/dist-tsc/state/hooks.d.ts.map +1 -1
  28. package/dist-tsc/state/hooks.js +344 -13
  29. package/dist-tsc/state/hooks.js.map +1 -1
  30. package/dist-tsc/vitessce-grid-layout/VitessceGridLayout.d.ts.map +1 -1
  31. package/dist-tsc/vitessce-grid-layout/VitessceGridLayout.js +4 -4
  32. package/dist-tsc/vitessce-grid-layout/VitessceGridLayout.js.map +1 -1
  33. package/package.json +13 -13
  34. package/src/VitS.js +4 -0
  35. package/src/VitessceGrid.js +2 -0
  36. package/src/data-hooks.js +23 -0
  37. package/src/hooks.js +55 -23
  38. package/src/hooks.test.jsx +114 -3
  39. package/src/index.js +4 -0
  40. package/src/state/annotation-frames.test.js +185 -0
  41. package/src/state/hooks.js +395 -9
  42. package/src/vitessce-grid-layout/VitessceGridLayout.js +4 -2
@@ -1,14 +1,14 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  /* eslint-disable max-len */
3
3
  /* eslint-disable react-refresh/only-export-components */
4
- import React, { useCallback, useRef, useMemo, createContext, useContext, } from 'react';
4
+ import React, { useCallback, useEffect, useRef, useMemo, createContext, useContext, } from 'react';
5
5
  import { create, useStore } from 'zustand';
6
6
  import { useShallow } from 'zustand/shallow';
7
7
  import { subscribeWithSelector } from 'zustand/middleware';
8
8
  import { isMatch, cloneDeep } from 'lodash-es';
9
9
  import { CoordinationType } from '@vitessce/constants-internal';
10
- import { capitalize } from '@vitessce/utils';
11
- import { getCoordinationSpaceAndScopes } from '@vitessce/config';
10
+ import { capitalize, getAnnotationFrameCoordinationValues } from '@vitessce/utils';
11
+ import { getCoordinationSpaceAndScopes, CoordinationLevel as CL } from '@vitessce/config';
12
12
  import { removeImageChannelInMetaCoordinationScopesHelper, addImageChannelInMetaCoordinationScopesHelper, } from './spatial-reducers.js';
13
13
  // References for Zustand v3:
14
14
  // - https://github.com/pmndrs/zustand#react-context
@@ -183,6 +183,163 @@ export function getParameterScopeBy(parameter, byType, typeScope, coordinationSc
183
183
  // console.error(`coordination scope for ${parameter} was not found.`);
184
184
  return parameterScopeGlobal;
185
185
  }
186
+ /**
187
+ * Determine whether a view defines a value for a coordination type directly,
188
+ * in which case that value takes precedence over any coordination scope
189
+ * (from coordinationScopes or metaCoordinationScopes) for the same type.
190
+ * @param {object|undefined} coordinationValues The view.coordinationValues object, if any.
191
+ * @param {string} parameter A coordination type.
192
+ * @returns {boolean} True if there is a direct value for this coordination type.
193
+ */
194
+ export function hasCoordinationValue(coordinationValues, parameter) {
195
+ return coordinationValues !== undefined
196
+ && parameter in coordinationValues;
197
+ }
198
+ // The sentinel used to represent `CL(...)` (multi-level coordination)
199
+ // within JSON, such as within annotation frame coordination values.
200
+ // Reference: https://github.com/keller-mark/use-coordination/pull/115
201
+ export const COORDINATION_LEVEL_SENTINEL = '$CL';
202
+ function isPlainObject(value) {
203
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
204
+ }
205
+ function isMultiLevelValue(value) {
206
+ return isPlainObject(value) && Object.hasOwn(value, COORDINATION_LEVEL_SENTINEL);
207
+ }
208
+ function toArray(value) {
209
+ if (value === undefined || value === null)
210
+ return [];
211
+ return Array.isArray(value) ? value : [value];
212
+ }
213
+ /**
214
+ * Revive the `{ "$CL": ... }` sentinels within a JSON value
215
+ * into CoordinationLevel instances.
216
+ * @param {*} value The JSON value.
217
+ * @returns {*} The revived value.
218
+ */
219
+ export function reviveCoordinationLevels(value) {
220
+ if (Array.isArray(value)) {
221
+ return value.map(reviveCoordinationLevels);
222
+ }
223
+ if (isMultiLevelValue(value)) {
224
+ return CL(reviveCoordinationLevels(value[COORDINATION_LEVEL_SENTINEL]));
225
+ }
226
+ if (isPlainObject(value)) {
227
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => ([k, reviveCoordinationLevels(v)])));
228
+ }
229
+ return value;
230
+ }
231
+ /**
232
+ * Merge the coordination values that an annotation frame defines for a view
233
+ * into the view config. The frame values take precedence over the current values.
234
+ * The frame itself is treated as read-only.
235
+ * - Single-level values are written wherever the view's setter would write them:
236
+ * in-place if the view defines the value directly (view.coordinationValues),
237
+ * otherwise into the coordination scope(s) that the view is mapped to
238
+ * (after accounting for meta-coordination), so that linked views follow along.
239
+ * - Multi-level values (`{ "$CL": [...] }`) are expanded into new coordination scopes,
240
+ * and the view's existing (meta-)coordination scope mapping for those coordination types
241
+ * is re-pointed to the new scopes (so that linked views follow along).
242
+ * @param {object} viewConfig The current view config.
243
+ * @param {string} viewUid The view of interest.
244
+ * @param {object} frameCoordinationValues The coordination values for the view in the frame.
245
+ * @param {string} scopePrefix Prefix for any newly-created coordination scope names.
246
+ * @returns {object} The new view config.
247
+ */
248
+ export function applyAnnotationFrameToViewConfig(viewConfig, viewUid, frameCoordinationValues, scopePrefix) {
249
+ const { coordinationSpace = {}, layout } = viewConfig;
250
+ const viewObj = layout.find(l => l.uid === viewUid);
251
+ if (!viewObj || !frameCoordinationValues) {
252
+ return viewConfig;
253
+ }
254
+ const META = CoordinationType.META_COORDINATION_SCOPES;
255
+ const META_BY = CoordinationType.META_COORDINATION_SCOPES_BY;
256
+ const newSpace = { ...coordinationSpace };
257
+ const newViewObj = { ...viewObj };
258
+ const setViewValue = (coordinationType, value) => {
259
+ newViewObj.coordinationValues = {
260
+ ...newViewObj.coordinationValues,
261
+ [coordinationType]: value,
262
+ };
263
+ };
264
+ const entries = Object.entries(frameCoordinationValues);
265
+ const singleLevelEntries = entries.filter(([, value]) => !isMultiLevelValue(value));
266
+ const multiLevelEntries = entries.filter(([, value]) => isMultiLevelValue(value));
267
+ // Single-level values.
268
+ const resolvedScopes = getScopes(viewObj.coordinationScopes || {}, coordinationSpace[META]);
269
+ singleLevelEntries.forEach(([coordinationType, value]) => {
270
+ const scopes = toArray(resolvedScopes[coordinationType]);
271
+ if (hasCoordinationValue(viewObj.coordinationValues, coordinationType) || scopes.length === 0) {
272
+ // The view defines the value directly, or is not mapped to any scope for this type.
273
+ setViewValue(coordinationType, value);
274
+ return;
275
+ }
276
+ newSpace[coordinationType] = {
277
+ ...newSpace[coordinationType],
278
+ ...Object.fromEntries(scopes.map(scope => ([scope, value]))),
279
+ };
280
+ });
281
+ // Multi-level values.
282
+ if (multiLevelEntries.length > 0) {
283
+ const { coordinationSpace: generatedSpace, coordinationScopes: generatedScopes, } = getCoordinationSpaceAndScopes(Object.fromEntries(multiLevelEntries.map(([coordinationType, value]) => ([
284
+ coordinationType, reviveCoordinationLevels(value),
285
+ ]))), scopePrefix);
286
+ // Merge the values for the new scopes into the coordination space.
287
+ Object.entries(generatedSpace).forEach(([coordinationType, scopeValues]) => {
288
+ if ([META, META_BY, CoordinationType.DATASET].includes(coordinationType))
289
+ return;
290
+ newSpace[coordinationType] = {
291
+ ...newSpace[coordinationType],
292
+ ...scopeValues,
293
+ };
294
+ });
295
+ const newTypeScopes = generatedSpace[META]?.[generatedScopes[META]?.[0]] || {};
296
+ const newTypeScopesBy = generatedSpace[META_BY]?.[generatedScopes[META_BY]?.[0]] || {};
297
+ // Re-point the view's mapping, in the last meta-coordination scope which
298
+ // defines it (since later meta-scopes take precedence), otherwise on the view.
299
+ const viewMetaScopes = toArray(viewObj.coordinationScopes?.[META]);
300
+ Object.entries(newTypeScopes).forEach(([coordinationType, scopes]) => {
301
+ const owner = [...viewMetaScopes].reverse()
302
+ .find(m => Object.hasOwn(newSpace[META]?.[m] || {}, coordinationType));
303
+ if (owner) {
304
+ newSpace[META] = {
305
+ ...newSpace[META],
306
+ [owner]: { ...newSpace[META][owner], [coordinationType]: scopes },
307
+ };
308
+ }
309
+ else {
310
+ newViewObj.coordinationScopes = {
311
+ ...newViewObj.coordinationScopes,
312
+ [coordinationType]: scopes,
313
+ };
314
+ }
315
+ });
316
+ const viewMetaScopesBy = toArray(viewObj.coordinationScopes?.[META_BY]);
317
+ Object.entries(newTypeScopesBy).forEach(([primaryType, primaryObj]) => {
318
+ // If no meta-scope defines this (e.g., newly-nested) primary type yet,
319
+ // fall back to the last meta-scope, so that linked views also receive it.
320
+ const owner = [...viewMetaScopesBy].reverse()
321
+ .find(m => Object.hasOwn(newSpace[META_BY]?.[m] || {}, primaryType))
322
+ ?? viewMetaScopesBy.at(-1);
323
+ if (owner) {
324
+ newSpace[META_BY] = {
325
+ ...newSpace[META_BY],
326
+ [owner]: { ...newSpace[META_BY][owner], [primaryType]: primaryObj },
327
+ };
328
+ }
329
+ else {
330
+ newViewObj.coordinationScopesBy = {
331
+ ...newViewObj.coordinationScopesBy,
332
+ [primaryType]: primaryObj,
333
+ };
334
+ }
335
+ });
336
+ }
337
+ return {
338
+ ...viewConfig,
339
+ coordinationSpace: newSpace,
340
+ layout: layout.map(l => (l.uid === viewUid ? newViewObj : l)),
341
+ };
342
+ }
186
343
  /**
187
344
  * The useViewConfigStore hook is initialized via the zustand
188
345
  * create() function, which sets up both the state variables
@@ -214,8 +371,37 @@ export const createViewConfigStore = (initialLoaders, initialConfig) => create()
214
371
  mostRecentConfigSource: 'internal',
215
372
  }),
216
373
  setLoaders: loaders => set({ loaders }),
217
- setCoordinationValue: ({ parameter, value, coordinationScopes, byType, typeScope, coordinationScopesBy, }) => set((state) => {
218
- const { coordinationSpace } = state.viewConfig;
374
+ setCoordinationValue: ({ parameter, value, coordinationScopes, byType, typeScope, coordinationScopesBy,
375
+ // The viewUid is only required to write back to view-level coordinationValues.
376
+ viewUid, }) => set((state) => {
377
+ const { coordinationSpace, layout } = state.viewConfig;
378
+ // If the view defines the value directly, then update it in-place,
379
+ // as long as there is no more fine-grained ({byType}-specific)
380
+ // coordination scope which would take precedence.
381
+ const viewObj = viewUid === undefined
382
+ ? undefined
383
+ : layout.find(l => l.uid === viewUid);
384
+ const coordinationValues = viewObj?.coordinationValues;
385
+ const byTypeScope = byType
386
+ ? coordinationScopesBy?.[byType]?.[parameter]?.[typeScope]
387
+ : undefined;
388
+ if (viewUid !== undefined && hasCoordinationValue(coordinationValues, parameter) && !byTypeScope) {
389
+ return {
390
+ viewConfig: {
391
+ ...state.viewConfig,
392
+ layout: layout.map(l => (l.uid === viewUid
393
+ ? {
394
+ ...l,
395
+ coordinationValues: {
396
+ ...coordinationValues,
397
+ [parameter]: value,
398
+ },
399
+ }
400
+ : l)),
401
+ },
402
+ mostRecentConfigSource: 'internal',
403
+ };
404
+ }
219
405
  let scope;
220
406
  if (!byType) {
221
407
  scope = getParameterScope(parameter, coordinationScopes);
@@ -320,6 +506,10 @@ export const createViewConfigStore = (initialLoaders, initialConfig) => create()
320
506
  viewConfig: newViewConfig, mostRecentConfigSource: 'internal',
321
507
  };
322
508
  }),
509
+ applyAnnotationFrameCoordination: ({ viewUid, frameCoordinationValues, scopePrefix, }) => set(state => ({
510
+ viewConfig: applyAnnotationFrameToViewConfig(state.viewConfig, viewUid, frameCoordinationValues, scopePrefix),
511
+ mostRecentConfigSource: 'internal',
512
+ })),
323
513
  removeImageChannelInMetaCoordinationScopes: (coordinationScopesRaw, layerScope, channelScope) => set((state) => {
324
514
  const { coordinationSpace } = state.viewConfig;
325
515
  return {
@@ -458,12 +648,18 @@ const useGridSizeStore = create(set => ({
458
648
  * @param {string[]} parameters Array of coordination types.
459
649
  * @param {object} coordinationScopes Mapping of coordination types
460
650
  * to scope names.
651
+ * @param {object} coordinationValues Mapping of coordination types to
652
+ * values which the view defines directly. Optional.
461
653
  * @returns {object} Object containing all coordination values.
462
654
  */
463
- export function useInitialCoordination(parameters, coordinationScopes) {
655
+ export function useInitialCoordination(parameters, coordinationScopes, coordinationValues) {
464
656
  const values = useViewConfigStoreShallow((state) => {
465
657
  const { coordinationSpace } = state.initialViewConfig;
466
658
  return Object.fromEntries(parameters.map((parameter) => {
659
+ // Values defined directly by the view take precedence over scoped values.
660
+ if (coordinationValues && hasCoordinationValue(coordinationValues, parameter)) {
661
+ return [parameter, coordinationValues[parameter]];
662
+ }
467
663
  if (coordinationSpace && coordinationSpace[parameter]) {
468
664
  const value = coordinationSpace[parameter][coordinationScopes[parameter]];
469
665
  return [parameter, value];
@@ -483,17 +679,25 @@ export function useInitialCoordination(parameters, coordinationScopes) {
483
679
  * @param {string[]} parameters Array of coordination types.
484
680
  * @param {object} coordinationScopes Mapping of coordination types
485
681
  * to scope names.
682
+ * @param {object} coordinationValues Mapping of coordination types to
683
+ * values which the view defines directly. Optional.
684
+ * @param {string} viewUid The view of interest. Optional, but required for
685
+ * setter functions to be able to write back to view-level coordinationValues.
486
686
  * @returns {array} Returns a tuple [values, setters]
487
687
  * where values is an object containing all coordination values,
488
688
  * and setters is an object containing all coordination setter
489
689
  * functions for the values in `values`, named with a "set"
490
690
  * prefix.
491
691
  */
492
- export function useCoordination(parameters, coordinationScopes) {
692
+ export function useCoordination(parameters, coordinationScopes, coordinationValues, viewUid) {
493
693
  const setCoordinationValue = useViewConfigStore(state => state.setCoordinationValue);
494
694
  const values = useViewConfigStoreShallow((state) => {
495
695
  const { coordinationSpace } = state.viewConfig;
496
696
  return Object.fromEntries(parameters.map((parameter) => {
697
+ // Values defined directly by the view take precedence over scoped values.
698
+ if (coordinationValues && hasCoordinationValue(coordinationValues, parameter)) {
699
+ return [parameter, coordinationValues[parameter]];
700
+ }
497
701
  if (coordinationSpace) {
498
702
  const parameterScope = getParameterScope(parameter, coordinationScopes);
499
703
  if (coordinationSpace && coordinationSpace[parameter]) {
@@ -509,11 +713,12 @@ export function useCoordination(parameters, coordinationScopes) {
509
713
  const setterFunc = value => setCoordinationValue({
510
714
  parameter,
511
715
  coordinationScopes,
716
+ viewUid,
512
717
  value,
513
718
  });
514
719
  return [setterName, setterFunc];
515
720
  // eslint-disable-next-line react-hooks/exhaustive-deps
516
- })), [parameters, coordinationScopes]);
721
+ })), [parameters, coordinationScopes, viewUid]);
517
722
  return [values, setters];
518
723
  }
519
724
  export function useMultiCoordinationScopes(parameter, coordinationScopes) {
@@ -668,12 +873,16 @@ export function useDatasetUids(coordinationScopes) {
668
873
  * mapping object for a view.
669
874
  * @param {string} byType The {coordinationType} to use for per-{coordinationType} coordination
670
875
  * scope mappings.
876
+ * @param {object} coordinationValues Mapping of coordination types to
877
+ * values which the view defines directly. Optional.
878
+ * @param {string} viewUid The view of interest. Optional, but required for
879
+ * setter functions to be able to write back to view-level coordinationValues.
671
880
  * @returns {array} [cValues, cSetters] where
672
881
  * cValues is a mapping from coordination scope name to { coordinationType: coordinationValue },
673
882
  * and cSetters is a mapping from coordination scope name to { setCoordinationType }
674
883
  * setter functions.
675
884
  */
676
- export function useComplexCoordination(parameters, coordinationScopes, coordinationScopesBy, byType) {
885
+ export function useComplexCoordination(parameters, coordinationScopes, coordinationScopesBy, byType, coordinationValues, viewUid) {
677
886
  const setCoordinationValue = useViewConfigStore(state => state.setCoordinationValue);
678
887
  const parameterSpaces = useViewConfigStoreShallow((state) => {
679
888
  const { coordinationSpace } = state.viewConfig;
@@ -686,6 +895,14 @@ export function useComplexCoordination(parameters, coordinationScopes, coordinat
686
895
  const typeScopesArr = Array.isArray(typeScopes) ? typeScopes : [typeScopes];
687
896
  return Object.fromEntries(typeScopesArr.map((datasetScope) => {
688
897
  const datasetValues = Object.fromEntries(parameters.map((parameter, i) => {
898
+ // Values defined directly by the view take precedence over
899
+ // view-level coordination scopes, but not over the more
900
+ // fine-grained ({byType}-specific) coordination scopes.
901
+ if (coordinationValues
902
+ && hasCoordinationValue(coordinationValues, parameter)
903
+ && !coordinationScopesBy?.[byType]?.[parameter]?.[datasetScope]) {
904
+ return [parameter, coordinationValues[parameter]];
905
+ }
689
906
  if (parameterSpaces[i]) {
690
907
  const parameterSpace = parameterSpaces[i];
691
908
  const parameterScope = getParameterScopeBy(parameter, byType, datasetScope, coordinationScopes, coordinationScopesBy);
@@ -704,7 +921,7 @@ export function useComplexCoordination(parameters, coordinationScopes, coordinat
704
921
  }));
705
922
  }
706
923
  return {};
707
- }, [byType, coordinationScopes, coordinationScopesBy, parameterSpaces]);
924
+ }, [byType, coordinationScopes, coordinationScopesBy, parameterSpaces, coordinationValues]);
708
925
  const setters = useMemo(() => {
709
926
  const typeScopes = getParameterScope(byType, coordinationScopes);
710
927
  if (typeScopes) {
@@ -719,6 +936,7 @@ export function useComplexCoordination(parameters, coordinationScopes, coordinat
719
936
  typeScope: datasetScope,
720
937
  coordinationScopes,
721
938
  coordinationScopesBy,
939
+ viewUid,
722
940
  value,
723
941
  });
724
942
  return [setterName, setterFunc];
@@ -729,7 +947,7 @@ export function useComplexCoordination(parameters, coordinationScopes, coordinat
729
947
  return {};
730
948
  // eslint-disable-next-line react-hooks/exhaustive-deps
731
949
  // parameters is assumed to be a constant array.
732
- }, [coordinationScopes]);
950
+ }, [coordinationScopes, viewUid]);
733
951
  return [values, setters];
734
952
  }
735
953
  /**
@@ -769,15 +987,53 @@ export function useCoordinationScopesBy(coordinationScopes, coordinationScopesBy
769
987
  }, [coordinationScopes, coordinationScopesBy, metaSpaceBy]);
770
988
  return vals;
771
989
  }
990
+ /**
991
+ * Get the coordination information for a view, as defined directly on the
992
+ * corresponding layout entry (i.e., prior to accounting for meta-coordination).
993
+ * @param {string} viewUid The view of interest.
994
+ * @returns {[coordinationScopes, coordinationScopesBy, coordinationValues]}
995
+ */
996
+ export function useRawViewMapping(viewUid) {
997
+ const viewObj = useViewConfigStoreShallow((state) => {
998
+ const { layout } = state.viewConfig;
999
+ return layout.find(l => l.uid === viewUid);
1000
+ });
1001
+ return useMemo(() => ([
1002
+ viewObj?.coordinationScopes,
1003
+ viewObj?.coordinationScopesBy,
1004
+ viewObj?.coordinationValues,
1005
+ ]), [viewObj]);
1006
+ }
1007
+ /**
1008
+ * Get the coordination information for a view,
1009
+ * after accounting for meta-coordination.
1010
+ * Note that coordinationValues are defined by the view directly,
1011
+ * so they are not affected by meta-coordination.
1012
+ * @param {string} viewUid The view of interest.
1013
+ * @returns {[coordinationScopes, coordinationScopesBy, coordinationValues]}
1014
+ */
1015
+ export function useViewMapping(viewUid) {
1016
+ const [coordinationScopesRaw, coordinationScopesByRaw, coordinationValues,] = useRawViewMapping(viewUid);
1017
+ // TODO: use the annotation frame information here so that coordination state (e.g., coordinationValues) from the currently-selected frame
1018
+ // overrides the coordination state from the view-level coordinationValues, if applicable.
1019
+ // Note: will need to have already loaded the annotation frames for this to work.
1020
+ const coordinationScopes = useCoordinationScopes(coordinationScopesRaw || {});
1021
+ const coordinationScopesBy = useCoordinationScopesBy(coordinationScopesRaw || {}, coordinationScopesByRaw || {});
1022
+ return [coordinationScopes, coordinationScopesBy, coordinationValues];
1023
+ }
772
1024
  /**
773
1025
  * Use a second level of complex coordination.
774
1026
  * @param {string[]} parameters Array of coordination types.
775
1027
  * @param {object} coordinationScopesBy The coordinationScopesBy object from the view definition.
776
1028
  * @param {string} primaryType The first-level coordination type, such as spatialImageLayer.
777
1029
  * @param {string} secondaryType The second-level coordination type, such as spatialImageChannel.
1030
+ * @param {object} coordinationValues Mapping of coordination types to
1031
+ * values which the view defines directly. Optional.
1032
+ * @param {string} viewUid The view of interest. Optional, but required for
1033
+ * setter functions to be able to write back to view-level coordinationValues.
778
1034
  * @returns The results of useComplexCoordination.
779
1035
  */
780
- export function useComplexCoordinationSecondary(parameters, coordinationScopes, coordinationScopesBy, primaryType, secondaryType) {
1036
+ export function useComplexCoordinationSecondary(parameters, coordinationScopes, coordinationScopesBy, primaryType, secondaryType, coordinationValues, viewUid) {
781
1037
  const coordinationScopesFake = useMemo(() => {
782
1038
  if (coordinationScopesBy?.[primaryType]?.[secondaryType]) {
783
1039
  return {
@@ -796,7 +1052,7 @@ export function useComplexCoordinationSecondary(parameters, coordinationScopes,
796
1052
  // Finally, fall back to empty array.
797
1053
  return { [secondaryType]: [] };
798
1054
  }, [coordinationScopesBy, primaryType, secondaryType]);
799
- const [flatValues, flatSetters] = useComplexCoordination(parameters, coordinationScopesFake, coordinationScopesBy, secondaryType);
1055
+ const [flatValues, flatSetters] = useComplexCoordination(parameters, coordinationScopesFake, coordinationScopesBy, secondaryType, coordinationValues, viewUid);
800
1056
  const nestedValues = useMemo(() => {
801
1057
  // Re-nest
802
1058
  const result = {};
@@ -1043,6 +1299,81 @@ export function useSetLoaders() {
1043
1299
  export function useMergeCoordination() {
1044
1300
  return useViewConfigStore(state => state.mergeCoordination);
1045
1301
  }
1302
+ const ANNOTATION_FRAME_COORDINATION_TYPES = [
1303
+ CoordinationType.ANNOTATION_STORY,
1304
+ CoordinationType.ANNOTATION_FRAME_INDEX,
1305
+ ];
1306
+ /**
1307
+ * Determine which coordination values to merge for a view
1308
+ * when the story or the current annotation frame changes.
1309
+ * - When a frame is active, the values that the frame defines for the view.
1310
+ * If the frame does not define annotationShapes for the view, they are cleared,
1311
+ * so that shapes from a previous frame do not persist.
1312
+ * - When the frame index becomes null (after a frame was active),
1313
+ * annotationShapes are cleared.
1314
+ * Clearing annotationShapes is the only exception to leaving the
1315
+ * state untouched for values that a frame does not define.
1316
+ * @param {object|null} annotationStory The annotation story.
1317
+ * @param {number|null|undefined} prevFrameIndex The previously-applied frame index.
1318
+ * @param {number|null} annotationFrameIndex The current frame index.
1319
+ * @param {string} viewUid The view of interest.
1320
+ * @returns {object|null} The { frameCoordinationValues, scopePrefix } to apply, or null.
1321
+ */
1322
+ export function getAnnotationFrameUpdate(annotationStory, prevFrameIndex, annotationFrameIndex, viewUid) {
1323
+ if (typeof annotationFrameIndex !== 'number') {
1324
+ if (typeof prevFrameIndex === 'number') {
1325
+ return {
1326
+ frameCoordinationValues: { [CoordinationType.ANNOTATION_SHAPES]: null },
1327
+ scopePrefix: '',
1328
+ };
1329
+ }
1330
+ return null;
1331
+ }
1332
+ const frame = annotationStory?.frames?.[annotationFrameIndex];
1333
+ if (!frame) {
1334
+ // The story may not have been loaded yet.
1335
+ return null;
1336
+ }
1337
+ const frameCoordinationValues = getAnnotationFrameCoordinationValues(annotationStory, annotationFrameIndex, viewUid);
1338
+ return {
1339
+ frameCoordinationValues: {
1340
+ [CoordinationType.ANNOTATION_SHAPES]: null,
1341
+ ...frameCoordinationValues,
1342
+ },
1343
+ scopePrefix: `annotation_${annotationStory.uid}_${frame.uid}_${viewUid}_`,
1344
+ };
1345
+ }
1346
+ /**
1347
+ * When the current annotation frame changes, merge the coordination values
1348
+ * that the frame defines for this view into the coordination space.
1349
+ * The annotation story is treated as read-only: after the frame has been applied,
1350
+ * the view reads (and its setters write) the coordination space as usual,
1351
+ * so user interactions (e.g., panning) are not overridden by the frame.
1352
+ * Values which the frame does not define keep their current values,
1353
+ * except that annotationShapes are cleared when the frame does not define them
1354
+ * or when the frame index becomes null.
1355
+ * @param {string} viewUid The view of interest.
1356
+ * @param {object} coordinationScopes The coordination scopes for the view,
1357
+ * after accounting for meta-coordination.
1358
+ * @param {object} coordinationValues Values which the view defines directly. Optional.
1359
+ */
1360
+ export function useAnnotationFrameCoordination(viewUid, coordinationScopes, coordinationValues) {
1361
+ const [{ annotationStory, annotationFrameIndex, }] = useCoordination(ANNOTATION_FRAME_COORDINATION_TYPES, coordinationScopes, coordinationValues);
1362
+ const applyAnnotationFrameCoordination = useViewConfigStore(state => state.applyAnnotationFrameCoordination);
1363
+ // Track the previously-applied frame index, to detect when the frame index becomes null.
1364
+ const prevFrameIndexRef = useRef(undefined);
1365
+ useEffect(() => {
1366
+ const update = getAnnotationFrameUpdate(annotationStory, prevFrameIndexRef.current, annotationFrameIndex, viewUid);
1367
+ prevFrameIndexRef.current = annotationFrameIndex;
1368
+ if (update) {
1369
+ applyAnnotationFrameCoordination({ viewUid, ...update });
1370
+ }
1371
+ // Deliberate dependency omissions: only apply the frame when the story or
1372
+ // the current frame changes, not when the coordination scopes change
1373
+ // (which happens as a result of applying multi-level frame values).
1374
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1375
+ }, [annotationStory, annotationFrameIndex, viewUid]);
1376
+ }
1046
1377
  /**
1047
1378
  * Obtain the current view config object from
1048
1379
  * the global app state.