@olenbetong/synergi-react 5.3.1 → 5.4.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/ImageEditor/ImageEditor.d.ts +6 -0
- package/es/components/ImageEditor/ImageEditor.js +6 -2
- package/es/components/ImageEditor/ResponsiveButton.d.ts +2 -1
- package/es/components/ImageEditor/ResponsiveButton.js +3 -3
- package/es/components/ImageEditor/plugins/cropPlugin/CropContext.d.ts +2 -1
- package/es/components/ImageEditor/plugins/cropPlugin/CropContext.js +2 -2
- package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +27 -7
- package/es/components/ImageEditor/types.d.ts +5 -0
- package/package.json +6 -6
- package/src/components/ImageEditor/ImageEditor.test.tsx +103 -0
- package/src/components/ImageEditor/ImageEditor.tsx +18 -2
- package/src/components/ImageEditor/ResponsiveButton.tsx +4 -3
- package/src/components/ImageEditor/plugins/cropPlugin/CropContext.tsx +5 -2
- package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.test.tsx +118 -0
- package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +52 -18
- package/src/components/ImageEditor/types.ts +5 -0
|
@@ -40,6 +40,12 @@ interface Props {
|
|
|
40
40
|
* plain image. Cancelling the tool returns to the plain image as usual.
|
|
41
41
|
*/
|
|
42
42
|
initialTool?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Id of a tool plugin (e.g. `"crop"`) that also offers a Done button, which applies the tool and hands the
|
|
45
|
+
* result to `onConfirm` so the user does not confirm twice. Its apply button still returns to the plain
|
|
46
|
+
* image, for the other tools and the floating Done button.
|
|
47
|
+
*/
|
|
48
|
+
finishWithTool?: string;
|
|
43
49
|
/**
|
|
44
50
|
* How the image is handed out (default JPEG at 0.92, any size). Edits are kept lossless in between and
|
|
45
51
|
* encoded once, here, so repeated rotations or crops do not lose quality step by step. When set, it applies
|
|
@@ -15,7 +15,7 @@ import { getLocalizedString } from "../../i18n.js";
|
|
|
15
15
|
* Tool plugins contribute an overlay and toolbar; action plugins execute immediately.
|
|
16
16
|
* Supports undo/redo: each applied transformation is recorded with its EditAction list.
|
|
17
17
|
*/
|
|
18
|
-
export const ImageEditor = forwardRef(function ImageEditor({ imageUrl, onConfirm, onCancel, plugins, viewPlugin, initialTool, output }, ref) {
|
|
18
|
+
export const ImageEditor = forwardRef(function ImageEditor({ imageUrl, onConfirm, onCancel, plugins, viewPlugin, initialTool, finishWithTool, output }, ref) {
|
|
19
19
|
let registry = useMemo(() => buildActionRegistry(plugins), [plugins]);
|
|
20
20
|
let history = useImageHistory(imageUrl, registry);
|
|
21
21
|
let { activePlugin, activateTool, deactivateTool } = useActiveTool(plugins, initialTool ?? null);
|
|
@@ -62,6 +62,10 @@ export const ImageEditor = forwardRef(function ImageEditor({ imageUrl, onConfirm
|
|
|
62
62
|
history.applyActions(blob, actions);
|
|
63
63
|
deactivateTool();
|
|
64
64
|
}
|
|
65
|
+
// The history never sees this result: the editor closes on it, so it goes straight out.
|
|
66
|
+
async function handleToolFinish(blob) {
|
|
67
|
+
onConfirm?.(await encodeImage(blob, outputOptions()));
|
|
68
|
+
}
|
|
65
69
|
function outputOptions(options) {
|
|
66
70
|
return { ...output, ...options };
|
|
67
71
|
}
|
|
@@ -94,7 +98,7 @@ export const ImageEditor = forwardRef(function ImageEditor({ imageUrl, onConfirm
|
|
|
94
98
|
// Wrap with the active tool's provider so Overlay and Toolbar share context.
|
|
95
99
|
if (activePlugin) {
|
|
96
100
|
let ActiveProvider = activePlugin.Provider;
|
|
97
|
-
inner = (_jsx(ActiveProvider, { imageUrl: history.currentImageUrl, onApply: handleToolApply, onCancel: deactivateTool, children: inner }));
|
|
101
|
+
inner = (_jsx(ActiveProvider, { imageUrl: history.currentImageUrl, onApply: handleToolApply, onCancel: deactivateTool, onFinish: onConfirm && activePlugin.id === finishWithTool ? handleToolFinish : undefined, children: inner }));
|
|
98
102
|
}
|
|
99
103
|
// Wrap with the view plugin's provider (outermost) so both CanvasWrapper and TopBarControls share state.
|
|
100
104
|
if (viewPlugin) {
|
|
@@ -4,8 +4,9 @@ interface Props {
|
|
|
4
4
|
label: string;
|
|
5
5
|
onClick: () => void;
|
|
6
6
|
disabled?: boolean;
|
|
7
|
+
pressed?: boolean;
|
|
7
8
|
isSmall: boolean;
|
|
8
9
|
}
|
|
9
10
|
/** Renders a labelled button on large screens and a tooltipped icon button on small ones. */
|
|
10
|
-
export declare function ResponsiveButton({ icon, label, onClick, disabled, isSmall }: Props): import("react").JSX.Element;
|
|
11
|
+
export declare function ResponsiveButton({ icon, label, onClick, disabled, pressed, isSmall }: Props): import("react").JSX.Element;
|
|
11
12
|
export {};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Button, Tooltip } from "@digdir/designsystemet-react";
|
|
3
3
|
/** Renders a labelled button on large screens and a tooltipped icon button on small ones. */
|
|
4
|
-
export function ResponsiveButton({ icon, label, onClick, disabled, isSmall }) {
|
|
4
|
+
export function ResponsiveButton({ icon, label, onClick, disabled, pressed, isSmall }) {
|
|
5
5
|
if (isSmall) {
|
|
6
|
-
return (_jsx(Tooltip, { content: label, children: _jsx(Button, { icon: true, variant: "tertiary", "aria-label": label, onClick: onClick, disabled: disabled, children: icon }) }));
|
|
6
|
+
return (_jsx(Tooltip, { content: label, children: _jsx(Button, { icon: true, variant: "tertiary", "aria-label": label, "aria-pressed": pressed, onClick: onClick, disabled: disabled, children: icon }) }));
|
|
7
7
|
}
|
|
8
|
-
return (_jsxs(Button, { variant: "tertiary", onClick: onClick, disabled: disabled, children: [icon, label] }));
|
|
8
|
+
return (_jsxs(Button, { variant: "tertiary", "aria-pressed": pressed, onClick: onClick, disabled: disabled, children: [icon, label] }));
|
|
9
9
|
}
|
|
@@ -4,7 +4,8 @@ interface CropContextValue extends CropToolState {
|
|
|
4
4
|
imageUrl: string;
|
|
5
5
|
onApply: (blob: Blob, actions: EditAction[]) => void;
|
|
6
6
|
onCancel: () => void;
|
|
7
|
+
onFinish?: (blob: Blob) => Promise<void>;
|
|
7
8
|
}
|
|
8
|
-
export declare function CropProvider({ imageUrl, onApply, onCancel, children }: ToolProviderProps): import("react").JSX.Element;
|
|
9
|
+
export declare function CropProvider({ imageUrl, onApply, onCancel, onFinish, children }: ToolProviderProps): import("react").JSX.Element;
|
|
9
10
|
export declare function useCropContext(): CropContextValue;
|
|
10
11
|
export {};
|
|
@@ -2,9 +2,9 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { createContext, useContext } from "react";
|
|
3
3
|
import { useCropTool } from "./useCropTool.js";
|
|
4
4
|
let CropContext = createContext(null);
|
|
5
|
-
export function CropProvider({ imageUrl, onApply, onCancel, children }) {
|
|
5
|
+
export function CropProvider({ imageUrl, onApply, onCancel, onFinish, children }) {
|
|
6
6
|
let state = useCropTool(imageUrl, true);
|
|
7
|
-
return _jsx(CropContext.Provider, { value: { ...state, imageUrl, onApply, onCancel }, children: children });
|
|
7
|
+
return (_jsx(CropContext.Provider, { value: { ...state, imageUrl, onApply, onCancel, onFinish }, children: children }));
|
|
8
8
|
}
|
|
9
9
|
export function useCropContext() {
|
|
10
10
|
let ctx = useContext(CropContext);
|
|
@@ -1,16 +1,36 @@
|
|
|
1
1
|
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Button, Spinner } from "@digdir/designsystemet-react";
|
|
3
|
-
import { CheckmarkIcon, ExpandIcon, SquareIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
3
|
+
import { CheckmarkIcon, ExpandIcon, ScissorsIcon, SquareIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
4
|
+
import { useState } from "react";
|
|
4
5
|
import { useCropContext } from "./CropContext.js";
|
|
5
6
|
import { getLocalizedString } from "../../../../i18n.js";
|
|
7
|
+
import { ResponsiveButton } from "../../ResponsiveButton.js";
|
|
8
|
+
import { useIsSmall } from "../../useIsSmall.js";
|
|
6
9
|
/** Toolbar shown while the crop tool is active. Reads state from CropContext. */
|
|
7
10
|
export function CropToolbar() {
|
|
8
|
-
let { applyCrop, cropping, mode, hasSelection, startRectMode, cancelRectMode, onApply, onCancel } = useCropContext();
|
|
11
|
+
let { applyCrop, cropping, mode, hasSelection, startRectMode, cancelRectMode, onApply, onCancel, onFinish } = useCropContext();
|
|
9
12
|
let inRectMode = mode === "rect";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
// Covers the crop and whatever the editor does with the result, so a second click cannot apply it twice.
|
|
14
|
+
let [running, setRunning] = useState(null);
|
|
15
|
+
let busy = cropping || running !== null;
|
|
16
|
+
// With Done as well there are too many labelled buttons for a phone.
|
|
17
|
+
let compact = useIsSmall() && !!onFinish;
|
|
18
|
+
async function run(kind) {
|
|
19
|
+
setRunning(kind);
|
|
20
|
+
try {
|
|
21
|
+
let result = await applyCrop();
|
|
22
|
+
if (!result)
|
|
23
|
+
return;
|
|
24
|
+
if (kind === "finish")
|
|
25
|
+
await onFinish?.(result.blob);
|
|
26
|
+
else
|
|
27
|
+
onApply(result.blob, result.actions);
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
setRunning(null);
|
|
31
|
+
}
|
|
14
32
|
}
|
|
15
|
-
|
|
33
|
+
let spinner = _jsx(Spinner, { "aria-hidden": true, "data-size": "xs" });
|
|
34
|
+
let cannotApply = busy || inRectMode || !hasSelection;
|
|
35
|
+
return (_jsxs(_Fragment, { children: [_jsx("div", { className: "image-editor__toolbar-group", children: _jsxs(Button, { variant: "tertiary", onClick: onCancel, disabled: busy, children: [_jsx(XMarkIcon, { "aria-hidden": true }), getLocalizedString("Cancel")] }) }), _jsxs("div", { className: "image-editor__toolbar-group", children: [_jsx(ResponsiveButton, { icon: inRectMode ? _jsx(ExpandIcon, { "aria-hidden": true }) : _jsx(SquareIcon, { "aria-hidden": true }), label: inRectMode ? getLocalizedString("Adjust corners") : getLocalizedString("Draw rectangle"), onClick: inRectMode ? cancelRectMode : startRectMode, disabled: busy, pressed: inRectMode, isSmall: compact }), onFinish ? (_jsxs(_Fragment, { children: [_jsx(ResponsiveButton, { icon: running === "apply" ? spinner : _jsx(ScissorsIcon, { "aria-hidden": true }), label: getLocalizedString("Apply crop"), onClick: () => void run("apply"), disabled: cannotApply, isSmall: compact }), _jsxs(Button, { variant: "primary", onClick: () => void run("finish"), disabled: cannotApply, children: [running === "finish" ? spinner : _jsx(CheckmarkIcon, { "aria-hidden": true }), getLocalizedString("Done")] })] })) : (_jsxs(Button, { variant: "primary", onClick: () => void run("apply"), disabled: cannotApply, children: [running === "apply" ? spinner : _jsx(CheckmarkIcon, { "aria-hidden": true }), getLocalizedString("Apply crop")] }))] })] }));
|
|
16
36
|
}
|
|
@@ -19,6 +19,11 @@ export interface ToolProviderProps {
|
|
|
19
19
|
/** Called when the tool has produced a transformed image. */
|
|
20
20
|
onApply: (blob: Blob, actions: EditAction[]) => void;
|
|
21
21
|
onCancel: () => void;
|
|
22
|
+
/**
|
|
23
|
+
* Set when the app lets this tool finish the editing. The tool should then offer it next to applying, and
|
|
24
|
+
* call this instead of `onApply`. Settles once the result has been handed out.
|
|
25
|
+
*/
|
|
26
|
+
onFinish?: (blob: Blob) => Promise<void>;
|
|
22
27
|
children: ReactNode;
|
|
23
28
|
}
|
|
24
29
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/synergi-react",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.4.0",
|
|
4
4
|
"description": "Standalone React component for SynergiWeb and Partner Portal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -55,9 +55,9 @@
|
|
|
55
55
|
"@emotion/styled": "^11.14.1",
|
|
56
56
|
"@mui/icons-material": "9.5.0",
|
|
57
57
|
"@mui/material": "9.5.0",
|
|
58
|
-
"@olenbetong/appframe-core": "^2.
|
|
59
|
-
"@olenbetong/appframe-data": "^1.
|
|
60
|
-
"@olenbetong/appframe-ds": "^1.
|
|
58
|
+
"@olenbetong/appframe-core": "^2.16.0",
|
|
59
|
+
"@olenbetong/appframe-data": "^1.9.0",
|
|
60
|
+
"@olenbetong/appframe-ds": "^1.10.0",
|
|
61
61
|
"@olenbetong/appframe-mui": "^7.0.13",
|
|
62
62
|
"@olenbetong/appframe-react": "^2.2.8",
|
|
63
63
|
"clsx": "^2.1.1",
|
|
@@ -92,7 +92,7 @@
|
|
|
92
92
|
"@digdir/designsystemet-css": "1.23.0",
|
|
93
93
|
"@digdir/designsystemet-react": "1.23.0",
|
|
94
94
|
"@digdir/designsystemet-web": "1.23.0",
|
|
95
|
-
"@navikt/aksel-icons": "8.
|
|
95
|
+
"@navikt/aksel-icons": "8.21.0",
|
|
96
96
|
"@testing-library/dom": "^10.4.2",
|
|
97
97
|
"@testing-library/jest-dom": "^7.0.1",
|
|
98
98
|
"@testing-library/react": "16.3.3",
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
"happy-dom": "21.0.4",
|
|
105
105
|
"playwright": "^1.64.1",
|
|
106
106
|
"replace-in-files": "^3.0.0",
|
|
107
|
-
"sass": "1.
|
|
107
|
+
"sass": "1.107.1",
|
|
108
108
|
"typescript": "7.0.2",
|
|
109
109
|
"vitest": "5.0.5"
|
|
110
110
|
},
|
|
@@ -63,6 +63,7 @@ function toolPlugin(id: string, label: string) {
|
|
|
63
63
|
<>
|
|
64
64
|
<button onClick={() => tool?.onApply(applied, [{ type: id, params: { n: 1 } }])}>Apply {id}</button>
|
|
65
65
|
<button onClick={() => tool?.onCancel()}>Cancel {id}</button>
|
|
66
|
+
{tool?.onFinish && <button onClick={() => void tool.onFinish?.(applied)}>Finish {id}</button>}
|
|
66
67
|
</>
|
|
67
68
|
);
|
|
68
69
|
},
|
|
@@ -210,6 +211,108 @@ describe("ImageEditor", () => {
|
|
|
210
211
|
});
|
|
211
212
|
});
|
|
212
213
|
|
|
214
|
+
describe("finishWithTool", () => {
|
|
215
|
+
it("hands the result of the tool to the app without another confirmation", async () => {
|
|
216
|
+
let onConfirm = vi.fn();
|
|
217
|
+
let crop = toolPlugin("crop", "Crop");
|
|
218
|
+
render(
|
|
219
|
+
<ImageEditor
|
|
220
|
+
imageUrl={SOURCE}
|
|
221
|
+
plugins={[crop.plugin]}
|
|
222
|
+
initialTool="crop"
|
|
223
|
+
finishWithTool="crop"
|
|
224
|
+
onConfirm={onConfirm}
|
|
225
|
+
/>,
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
fireEvent.click(button("Finish crop"));
|
|
229
|
+
|
|
230
|
+
await waitFor(() => expect(onConfirm).toHaveBeenCalledOnce());
|
|
231
|
+
expect(await (onConfirm.mock.calls[0][0] as Blob).text()).toBe("crop result");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("applying the tool instead keeps editing, so the other tools can follow the crop", async () => {
|
|
235
|
+
let onConfirm = vi.fn();
|
|
236
|
+
let rotate = actionPlugin("rotate-cw", "Rotate right");
|
|
237
|
+
let crop = toolPlugin("crop", "Crop");
|
|
238
|
+
render(
|
|
239
|
+
<ImageEditor
|
|
240
|
+
imageUrl={SOURCE}
|
|
241
|
+
plugins={[rotate, crop.plugin]}
|
|
242
|
+
initialTool="crop"
|
|
243
|
+
finishWithTool="crop"
|
|
244
|
+
onConfirm={onConfirm}
|
|
245
|
+
/>,
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
fireEvent.click(button("Apply crop"));
|
|
249
|
+
|
|
250
|
+
expect(image()).toHaveAttribute("src", "blob:edit-1");
|
|
251
|
+
expect(onConfirm).not.toHaveBeenCalled();
|
|
252
|
+
|
|
253
|
+
fireEvent.click(button("Rotate right"));
|
|
254
|
+
await waitFor(() => expect(image()).toHaveAttribute("src", "blob:edit-2"));
|
|
255
|
+
fireEvent.click(button("Done"));
|
|
256
|
+
|
|
257
|
+
await waitFor(() => expect(onConfirm).toHaveBeenCalledOnce());
|
|
258
|
+
expect(await (onConfirm.mock.calls[0][0] as Blob).text()).toBe("rotate-cw(blob:edit-1)");
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("only offers finishing on the tool that was named", () => {
|
|
262
|
+
let props: Record<string, ToolProviderProps> = {};
|
|
263
|
+
let spy = (id: string, plugin: ToolPlugin): ToolPlugin => ({
|
|
264
|
+
...plugin,
|
|
265
|
+
Provider: (toolProps: ToolProviderProps) => {
|
|
266
|
+
props[id] = toolProps;
|
|
267
|
+
return <plugin.Provider {...toolProps} />;
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
render(
|
|
271
|
+
<ImageEditor
|
|
272
|
+
imageUrl={SOURCE}
|
|
273
|
+
plugins={[
|
|
274
|
+
spy("crop", toolPlugin("crop", "Crop").plugin),
|
|
275
|
+
spy("redact", toolPlugin("redact", "Redact").plugin),
|
|
276
|
+
]}
|
|
277
|
+
finishWithTool="crop"
|
|
278
|
+
onConfirm={() => {}}
|
|
279
|
+
/>,
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
fireEvent.click(button("Redact"));
|
|
283
|
+
expect(props.redact.onFinish).toBeUndefined();
|
|
284
|
+
fireEvent.click(button("Cancel redact"));
|
|
285
|
+
|
|
286
|
+
fireEvent.click(button("Crop"));
|
|
287
|
+
expect(props.crop.onFinish).toBeInstanceOf(Function);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("shows the usual Done button once the tool is cancelled", () => {
|
|
291
|
+
let crop = toolPlugin("crop", "Crop");
|
|
292
|
+
render(
|
|
293
|
+
<ImageEditor
|
|
294
|
+
imageUrl={SOURCE}
|
|
295
|
+
plugins={[crop.plugin]}
|
|
296
|
+
initialTool="crop"
|
|
297
|
+
finishWithTool="crop"
|
|
298
|
+
onConfirm={() => {}}
|
|
299
|
+
/>,
|
|
300
|
+
);
|
|
301
|
+
expect(screen.queryByRole("button", { name: "Done" })).not.toBeInTheDocument();
|
|
302
|
+
|
|
303
|
+
fireEvent.click(button("Cancel crop"));
|
|
304
|
+
|
|
305
|
+
expect(button("Done")).toBeInTheDocument();
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it("keeps the tool an ordinary one when the app takes no result", () => {
|
|
309
|
+
let crop = toolPlugin("crop", "Crop");
|
|
310
|
+
render(<ImageEditor imageUrl={SOURCE} plugins={[crop.plugin]} initialTool="crop" finishWithTool="crop" />);
|
|
311
|
+
|
|
312
|
+
expect(screen.queryByRole("button", { name: "Finish crop" })).not.toBeInTheDocument();
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
|
|
213
316
|
describe("ref (external toolbars, as in the delivery ticket scans app)", () => {
|
|
214
317
|
it("runs actions one after another, each on the previous result", async () => {
|
|
215
318
|
let ref = createRef<ImageEditorRef>();
|
|
@@ -51,6 +51,12 @@ interface Props {
|
|
|
51
51
|
* plain image. Cancelling the tool returns to the plain image as usual.
|
|
52
52
|
*/
|
|
53
53
|
initialTool?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Id of a tool plugin (e.g. `"crop"`) that also offers a Done button, which applies the tool and hands the
|
|
56
|
+
* result to `onConfirm` so the user does not confirm twice. Its apply button still returns to the plain
|
|
57
|
+
* image, for the other tools and the floating Done button.
|
|
58
|
+
*/
|
|
59
|
+
finishWithTool?: string;
|
|
54
60
|
/**
|
|
55
61
|
* How the image is handed out (default JPEG at 0.92, any size). Edits are kept lossless in between and
|
|
56
62
|
* encoded once, here, so repeated rotations or crops do not lose quality step by step. When set, it applies
|
|
@@ -67,7 +73,7 @@ interface Props {
|
|
|
67
73
|
* Supports undo/redo: each applied transformation is recorded with its EditAction list.
|
|
68
74
|
*/
|
|
69
75
|
export const ImageEditor = forwardRef<ImageEditorRef, Props>(function ImageEditor(
|
|
70
|
-
{ imageUrl, onConfirm, onCancel, plugins, viewPlugin, initialTool, output }: Props,
|
|
76
|
+
{ imageUrl, onConfirm, onCancel, plugins, viewPlugin, initialTool, finishWithTool, output }: Props,
|
|
71
77
|
ref,
|
|
72
78
|
) {
|
|
73
79
|
let registry = useMemo(() => buildActionRegistry(plugins), [plugins]);
|
|
@@ -121,6 +127,11 @@ export const ImageEditor = forwardRef<ImageEditorRef, Props>(function ImageEdito
|
|
|
121
127
|
deactivateTool();
|
|
122
128
|
}
|
|
123
129
|
|
|
130
|
+
// The history never sees this result: the editor closes on it, so it goes straight out.
|
|
131
|
+
async function handleToolFinish(blob: Blob) {
|
|
132
|
+
onConfirm?.(await encodeImage(blob, outputOptions()));
|
|
133
|
+
}
|
|
134
|
+
|
|
124
135
|
function outputOptions(options?: ImageOutputOptions): ImageOutputOptions {
|
|
125
136
|
return { ...output, ...options };
|
|
126
137
|
}
|
|
@@ -274,7 +285,12 @@ export const ImageEditor = forwardRef<ImageEditorRef, Props>(function ImageEdito
|
|
|
274
285
|
if (activePlugin) {
|
|
275
286
|
let ActiveProvider = activePlugin.Provider;
|
|
276
287
|
inner = (
|
|
277
|
-
<ActiveProvider
|
|
288
|
+
<ActiveProvider
|
|
289
|
+
imageUrl={history.currentImageUrl}
|
|
290
|
+
onApply={handleToolApply}
|
|
291
|
+
onCancel={deactivateTool}
|
|
292
|
+
onFinish={onConfirm && activePlugin.id === finishWithTool ? handleToolFinish : undefined}
|
|
293
|
+
>
|
|
278
294
|
{inner}
|
|
279
295
|
</ActiveProvider>
|
|
280
296
|
);
|
|
@@ -6,15 +6,16 @@ interface Props {
|
|
|
6
6
|
label: string;
|
|
7
7
|
onClick: () => void;
|
|
8
8
|
disabled?: boolean;
|
|
9
|
+
pressed?: boolean;
|
|
9
10
|
isSmall: boolean;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
/** Renders a labelled button on large screens and a tooltipped icon button on small ones. */
|
|
13
|
-
export function ResponsiveButton({ icon, label, onClick, disabled, isSmall }: Props) {
|
|
14
|
+
export function ResponsiveButton({ icon, label, onClick, disabled, pressed, isSmall }: Props) {
|
|
14
15
|
if (isSmall) {
|
|
15
16
|
return (
|
|
16
17
|
<Tooltip content={label}>
|
|
17
|
-
<Button icon variant="tertiary" aria-label={label} onClick={onClick} disabled={disabled}>
|
|
18
|
+
<Button icon variant="tertiary" aria-label={label} aria-pressed={pressed} onClick={onClick} disabled={disabled}>
|
|
18
19
|
{icon}
|
|
19
20
|
</Button>
|
|
20
21
|
</Tooltip>
|
|
@@ -22,7 +23,7 @@ export function ResponsiveButton({ icon, label, onClick, disabled, isSmall }: Pr
|
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
return (
|
|
25
|
-
<Button variant="tertiary" onClick={onClick} disabled={disabled}>
|
|
26
|
+
<Button variant="tertiary" aria-pressed={pressed} onClick={onClick} disabled={disabled}>
|
|
26
27
|
{icon}
|
|
27
28
|
{label}
|
|
28
29
|
</Button>
|
|
@@ -8,13 +8,16 @@ interface CropContextValue extends CropToolState {
|
|
|
8
8
|
imageUrl: string;
|
|
9
9
|
onApply: (blob: Blob, actions: EditAction[]) => void;
|
|
10
10
|
onCancel: () => void;
|
|
11
|
+
onFinish?: (blob: Blob) => Promise<void>;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
let CropContext = createContext<CropContextValue | null>(null);
|
|
14
15
|
|
|
15
|
-
export function CropProvider({ imageUrl, onApply, onCancel, children }: ToolProviderProps) {
|
|
16
|
+
export function CropProvider({ imageUrl, onApply, onCancel, onFinish, children }: ToolProviderProps) {
|
|
16
17
|
let state = useCropTool(imageUrl, true);
|
|
17
|
-
return
|
|
18
|
+
return (
|
|
19
|
+
<CropContext.Provider value={{ ...state, imageUrl, onApply, onCancel, onFinish }}>{children}</CropContext.Provider>
|
|
20
|
+
);
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
export function useCropContext(): CropContextValue {
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { CropToolbar } from "./CropToolbar";
|
|
5
|
+
|
|
6
|
+
const context = vi.hoisted(() => ({
|
|
7
|
+
applyCrop: vi.fn(),
|
|
8
|
+
cropping: false,
|
|
9
|
+
mode: "corners",
|
|
10
|
+
hasSelection: true,
|
|
11
|
+
startRectMode: vi.fn(),
|
|
12
|
+
cancelRectMode: vi.fn(),
|
|
13
|
+
onApply: vi.fn(),
|
|
14
|
+
onCancel: vi.fn(),
|
|
15
|
+
onFinish: undefined as undefined | ((blob: Blob) => Promise<void>),
|
|
16
|
+
}));
|
|
17
|
+
const small = vi.hoisted(() => ({ value: false }));
|
|
18
|
+
|
|
19
|
+
vi.mock("./CropContext.js", () => ({ useCropContext: () => context }));
|
|
20
|
+
vi.mock("../../useIsSmall.js", () => ({ useIsSmall: () => small.value }));
|
|
21
|
+
|
|
22
|
+
const cropped = new Blob(["cropped"]);
|
|
23
|
+
const actions = [{ type: "crop", params: {} }];
|
|
24
|
+
|
|
25
|
+
function button(name: string) {
|
|
26
|
+
return screen.getByRole("button", { name });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
// The spinner synchronises its animation, which jsdom does not have.
|
|
31
|
+
document.getAnimations = () => [];
|
|
32
|
+
vi.clearAllMocks();
|
|
33
|
+
context.applyCrop.mockResolvedValue({ blob: cropped, actions });
|
|
34
|
+
context.mode = "corners";
|
|
35
|
+
context.hasSelection = true;
|
|
36
|
+
context.onFinish = undefined;
|
|
37
|
+
small.value = false;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("CropToolbar", () => {
|
|
41
|
+
it("only applies when the crop does not finish the editing", async () => {
|
|
42
|
+
render(<CropToolbar />);
|
|
43
|
+
|
|
44
|
+
expect(screen.queryByRole("button", { name: "Done" })).not.toBeInTheDocument();
|
|
45
|
+
fireEvent.click(button("Apply crop"));
|
|
46
|
+
|
|
47
|
+
await waitFor(() => expect(context.onApply).toHaveBeenCalledWith(cropped, actions));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("when the crop can finish the editing", () => {
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
context.onFinish = vi.fn(async () => {});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("Done hands the crop out", async () => {
|
|
56
|
+
render(<CropToolbar />);
|
|
57
|
+
|
|
58
|
+
fireEvent.click(button("Done"));
|
|
59
|
+
|
|
60
|
+
await waitFor(() => expect(context.onFinish).toHaveBeenCalledWith(cropped));
|
|
61
|
+
expect(context.onApply).not.toHaveBeenCalled();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("Apply crop keeps editing", async () => {
|
|
65
|
+
render(<CropToolbar />);
|
|
66
|
+
|
|
67
|
+
fireEvent.click(button("Apply crop"));
|
|
68
|
+
|
|
69
|
+
await waitFor(() => expect(context.onApply).toHaveBeenCalledWith(cropped, actions));
|
|
70
|
+
expect(context.onFinish).not.toHaveBeenCalled();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("does nothing with a crop that failed", async () => {
|
|
74
|
+
context.applyCrop.mockResolvedValue(null);
|
|
75
|
+
render(<CropToolbar />);
|
|
76
|
+
|
|
77
|
+
await act(async () => {
|
|
78
|
+
fireEvent.click(button("Done"));
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
expect(context.onFinish).not.toHaveBeenCalled();
|
|
82
|
+
expect(button("Done")).toBeEnabled();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("blocks both buttons while a crop is running, so it cannot be applied twice", async () => {
|
|
86
|
+
let finish!: () => void;
|
|
87
|
+
context.onFinish = vi.fn(() => new Promise<void>((resolve) => (finish = resolve)));
|
|
88
|
+
render(<CropToolbar />);
|
|
89
|
+
|
|
90
|
+
fireEvent.click(button("Done"));
|
|
91
|
+
await waitFor(() => expect(context.onFinish).toHaveBeenCalled());
|
|
92
|
+
|
|
93
|
+
expect(button("Done")).toBeDisabled();
|
|
94
|
+
expect(button("Apply crop")).toBeDisabled();
|
|
95
|
+
|
|
96
|
+
await act(async () => finish());
|
|
97
|
+
expect(button("Done")).toBeEnabled();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("cannot apply or finish without a selection", () => {
|
|
101
|
+
context.hasSelection = false;
|
|
102
|
+
render(<CropToolbar />);
|
|
103
|
+
|
|
104
|
+
expect(button("Done")).toBeDisabled();
|
|
105
|
+
expect(button("Apply crop")).toBeDisabled();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("keeps the buttons reachable by name when they collapse to icons on a phone", () => {
|
|
109
|
+
small.value = true;
|
|
110
|
+
render(<CropToolbar />);
|
|
111
|
+
|
|
112
|
+
expect(button("Apply crop")).toHaveTextContent("");
|
|
113
|
+
expect(button("Draw rectangle")).toHaveAttribute("aria-pressed", "false");
|
|
114
|
+
expect(button("Done")).toHaveTextContent("Done");
|
|
115
|
+
expect(button("Cancel")).toHaveTextContent("Cancel");
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -1,41 +1,75 @@
|
|
|
1
1
|
import { Button, Spinner } from "@digdir/designsystemet-react";
|
|
2
|
-
import { CheckmarkIcon, ExpandIcon, SquareIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
2
|
+
import { CheckmarkIcon, ExpandIcon, ScissorsIcon, SquareIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
3
|
+
import { useState } from "react";
|
|
3
4
|
|
|
4
5
|
import { useCropContext } from "./CropContext.js";
|
|
5
6
|
import { getLocalizedString } from "../../../../i18n.js";
|
|
7
|
+
import { ResponsiveButton } from "../../ResponsiveButton.js";
|
|
8
|
+
import { useIsSmall } from "../../useIsSmall.js";
|
|
6
9
|
|
|
7
10
|
/** Toolbar shown while the crop tool is active. Reads state from CropContext. */
|
|
8
11
|
export function CropToolbar() {
|
|
9
|
-
let { applyCrop, cropping, mode, hasSelection, startRectMode, cancelRectMode, onApply, onCancel } =
|
|
12
|
+
let { applyCrop, cropping, mode, hasSelection, startRectMode, cancelRectMode, onApply, onCancel, onFinish } =
|
|
13
|
+
useCropContext();
|
|
10
14
|
let inRectMode = mode === "rect";
|
|
15
|
+
// Covers the crop and whatever the editor does with the result, so a second click cannot apply it twice.
|
|
16
|
+
let [running, setRunning] = useState<"apply" | "finish" | null>(null);
|
|
17
|
+
let busy = cropping || running !== null;
|
|
18
|
+
// With Done as well there are too many labelled buttons for a phone.
|
|
19
|
+
let compact = useIsSmall() && !!onFinish;
|
|
11
20
|
|
|
12
|
-
async function
|
|
13
|
-
|
|
14
|
-
|
|
21
|
+
async function run(kind: "apply" | "finish") {
|
|
22
|
+
setRunning(kind);
|
|
23
|
+
try {
|
|
24
|
+
let result = await applyCrop();
|
|
25
|
+
if (!result) return;
|
|
26
|
+
if (kind === "finish") await onFinish?.(result.blob);
|
|
27
|
+
else onApply(result.blob, result.actions);
|
|
28
|
+
} finally {
|
|
29
|
+
setRunning(null);
|
|
30
|
+
}
|
|
15
31
|
}
|
|
16
32
|
|
|
33
|
+
let spinner = <Spinner aria-hidden data-size="xs" />;
|
|
34
|
+
let cannotApply = busy || inRectMode || !hasSelection;
|
|
35
|
+
|
|
17
36
|
return (
|
|
18
37
|
<>
|
|
19
38
|
<div className="image-editor__toolbar-group">
|
|
20
|
-
<Button variant="tertiary" onClick={onCancel} disabled={
|
|
39
|
+
<Button variant="tertiary" onClick={onCancel} disabled={busy}>
|
|
21
40
|
<XMarkIcon aria-hidden />
|
|
22
41
|
{getLocalizedString("Cancel")}
|
|
23
42
|
</Button>
|
|
24
43
|
</div>
|
|
25
44
|
<div className="image-editor__toolbar-group">
|
|
26
|
-
<
|
|
27
|
-
|
|
28
|
-
|
|
45
|
+
<ResponsiveButton
|
|
46
|
+
icon={inRectMode ? <ExpandIcon aria-hidden /> : <SquareIcon aria-hidden />}
|
|
47
|
+
label={inRectMode ? getLocalizedString("Adjust corners") : getLocalizedString("Draw rectangle")}
|
|
29
48
|
onClick={inRectMode ? cancelRectMode : startRectMode}
|
|
30
|
-
disabled={
|
|
31
|
-
|
|
32
|
-
{
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
disabled={busy}
|
|
50
|
+
pressed={inRectMode}
|
|
51
|
+
isSmall={compact}
|
|
52
|
+
/>
|
|
53
|
+
{onFinish ? (
|
|
54
|
+
<>
|
|
55
|
+
<ResponsiveButton
|
|
56
|
+
icon={running === "apply" ? spinner : <ScissorsIcon aria-hidden />}
|
|
57
|
+
label={getLocalizedString("Apply crop")}
|
|
58
|
+
onClick={() => void run("apply")}
|
|
59
|
+
disabled={cannotApply}
|
|
60
|
+
isSmall={compact}
|
|
61
|
+
/>
|
|
62
|
+
<Button variant="primary" onClick={() => void run("finish")} disabled={cannotApply}>
|
|
63
|
+
{running === "finish" ? spinner : <CheckmarkIcon aria-hidden />}
|
|
64
|
+
{getLocalizedString("Done")}
|
|
65
|
+
</Button>
|
|
66
|
+
</>
|
|
67
|
+
) : (
|
|
68
|
+
<Button variant="primary" onClick={() => void run("apply")} disabled={cannotApply}>
|
|
69
|
+
{running === "apply" ? spinner : <CheckmarkIcon aria-hidden />}
|
|
70
|
+
{getLocalizedString("Apply crop")}
|
|
71
|
+
</Button>
|
|
72
|
+
)}
|
|
39
73
|
</div>
|
|
40
74
|
</>
|
|
41
75
|
);
|
|
@@ -23,6 +23,11 @@ export interface ToolProviderProps {
|
|
|
23
23
|
/** Called when the tool has produced a transformed image. */
|
|
24
24
|
onApply: (blob: Blob, actions: EditAction[]) => void;
|
|
25
25
|
onCancel: () => void;
|
|
26
|
+
/**
|
|
27
|
+
* Set when the app lets this tool finish the editing. The tool should then offer it next to applying, and
|
|
28
|
+
* call this instead of `onApply`. Settles once the result has been handed out.
|
|
29
|
+
*/
|
|
30
|
+
onFinish?: (blob: Blob) => Promise<void>;
|
|
26
31
|
children: ReactNode;
|
|
27
32
|
}
|
|
28
33
|
|