@vitessce/vit-s 4.0.9 → 4.1.0
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/index.js +5156 -3845
- package/dist-tsc/VitS.d.ts.map +1 -1
- package/dist-tsc/VitS.js +2 -1
- package/dist-tsc/VitS.js.map +1 -1
- package/dist-tsc/index.d.ts +2 -1
- package/dist-tsc/index.js +2 -1
- package/dist-tsc/index.js.map +1 -1
- package/dist-tsc/shared-mui/container.d.ts +1 -1
- package/dist-tsc/shared-mui/container.d.ts.map +1 -1
- package/dist-tsc/shared-mui/styles.d.ts +3 -3
- package/dist-tsc/shared-plot-options/styles.d.ts +1 -1
- package/dist-tsc/shared-plot-options/styles.d.ts.map +1 -1
- package/dist-tsc/shared-warning-styles.d.ts +1 -1
- package/dist-tsc/shared-warning-styles.d.ts.map +1 -1
- package/dist-tsc/state/annotation-editing.d.ts +55 -0
- package/dist-tsc/state/annotation-editing.d.ts.map +1 -0
- package/dist-tsc/state/annotation-editing.js +192 -0
- package/dist-tsc/state/annotation-editing.js.map +1 -0
- package/dist-tsc/state/annotation-editing.test.d.ts +2 -0
- package/dist-tsc/state/annotation-editing.test.d.ts.map +1 -0
- package/dist-tsc/state/annotation-editing.test.js +128 -0
- package/dist-tsc/state/annotation-editing.test.js.map +1 -0
- package/dist-tsc/state/annotation-frames.test.js +24 -0
- package/dist-tsc/state/annotation-frames.test.js.map +1 -1
- package/dist-tsc/state/hooks.d.ts +7 -1
- package/dist-tsc/state/hooks.d.ts.map +1 -1
- package/dist-tsc/state/hooks.js +26 -2
- package/dist-tsc/state/hooks.js.map +1 -1
- package/dist-tsc/title-styles.d.ts +1 -1
- package/dist-tsc/title-styles.d.ts.map +1 -1
- package/package.json +13 -13
- package/src/VitS.js +33 -27
- package/src/index.js +8 -0
- package/src/state/annotation-editing.js +240 -0
- package/src/state/annotation-editing.test.js +148 -0
- package/src/state/annotation-frames.test.js +26 -0
- package/src/state/hooks.js +27 -1
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/* eslint-disable react-refresh/only-export-components */
|
|
2
|
+
import React, {
|
|
3
|
+
useCallback,
|
|
4
|
+
useRef,
|
|
5
|
+
createContext,
|
|
6
|
+
useContext,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import { create, useStore } from 'zustand';
|
|
9
|
+
import { useShallow } from 'zustand/shallow';
|
|
10
|
+
import { subscribeWithSelector } from 'zustand/middleware';
|
|
11
|
+
import { CoordinationType } from '@vitessce/constants-internal';
|
|
12
|
+
import {
|
|
13
|
+
getScopes,
|
|
14
|
+
getScopesBy,
|
|
15
|
+
hasCoordinationValue,
|
|
16
|
+
COORDINATION_LEVEL_SENTINEL,
|
|
17
|
+
} from './hooks.js';
|
|
18
|
+
|
|
19
|
+
// The annotation editing store holds ephemeral (non-config) state related to
|
|
20
|
+
// authoring annotation shapes: the active drawing tool, the shape currently
|
|
21
|
+
// being drawn (and in which view), and the currently-selected shape.
|
|
22
|
+
// Views may write in-progress drawn shapes here, but only the annotation controller
|
|
23
|
+
// commits completed shapes to the annotation story.
|
|
24
|
+
|
|
25
|
+
// Shape types which are completed after two clicks.
|
|
26
|
+
const TWO_POINT_SHAPE_TYPES = ['rectangle', 'line', 'ellipse'];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Compute the next drawing state after the user has clicked
|
|
30
|
+
* on a view while a drawing tool is active.
|
|
31
|
+
* Two-point shapes (rectangle, line, ellipse) are completed on the second click.
|
|
32
|
+
* Multi-point shapes (polygon, polyline) are completed via finishDrawing.
|
|
33
|
+
* Clicking in a different view than the in-progress shape restarts the drawing.
|
|
34
|
+
* @param {object} state The current store state.
|
|
35
|
+
* @param {object} params
|
|
36
|
+
* @param {string} params.viewUid The uid of the view that was clicked.
|
|
37
|
+
* @param {number[]} params.coordinate The [x, y] clicked position, in view coordinates.
|
|
38
|
+
* @returns {object} The partial state update.
|
|
39
|
+
*/
|
|
40
|
+
export function getNextDrawingState(state, { viewUid, coordinate }) {
|
|
41
|
+
const { activeTool, drawing } = state;
|
|
42
|
+
if (!activeTool || !Array.isArray(coordinate)) {
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
45
|
+
const vertex = [coordinate[0], coordinate[1]];
|
|
46
|
+
const isContinuing = drawing?.viewUid === viewUid && drawing?.type === activeTool;
|
|
47
|
+
const nextDrawing = {
|
|
48
|
+
viewUid,
|
|
49
|
+
type: activeTool,
|
|
50
|
+
vertices: isContinuing ? [...drawing.vertices, vertex] : [vertex],
|
|
51
|
+
};
|
|
52
|
+
if (TWO_POINT_SHAPE_TYPES.includes(activeTool) && nextDrawing.vertices.length === 2) {
|
|
53
|
+
return { drawing: null, completedDrawing: nextDrawing };
|
|
54
|
+
}
|
|
55
|
+
return { drawing: nextDrawing };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const createAnnotationEditingStore = () => create(subscribeWithSelector(set => ({
|
|
59
|
+
// The shape type of the active drawing tool, or null.
|
|
60
|
+
activeTool: null,
|
|
61
|
+
// The in-progress shape, as { viewUid, type, vertices }, or null.
|
|
62
|
+
drawing: null,
|
|
63
|
+
// A shape which the user has finished drawing, as { viewUid, type, vertices },
|
|
64
|
+
// which has not yet been committed to the story by the annotation controller.
|
|
65
|
+
completedDrawing: null,
|
|
66
|
+
// The selected shape, as { viewUid, shapeUid }, or null.
|
|
67
|
+
selectedShape: null,
|
|
68
|
+
setActiveTool: activeTool => set({ activeTool, drawing: null }),
|
|
69
|
+
addDrawingVertex: params => set(state => getNextDrawingState(state, params)),
|
|
70
|
+
finishDrawing: () => set(state => ({ drawing: null, completedDrawing: state.drawing })),
|
|
71
|
+
cancelDrawing: () => set({ drawing: null }),
|
|
72
|
+
clearCompletedDrawing: () => set({ completedDrawing: null }),
|
|
73
|
+
setSelectedShape: selectedShape => set({ selectedShape }),
|
|
74
|
+
resetAnnotationEditing: () => set({
|
|
75
|
+
activeTool: null, drawing: null, completedDrawing: null, selectedShape: null,
|
|
76
|
+
}),
|
|
77
|
+
})));
|
|
78
|
+
|
|
79
|
+
const AnnotationEditingStoreContext = createContext(null);
|
|
80
|
+
|
|
81
|
+
export function AnnotationEditingProvider(props) {
|
|
82
|
+
const {
|
|
83
|
+
createStore,
|
|
84
|
+
children,
|
|
85
|
+
} = props;
|
|
86
|
+
|
|
87
|
+
// Reference: https://github.com/pmndrs/zustand/discussions/1180
|
|
88
|
+
const storeRef = useRef();
|
|
89
|
+
if (!storeRef.current) {
|
|
90
|
+
storeRef.current = createStore();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<AnnotationEditingStoreContext.Provider value={storeRef.current}>
|
|
95
|
+
{children}
|
|
96
|
+
</AnnotationEditingStoreContext.Provider>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function useAnnotationEditingStoreApi() {
|
|
101
|
+
const store = useContext(AnnotationEditingStoreContext);
|
|
102
|
+
if (!store) {
|
|
103
|
+
throw new Error('Missing AnnotationEditingProvider');
|
|
104
|
+
}
|
|
105
|
+
return store;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function useAnnotationEditingStore(selector) {
|
|
109
|
+
const store = useAnnotationEditingStoreApi();
|
|
110
|
+
return useStore(store, selector);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function useAnnotationEditingStoreShallow(selector) {
|
|
114
|
+
return useAnnotationEditingStore(useShallow(selector));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Get the annotation editing props for a view which renders annotation shapes
|
|
119
|
+
* (i.e., an AnnotationLayer) and supports drawing new shapes.
|
|
120
|
+
* @param {string} viewUid The uid of the view.
|
|
121
|
+
* @param {boolean} isEditable Whether shapes can currently be drawn in this view,
|
|
122
|
+
* i.e., annotation editing is enabled and an annotation frame is active.
|
|
123
|
+
* @returns {object} The props to pass to the view's DeckGL component.
|
|
124
|
+
*/
|
|
125
|
+
export function useAnnotationEditingForView(viewUid, isEditable) {
|
|
126
|
+
const {
|
|
127
|
+
activeTool, drawing, selectedShape, addDrawingVertex,
|
|
128
|
+
} = useAnnotationEditingStoreShallow(state => ({
|
|
129
|
+
activeTool: state.activeTool,
|
|
130
|
+
drawing: state.drawing,
|
|
131
|
+
selectedShape: state.selectedShape,
|
|
132
|
+
addDrawingVertex: state.addDrawingVertex,
|
|
133
|
+
}));
|
|
134
|
+
const onAnnotationVertexAdd = useCallback(
|
|
135
|
+
coordinate => addDrawingVertex({ viewUid, coordinate }),
|
|
136
|
+
[addDrawingVertex, viewUid],
|
|
137
|
+
);
|
|
138
|
+
const annotationSelectedShapeUid = selectedShape?.viewUid === viewUid
|
|
139
|
+
? selectedShape.shapeUid
|
|
140
|
+
: null;
|
|
141
|
+
if (!isEditable) {
|
|
142
|
+
return {
|
|
143
|
+
annotationActiveTool: null,
|
|
144
|
+
annotationInProgressShape: null,
|
|
145
|
+
annotationSelectedShapeUid,
|
|
146
|
+
onAnnotationVertexAdd: undefined,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
annotationActiveTool: activeTool,
|
|
151
|
+
annotationInProgressShape: drawing?.viewUid === viewUid ? drawing : null,
|
|
152
|
+
annotationSelectedShapeUid,
|
|
153
|
+
onAnnotationVertexAdd,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function toArray(value) {
|
|
158
|
+
if (value === undefined || value === null) return [];
|
|
159
|
+
return Array.isArray(value) ? value : [value];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Serialize a (multi-level) coordination level into its JSON
|
|
164
|
+
* `{ "$CL": [...] }` representation, which is the inverse of the
|
|
165
|
+
* expansion performed when a frame is applied.
|
|
166
|
+
* @param {string} levelType The coordination type of the level (e.g., imageLayer).
|
|
167
|
+
* @param {string[]} levelScopes The scopes of the level.
|
|
168
|
+
* @param {object} coordinationSpace The coordination space.
|
|
169
|
+
* @param {object} scopesBy The view's coordinationScopesBy, after meta-coordination.
|
|
170
|
+
* @returns {object} The `{ "$CL": [...] }` value.
|
|
171
|
+
*/
|
|
172
|
+
function getMultiLevelValue(levelType, levelScopes, coordinationSpace, scopesBy) {
|
|
173
|
+
const secondaryTypes = Object.entries(scopesBy[levelType] || {});
|
|
174
|
+
return {
|
|
175
|
+
[COORDINATION_LEVEL_SENTINEL]: levelScopes.map(levelScope => Object.fromEntries(
|
|
176
|
+
secondaryTypes
|
|
177
|
+
.filter(([, byScope]) => byScope?.[levelScope] !== undefined)
|
|
178
|
+
.map(([secondaryType, byScope]) => {
|
|
179
|
+
const secondaryScope = byScope[levelScope];
|
|
180
|
+
if (Array.isArray(secondaryScope)) {
|
|
181
|
+
return [secondaryType, getMultiLevelValue(
|
|
182
|
+
secondaryType, secondaryScope, coordinationSpace, scopesBy,
|
|
183
|
+
)];
|
|
184
|
+
}
|
|
185
|
+
return [secondaryType, coordinationSpace[secondaryType]?.[secondaryScope]];
|
|
186
|
+
}),
|
|
187
|
+
)),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Get the current coordination values of a view, for the given coordination types,
|
|
193
|
+
* in the form that an annotation frame stores them
|
|
194
|
+
* (i.e., `frame.layout[].coordinationValues`).
|
|
195
|
+
* Single-level values are read from the view (if defined directly)
|
|
196
|
+
* or from the scope that the view is mapped to (after meta-coordination).
|
|
197
|
+
* Multi-level values are serialized as `{ "$CL": [...] }`, including all
|
|
198
|
+
* of the values in the level tree, since applying a multi-level value
|
|
199
|
+
* replaces the view's mapping for that coordination type.
|
|
200
|
+
* @param {object} viewConfig The current view config.
|
|
201
|
+
* @param {string} viewUid The view of interest.
|
|
202
|
+
* @param {string[]} coordinationTypes The coordination types of interest.
|
|
203
|
+
* @returns {object} Mapping from coordination type to value,
|
|
204
|
+
* omitting types for which the view has no value.
|
|
205
|
+
*/
|
|
206
|
+
export function getViewCoordinationValuesForFrame(viewConfig, viewUid, coordinationTypes) {
|
|
207
|
+
const { coordinationSpace = {}, layout = [] } = viewConfig || {};
|
|
208
|
+
const viewObj = layout.find(l => l.uid === viewUid);
|
|
209
|
+
if (!viewObj) {
|
|
210
|
+
return {};
|
|
211
|
+
}
|
|
212
|
+
const viewScopes = viewObj.coordinationScopes || {};
|
|
213
|
+
const scopes = getScopes(
|
|
214
|
+
viewScopes, coordinationSpace[CoordinationType.META_COORDINATION_SCOPES],
|
|
215
|
+
);
|
|
216
|
+
const scopesBy = getScopesBy(
|
|
217
|
+
viewScopes,
|
|
218
|
+
viewObj.coordinationScopesBy || {},
|
|
219
|
+
coordinationSpace[CoordinationType.META_COORDINATION_SCOPES_BY],
|
|
220
|
+
);
|
|
221
|
+
return Object.fromEntries(
|
|
222
|
+
coordinationTypes
|
|
223
|
+
.map((coordinationType) => {
|
|
224
|
+
if (hasCoordinationValue(viewObj.coordinationValues, coordinationType)) {
|
|
225
|
+
return [coordinationType, viewObj.coordinationValues[coordinationType]];
|
|
226
|
+
}
|
|
227
|
+
const scope = scopes[coordinationType];
|
|
228
|
+
if (Array.isArray(scope)) {
|
|
229
|
+
// Only arrays of scopes which define a level tree are multi-level values.
|
|
230
|
+
return scopesBy[coordinationType]
|
|
231
|
+
? [coordinationType, getMultiLevelValue(
|
|
232
|
+
coordinationType, toArray(scope), coordinationSpace, scopesBy,
|
|
233
|
+
)]
|
|
234
|
+
: [coordinationType, undefined];
|
|
235
|
+
}
|
|
236
|
+
return [coordinationType, coordinationSpace[coordinationType]?.[scope]];
|
|
237
|
+
})
|
|
238
|
+
.filter(([, value]) => value !== undefined),
|
|
239
|
+
);
|
|
240
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
getNextDrawingState,
|
|
4
|
+
getViewCoordinationValuesForFrame,
|
|
5
|
+
createAnnotationEditingStore,
|
|
6
|
+
} from './annotation-editing.js';
|
|
7
|
+
import { applyAnnotationFrameToViewConfig } from './hooks.js';
|
|
8
|
+
|
|
9
|
+
describe('getNextDrawingState', () => {
|
|
10
|
+
it('does nothing when no tool is active', () => {
|
|
11
|
+
expect(getNextDrawingState({ activeTool: null, drawing: null }, { viewUid: 'v', coordinate: [1, 2] }))
|
|
12
|
+
.toEqual({});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('completes two-point shapes on the second click', () => {
|
|
16
|
+
const first = getNextDrawingState(
|
|
17
|
+
{ activeTool: 'rectangle', drawing: null },
|
|
18
|
+
{ viewUid: 'v', coordinate: [1, 2, 0] },
|
|
19
|
+
);
|
|
20
|
+
expect(first).toEqual({ drawing: { viewUid: 'v', type: 'rectangle', vertices: [[1, 2]] } });
|
|
21
|
+
const second = getNextDrawingState(
|
|
22
|
+
{ activeTool: 'rectangle', ...first },
|
|
23
|
+
{ viewUid: 'v', coordinate: [3, 4] },
|
|
24
|
+
);
|
|
25
|
+
expect(second).toEqual({
|
|
26
|
+
drawing: null,
|
|
27
|
+
completedDrawing: { viewUid: 'v', type: 'rectangle', vertices: [[1, 2], [3, 4]] },
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('keeps adding vertices to multi-point shapes', () => {
|
|
32
|
+
let state = { activeTool: 'polygon', drawing: null };
|
|
33
|
+
[[0, 0], [1, 0], [1, 1]].forEach((coordinate) => {
|
|
34
|
+
state = { ...state, ...getNextDrawingState(state, { viewUid: 'v', coordinate }) };
|
|
35
|
+
});
|
|
36
|
+
expect(state.drawing.vertices).toEqual([[0, 0], [1, 0], [1, 1]]);
|
|
37
|
+
expect(state.completedDrawing).toBeUndefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('restarts the drawing when a different view is clicked', () => {
|
|
41
|
+
const state = { activeTool: 'polyline', drawing: { viewUid: 'a', type: 'polyline', vertices: [[0, 0]] } };
|
|
42
|
+
expect(getNextDrawingState(state, { viewUid: 'b', coordinate: [5, 5] }))
|
|
43
|
+
.toEqual({ drawing: { viewUid: 'b', type: 'polyline', vertices: [[5, 5]] } });
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('createAnnotationEditingStore', () => {
|
|
48
|
+
it('moves the in-progress shape to completedDrawing when finished', () => {
|
|
49
|
+
const store = createAnnotationEditingStore();
|
|
50
|
+
store.getState().setActiveTool('polyline');
|
|
51
|
+
store.getState().addDrawingVertex({ viewUid: 'v', coordinate: [0, 0] });
|
|
52
|
+
store.getState().addDrawingVertex({ viewUid: 'v', coordinate: [1, 1] });
|
|
53
|
+
store.getState().finishDrawing();
|
|
54
|
+
expect(store.getState().drawing).toBeNull();
|
|
55
|
+
expect(store.getState().completedDrawing).toEqual({ viewUid: 'v', type: 'polyline', vertices: [[0, 0], [1, 1]] });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('clears the in-progress shape when the tool changes', () => {
|
|
59
|
+
const store = createAnnotationEditingStore();
|
|
60
|
+
store.getState().setActiveTool('polygon');
|
|
61
|
+
store.getState().addDrawingVertex({ viewUid: 'v', coordinate: [0, 0] });
|
|
62
|
+
store.getState().setActiveTool('line');
|
|
63
|
+
expect(store.getState().drawing).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
function makeConfig() {
|
|
68
|
+
return {
|
|
69
|
+
coordinationSpace: {
|
|
70
|
+
spatialZoom: { A: -5 },
|
|
71
|
+
spatialTargetX: { A: 100 },
|
|
72
|
+
imageLayer: { L0: '__dummy__' },
|
|
73
|
+
fileUid: { L0: 'img' },
|
|
74
|
+
spatialLayerVisible: { L0: true },
|
|
75
|
+
imageChannel: { C0: '__dummy__', C1: '__dummy__' },
|
|
76
|
+
spatialTargetC: { C0: 0, C1: 1 },
|
|
77
|
+
spatialChannelColor: { C0: [255, 0, 0], C1: [0, 255, 0] },
|
|
78
|
+
metaCoordinationScopes: {
|
|
79
|
+
META: { imageLayer: ['L0'] },
|
|
80
|
+
},
|
|
81
|
+
metaCoordinationScopesBy: {
|
|
82
|
+
META: {
|
|
83
|
+
imageLayer: {
|
|
84
|
+
fileUid: { L0: 'L0' },
|
|
85
|
+
spatialLayerVisible: { L0: 'L0' },
|
|
86
|
+
imageChannel: { L0: ['C0', 'C1'] },
|
|
87
|
+
},
|
|
88
|
+
imageChannel: {
|
|
89
|
+
spatialTargetC: { C0: 'C0', C1: 'C1' },
|
|
90
|
+
spatialChannelColor: { C0: 'C0', C1: 'C1' },
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
layout: [
|
|
96
|
+
{
|
|
97
|
+
uid: 'spatial',
|
|
98
|
+
component: 'spatialBeta',
|
|
99
|
+
coordinationScopes: {
|
|
100
|
+
spatialZoom: 'A',
|
|
101
|
+
spatialTargetX: 'A',
|
|
102
|
+
metaCoordinationScopes: ['META'],
|
|
103
|
+
metaCoordinationScopesBy: ['META'],
|
|
104
|
+
},
|
|
105
|
+
coordinationValues: {
|
|
106
|
+
spatialTargetY: 7,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
describe('getViewCoordinationValuesForFrame', () => {
|
|
114
|
+
it('reads single-level values from scopes and from the view', () => {
|
|
115
|
+
expect(getViewCoordinationValuesForFrame(makeConfig(), 'spatial', ['spatialZoom', 'spatialTargetX', 'spatialTargetY']))
|
|
116
|
+
.toEqual({ spatialZoom: -5, spatialTargetX: 100, spatialTargetY: 7 });
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('omits types without a value, and unknown views', () => {
|
|
120
|
+
expect(getViewCoordinationValuesForFrame(makeConfig(), 'spatial', ['spatialTargetZ'])).toEqual({});
|
|
121
|
+
expect(getViewCoordinationValuesForFrame(makeConfig(), 'missing', ['spatialZoom'])).toEqual({});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('serializes multi-level values as $CL', () => {
|
|
125
|
+
expect(getViewCoordinationValuesForFrame(makeConfig(), 'spatial', ['imageLayer'])).toEqual({
|
|
126
|
+
imageLayer: {
|
|
127
|
+
$CL: [{
|
|
128
|
+
fileUid: 'img',
|
|
129
|
+
spatialLayerVisible: true,
|
|
130
|
+
imageChannel: {
|
|
131
|
+
$CL: [
|
|
132
|
+
{ spatialTargetC: 0, spatialChannelColor: [255, 0, 0] },
|
|
133
|
+
{ spatialTargetC: 1, spatialChannelColor: [0, 255, 0] },
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
}],
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('round-trips through applyAnnotationFrameToViewConfig', () => {
|
|
142
|
+
const config = makeConfig();
|
|
143
|
+
const captured = getViewCoordinationValuesForFrame(config, 'spatial', ['spatialZoom', 'imageLayer']);
|
|
144
|
+
const applied = applyAnnotationFrameToViewConfig(config, 'spatial', captured, 'annotation_s_f_spatial_');
|
|
145
|
+
expect(getViewCoordinationValuesForFrame(applied, 'spatial', ['spatialZoom', 'imageLayer']))
|
|
146
|
+
.toEqual(captured);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -169,6 +169,32 @@ describe('getAnnotationFrameUpdate', () => {
|
|
|
169
169
|
});
|
|
170
170
|
});
|
|
171
171
|
|
|
172
|
+
it('only merges annotationShapes when the same frame has been edited in-place', () => {
|
|
173
|
+
const shapes = [{ uid: 's1', type: 'line', x1: 0, y1: 0, x2: 1, y2: 1 }];
|
|
174
|
+
const editedStory = {
|
|
175
|
+
uid: 'story',
|
|
176
|
+
frames: [{
|
|
177
|
+
uid: 'frame-0',
|
|
178
|
+
layout: [{ uid: 'spatial', coordinationValues: { spatialZoom: -2, annotationShapes: shapes } }],
|
|
179
|
+
}],
|
|
180
|
+
};
|
|
181
|
+
const prevAppliedFrame = { storyUid: 'story', frameUid: 'frame-0' };
|
|
182
|
+
expect(getAnnotationFrameUpdate(editedStory, 0, 0, 'spatial', prevAppliedFrame)).toEqual({
|
|
183
|
+
frameCoordinationValues: { annotationShapes: shapes },
|
|
184
|
+
scopePrefix: '',
|
|
185
|
+
});
|
|
186
|
+
expect(getAnnotationFrameUpdate(story, 0, 0, 'spatial', prevAppliedFrame).frameCoordinationValues)
|
|
187
|
+
.toEqual({ annotationShapes: null });
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('fully merges the frame when a different frame (or story) was previously applied', () => {
|
|
191
|
+
// E.g., the frames were re-ordered, so the frame at the same index has a different uid.
|
|
192
|
+
expect(getAnnotationFrameUpdate(story, 0, 0, 'spatial', { storyUid: 'story', frameUid: 'frame-1' }).frameCoordinationValues)
|
|
193
|
+
.toEqual({ annotationShapes: null, spatialZoom: -2 });
|
|
194
|
+
expect(getAnnotationFrameUpdate(story, 0, 0, 'spatial', { storyUid: 'other', frameUid: 'frame-0' }).frameCoordinationValues)
|
|
195
|
+
.toEqual({ annotationShapes: null, spatialZoom: -2 });
|
|
196
|
+
});
|
|
197
|
+
|
|
172
198
|
it('does nothing when the frame index is (and was) null', () => {
|
|
173
199
|
expect(getAnnotationFrameUpdate(story, undefined, null, 'spatial')).toBeNull();
|
|
174
200
|
expect(getAnnotationFrameUpdate(story, null, null, 'spatial')).toBeNull();
|
package/src/state/hooks.js
CHANGED
|
@@ -1490,16 +1490,22 @@ const ANNOTATION_FRAME_COORDINATION_TYPES = [
|
|
|
1490
1490
|
* so that shapes from a previous frame do not persist.
|
|
1491
1491
|
* - When the frame index becomes null (after a frame was active),
|
|
1492
1492
|
* annotationShapes are cleared.
|
|
1493
|
+
* - When the same frame (of the same story) was already applied, i.e., the story
|
|
1494
|
+
* has been edited in-place via the annotation controller, only annotationShapes
|
|
1495
|
+
* are merged, so that the author's current view state (e.g., zoom/pan)
|
|
1496
|
+
* is not reset by every edit.
|
|
1493
1497
|
* Clearing annotationShapes is the only exception to leaving the
|
|
1494
1498
|
* state untouched for values that a frame does not define.
|
|
1495
1499
|
* @param {object|null} annotationStory The annotation story.
|
|
1496
1500
|
* @param {number|null|undefined} prevFrameIndex The previously-applied frame index.
|
|
1497
1501
|
* @param {number|null} annotationFrameIndex The current frame index.
|
|
1498
1502
|
* @param {string} viewUid The view of interest.
|
|
1503
|
+
* @param {object|null|undefined} prevAppliedFrame The { storyUid, frameUid }
|
|
1504
|
+
* of the previously-applied frame, if any.
|
|
1499
1505
|
* @returns {object|null} The { frameCoordinationValues, scopePrefix } to apply, or null.
|
|
1500
1506
|
*/
|
|
1501
1507
|
export function getAnnotationFrameUpdate(
|
|
1502
|
-
annotationStory, prevFrameIndex, annotationFrameIndex, viewUid,
|
|
1508
|
+
annotationStory, prevFrameIndex, annotationFrameIndex, viewUid, prevAppliedFrame,
|
|
1503
1509
|
) {
|
|
1504
1510
|
if (typeof annotationFrameIndex !== 'number') {
|
|
1505
1511
|
if (typeof prevFrameIndex === 'number') {
|
|
@@ -1518,6 +1524,17 @@ export function getAnnotationFrameUpdate(
|
|
|
1518
1524
|
const frameCoordinationValues = getAnnotationFrameCoordinationValues(
|
|
1519
1525
|
annotationStory, annotationFrameIndex, viewUid,
|
|
1520
1526
|
);
|
|
1527
|
+
const isSameFrame = prevAppliedFrame?.storyUid === annotationStory.uid
|
|
1528
|
+
&& prevAppliedFrame?.frameUid === frame.uid;
|
|
1529
|
+
if (isSameFrame) {
|
|
1530
|
+
return {
|
|
1531
|
+
frameCoordinationValues: {
|
|
1532
|
+
[CoordinationType.ANNOTATION_SHAPES]:
|
|
1533
|
+
frameCoordinationValues?.[CoordinationType.ANNOTATION_SHAPES] ?? null,
|
|
1534
|
+
},
|
|
1535
|
+
scopePrefix: '',
|
|
1536
|
+
};
|
|
1537
|
+
}
|
|
1521
1538
|
return {
|
|
1522
1539
|
frameCoordinationValues: {
|
|
1523
1540
|
[CoordinationType.ANNOTATION_SHAPES]: null,
|
|
@@ -1553,12 +1570,21 @@ export function useAnnotationFrameCoordination(viewUid, coordinationScopes, coor
|
|
|
1553
1570
|
);
|
|
1554
1571
|
// Track the previously-applied frame index, to detect when the frame index becomes null.
|
|
1555
1572
|
const prevFrameIndexRef = useRef(undefined);
|
|
1573
|
+
// Track the previously-applied frame, to detect in-place edits of the story.
|
|
1574
|
+
const prevAppliedFrameRef = useRef(null);
|
|
1556
1575
|
|
|
1557
1576
|
useEffect(() => {
|
|
1558
1577
|
const update = getAnnotationFrameUpdate(
|
|
1559
1578
|
annotationStory, prevFrameIndexRef.current, annotationFrameIndex, viewUid,
|
|
1579
|
+
prevAppliedFrameRef.current,
|
|
1560
1580
|
);
|
|
1561
1581
|
prevFrameIndexRef.current = annotationFrameIndex;
|
|
1582
|
+
const frame = typeof annotationFrameIndex === 'number'
|
|
1583
|
+
? annotationStory?.frames?.[annotationFrameIndex]
|
|
1584
|
+
: null;
|
|
1585
|
+
prevAppliedFrameRef.current = frame
|
|
1586
|
+
? { storyUid: annotationStory.uid, frameUid: frame.uid }
|
|
1587
|
+
: null;
|
|
1562
1588
|
if (update) {
|
|
1563
1589
|
applyAnnotationFrameCoordination({ viewUid, ...update });
|
|
1564
1590
|
}
|