-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathThumbnailNavigation.js
More file actions
224 lines (207 loc) · 6.22 KB
/
ThumbnailNavigation.js
File metadata and controls
224 lines (207 loc) · 6.22 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
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
import { useCallback, useRef, useEffect } from 'react';
import PropTypes from 'prop-types';
import Paper from '@mui/material/Paper';
import AutoSizer from 'react-virtualized-auto-sizer';
import { VariableSizeList as List } from 'react-window';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import CanvasWorld from '../lib/CanvasWorld';
import ThumbnailCanvasGrouping from '../containers/ThumbnailCanvasGrouping';
import ns from '../config/css-ns';
/**
*/
export function ThumbnailNavigation({
canvasGroupings, canvasIndex, hasNextCanvas = false, hasPreviousCanvas = false, position,
setNextCanvas = () => {}, setPreviousCanvas = () => {}, thumbnailNavigation, view = undefined, viewingDirection = '', windowId,
}) {
const { t } = useTranslation();
const scrollbarSize = 15;
const spacing = 8; // 2 * (2px margin + 2px border + 2px padding + 2px padding)
const gridRef = useRef();
const previousView = useRef(view);
useEffect(() => {
if (previousView.current !== view && position !== 'off') {
previousView.current = view;
gridRef.current.resetAfterIndex(0);
}
}, [view, position]);
useEffect(() => {
let index = canvasIndex;
if (view === 'book') index = Math.ceil(index / 2);
gridRef.current?.scrollToItem(index, 'center');
}, [canvasIndex, view]);
/** */
const handleKeyDown = (e) => {
let nextKey = 'ArrowRight';
let previousKey = 'ArrowLeft';
if (position === 'far-right') {
nextKey = 'ArrowDown';
previousKey = 'ArrowUp';
}
switch (e.key) {
case nextKey:
nextCanvas();
break;
case previousKey:
previousCanvas();
break;
default:
break;
}
};
/**
* When on right, row height
* When on bottom, column width
*/
const calculateScaledSize = (index) => {
const canvases = canvasGroupings[index];
if (!canvases) return thumbnailNavigation.width + spacing;
const world = new CanvasWorld(canvases);
const bounds = world.worldBounds();
switch (position) {
case 'far-right': {
const calc = Math.floor(
calculatingWidth(canvases.length) * bounds[3] / bounds[2],
);
if (!Number.isInteger(calc)) return thumbnailNavigation.width + spacing;
return calc + spacing;
}
// Default case bottom
default: {
if (bounds[3] === 0) return thumbnailNavigation.width + spacing;
const calc = Math.ceil(
(thumbnailNavigation.height - scrollbarSize - spacing - 4)
* bounds[2] / bounds[3],
);
return calc;
}
}
};
/** */
const calculateForItems = (n) => {
let total = 0;
for (let i = 0; i < n; i += 1) {
total += calculateScaledSize(i);
}
return total + spacing;
};
/** */
const calculatingWidth = (canvasesLength) => {
if (canvasesLength === 1) {
return thumbnailNavigation.width;
}
return thumbnailNavigation.width * 2;
};
/** */
const style = useCallback(() => {
const width = view === 'book' ? thumbnailNavigation.width * 2 : thumbnailNavigation.width;
switch (position) {
case 'far-right':
return {
height: '100%',
minHeight: 0,
width: `${width + scrollbarSize + spacing}px`,
};
// Default case bottom
default:
return {
height: `${thumbnailNavigation.height}px`,
width: '100%',
};
}
}, [position, thumbnailNavigation, view]);
/** */
const areaHeight = (height) => {
switch (position) {
case 'far-right':
return height;
// Default case bottom
default:
return thumbnailNavigation.height;
}
};
/** */
const itemCount = () => canvasGroupings.length;
/**
*/
const nextCanvas = () => {
if (hasNextCanvas) setNextCanvas();
};
/**
*/
const previousCanvas = () => {
if (hasPreviousCanvas) setPreviousCanvas();
};
if (position === 'off') {
return null;
}
const htmlDir = viewingDirection === 'right-to-left' ? 'rtl' : 'ltr';
const itemData = {
canvasGroupings,
height: thumbnailNavigation.height - spacing - scrollbarSize,
position,
windowId,
};
return (
<Paper
className={classNames(
ns('thumb-navigation'),
)}
sx={{
'&:focus': {
boxShadow: 0,
outline: 0,
},
}}
aria-label={t('thumbnailNavigation')}
square
elevation={0}
style={style()}
tabIndex={0}
onKeyDown={handleKeyDown}
role="grid"
>
<div role="row" style={{ height: '100%', width: '100%' }}>
{ canvasGroupings.length > 0 && (
<AutoSizer
defaultHeight={100}
defaultWidth={400}
>
{({ height, width }) => {
const calculatedHeight = (position === 'far-bottom') ? areaHeight(height) : calculateForItems(thumbnailNavigation.count ?? 1);
const calculatedWidth = (position === 'far-bottom') ? calculateForItems(thumbnailNavigation.count ?? 1) : width;
const layout = (position === 'far-bottom') ? 'horizontal' : 'vertical';
return (
<List
direction={htmlDir}
height={thumbnailNavigation.limit ? calculatedHeight : areaHeight(height)}
itemCount={itemCount()}
itemSize={calculateScaledSize}
width={thumbnailNavigation.limit ? calculatedWidth : width}
layout={layout}
itemData={itemData}
ref={gridRef}
>
{ThumbnailCanvasGrouping}
</List>
);
} }
</AutoSizer>
)}
</div>
</Paper>
);
}
ThumbnailNavigation.propTypes = {
canvasGroupings: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
canvasIndex: PropTypes.number.isRequired,
hasNextCanvas: PropTypes.bool,
hasPreviousCanvas: PropTypes.bool,
position: PropTypes.string.isRequired,
setNextCanvas: PropTypes.func,
setPreviousCanvas: PropTypes.func,
thumbnailNavigation: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
view: PropTypes.string,
viewingDirection: PropTypes.string,
windowId: PropTypes.string.isRequired,
};