-
Notifications
You must be signed in to change notification settings - Fork 255
/
OpenSeadragonViewer.js
350 lines (314 loc) · 10.1 KB
/
OpenSeadragonViewer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import isEqual from 'lodash/isEqual';
import OpenSeadragon from 'openseadragon';
import classNames from 'classnames';
import ns from '../config/css-ns';
import OpenSeadragonCanvasOverlay from '../lib/OpenSeadragonCanvasOverlay';
import CanvasWorld from '../lib/CanvasWorld';
/**
* Represents a OpenSeadragonViewer in the mirador workspace. Responsible for mounting
* and rendering OSD.
*/
export class OpenSeadragonViewer extends Component {
/**
* annotationsMatch - compares previous annotations to current to determine
* whether to add a new updateCanvas method to draw annotations
* @param {Array} currentAnnotations
* @param {Array} prevAnnotations
* @return {Boolean}
*/
static annotationsMatch(currentAnnotations, prevAnnotations) {
if (currentAnnotations.length === 0 && prevAnnotations.length === 0) return true;
if (currentAnnotations.length !== prevAnnotations.length) return false;
return currentAnnotations.every((annotation, index) => {
const newIds = annotation.resources.map(r => r.id);
const prevIds = prevAnnotations[index].resources.map(r => r.id);
if (newIds.length === 0 && prevIds.length === 0) return true;
if (newIds.length !== prevIds.length) return false;
if ((annotation.id === prevAnnotations[index].id) && (isEqual(newIds, prevIds))) {
return true;
}
return false;
});
}
/**
* @param {Object} props
*/
constructor(props) {
super(props);
this.viewer = null;
this.osdCanvasOverlay = null;
// An initial value for the updateCanvas method
this.updateCanvas = () => {};
this.ref = React.createRef();
this.onUpdateViewport = this.onUpdateViewport.bind(this);
this.onViewportChange = this.onViewportChange.bind(this);
this.zoomToWorld = this.zoomToWorld.bind(this);
this.osdUpdating = false;
}
/**
* React lifecycle event
*/
componentDidMount() {
const { osdConfig, viewer } = this.props;
if (!this.ref.current) {
return;
}
this.viewer = new OpenSeadragon({
id: this.ref.current.id,
...osdConfig,
});
this.osdCanvasOverlay = new OpenSeadragonCanvasOverlay(this.viewer);
this.viewer.addHandler('update-viewport', this.onUpdateViewport);
// Set a flag when OSD starts animating (so that viewer updates are not used)
this.viewer.addHandler('animation-start', () => {
this.osdUpdating = true;
});
this.viewer.addHandler('animation-finish', this.onViewportChange);
this.viewer.addHandler('animation-finish', () => {
this.osdUpdating = false;
});
this.updateCanvas = this.canvasUpdateCallback();
if (viewer) {
this.viewer.viewport.panTo(viewer, true);
this.viewer.viewport.zoomTo(viewer.zoom, viewer, true);
}
this.addAllTileSources();
}
/**
* When the tileSources change, make sure to close the OSD viewer.
* When the annotations change, reset the updateCanvas method to make sure
* they are added.
* When the viewport state changes, pan or zoom the OSD viewer as appropriate
*/
componentDidUpdate(prevProps) {
const {
viewer,
highlightedAnnotations, selectedAnnotations,
searchAnnotations, selectedContentSearchAnnotations,
} = this.props;
const highlightsUpdated = !OpenSeadragonViewer.annotationsMatch(
highlightedAnnotations, prevProps.highlightedAnnotations,
);
const selectionsUpdated = !OpenSeadragonViewer.annotationsMatch(
selectedAnnotations, prevProps.selectedAnnotations,
);
const searchAnnotationsUpdated = !OpenSeadragonViewer.annotationsMatch(
searchAnnotations, prevProps.searchAnnotations,
);
const selectedContentSearchAnnotationsUpdated = !OpenSeadragonViewer.annotationsMatch(
selectedContentSearchAnnotations, prevProps.selectedContentSearchAnnotations,
);
if (
searchAnnotationsUpdated
|| selectedContentSearchAnnotationsUpdated
|| highlightsUpdated
|| selectionsUpdated
) {
this.updateCanvas = this.canvasUpdateCallback();
this.viewer.forceRedraw();
}
if (!this.tileSourcesMatch(prevProps.tileSources)) {
this.viewer.close();
this.addAllTileSources();
} else if (viewer && !this.osdUpdating) {
const { viewport } = this.viewer;
if (viewer.x !== viewport.centerSpringX.target.value
|| viewer.y !== viewport.centerSpringY.target.value) {
this.viewer.viewport.panTo(viewer, false);
}
if (viewer.zoom !== viewport.zoomSpring.target.value) {
this.viewer.viewport.zoomTo(viewer.zoom, viewer, false);
}
}
}
/**
*/
componentWillUnmount() {
this.viewer.removeAllHandlers();
}
/**
* onUpdateViewport - fires during OpenSeadragon render method.
*/
onUpdateViewport(event) {
this.updateCanvas();
}
/**
* Forward OSD state to redux
*/
onViewportChange(event) {
const { updateViewport, windowId } = this.props;
const { viewport } = event.eventSource;
updateViewport(windowId, {
x: Math.round(viewport.centerSpringX.target.value),
y: Math.round(viewport.centerSpringY.target.value),
zoom: viewport.zoomSpring.target.value,
});
}
/** */
canvasUpdateCallback() {
return () => {
this.osdCanvasOverlay.clear();
this.osdCanvasOverlay.resize();
this.osdCanvasOverlay.canvasUpdate(this.renderAnnotations.bind(this));
};
}
/**
* annotationsToContext - converts anontations to a canvas context
*/
annotationsToContext(annotations, color = 'yellow') {
const { canvasWorld } = this.props;
const context = this.osdCanvasOverlay.context2d;
const zoom = this.viewer.viewport.getZoom(true);
const width = canvasWorld.worldBounds()[2];
annotations.forEach((annotation) => {
annotation.resources.forEach((resource) => {
if (!canvasWorld.canvasIds.includes(resource.targetId)) return;
const offset = canvasWorld.offsetByCanvas(resource.targetId);
const fragment = resource.fragmentSelector;
fragment[0] += offset.x;
context.strokeStyle = color;
context.lineWidth = Math.ceil(10 / (zoom * width));
context.strokeRect(...fragment);
});
});
}
/** */
addAllTileSources() {
const { tileSources } = this.props;
Promise.all(
tileSources.map((tileSource, i) => this.addTileSource(tileSource, i)),
).then(() => {
if (tileSources[0]) {
this.zoomToWorld();
}
});
}
/**
*/
addTileSource(tileSource, i = 0) {
const { canvasWorld } = this.props;
return new Promise((resolve, reject) => {
if (!this.viewer) {
return;
}
this.viewer.addTiledImage({
error: event => reject(event),
fitBounds: new OpenSeadragon.Rect(
...canvasWorld.canvasToWorldCoordinates(i),
),
success: event => resolve(event),
tileSource,
});
});
}
/**
*/
fitBounds(x, y, w, h, immediately = true) {
this.viewer.viewport.fitBounds(
new OpenSeadragon.Rect(x, y, w, h),
immediately,
);
}
/**
* tileSourcesMatch - compares previous tileSources to current to determine
* whether a refresh of the OSD viewer is needed.
* @param {Array} prevTileSources
* @return {Boolean}
*/
tileSourcesMatch(prevTileSources) {
const { tileSources } = this.props;
if (tileSources.length === 0 && prevTileSources.length === 0) return true;
return tileSources.some((tileSource, index) => {
if (!prevTileSources[index]) {
return false;
}
if (tileSource['@id'] === prevTileSources[index]['@id']) {
return true;
}
return false;
});
}
/**
* zoomToWorld - zooms the viewer to the extent of the canvas world
*/
zoomToWorld(immediately = true) {
const { canvasWorld } = this.props;
this.fitBounds(...canvasWorld.worldBounds(), immediately);
}
/** */
renderAnnotations() {
const {
searchAnnotations,
selectedContentSearchAnnotations,
highlightedAnnotations,
selectedAnnotations,
palette,
} = this.props;
this.annotationsToContext(searchAnnotations, palette.highlights.secondary);
this.annotationsToContext(
selectedContentSearchAnnotations,
palette.highlights.primary,
);
this.annotationsToContext(highlightedAnnotations, palette.highlights.secondary);
this.annotationsToContext(selectedAnnotations, palette.highlights.primary);
}
/**
* Renders things
*/
render() {
const {
children, classes, label, t, windowId,
} = this.props;
const enhancedChildren = React.Children.map(children, child => (
React.cloneElement(
child,
{
zoomToWorld: this.zoomToWorld,
},
)
));
return (
<>
<section
className={classNames(ns('osd-container'), classes.osdContainer)}
id={`${windowId}-osd`}
ref={this.ref}
aria-label={t('item', { label })}
>
{ enhancedChildren }
</section>
</>
);
}
}
OpenSeadragonViewer.defaultProps = {
children: null,
highlightedAnnotations: [],
label: null,
osdConfig: {},
palette: {},
searchAnnotations: [],
selectedAnnotations: [],
selectedContentSearchAnnotations: [],
tileSources: [],
viewer: null,
};
OpenSeadragonViewer.propTypes = {
canvasWorld: PropTypes.instanceOf(CanvasWorld).isRequired,
children: PropTypes.node,
classes: PropTypes.objectOf(PropTypes.string).isRequired,
highlightedAnnotations: PropTypes.arrayOf(PropTypes.object),
label: PropTypes.string,
osdConfig: PropTypes.object, // eslint-disable-line react/forbid-prop-types
palette: PropTypes.object, // eslint-disable-line react/forbid-prop-types
searchAnnotations: PropTypes.arrayOf(PropTypes.object),
selectedAnnotations: PropTypes.arrayOf(PropTypes.object),
selectedContentSearchAnnotations: PropTypes.arrayOf(PropTypes.object),
t: PropTypes.func.isRequired,
tileSources: PropTypes.arrayOf(PropTypes.object),
updateViewport: PropTypes.func.isRequired,
viewer: PropTypes.object, // eslint-disable-line react/forbid-prop-types
windowId: PropTypes.string.isRequired,
};