@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
@@ -2,6 +2,7 @@
2
2
  /* eslint-disable react-refresh/only-export-components */
3
3
  import React, {
4
4
  useCallback,
5
+ useEffect,
5
6
  useRef,
6
7
  useMemo,
7
8
  createContext,
@@ -12,8 +13,8 @@ import { useShallow } from 'zustand/shallow';
12
13
  import { subscribeWithSelector } from 'zustand/middleware';
13
14
  import { isMatch, cloneDeep } from 'lodash-es';
14
15
  import { CoordinationType } from '@vitessce/constants-internal';
15
- import { capitalize } from '@vitessce/utils';
16
- import { getCoordinationSpaceAndScopes } from '@vitessce/config';
16
+ import { capitalize, getAnnotationFrameCoordinationValues } from '@vitessce/utils';
17
+ import { getCoordinationSpaceAndScopes, CoordinationLevel as CL } from '@vitessce/config';
17
18
  import {
18
19
  removeImageChannelInMetaCoordinationScopesHelper,
19
20
  addImageChannelInMetaCoordinationScopesHelper,
@@ -225,6 +226,182 @@ export function getParameterScopeBy(
225
226
  return parameterScopeGlobal;
226
227
  }
227
228
 
229
+ /**
230
+ * Determine whether a view defines a value for a coordination type directly,
231
+ * in which case that value takes precedence over any coordination scope
232
+ * (from coordinationScopes or metaCoordinationScopes) for the same type.
233
+ * @param {object|undefined} coordinationValues The view.coordinationValues object, if any.
234
+ * @param {string} parameter A coordination type.
235
+ * @returns {boolean} True if there is a direct value for this coordination type.
236
+ */
237
+ export function hasCoordinationValue(coordinationValues, parameter) {
238
+ return coordinationValues !== undefined
239
+ && parameter in coordinationValues;
240
+ }
241
+
242
+ // The sentinel used to represent `CL(...)` (multi-level coordination)
243
+ // within JSON, such as within annotation frame coordination values.
244
+ // Reference: https://github.com/keller-mark/use-coordination/pull/115
245
+ export const COORDINATION_LEVEL_SENTINEL = '$CL';
246
+
247
+ function isPlainObject(value) {
248
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
249
+ }
250
+
251
+ function isMultiLevelValue(value) {
252
+ return isPlainObject(value) && Object.hasOwn(value, COORDINATION_LEVEL_SENTINEL);
253
+ }
254
+
255
+ function toArray(value) {
256
+ if (value === undefined || value === null) return [];
257
+ return Array.isArray(value) ? value : [value];
258
+ }
259
+
260
+ /**
261
+ * Revive the `{ "$CL": ... }` sentinels within a JSON value
262
+ * into CoordinationLevel instances.
263
+ * @param {*} value The JSON value.
264
+ * @returns {*} The revived value.
265
+ */
266
+ export function reviveCoordinationLevels(value) {
267
+ if (Array.isArray(value)) {
268
+ return value.map(reviveCoordinationLevels);
269
+ }
270
+ if (isMultiLevelValue(value)) {
271
+ return CL(reviveCoordinationLevels(value[COORDINATION_LEVEL_SENTINEL]));
272
+ }
273
+ if (isPlainObject(value)) {
274
+ return Object.fromEntries(
275
+ Object.entries(value).map(([k, v]) => ([k, reviveCoordinationLevels(v)])),
276
+ );
277
+ }
278
+ return value;
279
+ }
280
+
281
+ /**
282
+ * Merge the coordination values that an annotation frame defines for a view
283
+ * into the view config. The frame values take precedence over the current values.
284
+ * The frame itself is treated as read-only.
285
+ * - Single-level values are written wherever the view's setter would write them:
286
+ * in-place if the view defines the value directly (view.coordinationValues),
287
+ * otherwise into the coordination scope(s) that the view is mapped to
288
+ * (after accounting for meta-coordination), so that linked views follow along.
289
+ * - Multi-level values (`{ "$CL": [...] }`) are expanded into new coordination scopes,
290
+ * and the view's existing (meta-)coordination scope mapping for those coordination types
291
+ * is re-pointed to the new scopes (so that linked views follow along).
292
+ * @param {object} viewConfig The current view config.
293
+ * @param {string} viewUid The view of interest.
294
+ * @param {object} frameCoordinationValues The coordination values for the view in the frame.
295
+ * @param {string} scopePrefix Prefix for any newly-created coordination scope names.
296
+ * @returns {object} The new view config.
297
+ */
298
+ export function applyAnnotationFrameToViewConfig(
299
+ viewConfig, viewUid, frameCoordinationValues, scopePrefix,
300
+ ) {
301
+ const { coordinationSpace = {}, layout } = viewConfig;
302
+ const viewObj = layout.find(l => l.uid === viewUid);
303
+ if (!viewObj || !frameCoordinationValues) {
304
+ return viewConfig;
305
+ }
306
+ const META = CoordinationType.META_COORDINATION_SCOPES;
307
+ const META_BY = CoordinationType.META_COORDINATION_SCOPES_BY;
308
+
309
+ const newSpace = { ...coordinationSpace };
310
+ const newViewObj = { ...viewObj };
311
+ const setViewValue = (coordinationType, value) => {
312
+ newViewObj.coordinationValues = {
313
+ ...newViewObj.coordinationValues,
314
+ [coordinationType]: value,
315
+ };
316
+ };
317
+
318
+ const entries = Object.entries(frameCoordinationValues);
319
+ const singleLevelEntries = entries.filter(([, value]) => !isMultiLevelValue(value));
320
+ const multiLevelEntries = entries.filter(([, value]) => isMultiLevelValue(value));
321
+
322
+ // Single-level values.
323
+ const resolvedScopes = getScopes(viewObj.coordinationScopes || {}, coordinationSpace[META]);
324
+ singleLevelEntries.forEach(([coordinationType, value]) => {
325
+ const scopes = toArray(resolvedScopes[coordinationType]);
326
+ if (hasCoordinationValue(viewObj.coordinationValues, coordinationType) || scopes.length === 0) {
327
+ // The view defines the value directly, or is not mapped to any scope for this type.
328
+ setViewValue(coordinationType, value);
329
+ return;
330
+ }
331
+ newSpace[coordinationType] = {
332
+ ...newSpace[coordinationType],
333
+ ...Object.fromEntries(scopes.map(scope => ([scope, value]))),
334
+ };
335
+ });
336
+
337
+ // Multi-level values.
338
+ if (multiLevelEntries.length > 0) {
339
+ const {
340
+ coordinationSpace: generatedSpace,
341
+ coordinationScopes: generatedScopes,
342
+ } = getCoordinationSpaceAndScopes(
343
+ Object.fromEntries(multiLevelEntries.map(([coordinationType, value]) => ([
344
+ coordinationType, reviveCoordinationLevels(value),
345
+ ]))),
346
+ scopePrefix,
347
+ );
348
+ // Merge the values for the new scopes into the coordination space.
349
+ Object.entries(generatedSpace).forEach(([coordinationType, scopeValues]) => {
350
+ if ([META, META_BY, CoordinationType.DATASET].includes(coordinationType)) return;
351
+ newSpace[coordinationType] = {
352
+ ...newSpace[coordinationType],
353
+ ...scopeValues,
354
+ };
355
+ });
356
+ const newTypeScopes = generatedSpace[META]?.[generatedScopes[META]?.[0]] || {};
357
+ const newTypeScopesBy = generatedSpace[META_BY]?.[generatedScopes[META_BY]?.[0]] || {};
358
+
359
+ // Re-point the view's mapping, in the last meta-coordination scope which
360
+ // defines it (since later meta-scopes take precedence), otherwise on the view.
361
+ const viewMetaScopes = toArray(viewObj.coordinationScopes?.[META]);
362
+ Object.entries(newTypeScopes).forEach(([coordinationType, scopes]) => {
363
+ const owner = [...viewMetaScopes].reverse()
364
+ .find(m => Object.hasOwn(newSpace[META]?.[m] || {}, coordinationType));
365
+ if (owner) {
366
+ newSpace[META] = {
367
+ ...newSpace[META],
368
+ [owner]: { ...newSpace[META][owner], [coordinationType]: scopes },
369
+ };
370
+ } else {
371
+ newViewObj.coordinationScopes = {
372
+ ...newViewObj.coordinationScopes,
373
+ [coordinationType]: scopes,
374
+ };
375
+ }
376
+ });
377
+ const viewMetaScopesBy = toArray(viewObj.coordinationScopes?.[META_BY]);
378
+ Object.entries(newTypeScopesBy).forEach(([primaryType, primaryObj]) => {
379
+ // If no meta-scope defines this (e.g., newly-nested) primary type yet,
380
+ // fall back to the last meta-scope, so that linked views also receive it.
381
+ const owner = [...viewMetaScopesBy].reverse()
382
+ .find(m => Object.hasOwn(newSpace[META_BY]?.[m] || {}, primaryType))
383
+ ?? viewMetaScopesBy.at(-1);
384
+ if (owner) {
385
+ newSpace[META_BY] = {
386
+ ...newSpace[META_BY],
387
+ [owner]: { ...newSpace[META_BY][owner], [primaryType]: primaryObj },
388
+ };
389
+ } else {
390
+ newViewObj.coordinationScopesBy = {
391
+ ...newViewObj.coordinationScopesBy,
392
+ [primaryType]: primaryObj,
393
+ };
394
+ }
395
+ });
396
+ }
397
+
398
+ return {
399
+ ...viewConfig,
400
+ coordinationSpace: newSpace,
401
+ layout: layout.map(l => (l.uid === viewUid ? newViewObj : l)),
402
+ };
403
+ }
404
+
228
405
 
229
406
  /**
230
407
  * The useViewConfigStore hook is initialized via the zustand
@@ -261,8 +438,37 @@ export const createViewConfigStore = (initialLoaders, initialConfig) => create()
261
438
  setCoordinationValue: ({
262
439
  parameter, value, coordinationScopes,
263
440
  byType, typeScope, coordinationScopesBy,
441
+ // The viewUid is only required to write back to view-level coordinationValues.
442
+ viewUid,
264
443
  }) => set((state) => {
265
- const { coordinationSpace } = state.viewConfig;
444
+ const { coordinationSpace, layout } = state.viewConfig;
445
+ // If the view defines the value directly, then update it in-place,
446
+ // as long as there is no more fine-grained ({byType}-specific)
447
+ // coordination scope which would take precedence.
448
+ const viewObj = viewUid === undefined
449
+ ? undefined
450
+ : layout.find(l => l.uid === viewUid);
451
+ const coordinationValues = viewObj?.coordinationValues;
452
+ const byTypeScope = byType
453
+ ? coordinationScopesBy?.[byType]?.[parameter]?.[typeScope]
454
+ : undefined;
455
+ if (viewUid !== undefined && hasCoordinationValue(coordinationValues, parameter) && !byTypeScope) {
456
+ return {
457
+ viewConfig: {
458
+ ...state.viewConfig,
459
+ layout: layout.map(l => (l.uid === viewUid
460
+ ? {
461
+ ...l,
462
+ coordinationValues: {
463
+ ...coordinationValues,
464
+ [parameter]: value,
465
+ },
466
+ }
467
+ : l)),
468
+ },
469
+ mostRecentConfigSource: 'internal',
470
+ };
471
+ }
266
472
  let scope;
267
473
  if (!byType) {
268
474
  scope = getParameterScope(parameter, coordinationScopes);
@@ -370,6 +576,14 @@ export const createViewConfigStore = (initialLoaders, initialConfig) => create()
370
576
  viewConfig: newViewConfig, mostRecentConfigSource: 'internal',
371
577
  };
372
578
  }),
579
+ applyAnnotationFrameCoordination: ({
580
+ viewUid, frameCoordinationValues, scopePrefix,
581
+ }) => set(state => ({
582
+ viewConfig: applyAnnotationFrameToViewConfig(
583
+ state.viewConfig, viewUid, frameCoordinationValues, scopePrefix,
584
+ ),
585
+ mostRecentConfigSource: 'internal',
586
+ })),
373
587
  removeImageChannelInMetaCoordinationScopes: (coordinationScopesRaw, layerScope, channelScope) => set((state) => {
374
588
  const { coordinationSpace } = state.viewConfig;
375
589
  return {
@@ -528,12 +742,18 @@ const useGridSizeStore = create(set => ({
528
742
  * @param {string[]} parameters Array of coordination types.
529
743
  * @param {object} coordinationScopes Mapping of coordination types
530
744
  * to scope names.
745
+ * @param {object} coordinationValues Mapping of coordination types to
746
+ * values which the view defines directly. Optional.
531
747
  * @returns {object} Object containing all coordination values.
532
748
  */
533
- export function useInitialCoordination(parameters, coordinationScopes) {
749
+ export function useInitialCoordination(parameters, coordinationScopes, coordinationValues) {
534
750
  const values = useViewConfigStoreShallow((state) => {
535
751
  const { coordinationSpace } = state.initialViewConfig;
536
752
  return Object.fromEntries(parameters.map((parameter) => {
753
+ // Values defined directly by the view take precedence over scoped values.
754
+ if (coordinationValues && hasCoordinationValue(coordinationValues, parameter)) {
755
+ return [parameter, coordinationValues[parameter]];
756
+ }
537
757
  if (coordinationSpace && coordinationSpace[parameter]) {
538
758
  const value = coordinationSpace[parameter][coordinationScopes[parameter]];
539
759
  return [parameter, value];
@@ -554,18 +774,26 @@ export function useInitialCoordination(parameters, coordinationScopes) {
554
774
  * @param {string[]} parameters Array of coordination types.
555
775
  * @param {object} coordinationScopes Mapping of coordination types
556
776
  * to scope names.
777
+ * @param {object} coordinationValues Mapping of coordination types to
778
+ * values which the view defines directly. Optional.
779
+ * @param {string} viewUid The view of interest. Optional, but required for
780
+ * setter functions to be able to write back to view-level coordinationValues.
557
781
  * @returns {array} Returns a tuple [values, setters]
558
782
  * where values is an object containing all coordination values,
559
783
  * and setters is an object containing all coordination setter
560
784
  * functions for the values in `values`, named with a "set"
561
785
  * prefix.
562
786
  */
563
- export function useCoordination(parameters, coordinationScopes) {
787
+ export function useCoordination(parameters, coordinationScopes, coordinationValues, viewUid) {
564
788
  const setCoordinationValue = useViewConfigStore(state => state.setCoordinationValue);
565
789
 
566
790
  const values = useViewConfigStoreShallow((state) => {
567
791
  const { coordinationSpace } = state.viewConfig;
568
792
  return Object.fromEntries(parameters.map((parameter) => {
793
+ // Values defined directly by the view take precedence over scoped values.
794
+ if (coordinationValues && hasCoordinationValue(coordinationValues, parameter)) {
795
+ return [parameter, coordinationValues[parameter]];
796
+ }
569
797
  if (coordinationSpace) {
570
798
  const parameterScope = getParameterScope(parameter, coordinationScopes);
571
799
  if (coordinationSpace && coordinationSpace[parameter]) {
@@ -582,11 +810,12 @@ export function useCoordination(parameters, coordinationScopes) {
582
810
  const setterFunc = value => setCoordinationValue({
583
811
  parameter,
584
812
  coordinationScopes,
813
+ viewUid,
585
814
  value,
586
815
  });
587
816
  return [setterName, setterFunc];
588
817
  // eslint-disable-next-line react-hooks/exhaustive-deps
589
- })), [parameters, coordinationScopes]);
818
+ })), [parameters, coordinationScopes, viewUid]);
590
819
 
591
820
  return [values, setters];
592
821
  }
@@ -758,13 +987,17 @@ export function useDatasetUids(coordinationScopes) {
758
987
  * mapping object for a view.
759
988
  * @param {string} byType The {coordinationType} to use for per-{coordinationType} coordination
760
989
  * scope mappings.
990
+ * @param {object} coordinationValues Mapping of coordination types to
991
+ * values which the view defines directly. Optional.
992
+ * @param {string} viewUid The view of interest. Optional, but required for
993
+ * setter functions to be able to write back to view-level coordinationValues.
761
994
  * @returns {array} [cValues, cSetters] where
762
995
  * cValues is a mapping from coordination scope name to { coordinationType: coordinationValue },
763
996
  * and cSetters is a mapping from coordination scope name to { setCoordinationType }
764
997
  * setter functions.
765
998
  */
766
999
  export function useComplexCoordination(
767
- parameters, coordinationScopes, coordinationScopesBy, byType,
1000
+ parameters, coordinationScopes, coordinationScopesBy, byType, coordinationValues, viewUid,
768
1001
  ) {
769
1002
  const setCoordinationValue = useViewConfigStore(state => state.setCoordinationValue);
770
1003
 
@@ -780,6 +1013,16 @@ export function useComplexCoordination(
780
1013
  const typeScopesArr = Array.isArray(typeScopes) ? typeScopes : [typeScopes];
781
1014
  return Object.fromEntries(typeScopesArr.map((datasetScope) => {
782
1015
  const datasetValues = Object.fromEntries(parameters.map((parameter, i) => {
1016
+ // Values defined directly by the view take precedence over
1017
+ // view-level coordination scopes, but not over the more
1018
+ // fine-grained ({byType}-specific) coordination scopes.
1019
+ if (
1020
+ coordinationValues
1021
+ && hasCoordinationValue(coordinationValues, parameter)
1022
+ && !coordinationScopesBy?.[byType]?.[parameter]?.[datasetScope]
1023
+ ) {
1024
+ return [parameter, coordinationValues[parameter]];
1025
+ }
783
1026
  if (parameterSpaces[i]) {
784
1027
  const parameterSpace = parameterSpaces[i];
785
1028
  const parameterScope = getParameterScopeBy(
@@ -804,7 +1047,7 @@ export function useComplexCoordination(
804
1047
  }));
805
1048
  }
806
1049
  return {};
807
- }, [byType, coordinationScopes, coordinationScopesBy, parameterSpaces]);
1050
+ }, [byType, coordinationScopes, coordinationScopesBy, parameterSpaces, coordinationValues]);
808
1051
 
809
1052
  const setters = useMemo(() => {
810
1053
  const typeScopes = getParameterScope(byType, coordinationScopes);
@@ -820,6 +1063,7 @@ export function useComplexCoordination(
820
1063
  typeScope: datasetScope,
821
1064
  coordinationScopes,
822
1065
  coordinationScopesBy,
1066
+ viewUid,
823
1067
  value,
824
1068
  });
825
1069
  return [setterName, setterFunc];
@@ -830,7 +1074,7 @@ export function useComplexCoordination(
830
1074
  return {};
831
1075
  // eslint-disable-next-line react-hooks/exhaustive-deps
832
1076
  // parameters is assumed to be a constant array.
833
- }, [coordinationScopes]);
1077
+ }, [coordinationScopes, viewUid]);
834
1078
 
835
1079
  return [values, setters];
836
1080
  }
@@ -881,16 +1125,65 @@ export function useCoordinationScopesBy(coordinationScopes, coordinationScopesBy
881
1125
  return vals;
882
1126
  }
883
1127
 
1128
+ /**
1129
+ * Get the coordination information for a view, as defined directly on the
1130
+ * corresponding layout entry (i.e., prior to accounting for meta-coordination).
1131
+ * @param {string} viewUid The view of interest.
1132
+ * @returns {[coordinationScopes, coordinationScopesBy, coordinationValues]}
1133
+ */
1134
+ export function useRawViewMapping(viewUid) {
1135
+ const viewObj = useViewConfigStoreShallow((state) => {
1136
+ const { layout } = state.viewConfig;
1137
+ return layout.find(l => l.uid === viewUid);
1138
+ });
1139
+
1140
+ return useMemo(() => ([
1141
+ viewObj?.coordinationScopes,
1142
+ viewObj?.coordinationScopesBy,
1143
+ viewObj?.coordinationValues,
1144
+ ]), [viewObj]);
1145
+ }
1146
+
1147
+ /**
1148
+ * Get the coordination information for a view,
1149
+ * after accounting for meta-coordination.
1150
+ * Note that coordinationValues are defined by the view directly,
1151
+ * so they are not affected by meta-coordination.
1152
+ * @param {string} viewUid The view of interest.
1153
+ * @returns {[coordinationScopes, coordinationScopesBy, coordinationValues]}
1154
+ */
1155
+ export function useViewMapping(viewUid) {
1156
+ const [
1157
+ coordinationScopesRaw, coordinationScopesByRaw, coordinationValues,
1158
+ ] = useRawViewMapping(viewUid);
1159
+
1160
+ // TODO: use the annotation frame information here so that coordination state (e.g., coordinationValues) from the currently-selected frame
1161
+ // overrides the coordination state from the view-level coordinationValues, if applicable.
1162
+ // Note: will need to have already loaded the annotation frames for this to work.
1163
+
1164
+ const coordinationScopes = useCoordinationScopes(coordinationScopesRaw || {});
1165
+ const coordinationScopesBy = useCoordinationScopesBy(
1166
+ coordinationScopesRaw || {}, coordinationScopesByRaw || {},
1167
+ );
1168
+
1169
+ return [coordinationScopes, coordinationScopesBy, coordinationValues];
1170
+ }
1171
+
884
1172
  /**
885
1173
  * Use a second level of complex coordination.
886
1174
  * @param {string[]} parameters Array of coordination types.
887
1175
  * @param {object} coordinationScopesBy The coordinationScopesBy object from the view definition.
888
1176
  * @param {string} primaryType The first-level coordination type, such as spatialImageLayer.
889
1177
  * @param {string} secondaryType The second-level coordination type, such as spatialImageChannel.
1178
+ * @param {object} coordinationValues Mapping of coordination types to
1179
+ * values which the view defines directly. Optional.
1180
+ * @param {string} viewUid The view of interest. Optional, but required for
1181
+ * setter functions to be able to write back to view-level coordinationValues.
890
1182
  * @returns The results of useComplexCoordination.
891
1183
  */
892
1184
  export function useComplexCoordinationSecondary(
893
1185
  parameters, coordinationScopes, coordinationScopesBy, primaryType, secondaryType,
1186
+ coordinationValues, viewUid,
894
1187
  ) {
895
1188
  const coordinationScopesFake = useMemo(() => {
896
1189
  if (coordinationScopesBy?.[primaryType]?.[secondaryType]) {
@@ -912,6 +1205,7 @@ export function useComplexCoordinationSecondary(
912
1205
  }, [coordinationScopesBy, primaryType, secondaryType]);
913
1206
  const [flatValues, flatSetters] = useComplexCoordination(
914
1207
  parameters, coordinationScopesFake, coordinationScopesBy, secondaryType,
1208
+ coordinationValues, viewUid,
915
1209
  );
916
1210
  const nestedValues = useMemo(() => {
917
1211
  // Re-nest
@@ -1183,6 +1477,98 @@ export function useMergeCoordination() {
1183
1477
  return useViewConfigStore(state => state.mergeCoordination);
1184
1478
  }
1185
1479
 
1480
+ const ANNOTATION_FRAME_COORDINATION_TYPES = [
1481
+ CoordinationType.ANNOTATION_STORY,
1482
+ CoordinationType.ANNOTATION_FRAME_INDEX,
1483
+ ];
1484
+
1485
+ /**
1486
+ * Determine which coordination values to merge for a view
1487
+ * when the story or the current annotation frame changes.
1488
+ * - When a frame is active, the values that the frame defines for the view.
1489
+ * If the frame does not define annotationShapes for the view, they are cleared,
1490
+ * so that shapes from a previous frame do not persist.
1491
+ * - When the frame index becomes null (after a frame was active),
1492
+ * annotationShapes are cleared.
1493
+ * Clearing annotationShapes is the only exception to leaving the
1494
+ * state untouched for values that a frame does not define.
1495
+ * @param {object|null} annotationStory The annotation story.
1496
+ * @param {number|null|undefined} prevFrameIndex The previously-applied frame index.
1497
+ * @param {number|null} annotationFrameIndex The current frame index.
1498
+ * @param {string} viewUid The view of interest.
1499
+ * @returns {object|null} The { frameCoordinationValues, scopePrefix } to apply, or null.
1500
+ */
1501
+ export function getAnnotationFrameUpdate(
1502
+ annotationStory, prevFrameIndex, annotationFrameIndex, viewUid,
1503
+ ) {
1504
+ if (typeof annotationFrameIndex !== 'number') {
1505
+ if (typeof prevFrameIndex === 'number') {
1506
+ return {
1507
+ frameCoordinationValues: { [CoordinationType.ANNOTATION_SHAPES]: null },
1508
+ scopePrefix: '',
1509
+ };
1510
+ }
1511
+ return null;
1512
+ }
1513
+ const frame = annotationStory?.frames?.[annotationFrameIndex];
1514
+ if (!frame) {
1515
+ // The story may not have been loaded yet.
1516
+ return null;
1517
+ }
1518
+ const frameCoordinationValues = getAnnotationFrameCoordinationValues(
1519
+ annotationStory, annotationFrameIndex, viewUid,
1520
+ );
1521
+ return {
1522
+ frameCoordinationValues: {
1523
+ [CoordinationType.ANNOTATION_SHAPES]: null,
1524
+ ...frameCoordinationValues,
1525
+ },
1526
+ scopePrefix: `annotation_${annotationStory.uid}_${frame.uid}_${viewUid}_`,
1527
+ };
1528
+ }
1529
+
1530
+ /**
1531
+ * When the current annotation frame changes, merge the coordination values
1532
+ * that the frame defines for this view into the coordination space.
1533
+ * The annotation story is treated as read-only: after the frame has been applied,
1534
+ * the view reads (and its setters write) the coordination space as usual,
1535
+ * so user interactions (e.g., panning) are not overridden by the frame.
1536
+ * Values which the frame does not define keep their current values,
1537
+ * except that annotationShapes are cleared when the frame does not define them
1538
+ * or when the frame index becomes null.
1539
+ * @param {string} viewUid The view of interest.
1540
+ * @param {object} coordinationScopes The coordination scopes for the view,
1541
+ * after accounting for meta-coordination.
1542
+ * @param {object} coordinationValues Values which the view defines directly. Optional.
1543
+ */
1544
+ export function useAnnotationFrameCoordination(viewUid, coordinationScopes, coordinationValues) {
1545
+ const [{
1546
+ annotationStory,
1547
+ annotationFrameIndex,
1548
+ }] = useCoordination(
1549
+ ANNOTATION_FRAME_COORDINATION_TYPES, coordinationScopes, coordinationValues,
1550
+ );
1551
+ const applyAnnotationFrameCoordination = useViewConfigStore(
1552
+ state => state.applyAnnotationFrameCoordination,
1553
+ );
1554
+ // Track the previously-applied frame index, to detect when the frame index becomes null.
1555
+ const prevFrameIndexRef = useRef(undefined);
1556
+
1557
+ useEffect(() => {
1558
+ const update = getAnnotationFrameUpdate(
1559
+ annotationStory, prevFrameIndexRef.current, annotationFrameIndex, viewUid,
1560
+ );
1561
+ prevFrameIndexRef.current = annotationFrameIndex;
1562
+ if (update) {
1563
+ applyAnnotationFrameCoordination({ viewUid, ...update });
1564
+ }
1565
+ // Deliberate dependency omissions: only apply the frame when the story or
1566
+ // the current frame changes, not when the coordination scopes change
1567
+ // (which happens as a result of applying multi-level frame values).
1568
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1569
+ }, [annotationStory, annotationFrameIndex, viewUid]);
1570
+ }
1571
+
1186
1572
  /**
1187
1573
  * Obtain the current view config object from
1188
1574
  * the global app state.
@@ -23,7 +23,7 @@ export function VitessceGridLayout(props) {
23
23
  padding = 10, margin: marginProp = 10, draggableHandle: draggableHandleClass,
24
24
  onResize, onResizeStop, rowHeight, theme, height,
25
25
  onRemoveComponent, onLayoutChange: onLayoutChangeProp,
26
- isBounded,
26
+ isBounded, areAnnotationsEditable,
27
27
  pageMode, children,
28
28
  } = props;
29
29
 
@@ -146,6 +146,7 @@ export function VitessceGridLayout(props) {
146
146
  fileTypes={fileTypes}
147
147
  coordinationTypes={coordinationTypes}
148
148
  stores={stores}
149
+ areAnnotationsEditable={areAnnotationsEditable}
149
150
  />
150
151
  </ErrorBoundary>
151
152
  </div>
@@ -174,12 +175,13 @@ export function VitessceGridLayout(props) {
174
175
  fileTypes={fileTypes}
175
176
  coordinationTypes={coordinationTypes}
176
177
  stores={stores}
178
+ areAnnotationsEditable={areAnnotationsEditable}
177
179
  />
178
180
  </ErrorBoundary>
179
181
  </div>
180
182
  );
181
183
  }), contextValue];
182
- }, [gridComponents, getComponent, onRemoveComponent, theme]);
184
+ }, [gridComponents, getComponent, onRemoveComponent, theme, areAnnotationsEditable]);
183
185
 
184
186
  return (pageMode
185
187
  ? (