-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathOpenSeadragonViewer.js
More file actions
157 lines (140 loc) · 4.96 KB
/
OpenSeadragonViewer.js
File metadata and controls
157 lines (140 loc) · 4.96 KB
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
import {
useRef, Children, cloneElement, useCallback, useState, useEffect, useContext,
} from 'react';
import PropTypes from 'prop-types';
import { styled } from '@mui/material/styles';
import OpenSeadragon from 'openseadragon';
import classNames from 'classnames';
import ns from '../config/css-ns';
import AnnotationsOverlay from '../containers/AnnotationsOverlay';
import CanvasWorld from '../lib/CanvasWorld';
import { PluginHook } from './PluginHook';
import { OSDReferences } from '../plugins/OSDReferences';
import OpenSeadragonComponent from './OpenSeadragonComponent';
import TileSource from './OpenSeadragonTileSource';
import OpenSeadragonWorkspaceReferencesContext from '../contexts/OpenSeadragonWorkspaceReferencesContext';
const StyledSection = styled('section')({
cursor: 'grab',
flex: 1,
position: 'relative',
});
/**
* Represents a OpenSeadragonViewer in the mirador workspace. Responsible for mounting
* and rendering OSD.
*/
export function OpenSeadragonViewer({
children = null, label = null, t, windowId, osdConfig = {}, viewerConfig = null,
drawAnnotations = false, infoResponses = [], canvasWorld, nonTiledImages = [], updateViewport,
}) {
const apiRef = useRef();
const workspaceReferencesContext = useContext(OpenSeadragonWorkspaceReferencesContext);
const [viewer, setViewer] = useState(null);
const onViewportChange = useCallback(({
flip, rotation, x, y, zoom,
}) => {
updateViewport(windowId, {
flip,
rotation,
x,
y,
zoom,
});
}, [updateViewport, windowId]);
const zoomToWorld = useCallback((immediately = true) => {
if (!apiRef.current?.viewport) return;
apiRef.current.viewport.fitBounds(
new OpenSeadragon.Rect(...canvasWorld.worldBounds()),
immediately,
);
}, [canvasWorld, apiRef]);
useEffect(() => {
OSDReferences.set(windowId, apiRef);
}, [apiRef, windowId]);
useEffect(() => {
apiRef.current = viewer;
workspaceReferencesContext.current[windowId] = apiRef;
}, [apiRef, windowId, viewer, workspaceReferencesContext]);
const enhancedChildren = Children.map(children, child => (
cloneElement(
child,
{
zoomToWorld,
},
)
));
const pluginProps = {
canvasWorld,
drawAnnotations,
infoResponses,
label,
nonTiledImages,
osdConfig,
t,
updateViewport,
viewerConfig,
windowId,
};
return (
<OpenSeadragonComponent
className={classNames(ns('osd-container'))}
Container={StyledSection}
osdConfig={osdConfig}
viewerConfig={viewerConfig || { bounds: canvasWorld.worldBounds() }}
onUpdateViewport={onViewportChange}
setViewer={setViewer}
aria-label={t('item', { label })}
aria-live="polite"
>
{ infoResponses.map((infoResponse) => {
const contentResource = canvasWorld.contentResource(infoResponse.id);
if (!contentResource) return null;
const fitBounds = canvasWorld.contentResourceToWorldCoordinates(contentResource);
const index = canvasWorld.layerIndexOfImageResource(contentResource);
const opacity = canvasWorld.layerOpacityOfImageResource(contentResource);
return (
<TileSource
key={infoResponse.id}
tileSource={infoResponse.json}
fitBounds={fitBounds}
index={index}
opacity={opacity}
/>
);
})}
{ nonTiledImages.map((contentResource) => {
const type = contentResource.getProperty('type');
const format = contentResource.getProperty('format') || '';
if (!(type === 'Image' || type === 'dctypes:Image' || format.startsWith('image/'))) return null;
const fitBounds = canvasWorld.contentResourceToWorldCoordinates(contentResource);
const index = canvasWorld.layerIndexOfImageResource(contentResource);
const opacity = canvasWorld.layerOpacityOfImageResource(contentResource);
return (
<TileSource
key={contentResource.id}
url={contentResource.id}
fitBounds={fitBounds}
index={index}
opacity={opacity}
/>
);
})}
{ drawAnnotations
&& <AnnotationsOverlay viewer={viewer} windowId={windowId} /> }
{ enhancedChildren }
<PluginHook viewer={viewer} {...pluginProps} />
</OpenSeadragonComponent>
);
}
OpenSeadragonViewer.propTypes = {
canvasWorld: PropTypes.instanceOf(CanvasWorld).isRequired,
children: PropTypes.node,
drawAnnotations: PropTypes.bool,
infoResponses: PropTypes.arrayOf(PropTypes.object), // eslint-disable-line react/forbid-prop-types
label: PropTypes.string,
nonTiledImages: PropTypes.array, // eslint-disable-line react/forbid-prop-types
osdConfig: PropTypes.object, // eslint-disable-line react/forbid-prop-types
t: PropTypes.func.isRequired,
updateViewport: PropTypes.func.isRequired,
viewerConfig: PropTypes.object, // eslint-disable-line react/forbid-prop-types
windowId: PropTypes.string.isRequired,
};