@olenbetong/synergi-react 5.0.0 → 5.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/es/components/Checkbox/index.js +1 -9
- package/es/components/CodeEditor/CodeEditor.css +6 -6
- package/es/components/FilePond.js +3 -12
- package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
- package/es/components/ImageEditor/useImageHistory.js +13 -1
- package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +1 -1
- package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
- package/es/components/Portal/index.js +20 -20
- package/es/components/SplitContainer/index.js +3 -1
- package/es/components/Toolbar/Toolbar.css +137 -137
- package/es/components/Toolbar/index.js +6 -1
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
- package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
- package/es/elements-ds/DescriptionInput.d.ts +16 -0
- package/es/elements-ds/DescriptionInput.js +41 -0
- package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
- package/es/elements-ds/DuplicateElementDamage.js +57 -0
- package/es/elements-ds/ElementLookup.d.ts +23 -0
- package/es/elements-ds/ElementLookup.js +35 -0
- package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
- package/es/elements-ds/ElementLookupDialog.js +84 -0
- package/es/elements-ds/FormFields.d.ts +43 -0
- package/es/elements-ds/FormFields.js +67 -0
- package/es/elements-ds/HIDScanner.d.ts +7 -0
- package/es/elements-ds/HIDScanner.js +36 -0
- package/es/elements-ds/ManualForm.d.ts +7 -0
- package/es/elements-ds/ManualForm.js +70 -0
- package/es/elements-ds/ProjectSelect.d.ts +28 -0
- package/es/elements-ds/ProjectSelect.js +45 -0
- package/es/elements-ds/elements-ds.css +157 -0
- package/es/elements-ds/index.d.ts +12 -0
- package/es/elements-ds/index.js +14 -0
- package/es/useTranslation/index.js +5 -3
- package/package.json +15 -3
- package/src/chrome/AppShell/AppShell.test.tsx +48 -0
- package/src/chrome/AppShell/index.tsx +29 -29
- package/src/chrome/openReport.test.ts +50 -0
- package/src/chrome/openReport.ts +34 -34
- package/src/colorScheme.test.ts +188 -0
- package/src/colorScheme.ts +125 -125
- package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
- package/src/components/Checkbox/index.tsx +1 -10
- package/src/components/CodeEditor/CodeEditor.css +6 -6
- package/src/components/CodeEditor/CodeEditor.tsx +173 -173
- package/src/components/CodeEditor/index.ts +1 -1
- package/src/components/ColorCard/ColorCard.test.tsx +34 -0
- package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
- package/src/components/DateNavigator/index.tsx +186 -186
- package/src/components/FilePond.browser.test.tsx +389 -0
- package/src/components/FilePond.tsx +303 -312
- package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
- package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
- package/src/components/ImageEditor/ImageEditor.tsx +348 -348
- 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.test.ts +225 -0
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
- package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
- package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
- package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
- package/src/components/ImageEditor/useActiveTool.ts +21 -21
- package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
- package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
- package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
- package/src/components/ImageEditor/useImageHistory.ts +206 -197
- package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
- package/src/components/ImageEditor/useOpenCV.ts +184 -184
- package/src/components/LinkedCardList/index.scss +95 -95
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +1 -0
- package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
- package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
- package/src/components/PageBanner/index.tsx +80 -80
- package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
- package/src/components/PdfViewer/PdfViewer.tsx +158 -158
- package/src/components/Portal/Portal.test.tsx +106 -0
- package/src/components/Portal/index.tsx +22 -25
- package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
- package/src/components/Sidebar/Sidebar.test.tsx +123 -0
- package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
- package/src/components/SplitContainer/index.tsx +335 -333
- package/src/components/Toolbar/Toolbar.css +137 -137
- package/src/components/Toolbar/Toolbar.test.tsx +216 -0
- package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
- package/src/components/Toolbar/index.tsx +270 -265
- package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
- 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/BarcodeScanner.browser.test.tsx +175 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
- package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
- package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
- package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
- package/src/elements/ElementLookupDialog/index.tsx +226 -226
- package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
- 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/elements-ds/DamageForm.browser.test.tsx +530 -0
- package/src/elements-ds/DescriptionInput.tsx +114 -0
- package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
- package/src/elements-ds/ElementLookup.tsx +114 -0
- package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
- package/src/elements-ds/ElementLookupDialog.tsx +196 -0
- package/src/elements-ds/FormFields.tsx +176 -0
- package/src/elements-ds/HIDScanner.tsx +53 -0
- package/src/elements-ds/ManualForm.tsx +129 -0
- package/src/elements-ds/ProjectSelect.tsx +120 -0
- package/src/elements-ds/elements-ds.css +157 -0
- package/src/elements-ds/index.ts +16 -0
- package/src/filter/index.ts +34 -34
- package/src/i18n.test.ts +31 -0
- package/src/i18n.ts +13 -13
- package/src/mui/MuiThemeProvider.tsx +135 -135
- package/src/test/fakeCamera.ts +98 -0
- package/src/test/fakeDataApi.ts +90 -0
- package/src/test/setup.browser.ts +21 -0
- package/src/test/setup.ts +23 -23
- package/src/useTranslation/index.ts +5 -3
- package/src/useTranslation/useTranslation.test.ts +128 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
- package/es/test/setup.d.ts +0 -6
- package/es/test/setup.js +0 -6
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { defaultPlugins, defaultViewPlugin } from "./defaultPlugins.js";
|
|
2
|
-
export type { ImageEditorRef } from "./ImageEditor.js";
|
|
3
|
-
export { ImageEditor } from "./ImageEditor.js";
|
|
4
|
-
export { cropPlugin } from "./plugins/cropPlugin/index.js";
|
|
5
|
-
export { redactPlugin } from "./plugins/redactPlugin/index.js";
|
|
6
|
-
export { zoomPlugin } from "./plugins/zoomPlugin/index.js";
|
|
7
|
-
export { rotateCcwPlugin, rotateCwPlugin } from "./plugins/rotatePlugin.js";
|
|
8
|
-
export type { ActionPlugin, EditAction, Plugin, ToolPlugin, ViewPlugin } from "./types.js";
|
|
9
|
-
export { configureOpenCV, DEFAULT_OPENCV_URLS, loadOpenCV } from "./useOpenCV.js";
|
|
1
|
+
export { defaultPlugins, defaultViewPlugin } from "./defaultPlugins.js";
|
|
2
|
+
export type { ImageEditorRef } from "./ImageEditor.js";
|
|
3
|
+
export { ImageEditor } from "./ImageEditor.js";
|
|
4
|
+
export { cropPlugin } from "./plugins/cropPlugin/index.js";
|
|
5
|
+
export { redactPlugin } from "./plugins/redactPlugin/index.js";
|
|
6
|
+
export { zoomPlugin } from "./plugins/zoomPlugin/index.js";
|
|
7
|
+
export { rotateCcwPlugin, rotateCwPlugin } from "./plugins/rotatePlugin.js";
|
|
8
|
+
export type { ActionPlugin, EditAction, Plugin, ToolPlugin, ViewPlugin } from "./types.js";
|
|
9
|
+
export { configureOpenCV, DEFAULT_OPENCV_URLS, loadOpenCV } from "./useOpenCV.js";
|
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
import { Paragraph, Spinner } from "@digdir/designsystemet-react";
|
|
2
|
-
|
|
3
|
-
import { CropCanvas } from "../../CropCanvas.js";
|
|
4
|
-
import { DragHandle } from "../../DragHandle.js";
|
|
5
|
-
import { EdgeHandle } from "../../EdgeHandle.js";
|
|
6
|
-
import { useZoomLevel } from "../zoomPlugin/ZoomContext.js";
|
|
7
|
-
import { useCropContext } from "./CropContext.js";
|
|
8
|
-
import { getLocalizedString } from "../../../../i18n.js";
|
|
9
|
-
|
|
10
|
-
/** Canvas-area overlay for the crop tool. Reads all state from CropContext. */
|
|
11
|
-
export function CropOverlay() {
|
|
12
|
-
let {
|
|
13
|
-
imageUrl,
|
|
14
|
-
activeCorners,
|
|
15
|
-
mode,
|
|
16
|
-
hasSelection,
|
|
17
|
-
scale,
|
|
18
|
-
displaySize,
|
|
19
|
-
canvasRef,
|
|
20
|
-
cvReady,
|
|
21
|
-
cvError,
|
|
22
|
-
cropping,
|
|
23
|
-
error,
|
|
24
|
-
handleCanvasReady,
|
|
25
|
-
updateCorner,
|
|
26
|
-
startRectDraw,
|
|
27
|
-
updateRectDraw,
|
|
28
|
-
finishRectDraw,
|
|
29
|
-
} = useCropContext();
|
|
30
|
-
|
|
31
|
-
let zoom = useZoomLevel();
|
|
32
|
-
let showHandles = mode === "corners" && displaySize.width > 0;
|
|
33
|
-
|
|
34
|
-
function pointerPosition(e: React.PointerEvent<HTMLDivElement>) {
|
|
35
|
-
// getBoundingClientRect() is screen-space (affected by the zoom transform),
|
|
36
|
-
// so divide by zoom to get back to the CSS-pixel space the tool expects.
|
|
37
|
-
let rect = e.currentTarget.getBoundingClientRect();
|
|
38
|
-
return { x: (e.clientX - rect.left) / zoom, y: (e.clientY - rect.top) / zoom };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function onPointerDown(e: React.PointerEvent<HTMLDivElement>) {
|
|
42
|
-
e.currentTarget.setPointerCapture(e.pointerId);
|
|
43
|
-
let { x, y } = pointerPosition(e);
|
|
44
|
-
startRectDraw(x, y);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function onPointerMove(e: React.PointerEvent<HTMLDivElement>) {
|
|
48
|
-
if (!(e.buttons & 1)) return;
|
|
49
|
-
let { x, y } = pointerPosition(e);
|
|
50
|
-
updateRectDraw(x, y);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return (
|
|
54
|
-
<div className="crop-root">
|
|
55
|
-
<div className="crop-inner">
|
|
56
|
-
<CropCanvas ref={canvasRef} imageUrl={imageUrl} corners={activeCorners} onReady={handleCanvasReady} />
|
|
57
|
-
|
|
58
|
-
{mode === "rect" && displaySize.width > 0 && (
|
|
59
|
-
<div
|
|
60
|
-
role="application"
|
|
61
|
-
aria-label={getLocalizedString("Drag to draw a rectangle to crop to")}
|
|
62
|
-
className="crop-draw-surface"
|
|
63
|
-
onPointerDown={onPointerDown}
|
|
64
|
-
onPointerMove={onPointerMove}
|
|
65
|
-
onPointerUp={finishRectDraw}
|
|
66
|
-
onPointerCancel={finishRectDraw}
|
|
67
|
-
/>
|
|
68
|
-
)}
|
|
69
|
-
|
|
70
|
-
{showHandles &&
|
|
71
|
-
activeCorners.map((corner, i) => (
|
|
72
|
-
<DragHandle
|
|
73
|
-
key={i}
|
|
74
|
-
x={corner.x * scale}
|
|
75
|
-
y={corner.y * scale}
|
|
76
|
-
onChange={(x, y) => updateCorner(i, x, y)}
|
|
77
|
-
containerWidth={displaySize.width}
|
|
78
|
-
containerHeight={displaySize.height}
|
|
79
|
-
/>
|
|
80
|
-
))}
|
|
81
|
-
|
|
82
|
-
{showHandles &&
|
|
83
|
-
(() => {
|
|
84
|
-
let [tl, tr, br, bl] = activeCorners;
|
|
85
|
-
let s = scale;
|
|
86
|
-
return (
|
|
87
|
-
<>
|
|
88
|
-
{/* Top edge — moves TL (0) and TR (1) */}
|
|
89
|
-
<EdgeHandle
|
|
90
|
-
x={((tl.x + tr.x) / 2) * s}
|
|
91
|
-
y={((tl.y + tr.y) / 2) * s}
|
|
92
|
-
axis="y"
|
|
93
|
-
onChange={(dx, dy) => {
|
|
94
|
-
updateCorner(0, tl.x * s + dx, tl.y * s + dy);
|
|
95
|
-
updateCorner(1, tr.x * s + dx, tr.y * s + dy);
|
|
96
|
-
}}
|
|
97
|
-
/>
|
|
98
|
-
{/* Right edge — moves TR (1) and BR (2) */}
|
|
99
|
-
<EdgeHandle
|
|
100
|
-
x={((tr.x + br.x) / 2) * s}
|
|
101
|
-
y={((tr.y + br.y) / 2) * s}
|
|
102
|
-
axis="x"
|
|
103
|
-
onChange={(dx, dy) => {
|
|
104
|
-
updateCorner(1, tr.x * s + dx, tr.y * s + dy);
|
|
105
|
-
updateCorner(2, br.x * s + dx, br.y * s + dy);
|
|
106
|
-
}}
|
|
107
|
-
/>
|
|
108
|
-
{/* Bottom edge — moves BR (2) and BL (3) */}
|
|
109
|
-
<EdgeHandle
|
|
110
|
-
x={((br.x + bl.x) / 2) * s}
|
|
111
|
-
y={((br.y + bl.y) / 2) * s}
|
|
112
|
-
axis="y"
|
|
113
|
-
onChange={(dx, dy) => {
|
|
114
|
-
updateCorner(2, br.x * s + dx, br.y * s + dy);
|
|
115
|
-
updateCorner(3, bl.x * s + dx, bl.y * s + dy);
|
|
116
|
-
}}
|
|
117
|
-
/>
|
|
118
|
-
{/* Left edge — moves BL (3) and TL (0) */}
|
|
119
|
-
<EdgeHandle
|
|
120
|
-
x={((bl.x + tl.x) / 2) * s}
|
|
121
|
-
y={((bl.y + tl.y) / 2) * s}
|
|
122
|
-
axis="x"
|
|
123
|
-
onChange={(dx, dy) => {
|
|
124
|
-
updateCorner(3, bl.x * s + dx, bl.y * s + dy);
|
|
125
|
-
updateCorner(0, tl.x * s + dx, tl.y * s + dy);
|
|
126
|
-
}}
|
|
127
|
-
/>
|
|
128
|
-
</>
|
|
129
|
-
);
|
|
130
|
-
})()}
|
|
131
|
-
</div>
|
|
132
|
-
|
|
133
|
-
{/* Status line. OpenCV only powers the border suggestion, so a load failure is
|
|
134
|
-
informational — cropping stays available either way. */}
|
|
135
|
-
{cropping ? (
|
|
136
|
-
<div className="crop-loading">
|
|
137
|
-
<Spinner aria-hidden data-size="xs" />
|
|
138
|
-
</div>
|
|
139
|
-
) : error ? (
|
|
140
|
-
<div className="crop-loading">
|
|
141
|
-
<Paragraph data-size="sm">{error}</Paragraph>
|
|
142
|
-
</div>
|
|
143
|
-
) : mode === "rect" ? (
|
|
144
|
-
<div className="crop-loading">
|
|
145
|
-
<Paragraph data-size="sm">
|
|
146
|
-
{hasSelection
|
|
147
|
-
? getLocalizedString("Release to use this rectangle")
|
|
148
|
-
: getLocalizedString("Drag to draw a rectangle to crop to")}
|
|
149
|
-
</Paragraph>
|
|
150
|
-
</div>
|
|
151
|
-
) : !cvReady && !cvError ? (
|
|
152
|
-
<div className="crop-loading">
|
|
153
|
-
<Spinner aria-hidden data-size="xs" />
|
|
154
|
-
<Paragraph data-size="sm">{getLocalizedString("Detecting borders…")}</Paragraph>
|
|
155
|
-
</div>
|
|
156
|
-
) : cvError ? (
|
|
157
|
-
<div className="crop-loading">
|
|
158
|
-
<Paragraph data-size="sm">
|
|
159
|
-
{getLocalizedString("Automatic border detection is unavailable. Adjust the selection manually.")}
|
|
160
|
-
</Paragraph>
|
|
161
|
-
</div>
|
|
162
|
-
) : null}
|
|
163
|
-
</div>
|
|
164
|
-
);
|
|
165
|
-
}
|
|
1
|
+
import { Paragraph, Spinner } from "@digdir/designsystemet-react";
|
|
2
|
+
|
|
3
|
+
import { CropCanvas } from "../../CropCanvas.js";
|
|
4
|
+
import { DragHandle } from "../../DragHandle.js";
|
|
5
|
+
import { EdgeHandle } from "../../EdgeHandle.js";
|
|
6
|
+
import { useZoomLevel } from "../zoomPlugin/ZoomContext.js";
|
|
7
|
+
import { useCropContext } from "./CropContext.js";
|
|
8
|
+
import { getLocalizedString } from "../../../../i18n.js";
|
|
9
|
+
|
|
10
|
+
/** Canvas-area overlay for the crop tool. Reads all state from CropContext. */
|
|
11
|
+
export function CropOverlay() {
|
|
12
|
+
let {
|
|
13
|
+
imageUrl,
|
|
14
|
+
activeCorners,
|
|
15
|
+
mode,
|
|
16
|
+
hasSelection,
|
|
17
|
+
scale,
|
|
18
|
+
displaySize,
|
|
19
|
+
canvasRef,
|
|
20
|
+
cvReady,
|
|
21
|
+
cvError,
|
|
22
|
+
cropping,
|
|
23
|
+
error,
|
|
24
|
+
handleCanvasReady,
|
|
25
|
+
updateCorner,
|
|
26
|
+
startRectDraw,
|
|
27
|
+
updateRectDraw,
|
|
28
|
+
finishRectDraw,
|
|
29
|
+
} = useCropContext();
|
|
30
|
+
|
|
31
|
+
let zoom = useZoomLevel();
|
|
32
|
+
let showHandles = mode === "corners" && displaySize.width > 0;
|
|
33
|
+
|
|
34
|
+
function pointerPosition(e: React.PointerEvent<HTMLDivElement>) {
|
|
35
|
+
// getBoundingClientRect() is screen-space (affected by the zoom transform),
|
|
36
|
+
// so divide by zoom to get back to the CSS-pixel space the tool expects.
|
|
37
|
+
let rect = e.currentTarget.getBoundingClientRect();
|
|
38
|
+
return { x: (e.clientX - rect.left) / zoom, y: (e.clientY - rect.top) / zoom };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function onPointerDown(e: React.PointerEvent<HTMLDivElement>) {
|
|
42
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
43
|
+
let { x, y } = pointerPosition(e);
|
|
44
|
+
startRectDraw(x, y);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function onPointerMove(e: React.PointerEvent<HTMLDivElement>) {
|
|
48
|
+
if (!(e.buttons & 1)) return;
|
|
49
|
+
let { x, y } = pointerPosition(e);
|
|
50
|
+
updateRectDraw(x, y);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div className="crop-root">
|
|
55
|
+
<div className="crop-inner">
|
|
56
|
+
<CropCanvas ref={canvasRef} imageUrl={imageUrl} corners={activeCorners} onReady={handleCanvasReady} />
|
|
57
|
+
|
|
58
|
+
{mode === "rect" && displaySize.width > 0 && (
|
|
59
|
+
<div
|
|
60
|
+
role="application"
|
|
61
|
+
aria-label={getLocalizedString("Drag to draw a rectangle to crop to")}
|
|
62
|
+
className="crop-draw-surface"
|
|
63
|
+
onPointerDown={onPointerDown}
|
|
64
|
+
onPointerMove={onPointerMove}
|
|
65
|
+
onPointerUp={finishRectDraw}
|
|
66
|
+
onPointerCancel={finishRectDraw}
|
|
67
|
+
/>
|
|
68
|
+
)}
|
|
69
|
+
|
|
70
|
+
{showHandles &&
|
|
71
|
+
activeCorners.map((corner, i) => (
|
|
72
|
+
<DragHandle
|
|
73
|
+
key={i}
|
|
74
|
+
x={corner.x * scale}
|
|
75
|
+
y={corner.y * scale}
|
|
76
|
+
onChange={(x, y) => updateCorner(i, x, y)}
|
|
77
|
+
containerWidth={displaySize.width}
|
|
78
|
+
containerHeight={displaySize.height}
|
|
79
|
+
/>
|
|
80
|
+
))}
|
|
81
|
+
|
|
82
|
+
{showHandles &&
|
|
83
|
+
(() => {
|
|
84
|
+
let [tl, tr, br, bl] = activeCorners;
|
|
85
|
+
let s = scale;
|
|
86
|
+
return (
|
|
87
|
+
<>
|
|
88
|
+
{/* Top edge — moves TL (0) and TR (1) */}
|
|
89
|
+
<EdgeHandle
|
|
90
|
+
x={((tl.x + tr.x) / 2) * s}
|
|
91
|
+
y={((tl.y + tr.y) / 2) * s}
|
|
92
|
+
axis="y"
|
|
93
|
+
onChange={(dx, dy) => {
|
|
94
|
+
updateCorner(0, tl.x * s + dx, tl.y * s + dy);
|
|
95
|
+
updateCorner(1, tr.x * s + dx, tr.y * s + dy);
|
|
96
|
+
}}
|
|
97
|
+
/>
|
|
98
|
+
{/* Right edge — moves TR (1) and BR (2) */}
|
|
99
|
+
<EdgeHandle
|
|
100
|
+
x={((tr.x + br.x) / 2) * s}
|
|
101
|
+
y={((tr.y + br.y) / 2) * s}
|
|
102
|
+
axis="x"
|
|
103
|
+
onChange={(dx, dy) => {
|
|
104
|
+
updateCorner(1, tr.x * s + dx, tr.y * s + dy);
|
|
105
|
+
updateCorner(2, br.x * s + dx, br.y * s + dy);
|
|
106
|
+
}}
|
|
107
|
+
/>
|
|
108
|
+
{/* Bottom edge — moves BR (2) and BL (3) */}
|
|
109
|
+
<EdgeHandle
|
|
110
|
+
x={((br.x + bl.x) / 2) * s}
|
|
111
|
+
y={((br.y + bl.y) / 2) * s}
|
|
112
|
+
axis="y"
|
|
113
|
+
onChange={(dx, dy) => {
|
|
114
|
+
updateCorner(2, br.x * s + dx, br.y * s + dy);
|
|
115
|
+
updateCorner(3, bl.x * s + dx, bl.y * s + dy);
|
|
116
|
+
}}
|
|
117
|
+
/>
|
|
118
|
+
{/* Left edge — moves BL (3) and TL (0) */}
|
|
119
|
+
<EdgeHandle
|
|
120
|
+
x={((bl.x + tl.x) / 2) * s}
|
|
121
|
+
y={((bl.y + tl.y) / 2) * s}
|
|
122
|
+
axis="x"
|
|
123
|
+
onChange={(dx, dy) => {
|
|
124
|
+
updateCorner(3, bl.x * s + dx, bl.y * s + dy);
|
|
125
|
+
updateCorner(0, tl.x * s + dx, tl.y * s + dy);
|
|
126
|
+
}}
|
|
127
|
+
/>
|
|
128
|
+
</>
|
|
129
|
+
);
|
|
130
|
+
})()}
|
|
131
|
+
</div>
|
|
132
|
+
|
|
133
|
+
{/* Status line. OpenCV only powers the border suggestion, so a load failure is
|
|
134
|
+
informational — cropping stays available either way. */}
|
|
135
|
+
{cropping ? (
|
|
136
|
+
<div className="crop-loading">
|
|
137
|
+
<Spinner aria-hidden data-size="xs" />
|
|
138
|
+
</div>
|
|
139
|
+
) : error ? (
|
|
140
|
+
<div className="crop-loading">
|
|
141
|
+
<Paragraph data-size="sm">{error}</Paragraph>
|
|
142
|
+
</div>
|
|
143
|
+
) : mode === "rect" ? (
|
|
144
|
+
<div className="crop-loading">
|
|
145
|
+
<Paragraph data-size="sm">
|
|
146
|
+
{hasSelection
|
|
147
|
+
? getLocalizedString("Release to use this rectangle")
|
|
148
|
+
: getLocalizedString("Drag to draw a rectangle to crop to")}
|
|
149
|
+
</Paragraph>
|
|
150
|
+
</div>
|
|
151
|
+
) : !cvReady && !cvError ? (
|
|
152
|
+
<div className="crop-loading">
|
|
153
|
+
<Spinner aria-hidden data-size="xs" />
|
|
154
|
+
<Paragraph data-size="sm">{getLocalizedString("Detecting borders…")}</Paragraph>
|
|
155
|
+
</div>
|
|
156
|
+
) : cvError ? (
|
|
157
|
+
<div className="crop-loading">
|
|
158
|
+
<Paragraph data-size="sm">
|
|
159
|
+
{getLocalizedString("Automatic border detection is unavailable. Adjust the selection manually.")}
|
|
160
|
+
</Paragraph>
|
|
161
|
+
</div>
|
|
162
|
+
) : null}
|
|
163
|
+
</div>
|
|
164
|
+
);
|
|
165
|
+
}
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import { Button, Spinner } from "@digdir/designsystemet-react";
|
|
2
|
-
import { CheckmarkIcon, ExpandIcon, SquareIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
3
|
-
|
|
4
|
-
import { useCropContext } from "./CropContext.js";
|
|
5
|
-
import { getLocalizedString } from "../../../../i18n.js";
|
|
6
|
-
|
|
7
|
-
/** Toolbar shown while the crop tool is active. Reads state from CropContext. */
|
|
8
|
-
export function CropToolbar() {
|
|
9
|
-
let { applyCrop, cropping, mode, hasSelection, startRectMode, cancelRectMode, onApply, onCancel } = useCropContext();
|
|
10
|
-
let inRectMode = mode === "rect";
|
|
11
|
-
|
|
12
|
-
async function handleApply() {
|
|
13
|
-
let result = await applyCrop();
|
|
14
|
-
if (result) onApply(result.blob, result.actions);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return (
|
|
18
|
-
<>
|
|
19
|
-
<div className="image-editor__toolbar-group">
|
|
20
|
-
<Button variant="tertiary" data-size="sm" onClick={onCancel} disabled={cropping}>
|
|
21
|
-
<XMarkIcon aria-hidden />
|
|
22
|
-
{getLocalizedString("Cancel")}
|
|
23
|
-
</Button>
|
|
24
|
-
</div>
|
|
25
|
-
<div className="image-editor__toolbar-group">
|
|
26
|
-
<Button
|
|
27
|
-
variant="tertiary"
|
|
28
|
-
data-size="sm"
|
|
29
|
-
aria-pressed={inRectMode}
|
|
30
|
-
onClick={inRectMode ? cancelRectMode : startRectMode}
|
|
31
|
-
disabled={cropping}
|
|
32
|
-
>
|
|
33
|
-
{inRectMode ? <ExpandIcon aria-hidden /> : <SquareIcon aria-hidden />}
|
|
34
|
-
{inRectMode ? getLocalizedString("Adjust corners") : getLocalizedString("Draw rectangle")}
|
|
35
|
-
</Button>
|
|
36
|
-
<Button
|
|
37
|
-
variant="primary"
|
|
38
|
-
data-size="sm"
|
|
39
|
-
onClick={handleApply}
|
|
40
|
-
disabled={cropping || inRectMode || !hasSelection}
|
|
41
|
-
>
|
|
42
|
-
{cropping ? <Spinner aria-hidden data-size="xs" /> : <CheckmarkIcon aria-hidden />}
|
|
43
|
-
{getLocalizedString("Apply crop")}
|
|
44
|
-
</Button>
|
|
45
|
-
</div>
|
|
46
|
-
</>
|
|
47
|
-
);
|
|
48
|
-
}
|
|
1
|
+
import { Button, Spinner } from "@digdir/designsystemet-react";
|
|
2
|
+
import { CheckmarkIcon, ExpandIcon, SquareIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
3
|
+
|
|
4
|
+
import { useCropContext } from "./CropContext.js";
|
|
5
|
+
import { getLocalizedString } from "../../../../i18n.js";
|
|
6
|
+
|
|
7
|
+
/** Toolbar shown while the crop tool is active. Reads state from CropContext. */
|
|
8
|
+
export function CropToolbar() {
|
|
9
|
+
let { applyCrop, cropping, mode, hasSelection, startRectMode, cancelRectMode, onApply, onCancel } = useCropContext();
|
|
10
|
+
let inRectMode = mode === "rect";
|
|
11
|
+
|
|
12
|
+
async function handleApply() {
|
|
13
|
+
let result = await applyCrop();
|
|
14
|
+
if (result) onApply(result.blob, result.actions);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return (
|
|
18
|
+
<>
|
|
19
|
+
<div className="image-editor__toolbar-group">
|
|
20
|
+
<Button variant="tertiary" data-size="sm" onClick={onCancel} disabled={cropping}>
|
|
21
|
+
<XMarkIcon aria-hidden />
|
|
22
|
+
{getLocalizedString("Cancel")}
|
|
23
|
+
</Button>
|
|
24
|
+
</div>
|
|
25
|
+
<div className="image-editor__toolbar-group">
|
|
26
|
+
<Button
|
|
27
|
+
variant="tertiary"
|
|
28
|
+
data-size="sm"
|
|
29
|
+
aria-pressed={inRectMode}
|
|
30
|
+
onClick={inRectMode ? cancelRectMode : startRectMode}
|
|
31
|
+
disabled={cropping}
|
|
32
|
+
>
|
|
33
|
+
{inRectMode ? <ExpandIcon aria-hidden /> : <SquareIcon aria-hidden />}
|
|
34
|
+
{inRectMode ? getLocalizedString("Adjust corners") : getLocalizedString("Draw rectangle")}
|
|
35
|
+
</Button>
|
|
36
|
+
<Button
|
|
37
|
+
variant="primary"
|
|
38
|
+
data-size="sm"
|
|
39
|
+
onClick={handleApply}
|
|
40
|
+
disabled={cropping || inRectMode || !hasSelection}
|
|
41
|
+
>
|
|
42
|
+
{cropping ? <Spinner aria-hidden data-size="xs" /> : <CheckmarkIcon aria-hidden />}
|
|
43
|
+
{getLocalizedString("Apply crop")}
|
|
44
|
+
</Button>
|
|
45
|
+
</div>
|
|
46
|
+
</>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import type { Quad } from "../../useEdgeDetection";
|
|
5
|
+
import { useCropTool } from "./useCropTool";
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* OpenCV is never downloaded here. The tool's own geometry is tested against a stubbed
|
|
9
|
+
* loader and a border suggestion the test controls.
|
|
10
|
+
*/
|
|
11
|
+
let openCV: { cv: unknown; ready: boolean; error: Error | null } = { cv: null, ready: false, error: null };
|
|
12
|
+
let suggestion: Quad | null = null;
|
|
13
|
+
|
|
14
|
+
vi.mock("../../useOpenCV.js", () => ({
|
|
15
|
+
useOpenCV: () => openCV,
|
|
16
|
+
loadOpenCV: () => Promise.reject(new Error("offline")),
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
vi.mock("../../useEdgeDetection.js", async (importOriginal) => ({
|
|
20
|
+
...(await importOriginal<typeof import("../../useEdgeDetection")>()),
|
|
21
|
+
useEdgeDetection: () => ({ corners: suggestion }),
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
openCV = { cv: null, ready: false, error: null };
|
|
26
|
+
suggestion = null;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/** An 800×400 image shown at half size (400×200 CSS px), as the crop canvas reports it. */
|
|
30
|
+
function setup(imageUrl = "blob:image") {
|
|
31
|
+
let hook = renderHook(({ url }) => useCropTool(url, true), { initialProps: { url: imageUrl } });
|
|
32
|
+
act(() => {
|
|
33
|
+
hook.result.current.handleCanvasReady({ clientWidth: 400, clientHeight: 200 } as HTMLCanvasElement, 800, 400);
|
|
34
|
+
});
|
|
35
|
+
return hook;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const FULL: Quad = [
|
|
39
|
+
{ x: 0, y: 0 },
|
|
40
|
+
{ x: 800, y: 0 },
|
|
41
|
+
{ x: 800, y: 400 },
|
|
42
|
+
{ x: 0, y: 400 },
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
const SUGGESTED: Quad = [
|
|
46
|
+
{ x: 100, y: 50 },
|
|
47
|
+
{ x: 700, y: 60 },
|
|
48
|
+
{ x: 690, y: 350 },
|
|
49
|
+
{ x: 110, y: 340 },
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
describe("useCropTool", () => {
|
|
53
|
+
it("selects the whole image until a border is suggested", () => {
|
|
54
|
+
let { result } = setup();
|
|
55
|
+
|
|
56
|
+
expect(result.current.activeCorners).toEqual(FULL);
|
|
57
|
+
expect(result.current.scale).toBe(0.5);
|
|
58
|
+
expect(result.current.displaySize).toEqual({ width: 400, height: 200 });
|
|
59
|
+
expect(result.current.mode).toBe("corners");
|
|
60
|
+
expect(result.current.hasSelection).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("uses the suggested document border when it arrives", () => {
|
|
64
|
+
let { result, rerender } = setup();
|
|
65
|
+
|
|
66
|
+
suggestion = SUGGESTED;
|
|
67
|
+
rerender({ url: "blob:image" });
|
|
68
|
+
|
|
69
|
+
expect(result.current.activeCorners).toEqual(SUGGESTED);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("moves a corner to where it is dragged, converted from screen to image pixels", () => {
|
|
73
|
+
let { result } = setup();
|
|
74
|
+
|
|
75
|
+
act(() => result.current.updateCorner(0, 50, 25));
|
|
76
|
+
|
|
77
|
+
expect(result.current.activeCorners[0]).toEqual({ x: 100, y: 50 });
|
|
78
|
+
expect(result.current.activeCorners.slice(1)).toEqual(FULL.slice(1));
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("keeps a dragged corner inside the image", () => {
|
|
82
|
+
let { result } = setup();
|
|
83
|
+
|
|
84
|
+
act(() => result.current.updateCorner(2, 1000, -30));
|
|
85
|
+
|
|
86
|
+
expect(result.current.activeCorners[2]).toEqual({ x: 800, y: 0 });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("moves both corners of an edge dragged in one event", () => {
|
|
90
|
+
let { result } = setup();
|
|
91
|
+
|
|
92
|
+
// What the top edge handle does: shift TL and TR by the same delta.
|
|
93
|
+
act(() => {
|
|
94
|
+
result.current.updateCorner(0, 0, 20);
|
|
95
|
+
result.current.updateCorner(1, 400, 20);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
expect(result.current.activeCorners.slice(0, 2)).toEqual([
|
|
99
|
+
{ x: 0, y: 40 },
|
|
100
|
+
{ x: 800, y: 40 },
|
|
101
|
+
]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("does not let a late suggestion overwrite corners the user has moved", () => {
|
|
105
|
+
let { result, rerender } = setup();
|
|
106
|
+
|
|
107
|
+
act(() => result.current.updateCorner(0, 50, 25));
|
|
108
|
+
suggestion = SUGGESTED;
|
|
109
|
+
rerender({ url: "blob:image" });
|
|
110
|
+
|
|
111
|
+
expect(result.current.activeCorners[0]).toEqual({ x: 100, y: 50 });
|
|
112
|
+
expect(result.current.activeCorners[1]).toEqual(FULL[1]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("drawing a rectangle", () => {
|
|
116
|
+
it("clears the selection until a rectangle is drawn", () => {
|
|
117
|
+
let { result } = setup();
|
|
118
|
+
|
|
119
|
+
act(() => result.current.startRectMode());
|
|
120
|
+
|
|
121
|
+
expect(result.current.mode).toBe("rect");
|
|
122
|
+
expect(result.current.hasSelection).toBe(false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("shows the rectangle while dragging and selects it on release", () => {
|
|
126
|
+
let { result } = setup();
|
|
127
|
+
act(() => result.current.startRectMode());
|
|
128
|
+
|
|
129
|
+
// Dragged from bottom-right to top-left, in CSS px.
|
|
130
|
+
act(() => result.current.startRectDraw(110, 60));
|
|
131
|
+
act(() => result.current.updateRectDraw(10, 10));
|
|
132
|
+
let expected: Quad = [
|
|
133
|
+
{ x: 20, y: 20 },
|
|
134
|
+
{ x: 220, y: 20 },
|
|
135
|
+
{ x: 220, y: 120 },
|
|
136
|
+
{ x: 20, y: 120 },
|
|
137
|
+
];
|
|
138
|
+
expect(result.current.hasSelection).toBe(true);
|
|
139
|
+
expect(result.current.activeCorners).toEqual(expected);
|
|
140
|
+
|
|
141
|
+
act(() => result.current.finishRectDraw());
|
|
142
|
+
expect(result.current.mode).toBe("corners");
|
|
143
|
+
expect(result.current.activeCorners).toEqual(expected);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("ignores a tap and stays in rectangle mode", () => {
|
|
147
|
+
let { result } = setup();
|
|
148
|
+
act(() => result.current.startRectMode());
|
|
149
|
+
|
|
150
|
+
act(() => result.current.startRectDraw(50, 50));
|
|
151
|
+
act(() => result.current.updateRectDraw(52, 90));
|
|
152
|
+
act(() => result.current.finishRectDraw());
|
|
153
|
+
|
|
154
|
+
expect(result.current.mode).toBe("rect");
|
|
155
|
+
expect(result.current.hasSelection).toBe(false);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("does not move corners while in rectangle mode", () => {
|
|
159
|
+
let { result } = setup();
|
|
160
|
+
act(() => result.current.startRectMode());
|
|
161
|
+
|
|
162
|
+
act(() => result.current.updateCorner(0, 50, 50));
|
|
163
|
+
act(() => result.current.cancelRectMode());
|
|
164
|
+
|
|
165
|
+
expect(result.current.activeCorners).toEqual(FULL);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("restores the previous selection when the user backs out", () => {
|
|
169
|
+
let { result } = setup();
|
|
170
|
+
act(() => result.current.updateCorner(0, 50, 25));
|
|
171
|
+
act(() => result.current.startRectMode());
|
|
172
|
+
|
|
173
|
+
act(() => result.current.cancelRectMode());
|
|
174
|
+
|
|
175
|
+
expect(result.current.mode).toBe("corners");
|
|
176
|
+
expect(result.current.activeCorners[0]).toEqual({ x: 100, y: 50 });
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("falls back to the suggested border when backing out before any selection was made", () => {
|
|
180
|
+
let { result, rerender } = setup();
|
|
181
|
+
suggestion = SUGGESTED;
|
|
182
|
+
rerender({ url: "blob:image" });
|
|
183
|
+
act(() => result.current.startRectMode());
|
|
184
|
+
|
|
185
|
+
act(() => result.current.cancelRectMode());
|
|
186
|
+
|
|
187
|
+
expect(result.current.activeCorners).toEqual(SUGGESTED);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("cannot apply until the rectangle is drawn", async () => {
|
|
191
|
+
let { result } = setup();
|
|
192
|
+
act(() => result.current.startRectMode());
|
|
193
|
+
|
|
194
|
+
let applied: unknown;
|
|
195
|
+
await act(async () => {
|
|
196
|
+
applied = await result.current.applyCrop();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
expect(applied).toBeNull();
|
|
200
|
+
expect(result.current.cropping).toBe(false);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("starts over on a new image, e.g. after a rotate", () => {
|
|
205
|
+
let { result, rerender } = setup();
|
|
206
|
+
act(() => result.current.updateCorner(0, 50, 25));
|
|
207
|
+
act(() => result.current.startRectMode());
|
|
208
|
+
|
|
209
|
+
rerender({ url: "blob:rotated" });
|
|
210
|
+
|
|
211
|
+
expect(result.current.mode).toBe("corners");
|
|
212
|
+
expect(result.current.activeCorners[0]).toEqual({ x: 0, y: 0 });
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("passes on an OpenCV load failure so the overlay can explain it", () => {
|
|
216
|
+
let failure = new Error("Failed to load OpenCV.js");
|
|
217
|
+
openCV = { cv: null, ready: false, error: failure };
|
|
218
|
+
|
|
219
|
+
let { result } = setup();
|
|
220
|
+
|
|
221
|
+
expect(result.current.cvReady).toBe(false);
|
|
222
|
+
expect(result.current.cvError).toBe(failure);
|
|
223
|
+
expect(result.current.activeCorners).toEqual(FULL);
|
|
224
|
+
});
|
|
225
|
+
});
|