@olenbetong/synergi-react 4.1.0 → 4.1.1
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/es/components/PdfViewer/PdfViewer.js +22 -3
- package/package.json +7 -7
- package/src/chrome/AppShell/index.tsx +29 -29
- package/src/chrome/openReport.ts +34 -34
- package/src/colorScheme.ts +81 -81
- package/src/components/DateNavigator/index.tsx +186 -186
- package/src/components/FilePond.tsx +312 -312
- package/src/components/ImageEditor/ImageEditor.tsx +343 -343
- package/src/components/ImageEditor/index.ts +9 -9
- package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
- package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +403 -403
- package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
- package/src/components/ImageEditor/useEdgeDetection.ts +127 -127
- package/src/components/ImageEditor/useImageHistory.ts +197 -197
- package/src/components/ImageEditor/useOpenCV.ts +184 -184
- package/src/components/LinkedCardList/index.scss +95 -95
- package/src/components/MergeProgressDialog/index.tsx +249 -249
- package/src/components/PageBanner/index.tsx +80 -80
- package/src/components/PdfViewer/PdfViewer.tsx +158 -139
- package/src/components/ProgressBar/index.scss +96 -96
- package/src/components/SplitContainer/index.tsx +333 -333
- package/src/components/Toolbar/index.tsx +212 -212
- package/src/designsystemet-web-types.d.ts +24 -24
- package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
- package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
- package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
- package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
- package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
- package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
- package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
- package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
- package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
- package/src/elements/ElementLookup.tsx +123 -123
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -32
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -118
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -103
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -95
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -118
- package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -21
- package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +90 -90
- package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +280 -280
- package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
- package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
- package/src/elements/ElementLookupDialog/index.tsx +226 -226
- package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
- package/src/elements/ProjectSelect.tsx +130 -130
- package/src/elements/index.ts +15 -15
- package/src/filter/index.ts +34 -34
- package/src/i18n.ts +13 -13
- package/src/index.ts +77 -77
- package/src/mui/MuiThemeProvider.tsx +174 -174
- package/src/mui/index.ts +1 -1
- package/src/test/setup.ts +8 -8
|
@@ -1,333 +1,333 @@
|
|
|
1
|
-
import "./index.scss";
|
|
2
|
-
|
|
3
|
-
import { createSettingsStore } from "@olenbetong/appframe-react";
|
|
4
|
-
|
|
5
|
-
import clsx from "clsx";
|
|
6
|
-
import type React from "react";
|
|
7
|
-
import { Children, forwardRef, useEffect, useRef, useState } from "react";
|
|
8
|
-
|
|
9
|
-
const article = globalThis.af?.article?.id ?? globalThis.window?.location.pathname.substring(1).split("/")[0];
|
|
10
|
-
const settingsStore = createSettingsStore(`AfSplitContainers.${globalThis.af?.userSession?.login ?? "anonymous"}`);
|
|
11
|
-
|
|
12
|
-
type SplitContainerSettings = {
|
|
13
|
-
layoutVersion: string | number;
|
|
14
|
-
fixedSize: number;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const settingsCache: Record<string, SplitContainerSettings> = {};
|
|
18
|
-
|
|
19
|
-
async function persistState(key: string, layoutVersion: string | number, fixedSize: number | null | undefined) {
|
|
20
|
-
if (fixedSize != null) {
|
|
21
|
-
let settings = {
|
|
22
|
-
layoutVersion,
|
|
23
|
-
fixedSize,
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
settingsCache[key] = settings;
|
|
27
|
-
await settingsStore.setItem<SplitContainerSettings>(key, settings);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function getSettings(key: string, layoutVersion: string | number): Promise<number | null> {
|
|
32
|
-
let settings = settingsCache[key] ?? (await settingsStore.getItem<SplitContainerSettings>(key));
|
|
33
|
-
|
|
34
|
-
if (settings && settings.layoutVersion === layoutVersion) {
|
|
35
|
-
return settings.fixedSize;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Load all article grid layouts even if the grid is not mounted yet.
|
|
42
|
-
// This ways the persisted state can be restored faster after the initial
|
|
43
|
-
// page load.
|
|
44
|
-
settingsStore.keys().then((keys) =>
|
|
45
|
-
Promise.allSettled(
|
|
46
|
-
keys
|
|
47
|
-
.filter((key) => key.startsWith(article))
|
|
48
|
-
.map((key) =>
|
|
49
|
-
settingsStore.getItem<SplitContainerSettings>(key).then((settings) => {
|
|
50
|
-
if (settings && !settingsCache[key]) {
|
|
51
|
-
settingsCache[key] = settings;
|
|
52
|
-
}
|
|
53
|
-
}),
|
|
54
|
-
),
|
|
55
|
-
),
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
type DividerProps = {
|
|
59
|
-
className?: string;
|
|
60
|
-
direction: "horizontal" | "vertical";
|
|
61
|
-
isDragging: boolean;
|
|
62
|
-
min: number;
|
|
63
|
-
max: number;
|
|
64
|
-
size: number;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const Divider = forwardRef<HTMLDivElement, DividerProps>(function Divider(props: DividerProps, ref) {
|
|
68
|
-
return (
|
|
69
|
-
// oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- NO!
|
|
70
|
-
<div
|
|
71
|
-
className={clsx("ObSplitContainer-divider", props.className)}
|
|
72
|
-
tabIndex={0}
|
|
73
|
-
ref={ref}
|
|
74
|
-
role="separator"
|
|
75
|
-
aria-orientation={props.direction}
|
|
76
|
-
aria-valuemin={props.min}
|
|
77
|
-
aria-valuemax={props.max}
|
|
78
|
-
aria-valuenow={props.size}
|
|
79
|
-
>
|
|
80
|
-
<span>•••</span>
|
|
81
|
-
</div>
|
|
82
|
-
);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
type SplitRootProps = React.HTMLProps<HTMLDivElement> & {
|
|
86
|
-
className?: string;
|
|
87
|
-
children: React.ReactNode;
|
|
88
|
-
direction: "horizontal" | "vertical";
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
function SplitRoot({ className, direction, ...props }: SplitRootProps) {
|
|
92
|
-
return <div className={clsx(className, "ObSplitContainer-root", direction)} {...props} />;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
type SplitPanelProps = {
|
|
96
|
-
children: React.ReactNode;
|
|
97
|
-
className?: string;
|
|
98
|
-
style?: React.CSSProperties;
|
|
99
|
-
} & (
|
|
100
|
-
| {
|
|
101
|
-
fixed: true;
|
|
102
|
-
size: number;
|
|
103
|
-
}
|
|
104
|
-
| {
|
|
105
|
-
fixed: false;
|
|
106
|
-
size?: number;
|
|
107
|
-
}
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
function SplitPanel(props: SplitPanelProps) {
|
|
111
|
-
let style = {
|
|
112
|
-
flex: props.fixed ? `0 0 ${Math.floor(props.size)}px` : "1 1 100%",
|
|
113
|
-
...props.style,
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
return (
|
|
117
|
-
<div className={clsx(props.className, "ObSplitContainer-panel")} style={style}>
|
|
118
|
-
{props.children}
|
|
119
|
-
</div>
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export type SplitContainerProps = {
|
|
124
|
-
className?: string;
|
|
125
|
-
panelClassName?: string;
|
|
126
|
-
children: React.ReactNode[];
|
|
127
|
-
direction?: "horizontal" | "vertical";
|
|
128
|
-
fixedPanel?: 1 | 2;
|
|
129
|
-
defaultFixedSize?: number;
|
|
130
|
-
min?: number;
|
|
131
|
-
max?: number;
|
|
132
|
-
/**
|
|
133
|
-
* When the fixed size is persisted, changing the layoutVersion will force clients
|
|
134
|
-
* to reset to the default value.
|
|
135
|
-
*/
|
|
136
|
-
layoutVersion?: string | number;
|
|
137
|
-
/**
|
|
138
|
-
* If you set an id, the fixed size will be persisted, and restored after page load
|
|
139
|
-
*/
|
|
140
|
-
id?: string;
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
export function SplitContainer({
|
|
144
|
-
className,
|
|
145
|
-
children,
|
|
146
|
-
direction = "vertical",
|
|
147
|
-
fixedPanel = 1,
|
|
148
|
-
panelClassName,
|
|
149
|
-
defaultFixedSize = 400,
|
|
150
|
-
layoutVersion = 1,
|
|
151
|
-
min = 0,
|
|
152
|
-
max,
|
|
153
|
-
id,
|
|
154
|
-
}: SplitContainerProps) {
|
|
155
|
-
let [fixedSize, setFixedSize] = useState<number>(defaultFixedSize);
|
|
156
|
-
let [isDragging, setIsDragging] = useState(false);
|
|
157
|
-
let dividerRef = useRef<HTMLDivElement>(null);
|
|
158
|
-
|
|
159
|
-
if (Children.count(children) !== 2) {
|
|
160
|
-
throw Error("SplitContainer requires exactly 2 children");
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
let containerRef = useRef<HTMLDivElement>(null);
|
|
164
|
-
let [containerSize, setContainerSize] = useState<number>(0);
|
|
165
|
-
|
|
166
|
-
useEffect(() => {
|
|
167
|
-
if (!containerRef.current) return;
|
|
168
|
-
|
|
169
|
-
let resizeObserver = new ResizeObserver((entries) => {
|
|
170
|
-
for (let entry of entries) {
|
|
171
|
-
if (entry.contentRect) {
|
|
172
|
-
setContainerSize(direction === "horizontal" ? entry.contentRect.width : entry.contentRect.height);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
resizeObserver.observe(containerRef.current);
|
|
178
|
-
return () => resizeObserver.disconnect();
|
|
179
|
-
}, [direction]);
|
|
180
|
-
|
|
181
|
-
useEffect(() => {
|
|
182
|
-
if (id) {
|
|
183
|
-
let key = `${article}.${id}`;
|
|
184
|
-
|
|
185
|
-
getSettings(key, layoutVersion).then((data: number | null) => {
|
|
186
|
-
if (data) {
|
|
187
|
-
// Persisted state exists, and matches the current layout version,
|
|
188
|
-
// so restore the state.
|
|
189
|
-
setFixedSize(data);
|
|
190
|
-
|
|
191
|
-
return Promise.resolve();
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
}, [id, layoutVersion]);
|
|
196
|
-
|
|
197
|
-
let childrenArray = Children.toArray(children);
|
|
198
|
-
|
|
199
|
-
useEffect(() => {
|
|
200
|
-
let divider = dividerRef.current;
|
|
201
|
-
if (divider) {
|
|
202
|
-
let initialPosition: { clientX: number; clientY: number; size: number } = {
|
|
203
|
-
clientX: 0,
|
|
204
|
-
clientY: 0,
|
|
205
|
-
size: 0,
|
|
206
|
-
};
|
|
207
|
-
let movingPanel: HTMLElement = (
|
|
208
|
-
fixedPanel === 1 ? divider.previousElementSibling : divider.nextElementSibling
|
|
209
|
-
) as HTMLElement;
|
|
210
|
-
|
|
211
|
-
function clampSize(size: number) {
|
|
212
|
-
let adjustedContainerSize = containerSize - 10; // make sure divider will not be stuck outside the container
|
|
213
|
-
let maxSize = Math.min(max ?? adjustedContainerSize, adjustedContainerSize);
|
|
214
|
-
if (size < min) {
|
|
215
|
-
size = min;
|
|
216
|
-
} else if (size > maxSize) {
|
|
217
|
-
size = maxSize;
|
|
218
|
-
}
|
|
219
|
-
return size;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function getNewSize(event: PointerEvent) {
|
|
223
|
-
let deltaSize =
|
|
224
|
-
direction === "horizontal"
|
|
225
|
-
? event.clientX - initialPosition.clientX
|
|
226
|
-
: event.clientY - initialPosition.clientY;
|
|
227
|
-
|
|
228
|
-
deltaSize *= fixedPanel === 1 ? -1 : 1;
|
|
229
|
-
|
|
230
|
-
return clampSize(initialPosition.size - deltaSize);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
function handleFixedSizeChanged(size: React.SetStateAction<number>) {
|
|
234
|
-
setFixedSize((current) => {
|
|
235
|
-
let newValue: number = clampSize(typeof size === "function" ? size(current) : size);
|
|
236
|
-
|
|
237
|
-
if (id) {
|
|
238
|
-
let key = `${article}.${id}`;
|
|
239
|
-
persistState(key, layoutVersion, newValue);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return newValue;
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function handlePointerUp(event: PointerEvent) {
|
|
247
|
-
let newSize = getNewSize(event);
|
|
248
|
-
handleFixedSizeChanged(newSize);
|
|
249
|
-
setIsDragging(false);
|
|
250
|
-
|
|
251
|
-
movingPanel.style.flex = `0 0 ${Math.floor(newSize)}px`;
|
|
252
|
-
window.removeEventListener("pointermove", handlePointerMove);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function handlePointerMove(event: PointerEvent) {
|
|
256
|
-
event.preventDefault();
|
|
257
|
-
|
|
258
|
-
let newSize = getNewSize(event);
|
|
259
|
-
let divider = dividerRef.current;
|
|
260
|
-
if (divider) {
|
|
261
|
-
divider.setAttribute("aria-valuenow", String(newSize));
|
|
262
|
-
}
|
|
263
|
-
movingPanel.style.flex = `0 0 ${newSize}px`;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
function handlePointerDown(event: PointerEvent) {
|
|
267
|
-
setIsDragging(true);
|
|
268
|
-
|
|
269
|
-
let rect = movingPanel.getBoundingClientRect();
|
|
270
|
-
let size = direction === "horizontal" ? rect.width : rect.height;
|
|
271
|
-
initialPosition = {
|
|
272
|
-
clientX: event.clientX,
|
|
273
|
-
clientY: event.clientY,
|
|
274
|
-
size,
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
window.addEventListener("pointermove", handlePointerMove);
|
|
278
|
-
window.addEventListener("pointerup", handlePointerUp, { once: true });
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function handleKeyDown(event: KeyboardEvent) {
|
|
282
|
-
let reduce = true;
|
|
283
|
-
let shouldHandle = false;
|
|
284
|
-
if (!event.altKey && !event.ctrlKey && !event.metaKey) {
|
|
285
|
-
if (direction === "horizontal" && ["ArrowLeft", "ArrowRight"].includes(event.key)) {
|
|
286
|
-
shouldHandle = true;
|
|
287
|
-
reduce =
|
|
288
|
-
(fixedPanel === 1 && event.key === "ArrowLeft") || (fixedPanel === 2 && event.key === "ArrowRight");
|
|
289
|
-
} else if (direction === "vertical" && ["ArrowUp", "ArrowDown"].includes(event.key)) {
|
|
290
|
-
shouldHandle = true;
|
|
291
|
-
reduce = (fixedPanel === 1 && event.key === "ArrowUp") || (fixedPanel === 2 && event.key === "ArrowDown");
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
if (shouldHandle) {
|
|
296
|
-
event.preventDefault();
|
|
297
|
-
event.stopPropagation();
|
|
298
|
-
|
|
299
|
-
let delta = event.shiftKey ? 25 : 5;
|
|
300
|
-
|
|
301
|
-
handleFixedSizeChanged((size) => size + (reduce ? -delta : delta));
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
divider.addEventListener("pointerdown", handlePointerDown);
|
|
306
|
-
divider.addEventListener("keydown", handleKeyDown);
|
|
307
|
-
|
|
308
|
-
return () => {
|
|
309
|
-
divider?.removeEventListener("pointerdown", handlePointerDown);
|
|
310
|
-
divider?.removeEventListener("keydown", handleKeyDown);
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
}, [direction, fixedPanel, layoutVersion, id, min, max, containerSize]);
|
|
314
|
-
|
|
315
|
-
return (
|
|
316
|
-
<SplitRoot direction={direction} className={className} ref={containerRef}>
|
|
317
|
-
<SplitPanel className={clsx(isDragging && "dragging", panelClassName)} fixed={fixedPanel === 1} size={fixedSize}>
|
|
318
|
-
{childrenArray[0]}
|
|
319
|
-
</SplitPanel>
|
|
320
|
-
<Divider
|
|
321
|
-
ref={dividerRef}
|
|
322
|
-
direction={direction}
|
|
323
|
-
isDragging={isDragging}
|
|
324
|
-
size={fixedSize}
|
|
325
|
-
min={min}
|
|
326
|
-
max={max ?? containerSize}
|
|
327
|
-
/>
|
|
328
|
-
<SplitPanel className={clsx(isDragging && "dragging", panelClassName)} fixed={fixedPanel === 2} size={fixedSize}>
|
|
329
|
-
{childrenArray[1]}
|
|
330
|
-
</SplitPanel>
|
|
331
|
-
</SplitRoot>
|
|
332
|
-
);
|
|
333
|
-
}
|
|
1
|
+
import "./index.scss";
|
|
2
|
+
|
|
3
|
+
import { createSettingsStore } from "@olenbetong/appframe-react";
|
|
4
|
+
|
|
5
|
+
import clsx from "clsx";
|
|
6
|
+
import type React from "react";
|
|
7
|
+
import { Children, forwardRef, useEffect, useRef, useState } from "react";
|
|
8
|
+
|
|
9
|
+
const article = globalThis.af?.article?.id ?? globalThis.window?.location.pathname.substring(1).split("/")[0];
|
|
10
|
+
const settingsStore = createSettingsStore(`AfSplitContainers.${globalThis.af?.userSession?.login ?? "anonymous"}`);
|
|
11
|
+
|
|
12
|
+
type SplitContainerSettings = {
|
|
13
|
+
layoutVersion: string | number;
|
|
14
|
+
fixedSize: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const settingsCache: Record<string, SplitContainerSettings> = {};
|
|
18
|
+
|
|
19
|
+
async function persistState(key: string, layoutVersion: string | number, fixedSize: number | null | undefined) {
|
|
20
|
+
if (fixedSize != null) {
|
|
21
|
+
let settings = {
|
|
22
|
+
layoutVersion,
|
|
23
|
+
fixedSize,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
settingsCache[key] = settings;
|
|
27
|
+
await settingsStore.setItem<SplitContainerSettings>(key, settings);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function getSettings(key: string, layoutVersion: string | number): Promise<number | null> {
|
|
32
|
+
let settings = settingsCache[key] ?? (await settingsStore.getItem<SplitContainerSettings>(key));
|
|
33
|
+
|
|
34
|
+
if (settings && settings.layoutVersion === layoutVersion) {
|
|
35
|
+
return settings.fixedSize;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Load all article grid layouts even if the grid is not mounted yet.
|
|
42
|
+
// This ways the persisted state can be restored faster after the initial
|
|
43
|
+
// page load.
|
|
44
|
+
settingsStore.keys().then((keys) =>
|
|
45
|
+
Promise.allSettled(
|
|
46
|
+
keys
|
|
47
|
+
.filter((key) => key.startsWith(article))
|
|
48
|
+
.map((key) =>
|
|
49
|
+
settingsStore.getItem<SplitContainerSettings>(key).then((settings) => {
|
|
50
|
+
if (settings && !settingsCache[key]) {
|
|
51
|
+
settingsCache[key] = settings;
|
|
52
|
+
}
|
|
53
|
+
}),
|
|
54
|
+
),
|
|
55
|
+
),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
type DividerProps = {
|
|
59
|
+
className?: string;
|
|
60
|
+
direction: "horizontal" | "vertical";
|
|
61
|
+
isDragging: boolean;
|
|
62
|
+
min: number;
|
|
63
|
+
max: number;
|
|
64
|
+
size: number;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const Divider = forwardRef<HTMLDivElement, DividerProps>(function Divider(props: DividerProps, ref) {
|
|
68
|
+
return (
|
|
69
|
+
// oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- NO!
|
|
70
|
+
<div
|
|
71
|
+
className={clsx("ObSplitContainer-divider", props.className)}
|
|
72
|
+
tabIndex={0}
|
|
73
|
+
ref={ref}
|
|
74
|
+
role="separator"
|
|
75
|
+
aria-orientation={props.direction}
|
|
76
|
+
aria-valuemin={props.min}
|
|
77
|
+
aria-valuemax={props.max}
|
|
78
|
+
aria-valuenow={props.size}
|
|
79
|
+
>
|
|
80
|
+
<span>•••</span>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
type SplitRootProps = React.HTMLProps<HTMLDivElement> & {
|
|
86
|
+
className?: string;
|
|
87
|
+
children: React.ReactNode;
|
|
88
|
+
direction: "horizontal" | "vertical";
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
function SplitRoot({ className, direction, ...props }: SplitRootProps) {
|
|
92
|
+
return <div className={clsx(className, "ObSplitContainer-root", direction)} {...props} />;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
type SplitPanelProps = {
|
|
96
|
+
children: React.ReactNode;
|
|
97
|
+
className?: string;
|
|
98
|
+
style?: React.CSSProperties;
|
|
99
|
+
} & (
|
|
100
|
+
| {
|
|
101
|
+
fixed: true;
|
|
102
|
+
size: number;
|
|
103
|
+
}
|
|
104
|
+
| {
|
|
105
|
+
fixed: false;
|
|
106
|
+
size?: number;
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
function SplitPanel(props: SplitPanelProps) {
|
|
111
|
+
let style = {
|
|
112
|
+
flex: props.fixed ? `0 0 ${Math.floor(props.size)}px` : "1 1 100%",
|
|
113
|
+
...props.style,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<div className={clsx(props.className, "ObSplitContainer-panel")} style={style}>
|
|
118
|
+
{props.children}
|
|
119
|
+
</div>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export type SplitContainerProps = {
|
|
124
|
+
className?: string;
|
|
125
|
+
panelClassName?: string;
|
|
126
|
+
children: React.ReactNode[];
|
|
127
|
+
direction?: "horizontal" | "vertical";
|
|
128
|
+
fixedPanel?: 1 | 2;
|
|
129
|
+
defaultFixedSize?: number;
|
|
130
|
+
min?: number;
|
|
131
|
+
max?: number;
|
|
132
|
+
/**
|
|
133
|
+
* When the fixed size is persisted, changing the layoutVersion will force clients
|
|
134
|
+
* to reset to the default value.
|
|
135
|
+
*/
|
|
136
|
+
layoutVersion?: string | number;
|
|
137
|
+
/**
|
|
138
|
+
* If you set an id, the fixed size will be persisted, and restored after page load
|
|
139
|
+
*/
|
|
140
|
+
id?: string;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export function SplitContainer({
|
|
144
|
+
className,
|
|
145
|
+
children,
|
|
146
|
+
direction = "vertical",
|
|
147
|
+
fixedPanel = 1,
|
|
148
|
+
panelClassName,
|
|
149
|
+
defaultFixedSize = 400,
|
|
150
|
+
layoutVersion = 1,
|
|
151
|
+
min = 0,
|
|
152
|
+
max,
|
|
153
|
+
id,
|
|
154
|
+
}: SplitContainerProps) {
|
|
155
|
+
let [fixedSize, setFixedSize] = useState<number>(defaultFixedSize);
|
|
156
|
+
let [isDragging, setIsDragging] = useState(false);
|
|
157
|
+
let dividerRef = useRef<HTMLDivElement>(null);
|
|
158
|
+
|
|
159
|
+
if (Children.count(children) !== 2) {
|
|
160
|
+
throw Error("SplitContainer requires exactly 2 children");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let containerRef = useRef<HTMLDivElement>(null);
|
|
164
|
+
let [containerSize, setContainerSize] = useState<number>(0);
|
|
165
|
+
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
if (!containerRef.current) return;
|
|
168
|
+
|
|
169
|
+
let resizeObserver = new ResizeObserver((entries) => {
|
|
170
|
+
for (let entry of entries) {
|
|
171
|
+
if (entry.contentRect) {
|
|
172
|
+
setContainerSize(direction === "horizontal" ? entry.contentRect.width : entry.contentRect.height);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
resizeObserver.observe(containerRef.current);
|
|
178
|
+
return () => resizeObserver.disconnect();
|
|
179
|
+
}, [direction]);
|
|
180
|
+
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
if (id) {
|
|
183
|
+
let key = `${article}.${id}`;
|
|
184
|
+
|
|
185
|
+
getSettings(key, layoutVersion).then((data: number | null) => {
|
|
186
|
+
if (data) {
|
|
187
|
+
// Persisted state exists, and matches the current layout version,
|
|
188
|
+
// so restore the state.
|
|
189
|
+
setFixedSize(data);
|
|
190
|
+
|
|
191
|
+
return Promise.resolve();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}, [id, layoutVersion]);
|
|
196
|
+
|
|
197
|
+
let childrenArray = Children.toArray(children);
|
|
198
|
+
|
|
199
|
+
useEffect(() => {
|
|
200
|
+
let divider = dividerRef.current;
|
|
201
|
+
if (divider) {
|
|
202
|
+
let initialPosition: { clientX: number; clientY: number; size: number } = {
|
|
203
|
+
clientX: 0,
|
|
204
|
+
clientY: 0,
|
|
205
|
+
size: 0,
|
|
206
|
+
};
|
|
207
|
+
let movingPanel: HTMLElement = (
|
|
208
|
+
fixedPanel === 1 ? divider.previousElementSibling : divider.nextElementSibling
|
|
209
|
+
) as HTMLElement;
|
|
210
|
+
|
|
211
|
+
function clampSize(size: number) {
|
|
212
|
+
let adjustedContainerSize = containerSize - 10; // make sure divider will not be stuck outside the container
|
|
213
|
+
let maxSize = Math.min(max ?? adjustedContainerSize, adjustedContainerSize);
|
|
214
|
+
if (size < min) {
|
|
215
|
+
size = min;
|
|
216
|
+
} else if (size > maxSize) {
|
|
217
|
+
size = maxSize;
|
|
218
|
+
}
|
|
219
|
+
return size;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function getNewSize(event: PointerEvent) {
|
|
223
|
+
let deltaSize =
|
|
224
|
+
direction === "horizontal"
|
|
225
|
+
? event.clientX - initialPosition.clientX
|
|
226
|
+
: event.clientY - initialPosition.clientY;
|
|
227
|
+
|
|
228
|
+
deltaSize *= fixedPanel === 1 ? -1 : 1;
|
|
229
|
+
|
|
230
|
+
return clampSize(initialPosition.size - deltaSize);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function handleFixedSizeChanged(size: React.SetStateAction<number>) {
|
|
234
|
+
setFixedSize((current) => {
|
|
235
|
+
let newValue: number = clampSize(typeof size === "function" ? size(current) : size);
|
|
236
|
+
|
|
237
|
+
if (id) {
|
|
238
|
+
let key = `${article}.${id}`;
|
|
239
|
+
persistState(key, layoutVersion, newValue);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return newValue;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function handlePointerUp(event: PointerEvent) {
|
|
247
|
+
let newSize = getNewSize(event);
|
|
248
|
+
handleFixedSizeChanged(newSize);
|
|
249
|
+
setIsDragging(false);
|
|
250
|
+
|
|
251
|
+
movingPanel.style.flex = `0 0 ${Math.floor(newSize)}px`;
|
|
252
|
+
window.removeEventListener("pointermove", handlePointerMove);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function handlePointerMove(event: PointerEvent) {
|
|
256
|
+
event.preventDefault();
|
|
257
|
+
|
|
258
|
+
let newSize = getNewSize(event);
|
|
259
|
+
let divider = dividerRef.current;
|
|
260
|
+
if (divider) {
|
|
261
|
+
divider.setAttribute("aria-valuenow", String(newSize));
|
|
262
|
+
}
|
|
263
|
+
movingPanel.style.flex = `0 0 ${newSize}px`;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function handlePointerDown(event: PointerEvent) {
|
|
267
|
+
setIsDragging(true);
|
|
268
|
+
|
|
269
|
+
let rect = movingPanel.getBoundingClientRect();
|
|
270
|
+
let size = direction === "horizontal" ? rect.width : rect.height;
|
|
271
|
+
initialPosition = {
|
|
272
|
+
clientX: event.clientX,
|
|
273
|
+
clientY: event.clientY,
|
|
274
|
+
size,
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
window.addEventListener("pointermove", handlePointerMove);
|
|
278
|
+
window.addEventListener("pointerup", handlePointerUp, { once: true });
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function handleKeyDown(event: KeyboardEvent) {
|
|
282
|
+
let reduce = true;
|
|
283
|
+
let shouldHandle = false;
|
|
284
|
+
if (!event.altKey && !event.ctrlKey && !event.metaKey) {
|
|
285
|
+
if (direction === "horizontal" && ["ArrowLeft", "ArrowRight"].includes(event.key)) {
|
|
286
|
+
shouldHandle = true;
|
|
287
|
+
reduce =
|
|
288
|
+
(fixedPanel === 1 && event.key === "ArrowLeft") || (fixedPanel === 2 && event.key === "ArrowRight");
|
|
289
|
+
} else if (direction === "vertical" && ["ArrowUp", "ArrowDown"].includes(event.key)) {
|
|
290
|
+
shouldHandle = true;
|
|
291
|
+
reduce = (fixedPanel === 1 && event.key === "ArrowUp") || (fixedPanel === 2 && event.key === "ArrowDown");
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (shouldHandle) {
|
|
296
|
+
event.preventDefault();
|
|
297
|
+
event.stopPropagation();
|
|
298
|
+
|
|
299
|
+
let delta = event.shiftKey ? 25 : 5;
|
|
300
|
+
|
|
301
|
+
handleFixedSizeChanged((size) => size + (reduce ? -delta : delta));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
divider.addEventListener("pointerdown", handlePointerDown);
|
|
306
|
+
divider.addEventListener("keydown", handleKeyDown);
|
|
307
|
+
|
|
308
|
+
return () => {
|
|
309
|
+
divider?.removeEventListener("pointerdown", handlePointerDown);
|
|
310
|
+
divider?.removeEventListener("keydown", handleKeyDown);
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
}, [direction, fixedPanel, layoutVersion, id, min, max, containerSize]);
|
|
314
|
+
|
|
315
|
+
return (
|
|
316
|
+
<SplitRoot direction={direction} className={className} ref={containerRef}>
|
|
317
|
+
<SplitPanel className={clsx(isDragging && "dragging", panelClassName)} fixed={fixedPanel === 1} size={fixedSize}>
|
|
318
|
+
{childrenArray[0]}
|
|
319
|
+
</SplitPanel>
|
|
320
|
+
<Divider
|
|
321
|
+
ref={dividerRef}
|
|
322
|
+
direction={direction}
|
|
323
|
+
isDragging={isDragging}
|
|
324
|
+
size={fixedSize}
|
|
325
|
+
min={min}
|
|
326
|
+
max={max ?? containerSize}
|
|
327
|
+
/>
|
|
328
|
+
<SplitPanel className={clsx(isDragging && "dragging", panelClassName)} fixed={fixedPanel === 2} size={fixedSize}>
|
|
329
|
+
{childrenArray[1]}
|
|
330
|
+
</SplitPanel>
|
|
331
|
+
</SplitRoot>
|
|
332
|
+
);
|
|
333
|
+
}
|