@rojaostudio/ds 2.0.0-next.2 → 2.0.0-next.20
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/README.md +42 -2
- package/THIRD_PARTY_NOTICES.md +66 -0
- package/dist/components/avatar.d.ts +20 -12
- package/dist/components/avatar.js +8 -6
- package/dist/components/badge.d.ts +7 -6
- package/dist/components/badge.js +2 -1
- package/dist/components/breadcrumb.d.ts +4 -4
- package/dist/components/breadcrumb.js +1 -1
- package/dist/components/bubble.d.ts +26 -11
- package/dist/components/bubble.js +3 -3
- package/dist/components/button.d.ts +1 -1
- package/dist/components/card.d.ts +28 -12
- package/dist/components/card.js +4 -2
- package/dist/components/chart.d.ts +17 -3
- package/dist/components/chart.js +70 -24
- package/dist/components/choice-carousel.d.ts +1 -1
- package/dist/components/choice-carousel.js +2 -4
- package/dist/components/choice-list.d.ts +40 -0
- package/dist/components/choice-list.js +122 -0
- package/dist/components/choice-preview-card.d.ts +1 -7
- package/dist/components/color-input.d.ts +1 -1
- package/dist/components/context-menu.d.ts +5 -2
- package/dist/components/currency-input.d.ts +0 -2
- package/dist/components/data-table-header.d.ts +26 -16
- package/dist/components/data-table-header.js +75 -12
- package/dist/components/date-picker.d.ts +1 -1
- package/dist/components/dialog.d.ts +6 -3
- package/dist/components/dialog.js +2 -2
- package/dist/components/dropdown-menu.d.ts +5 -2
- package/dist/components/dropzone.d.ts +2 -2
- package/dist/components/dropzone.js +1 -1
- package/dist/components/file-input.d.ts +13 -8
- package/dist/components/file-input.js +24 -21
- package/dist/components/filter-chip.d.ts +11 -5
- package/dist/components/filter-chip.js +15 -6
- package/dist/components/floating-stepper.d.ts +11 -28
- package/dist/components/floating-stepper.js +2 -30
- package/dist/components/heading.d.ts +42 -0
- package/dist/components/heading.js +25 -0
- package/dist/components/icon-button.d.ts +1 -1
- package/dist/components/image-crop-dialog.d.ts +60 -0
- package/dist/components/image-crop-dialog.js +240 -0
- package/dist/components/image-crop-modal.d.ts +12 -55
- package/dist/components/image-crop-modal.js +2 -249
- package/dist/components/image-upload.d.ts +8 -17
- package/dist/components/image-upload.js +3 -3
- package/dist/components/index.d.ts +11 -8
- package/dist/components/index.js +8 -0
- package/dist/components/internal/button.d.ts +1 -1
- package/dist/components/internal/icons.d.ts +11 -1
- package/dist/components/internal/icons.js +20 -0
- package/dist/components/internal/menu.d.ts +3 -2
- package/dist/components/internal/menu.js +1 -1
- package/dist/components/internal/modal.js +9 -0
- package/dist/components/internal/toggle.d.ts +1 -1
- package/dist/components/item.d.ts +14 -7
- package/dist/components/item.js +5 -2
- package/dist/components/marker.d.ts +11 -3
- package/dist/components/marker.js +4 -3
- package/dist/components/number-input.d.ts +1 -1
- package/dist/components/progress.d.ts +3 -3
- package/dist/components/progress.js +2 -2
- package/dist/components/row-actions.d.ts +3 -0
- package/dist/components/row-actions.js +8 -4
- package/dist/components/saving-bar.d.ts +16 -4
- package/dist/components/saving-bar.js +37 -9
- package/dist/components/section-header.d.ts +4 -3
- package/dist/components/selectable-card.d.ts +2 -10
- package/dist/components/sidebar.d.ts +8 -11
- package/dist/components/sidebar.js +4 -5
- package/dist/components/spinner.d.ts +9 -6
- package/dist/components/spinner.js +3 -3
- package/dist/components/star-rating.d.ts +3 -3
- package/dist/components/star-rating.js +2 -2
- package/dist/components/stat.d.ts +18 -6
- package/dist/components/stat.js +9 -3
- package/dist/components/status.d.ts +4 -4
- package/dist/components/status.js +2 -2
- package/dist/components/stepper.d.ts +33 -0
- package/dist/components/stepper.js +36 -0
- package/dist/components/summary-bar.d.ts +3 -2
- package/dist/components/summary-bar.js +10 -1
- package/dist/components/tile.d.ts +5 -5
- package/dist/components/tile.js +2 -2
- package/dist/components/time-picker.d.ts +1 -1
- package/dist/components/toast.d.ts +17 -6
- package/dist/components/toast.js +5 -3
- package/dist/components/toggle-card-compact.d.ts +6 -25
- package/dist/components/toggle-card-compact.js +8 -35
- package/dist/components/toggle-card.d.ts +23 -9
- package/dist/components/toggle-card.js +38 -2
- package/dist/components/toggle.d.ts +1 -1
- package/dist/components/typing-indicator.d.ts +2 -2
- package/dist/components/typing-indicator.js +1 -1
- package/package.json +4 -4
- package/styles/base.css +13 -1
- package/styles/rds/components-styles.css +593 -172
- package/styles/rds/components.css +40 -25
- package/styles/rds/foundation.css +1 -1
- package/styles/rds/theme.css +40 -29
- package/styles/rds.css +1 -1
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import "../chunk-ORMEWXMH.js";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { useEffect, useRef, useState } from "react";
|
|
5
|
+
import ReactCrop, { centerCrop } from "react-image-crop";
|
|
6
|
+
import "react-image-crop/dist/ReactCrop.css";
|
|
7
|
+
import { Alert } from "./alert.js";
|
|
8
|
+
import { Button } from "./button.js";
|
|
9
|
+
import { Dialog } from "./dialog.js";
|
|
10
|
+
import { RectangleHorizontalIcon, RectangleVerticalIcon, SquareDashedIcon, SquareIcon } from "./internal/icons.js";
|
|
11
|
+
import { Spinner } from "./spinner.js";
|
|
12
|
+
import { ToggleGroup, ToggleGroupItem } from "./toggle-group.js";
|
|
13
|
+
const DEFAULT_PRESETS = [
|
|
14
|
+
{ id: "square", label: "1:1", aspect: 1, icon: /* @__PURE__ */ jsx(SquareIcon, {}) },
|
|
15
|
+
{ id: "portrait", label: "4:5", aspect: 4 / 5, icon: /* @__PURE__ */ jsx(RectangleVerticalIcon, {}) },
|
|
16
|
+
{ id: "landscape", label: "16:9", aspect: 16 / 9, icon: /* @__PURE__ */ jsx(RectangleHorizontalIcon, {}) },
|
|
17
|
+
{ id: "free", label: "Livre", icon: /* @__PURE__ */ jsx(SquareDashedIcon, {}) }
|
|
18
|
+
];
|
|
19
|
+
const DEFAULT_MIN_ASPECT = 0.5;
|
|
20
|
+
const DEFAULT_MAX_ASPECT = 2;
|
|
21
|
+
function buildInitialCrop(width, height, aspect) {
|
|
22
|
+
if (!aspect) {
|
|
23
|
+
return { unit: "%", x: 0, y: 0, width: 100, height: 100 };
|
|
24
|
+
}
|
|
25
|
+
const imageAspect = width / height;
|
|
26
|
+
let cropW;
|
|
27
|
+
let cropH;
|
|
28
|
+
if (aspect >= imageAspect) {
|
|
29
|
+
cropW = 100;
|
|
30
|
+
cropH = imageAspect / aspect * 100;
|
|
31
|
+
} else {
|
|
32
|
+
cropH = 100;
|
|
33
|
+
cropW = aspect / imageAspect * 100;
|
|
34
|
+
}
|
|
35
|
+
return centerCrop({ unit: "%", x: 0, y: 0, width: cropW, height: cropH }, width, height);
|
|
36
|
+
}
|
|
37
|
+
const QUALITY_STEPS = [0.85, 0.75, 0.65, 0.55];
|
|
38
|
+
const MAX_SIDE_STEPS = [1600, 1200];
|
|
39
|
+
function canvasToBlob(canvas, mimeType, quality) {
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
canvas.toBlob((blob) => blob ? resolve(blob) : reject(new Error("toBlob failed")), mimeType, quality);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function drawCrop(image, crop, maxSide) {
|
|
45
|
+
const canvas = document.createElement("canvas");
|
|
46
|
+
const scaleX = image.naturalWidth / image.width;
|
|
47
|
+
const scaleY = image.naturalHeight / image.height;
|
|
48
|
+
let outW = Math.round(crop.width * scaleX);
|
|
49
|
+
let outH = Math.round(crop.height * scaleY);
|
|
50
|
+
if (maxSide && Math.max(outW, outH) > maxSide) {
|
|
51
|
+
const ratio = maxSide / Math.max(outW, outH);
|
|
52
|
+
outW = Math.round(outW * ratio);
|
|
53
|
+
outH = Math.round(outH * ratio);
|
|
54
|
+
}
|
|
55
|
+
canvas.width = outW;
|
|
56
|
+
canvas.height = outH;
|
|
57
|
+
const ctx = canvas.getContext("2d");
|
|
58
|
+
if (!ctx) throw new Error("Canvas not supported");
|
|
59
|
+
ctx.drawImage(
|
|
60
|
+
image,
|
|
61
|
+
crop.x * scaleX,
|
|
62
|
+
crop.y * scaleY,
|
|
63
|
+
crop.width * scaleX,
|
|
64
|
+
crop.height * scaleY,
|
|
65
|
+
0,
|
|
66
|
+
0,
|
|
67
|
+
outW,
|
|
68
|
+
outH
|
|
69
|
+
);
|
|
70
|
+
return canvas;
|
|
71
|
+
}
|
|
72
|
+
async function cropToBlob(image, crop, mimeType, maxSizeBytes) {
|
|
73
|
+
const baseCanvas = drawCrop(image, crop);
|
|
74
|
+
if (!maxSizeBytes) {
|
|
75
|
+
return canvasToBlob(baseCanvas, mimeType, 0.92);
|
|
76
|
+
}
|
|
77
|
+
const out = await canvasToBlob(baseCanvas, "image/jpeg", 0.92);
|
|
78
|
+
if (out.size <= maxSizeBytes) return out;
|
|
79
|
+
let best = out;
|
|
80
|
+
for (const q of QUALITY_STEPS) {
|
|
81
|
+
const candidate = await canvasToBlob(baseCanvas, "image/jpeg", q);
|
|
82
|
+
best = candidate;
|
|
83
|
+
if (candidate.size <= maxSizeBytes) return candidate;
|
|
84
|
+
}
|
|
85
|
+
for (const side of MAX_SIDE_STEPS) {
|
|
86
|
+
const scaledCanvas = drawCrop(image, crop, side);
|
|
87
|
+
for (const q of QUALITY_STEPS) {
|
|
88
|
+
const candidate = await canvasToBlob(scaledCanvas, "image/jpeg", q);
|
|
89
|
+
best = candidate;
|
|
90
|
+
if (candidate.size <= maxSizeBytes) return candidate;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return best;
|
|
94
|
+
}
|
|
95
|
+
function ImageCropDialog({
|
|
96
|
+
open,
|
|
97
|
+
file,
|
|
98
|
+
position,
|
|
99
|
+
onCancel,
|
|
100
|
+
onConfirm,
|
|
101
|
+
presets = DEFAULT_PRESETS,
|
|
102
|
+
minAspect = DEFAULT_MIN_ASPECT,
|
|
103
|
+
maxAspect = DEFAULT_MAX_ASPECT,
|
|
104
|
+
title = "Ajustar foto",
|
|
105
|
+
confirmLabel = "Aplicar e enviar",
|
|
106
|
+
maxSizeBytes
|
|
107
|
+
}) {
|
|
108
|
+
const [src, setSrc] = useState(null);
|
|
109
|
+
const [preset, setPreset] = useState(presets[0]);
|
|
110
|
+
const [crop, setCrop] = useState();
|
|
111
|
+
const [completedCrop, setCompletedCrop] = useState(null);
|
|
112
|
+
const [aspectError, setAspectError] = useState(null);
|
|
113
|
+
const [busy, setBusy] = useState(false);
|
|
114
|
+
const [imgLoaded, setImgLoaded] = useState(false);
|
|
115
|
+
const imgRef = useRef(null);
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (!file) {
|
|
118
|
+
setSrc(null);
|
|
119
|
+
setImgLoaded(false);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const url = URL.createObjectURL(file);
|
|
123
|
+
setSrc(url);
|
|
124
|
+
setPreset(presets[0]);
|
|
125
|
+
setCrop(void 0);
|
|
126
|
+
setCompletedCrop(null);
|
|
127
|
+
setAspectError(null);
|
|
128
|
+
setImgLoaded(false);
|
|
129
|
+
return () => URL.revokeObjectURL(url);
|
|
130
|
+
}, [file, presets]);
|
|
131
|
+
function handleImageLoad(e) {
|
|
132
|
+
var _a, _b, _c, _d;
|
|
133
|
+
const { width, height } = e.currentTarget;
|
|
134
|
+
const initial = buildInitialCrop(width, height, preset.aspect);
|
|
135
|
+
setCrop(initial);
|
|
136
|
+
const pixelInitial = {
|
|
137
|
+
unit: "px",
|
|
138
|
+
x: ((_a = initial.x) != null ? _a : 0) / 100 * width,
|
|
139
|
+
y: ((_b = initial.y) != null ? _b : 0) / 100 * height,
|
|
140
|
+
width: ((_c = initial.width) != null ? _c : 0) / 100 * width,
|
|
141
|
+
height: ((_d = initial.height) != null ? _d : 0) / 100 * height
|
|
142
|
+
};
|
|
143
|
+
setCompletedCrop(pixelInitial);
|
|
144
|
+
setImgLoaded(true);
|
|
145
|
+
}
|
|
146
|
+
function applyPreset(p) {
|
|
147
|
+
setPreset(p);
|
|
148
|
+
setAspectError(null);
|
|
149
|
+
if (imgRef.current) {
|
|
150
|
+
const { width, height } = imgRef.current;
|
|
151
|
+
setCrop(buildInitialCrop(width, height, p.aspect));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function validateFreeAspect(c) {
|
|
155
|
+
if (preset.aspect) return true;
|
|
156
|
+
if (!c.width || !c.height) return false;
|
|
157
|
+
const ratio = c.width / c.height;
|
|
158
|
+
if (ratio < minAspect) {
|
|
159
|
+
setAspectError(`Recorte muito alto. M\xEDnimo permitido: ${minAspect.toFixed(1)}.`);
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
if (ratio > maxAspect) {
|
|
163
|
+
setAspectError(`Recorte muito largo. M\xE1ximo permitido: ${maxAspect.toFixed(1)}.`);
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
setAspectError(null);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
async function handleConfirm() {
|
|
170
|
+
if (busy) return;
|
|
171
|
+
if (!completedCrop || !imgRef.current || !file) return;
|
|
172
|
+
if (!validateFreeAspect(completedCrop)) return;
|
|
173
|
+
setBusy(true);
|
|
174
|
+
try {
|
|
175
|
+
const blob = await cropToBlob(imgRef.current, completedCrop, file.type || "image/jpeg", maxSizeBytes);
|
|
176
|
+
await onConfirm(blob);
|
|
177
|
+
} catch (err) {
|
|
178
|
+
console.error("cropToBlob error", err);
|
|
179
|
+
} finally {
|
|
180
|
+
setBusy(false);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return /* @__PURE__ */ jsx(
|
|
184
|
+
Dialog,
|
|
185
|
+
{
|
|
186
|
+
open,
|
|
187
|
+
onOpenChange: (next) => {
|
|
188
|
+
if (!next) onCancel();
|
|
189
|
+
},
|
|
190
|
+
size: "lg",
|
|
191
|
+
title: position ? `${title} \xB7 ${position}` : title,
|
|
192
|
+
footer: /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
193
|
+
/* @__PURE__ */ jsx(Button, { tone: "neutral", variant: "ghost", onClick: onCancel, children: "Cancelar" }),
|
|
194
|
+
/* @__PURE__ */ jsx(Button, { onClick: handleConfirm, disabled: busy || !completedCrop || !!aspectError, loading: busy, children: busy ? "Aplicando\u2026" : confirmLabel })
|
|
195
|
+
] }),
|
|
196
|
+
children: /* @__PURE__ */ jsxs("div", { className: "rds-image-crop", children: [
|
|
197
|
+
presets.length > 1 && /* @__PURE__ */ jsx(
|
|
198
|
+
ToggleGroup,
|
|
199
|
+
{
|
|
200
|
+
type: "single",
|
|
201
|
+
"aria-label": "Propor\xE7\xE3o",
|
|
202
|
+
value: preset.id,
|
|
203
|
+
onValueChange: (id) => {
|
|
204
|
+
const next = presets.find((p) => p.id === id);
|
|
205
|
+
if (next) applyPreset(next);
|
|
206
|
+
},
|
|
207
|
+
className: "rds-image-crop__presets",
|
|
208
|
+
children: presets.map((p) => /* @__PURE__ */ jsx(ToggleGroupItem, { value: p.id, icon: p.icon, children: p.label }, p.id))
|
|
209
|
+
}
|
|
210
|
+
),
|
|
211
|
+
/* @__PURE__ */ jsxs("div", { className: "rds-image-crop__stage", children: [
|
|
212
|
+
src && /* @__PURE__ */ jsx(
|
|
213
|
+
ReactCrop,
|
|
214
|
+
{
|
|
215
|
+
crop,
|
|
216
|
+
onChange: (_, percentCrop) => setCrop(percentCrop),
|
|
217
|
+
onComplete: (c) => {
|
|
218
|
+
setCompletedCrop(c);
|
|
219
|
+
validateFreeAspect(c);
|
|
220
|
+
},
|
|
221
|
+
aspect: preset.aspect,
|
|
222
|
+
keepSelection: true,
|
|
223
|
+
minWidth: 50,
|
|
224
|
+
minHeight: 50,
|
|
225
|
+
className: "rds-image-crop__crop",
|
|
226
|
+
children: /* @__PURE__ */ jsx("img", { ref: imgRef, src, alt: "", onLoad: handleImageLoad, className: "rds-image-crop__image" })
|
|
227
|
+
}
|
|
228
|
+
),
|
|
229
|
+
!imgLoaded && /* @__PURE__ */ jsx("div", { className: "rds-image-crop__layer", children: /* @__PURE__ */ jsx(Spinner, { size: "lg", label: "Carregando imagem\u2026" }) }),
|
|
230
|
+
busy && /* @__PURE__ */ jsx("div", { className: "rds-image-crop__layer rds-image-crop__layer--busy", children: /* @__PURE__ */ jsx(Spinner, { size: "lg", tone: "inverse", label: "Enviando\u2026" }) })
|
|
231
|
+
] }),
|
|
232
|
+
aspectError && /* @__PURE__ */ jsx(Alert, { tone: "danger", title: aspectError, announce: "alert" }),
|
|
233
|
+
!aspectError && imgLoaded && !busy && /* @__PURE__ */ jsx("p", { className: "rds-image-crop__hint", children: preset.aspect ? "Arraste as al\xE7as para ajustar o recorte." : `Livre: propor\xE7\xE3o entre ${minAspect.toFixed(1)} (vertical) e ${maxAspect.toFixed(1)} (horizontal).` })
|
|
234
|
+
] })
|
|
235
|
+
}
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
export {
|
|
239
|
+
ImageCropDialog
|
|
240
|
+
};
|
|
@@ -1,59 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ImageCropDialog, ImageCropDialogProps } from './image-crop-dialog.js';
|
|
2
|
+
export { CropPreset } from './image-crop-dialog.js';
|
|
3
|
+
import 'react';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* Uso:
|
|
9
|
-
* <ImageCropModal
|
|
10
|
-
* open={open}
|
|
11
|
-
* file={selectedFile}
|
|
12
|
-
* onCancel={() => setOpen(false)}
|
|
13
|
-
* onConfirm={(blob) => uploadCroppedImage(blob)}
|
|
14
|
-
* />
|
|
15
|
-
*
|
|
16
|
-
* Presets default: Livre (1:2 a 2:1), 1:1, 4:5, 16:9.
|
|
17
|
-
* Customize via prop `presets`.
|
|
6
|
+
* image-crop-modal — the 1.x and 2.0-next name of the ImageCropDialog, kept so
|
|
7
|
+
* `@rojaostudio/ds/components/image-crop-modal` and `ImageCropModal` still work in 2.0. The codemod
|
|
8
|
+
* (`npx @rojaostudio/ds-codemod`) renames both. Like the dialog, it needs the optional peer `react-image-crop`.
|
|
18
9
|
*/
|
|
19
|
-
interface CropPreset {
|
|
20
|
-
id: string;
|
|
21
|
-
label: string;
|
|
22
|
-
/** Aspect ratio fixo. undefined = livre (com bounds MIN/MAX_ASPECT). */
|
|
23
|
-
aspect?: number;
|
|
24
|
-
/** Ícone opcional exibido antes do rótulo (ajuda quem não decodifica "16:9"). */
|
|
25
|
-
icon?: React.ReactNode;
|
|
26
|
-
}
|
|
27
|
-
interface ImageCropModalProps {
|
|
28
|
-
open: boolean;
|
|
29
|
-
file: File | null;
|
|
30
|
-
/** Label de contexto pro título (ex: "Foto 2 de 4"). */
|
|
31
|
-
position?: string;
|
|
32
|
-
onCancel: () => void;
|
|
33
|
-
/**
|
|
34
|
-
* Recebe o blob do recorte. Pode ser assíncrono (ex.: upload) — o modal AGUARDA a promise
|
|
35
|
-
* e mantém o botão travado/carregando até resolver, evitando duplo-clique que duplicaria a
|
|
36
|
-
* imagem (o upload roda fora do `busy` se o modal não esperar).
|
|
37
|
-
*/
|
|
38
|
-
onConfirm: (blob: Blob) => void | Promise<void>;
|
|
39
|
-
/** Presets disponíveis. default = Livre + 1:1 + 4:5 + 16:9. */
|
|
40
|
-
presets?: CropPreset[];
|
|
41
|
-
/** Limite mínimo de aspect ratio (alto-baixo). default = 0.5 (1:2). */
|
|
42
|
-
minAspect?: number;
|
|
43
|
-
/** Limite máximo de aspect ratio (largo-baixo). default = 2.0 (2:1). */
|
|
44
|
-
maxAspect?: number;
|
|
45
|
-
/** Título do modal. default = "Ajustar foto". */
|
|
46
|
-
title?: string;
|
|
47
|
-
/** Label do botão confirmar. default = "Aplicar e enviar". */
|
|
48
|
-
confirmLabel?: string;
|
|
49
|
-
/**
|
|
50
|
-
* Teto de tamanho do blob de saída (bytes). Quando definido, a saída do crop
|
|
51
|
-
* é forçada a JPEG e comprimida (qualidade + downscale) até caber no limite —
|
|
52
|
-
* evita que foto de celular grande estoure o limite de upload. Sem ele, a
|
|
53
|
-
* saída mantém o tipo original e qualidade fixa (comportamento inalterado).
|
|
54
|
-
*/
|
|
55
|
-
maxSizeBytes?: number;
|
|
56
|
-
}
|
|
57
|
-
declare function ImageCropModal({ open, file, position, onCancel, onConfirm, presets, minAspect, maxAspect, title, confirmLabel, maxSizeBytes, }: ImageCropModalProps): react.JSX.Element;
|
|
58
10
|
|
|
59
|
-
|
|
11
|
+
/** @deprecated Renamed to `ImageCropDialog` (`@rojaostudio/ds/components/image-crop-dialog`) in 2.0. Same props. */
|
|
12
|
+
declare const ImageCropModal: typeof ImageCropDialog;
|
|
13
|
+
/** @deprecated Renamed to `ImageCropDialogProps` in 2.0. */
|
|
14
|
+
type ImageCropModalProps = ImageCropDialogProps;
|
|
15
|
+
|
|
16
|
+
export { ImageCropModal, type ImageCropModalProps };
|
|
@@ -1,254 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import "../chunk-ORMEWXMH.js";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import ReactCrop, { centerCrop } from "react-image-crop";
|
|
6
|
-
import "react-image-crop/dist/ReactCrop.css";
|
|
7
|
-
import { Alert } from "./alert.js";
|
|
8
|
-
import { Button } from "./button.js";
|
|
9
|
-
import { Dialog } from "./dialog.js";
|
|
10
|
-
import { Spinner } from "./spinner.js";
|
|
11
|
-
import { ToggleGroup, ToggleGroupItem } from "./toggle-group.js";
|
|
12
|
-
function RatioIcon({ w, h }) {
|
|
13
|
-
const box = 16;
|
|
14
|
-
const rw = w >= h ? box : w / h * box;
|
|
15
|
-
const rh = h >= w ? box : h / w * box;
|
|
16
|
-
return /* @__PURE__ */ jsx("svg", { width: "16", height: "16", viewBox: "0 0 18 18", fill: "none", "aria-hidden": true, children: /* @__PURE__ */ jsx("rect", { x: (18 - rw) / 2, y: (18 - rh) / 2, width: rw, height: rh, rx: "1.5", stroke: "currentColor", strokeWidth: "1.6" }) });
|
|
17
|
-
}
|
|
18
|
-
function FreeIcon() {
|
|
19
|
-
return /* @__PURE__ */ jsx("svg", { width: "16", height: "16", viewBox: "0 0 18 18", fill: "none", "aria-hidden": true, children: /* @__PURE__ */ jsx("rect", { x: "2.5", y: "2.5", width: "13", height: "13", rx: "1.5", stroke: "currentColor", strokeWidth: "1.6", strokeDasharray: "2.5 2.2" }) });
|
|
20
|
-
}
|
|
21
|
-
const DEFAULT_PRESETS = [
|
|
22
|
-
{ id: "square", label: "1:1", aspect: 1, icon: /* @__PURE__ */ jsx(RatioIcon, { w: 1, h: 1 }) },
|
|
23
|
-
{ id: "portrait", label: "4:5", aspect: 4 / 5, icon: /* @__PURE__ */ jsx(RatioIcon, { w: 4, h: 5 }) },
|
|
24
|
-
{ id: "landscape", label: "16:9", aspect: 16 / 9, icon: /* @__PURE__ */ jsx(RatioIcon, { w: 16, h: 9 }) },
|
|
25
|
-
{ id: "free", label: "Livre", icon: /* @__PURE__ */ jsx(FreeIcon, {}) }
|
|
26
|
-
];
|
|
27
|
-
const DEFAULT_MIN_ASPECT = 0.5;
|
|
28
|
-
const DEFAULT_MAX_ASPECT = 2;
|
|
29
|
-
function buildInitialCrop(width, height, aspect) {
|
|
30
|
-
if (!aspect) {
|
|
31
|
-
return { unit: "%", x: 0, y: 0, width: 100, height: 100 };
|
|
32
|
-
}
|
|
33
|
-
const imageAspect = width / height;
|
|
34
|
-
let cropW;
|
|
35
|
-
let cropH;
|
|
36
|
-
if (aspect >= imageAspect) {
|
|
37
|
-
cropW = 100;
|
|
38
|
-
cropH = imageAspect / aspect * 100;
|
|
39
|
-
} else {
|
|
40
|
-
cropH = 100;
|
|
41
|
-
cropW = aspect / imageAspect * 100;
|
|
42
|
-
}
|
|
43
|
-
return centerCrop({ unit: "%", x: 0, y: 0, width: cropW, height: cropH }, width, height);
|
|
44
|
-
}
|
|
45
|
-
const QUALITY_STEPS = [0.85, 0.75, 0.65, 0.55];
|
|
46
|
-
const MAX_SIDE_STEPS = [1600, 1200];
|
|
47
|
-
function canvasToBlob(canvas, mimeType, quality) {
|
|
48
|
-
return new Promise((resolve, reject) => {
|
|
49
|
-
canvas.toBlob((blob) => blob ? resolve(blob) : reject(new Error("toBlob failed")), mimeType, quality);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
function drawCrop(image, crop, maxSide) {
|
|
53
|
-
const canvas = document.createElement("canvas");
|
|
54
|
-
const scaleX = image.naturalWidth / image.width;
|
|
55
|
-
const scaleY = image.naturalHeight / image.height;
|
|
56
|
-
let outW = Math.round(crop.width * scaleX);
|
|
57
|
-
let outH = Math.round(crop.height * scaleY);
|
|
58
|
-
if (maxSide && Math.max(outW, outH) > maxSide) {
|
|
59
|
-
const ratio = maxSide / Math.max(outW, outH);
|
|
60
|
-
outW = Math.round(outW * ratio);
|
|
61
|
-
outH = Math.round(outH * ratio);
|
|
62
|
-
}
|
|
63
|
-
canvas.width = outW;
|
|
64
|
-
canvas.height = outH;
|
|
65
|
-
const ctx = canvas.getContext("2d");
|
|
66
|
-
if (!ctx) throw new Error("Canvas not supported");
|
|
67
|
-
ctx.drawImage(
|
|
68
|
-
image,
|
|
69
|
-
crop.x * scaleX,
|
|
70
|
-
crop.y * scaleY,
|
|
71
|
-
crop.width * scaleX,
|
|
72
|
-
crop.height * scaleY,
|
|
73
|
-
0,
|
|
74
|
-
0,
|
|
75
|
-
outW,
|
|
76
|
-
outH
|
|
77
|
-
);
|
|
78
|
-
return canvas;
|
|
79
|
-
}
|
|
80
|
-
async function cropToBlob(image, crop, mimeType, maxSizeBytes) {
|
|
81
|
-
const baseCanvas = drawCrop(image, crop);
|
|
82
|
-
if (!maxSizeBytes) {
|
|
83
|
-
return canvasToBlob(baseCanvas, mimeType, 0.92);
|
|
84
|
-
}
|
|
85
|
-
const out = await canvasToBlob(baseCanvas, "image/jpeg", 0.92);
|
|
86
|
-
if (out.size <= maxSizeBytes) return out;
|
|
87
|
-
let best = out;
|
|
88
|
-
for (const q of QUALITY_STEPS) {
|
|
89
|
-
const candidate = await canvasToBlob(baseCanvas, "image/jpeg", q);
|
|
90
|
-
best = candidate;
|
|
91
|
-
if (candidate.size <= maxSizeBytes) return candidate;
|
|
92
|
-
}
|
|
93
|
-
for (const side of MAX_SIDE_STEPS) {
|
|
94
|
-
const scaledCanvas = drawCrop(image, crop, side);
|
|
95
|
-
for (const q of QUALITY_STEPS) {
|
|
96
|
-
const candidate = await canvasToBlob(scaledCanvas, "image/jpeg", q);
|
|
97
|
-
best = candidate;
|
|
98
|
-
if (candidate.size <= maxSizeBytes) return candidate;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return best;
|
|
102
|
-
}
|
|
103
|
-
function ImageCropModal({
|
|
104
|
-
open,
|
|
105
|
-
file,
|
|
106
|
-
position,
|
|
107
|
-
onCancel,
|
|
108
|
-
onConfirm,
|
|
109
|
-
presets = DEFAULT_PRESETS,
|
|
110
|
-
minAspect = DEFAULT_MIN_ASPECT,
|
|
111
|
-
maxAspect = DEFAULT_MAX_ASPECT,
|
|
112
|
-
title = "Ajustar foto",
|
|
113
|
-
confirmLabel = "Aplicar e enviar",
|
|
114
|
-
maxSizeBytes
|
|
115
|
-
}) {
|
|
116
|
-
const [src, setSrc] = useState(null);
|
|
117
|
-
const [preset, setPreset] = useState(presets[0]);
|
|
118
|
-
const [crop, setCrop] = useState();
|
|
119
|
-
const [completedCrop, setCompletedCrop] = useState(null);
|
|
120
|
-
const [aspectError, setAspectError] = useState(null);
|
|
121
|
-
const [busy, setBusy] = useState(false);
|
|
122
|
-
const [imgLoaded, setImgLoaded] = useState(false);
|
|
123
|
-
const imgRef = useRef(null);
|
|
124
|
-
useEffect(() => {
|
|
125
|
-
if (!file) {
|
|
126
|
-
setSrc(null);
|
|
127
|
-
setImgLoaded(false);
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
const url = URL.createObjectURL(file);
|
|
131
|
-
setSrc(url);
|
|
132
|
-
setPreset(presets[0]);
|
|
133
|
-
setCrop(void 0);
|
|
134
|
-
setCompletedCrop(null);
|
|
135
|
-
setAspectError(null);
|
|
136
|
-
setImgLoaded(false);
|
|
137
|
-
return () => URL.revokeObjectURL(url);
|
|
138
|
-
}, [file, presets]);
|
|
139
|
-
function handleImageLoad(e) {
|
|
140
|
-
var _a, _b, _c, _d;
|
|
141
|
-
const { width, height } = e.currentTarget;
|
|
142
|
-
const initial = buildInitialCrop(width, height, preset.aspect);
|
|
143
|
-
setCrop(initial);
|
|
144
|
-
const pixelInitial = {
|
|
145
|
-
unit: "px",
|
|
146
|
-
x: ((_a = initial.x) != null ? _a : 0) / 100 * width,
|
|
147
|
-
y: ((_b = initial.y) != null ? _b : 0) / 100 * height,
|
|
148
|
-
width: ((_c = initial.width) != null ? _c : 0) / 100 * width,
|
|
149
|
-
height: ((_d = initial.height) != null ? _d : 0) / 100 * height
|
|
150
|
-
};
|
|
151
|
-
setCompletedCrop(pixelInitial);
|
|
152
|
-
setImgLoaded(true);
|
|
153
|
-
}
|
|
154
|
-
function applyPreset(p) {
|
|
155
|
-
setPreset(p);
|
|
156
|
-
setAspectError(null);
|
|
157
|
-
if (imgRef.current) {
|
|
158
|
-
const { width, height } = imgRef.current;
|
|
159
|
-
setCrop(buildInitialCrop(width, height, p.aspect));
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
function validateFreeAspect(c) {
|
|
163
|
-
if (preset.aspect) return true;
|
|
164
|
-
if (!c.width || !c.height) return false;
|
|
165
|
-
const ratio = c.width / c.height;
|
|
166
|
-
if (ratio < minAspect) {
|
|
167
|
-
setAspectError(`Recorte muito alto. M\xEDnimo permitido: ${minAspect.toFixed(1)}.`);
|
|
168
|
-
return false;
|
|
169
|
-
}
|
|
170
|
-
if (ratio > maxAspect) {
|
|
171
|
-
setAspectError(`Recorte muito largo. M\xE1ximo permitido: ${maxAspect.toFixed(1)}.`);
|
|
172
|
-
return false;
|
|
173
|
-
}
|
|
174
|
-
setAspectError(null);
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
async function handleConfirm() {
|
|
178
|
-
if (busy) return;
|
|
179
|
-
if (!completedCrop || !imgRef.current || !file) return;
|
|
180
|
-
if (!validateFreeAspect(completedCrop)) return;
|
|
181
|
-
setBusy(true);
|
|
182
|
-
try {
|
|
183
|
-
const blob = await cropToBlob(imgRef.current, completedCrop, file.type || "image/jpeg", maxSizeBytes);
|
|
184
|
-
await onConfirm(blob);
|
|
185
|
-
} catch (err) {
|
|
186
|
-
console.error("cropToBlob error", err);
|
|
187
|
-
} finally {
|
|
188
|
-
setBusy(false);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return /* @__PURE__ */ jsx(
|
|
192
|
-
Dialog,
|
|
193
|
-
{
|
|
194
|
-
open,
|
|
195
|
-
onOpenChange: (next) => {
|
|
196
|
-
if (!next) onCancel();
|
|
197
|
-
},
|
|
198
|
-
size: "lg",
|
|
199
|
-
title: position ? `${title} \xB7 ${position}` : title,
|
|
200
|
-
footer: /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
201
|
-
/* @__PURE__ */ jsx(Button, { tone: "neutral", variant: "ghost", onClick: onCancel, children: "Cancelar" }),
|
|
202
|
-
/* @__PURE__ */ jsx(Button, { onClick: handleConfirm, disabled: busy || !completedCrop || !!aspectError, loading: busy, children: busy ? "Aplicando\u2026" : confirmLabel })
|
|
203
|
-
] }),
|
|
204
|
-
children: /* @__PURE__ */ jsxs("div", { className: "rds-image-crop", children: [
|
|
205
|
-
presets.length > 1 && /* @__PURE__ */ jsx(
|
|
206
|
-
ToggleGroup,
|
|
207
|
-
{
|
|
208
|
-
type: "single",
|
|
209
|
-
"aria-label": "Propor\xE7\xE3o",
|
|
210
|
-
value: preset.id,
|
|
211
|
-
onValueChange: (id) => {
|
|
212
|
-
const next = presets.find((p) => p.id === id);
|
|
213
|
-
if (next) applyPreset(next);
|
|
214
|
-
},
|
|
215
|
-
className: "rds-image-crop__presets",
|
|
216
|
-
children: presets.map((p) => /* @__PURE__ */ jsx(ToggleGroupItem, { value: p.id, icon: p.icon, children: p.label }, p.id))
|
|
217
|
-
}
|
|
218
|
-
),
|
|
219
|
-
/* @__PURE__ */ jsxs("div", { className: "rds-image-crop__stage", children: [
|
|
220
|
-
src && /* @__PURE__ */ jsx(
|
|
221
|
-
ReactCrop,
|
|
222
|
-
{
|
|
223
|
-
crop,
|
|
224
|
-
onChange: (_, percentCrop) => setCrop(percentCrop),
|
|
225
|
-
onComplete: (c) => {
|
|
226
|
-
setCompletedCrop(c);
|
|
227
|
-
validateFreeAspect(c);
|
|
228
|
-
},
|
|
229
|
-
aspect: preset.aspect,
|
|
230
|
-
keepSelection: true,
|
|
231
|
-
minWidth: 50,
|
|
232
|
-
minHeight: 50,
|
|
233
|
-
className: "rds-image-crop__crop",
|
|
234
|
-
children: /* @__PURE__ */ jsx("img", { ref: imgRef, src, alt: "", onLoad: handleImageLoad, className: "rds-image-crop__image" })
|
|
235
|
-
}
|
|
236
|
-
),
|
|
237
|
-
!imgLoaded && /* @__PURE__ */ jsx("div", { className: "rds-image-crop__layer", children: /* @__PURE__ */ jsx(Spinner, { size: "lg", label: "Carregando imagem\u2026" }) }),
|
|
238
|
-
busy && /* @__PURE__ */ jsx("div", { className: "rds-image-crop__layer rds-image-crop__layer--busy", children: /* @__PURE__ */ jsx(Spinner, { size: "lg", tone: "inverse", label: "Enviando\u2026" }) })
|
|
239
|
-
] }),
|
|
240
|
-
aspectError && /* @__PURE__ */ jsx(Alert, { tone: "danger", title: aspectError, announce: "alert" }),
|
|
241
|
-
!aspectError && preset.id === "free" && /* @__PURE__ */ jsxs("p", { className: "rds-image-crop__hint", children: [
|
|
242
|
-
"Livre: propor\xE7\xE3o entre ",
|
|
243
|
-
minAspect.toFixed(1),
|
|
244
|
-
" (vertical) e ",
|
|
245
|
-
maxAspect.toFixed(1),
|
|
246
|
-
" (horizontal)."
|
|
247
|
-
] })
|
|
248
|
-
] })
|
|
249
|
-
}
|
|
250
|
-
);
|
|
251
|
-
}
|
|
3
|
+
import { ImageCropDialog } from "./image-crop-dialog.js";
|
|
4
|
+
const ImageCropModal = ImageCropDialog;
|
|
252
5
|
export {
|
|
253
6
|
ImageCropModal
|
|
254
7
|
};
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import * as react from 'react';
|
|
2
|
-
import { CropPreset } from './image-crop-
|
|
2
|
+
import { CropPreset } from './image-crop-dialog.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @deprecated Use `<FileInput
|
|
6
|
-
* and the swap and remove buttons. ImageUpload is now a thin wrapper over it that keeps the 1.x API compiling and
|
|
7
|
-
* keeps the upload (`onUpload`) and the crop (`crop`).
|
|
5
|
+
* @deprecated Use `<FileInput layout="tile">` (Figma [RDS] Forms/FileInput): the image's square with the preview
|
|
6
|
+
* and the swap and remove buttons. ImageUpload is now a thin wrapper over it that keeps most of the 1.x API compiling and
|
|
7
|
+
* keeps the upload (`onUpload`) and the crop (`crop`). It is always the tile (`variant`, `aspect` and the preview
|
|
8
|
+
* class names left in 2.0): an image asks for a preview.
|
|
8
9
|
*/
|
|
9
|
-
type ImageUploadAspect = 'logo' | 'banner' | 'square';
|
|
10
|
-
type ImageUploadVariant = 'button' | 'dropzone' | 'tile';
|
|
11
10
|
interface ImageUploadLabels {
|
|
12
11
|
/** The empty tile's text (FileInput's `tileText`). */
|
|
13
12
|
select: string;
|
|
@@ -26,7 +25,7 @@ interface ImageUploadLabels {
|
|
|
26
25
|
/** The error message when the upload fails without one. */
|
|
27
26
|
error: string;
|
|
28
27
|
}
|
|
29
|
-
/** When given, the chosen image goes through the
|
|
28
|
+
/** When given, the chosen image goes through the ImageCropDialog before the upload. */
|
|
30
29
|
interface ImageUploadCrop {
|
|
31
30
|
presets?: CropPreset[];
|
|
32
31
|
minAspect?: number;
|
|
@@ -36,10 +35,6 @@ interface ImageUploadCrop {
|
|
|
36
35
|
confirmLabel?: string;
|
|
37
36
|
}
|
|
38
37
|
interface ImageUploadProps {
|
|
39
|
-
/** @deprecated Ignored: the tile's preview fills the square. */
|
|
40
|
-
previewClassName?: string;
|
|
41
|
-
/** @deprecated Ignored. */
|
|
42
|
-
previewWrapperClassName?: string;
|
|
43
38
|
/** The form field name: a hidden input carries the image's URL. */
|
|
44
39
|
name: string;
|
|
45
40
|
/** The image's URL, '' for none. */
|
|
@@ -49,16 +44,12 @@ interface ImageUploadProps {
|
|
|
49
44
|
onUpload: (file: File) => Promise<string>;
|
|
50
45
|
label?: string;
|
|
51
46
|
hint?: string;
|
|
52
|
-
/** @deprecated Ignored: the tile is a 112 square. */
|
|
53
|
-
aspect?: ImageUploadAspect;
|
|
54
47
|
accept?: string;
|
|
55
48
|
className?: string;
|
|
56
|
-
/** @deprecated Ignored: every variant is the tile. */
|
|
57
|
-
variant?: ImageUploadVariant;
|
|
58
49
|
crop?: ImageUploadCrop;
|
|
59
50
|
labels?: Partial<ImageUploadLabels>;
|
|
60
51
|
}
|
|
61
|
-
/** @deprecated Use `<FileInput
|
|
52
|
+
/** @deprecated Use `<FileInput layout="tile">`, with the upload in its `onFiles` and the URL in `preview`. */
|
|
62
53
|
declare function ImageUpload({ name, value, onChange, onUpload, label, hint, accept, className, crop, labels, }: ImageUploadProps): react.JSX.Element;
|
|
63
54
|
|
|
64
|
-
export { ImageUpload, type
|
|
55
|
+
export { ImageUpload, type ImageUploadCrop, type ImageUploadLabels, type ImageUploadProps };
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
6
|
import { useRef, useState } from "react";
|
|
7
7
|
import { FileInput } from "./file-input.js";
|
|
8
|
-
import {
|
|
8
|
+
import { ImageCropDialog } from "./image-crop-dialog.js";
|
|
9
9
|
const DEFAULT_LABELS = {
|
|
10
10
|
select: "Selecionar imagem",
|
|
11
11
|
change: "Trocar imagem",
|
|
@@ -63,7 +63,7 @@ function ImageUpload({
|
|
|
63
63
|
FileInput,
|
|
64
64
|
{
|
|
65
65
|
ref: input,
|
|
66
|
-
|
|
66
|
+
layout: "tile",
|
|
67
67
|
label,
|
|
68
68
|
"aria-label": label ? void 0 : L.aria,
|
|
69
69
|
hint,
|
|
@@ -81,7 +81,7 @@ function ImageUpload({
|
|
|
81
81
|
),
|
|
82
82
|
/* @__PURE__ */ jsx("input", { type: "hidden", name, value }),
|
|
83
83
|
crop && /* @__PURE__ */ jsx(
|
|
84
|
-
|
|
84
|
+
ImageCropDialog,
|
|
85
85
|
{
|
|
86
86
|
open: !!pendingFile,
|
|
87
87
|
file: pendingFile,
|