antd-img-crop 4.24.0 → 4.26.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/dist/antd-img-crop.cjs.js +33 -48
- package/dist/antd-img-crop.d.ts +9 -7
- package/dist/antd-img-crop.esm.js +33 -48
- package/package.json +36 -27
|
@@ -19,23 +19,28 @@ const ROTATION_STEP = 1;
|
|
|
19
19
|
const ASPECT_STEP = 0.01;
|
|
20
20
|
|
|
21
21
|
const EasyCrop = react.forwardRef((props, ref) => {
|
|
22
|
-
const { cropperRef, zoomSlider, rotationSlider, aspectSlider, showReset, resetBtnText, modalImage, aspect:
|
|
22
|
+
const { cropperRef, zoomSlider, rotationSlider, aspectSlider, showReset, resetBtnText, modalImage, aspect: propAspect, minZoom, maxZoom, minAspect, maxAspect, cropShape, showGrid, cropperProps, } = props;
|
|
23
|
+
const [crop, setCrop] = react.useState({ x: 0, y: 0 });
|
|
23
24
|
const [zoom, setZoom] = react.useState(ZOOM_INITIAL);
|
|
24
25
|
const [rotation, setRotation] = react.useState(ROTATION_INITIAL);
|
|
25
|
-
const [aspect, setAspect] = react.useState(
|
|
26
|
+
const [aspect, setAspect] = react.useState(propAspect);
|
|
27
|
+
const cropPixelsRef = react.useRef({ width: 0, height: 0, x: 0, y: 0 });
|
|
28
|
+
const onCropComplete = react.useCallback((_, croppedAreaPixels) => {
|
|
29
|
+
cropPixelsRef.current = croppedAreaPixels;
|
|
30
|
+
}, []);
|
|
31
|
+
const prevPropAspect = react.useRef(propAspect);
|
|
32
|
+
if (prevPropAspect.current !== propAspect) {
|
|
33
|
+
prevPropAspect.current = propAspect;
|
|
34
|
+
setAspect(propAspect);
|
|
35
|
+
}
|
|
26
36
|
const isResetActive = zoom !== ZOOM_INITIAL ||
|
|
27
37
|
rotation !== ROTATION_INITIAL ||
|
|
28
|
-
aspect !==
|
|
38
|
+
aspect !== propAspect;
|
|
29
39
|
const onReset = () => {
|
|
30
40
|
setZoom(ZOOM_INITIAL);
|
|
31
41
|
setRotation(ROTATION_INITIAL);
|
|
32
|
-
setAspect(
|
|
42
|
+
setAspect(propAspect);
|
|
33
43
|
};
|
|
34
|
-
const [crop, onCropChange] = react.useState({ x: 0, y: 0 });
|
|
35
|
-
const cropPixelsRef = react.useRef({ width: 0, height: 0, x: 0, y: 0 });
|
|
36
|
-
const onCropComplete = react.useCallback((_, croppedAreaPixels) => {
|
|
37
|
-
cropPixelsRef.current = croppedAreaPixels;
|
|
38
|
-
}, []);
|
|
39
44
|
react.useImperativeHandle(ref, () => ({
|
|
40
45
|
rotation,
|
|
41
46
|
cropPixelsRef,
|
|
@@ -44,46 +49,20 @@ const EasyCrop = react.forwardRef((props, ref) => {
|
|
|
44
49
|
const wrapperClass = '[display:flex] [align-items:center] [width:60%] [margin-inline:auto]';
|
|
45
50
|
const buttonClass = '[display:flex] [align-items:center] [justify-content:center] [height:32px] [width:32px] [background:transparent] [border:0] [font-family:inherit] [font-size:18px] [cursor:pointer] disabled:[opacity:20%] disabled:[cursor:default]';
|
|
46
51
|
const sliderClass = '[flex:1]';
|
|
47
|
-
return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(Cropper, Object.assign({}, cropperProps, { ref: cropperRef, image: modalImage, crop: crop,
|
|
48
|
-
//
|
|
49
|
-
zoom: zoom, rotation: rotation, aspect: aspect, minZoom: minZoom, maxZoom: maxZoom, zoomWithScroll: zoomSlider,
|
|
50
|
-
//
|
|
51
|
-
cropShape: cropShape, showGrid: showGrid, onCropChange: onCropChange, onZoomChange: setZoom, onRotationChange: setRotation, onCropComplete: onCropComplete, classes: {
|
|
52
|
+
return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(Cropper, Object.assign({}, cropperProps, { ref: cropperRef, image: modalImage, crop: crop, zoom: zoom, rotation: rotation, aspect: aspect, minZoom: minZoom, maxZoom: maxZoom, zoomWithScroll: zoomSlider, cropShape: cropShape, showGrid: showGrid, onCropChange: setCrop, onZoomChange: setZoom, onRotationChange: setRotation, onCropComplete: onCropComplete, classes: {
|
|
52
53
|
containerClassName: `${PREFIX}-container ![position:relative] [width:100%] [height:40vh] [&~section:first-of-type]:[margin-top:16px] [&~section:last-of-type]:[margin-bottom:16px]`,
|
|
53
54
|
mediaClassName: `${PREFIX}-media`,
|
|
54
|
-
} })), zoomSlider && (jsxRuntime.jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-zoom ${wrapperClass}`, children: [jsxRuntime.jsx("button", { className: buttonClass, onClick: () => setZoom(+(zoom - ZOOM_STEP).toFixed(1)), disabled: zoom - ZOOM_STEP < minZoom, children: "\uFF0D" }), jsxRuntime.jsx(AntSlider, { className: sliderClass, min: minZoom, max: maxZoom, step: ZOOM_STEP, value: zoom, onChange: setZoom }), jsxRuntime.jsx("button", { className: buttonClass, onClick: () => setZoom(+(zoom + ZOOM_STEP).toFixed(1)), disabled: zoom + ZOOM_STEP > maxZoom, children: "\uFF0B" })] })), rotationSlider && (jsxRuntime.jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-rotation ${wrapperClass}`, children: [jsxRuntime.jsx("button", { className: `${buttonClass} [font-size:16px]`, onClick: () => setRotation(rotation - ROTATION_STEP), disabled: rotation === ROTATION_MIN, children: "\u21BA" }), jsxRuntime.jsx(AntSlider, { className: sliderClass, min: ROTATION_MIN, max: ROTATION_MAX, step: ROTATION_STEP, value: rotation, onChange: setRotation }), jsxRuntime.jsx("button", { className: `${buttonClass} [font-size:16px]`, onClick: () => setRotation(rotation + ROTATION_STEP), disabled: rotation === ROTATION_MAX, children: "\u21BB" })] })), aspectSlider && (jsxRuntime.jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-aspect ${wrapperClass}`, children: [jsxRuntime.jsx("button", { className: buttonClass, onClick: () => setAspect(+(aspect - ASPECT_STEP).toFixed(2)), disabled: aspect - ASPECT_STEP < minAspect, children: "\u2195" }), jsxRuntime.jsx(AntSlider, { className: sliderClass, min: minAspect, max: maxAspect, step: ASPECT_STEP, value: aspect, onChange: setAspect }), jsxRuntime.jsx("button", { className: buttonClass, onClick: () => setAspect(+(aspect + ASPECT_STEP).toFixed(2)), disabled: aspect + ASPECT_STEP > maxAspect, children: "\u2194" })] })), showReset && (zoomSlider || rotationSlider || aspectSlider) && (jsxRuntime.jsx(AntButton, { className: "[
|
|
55
|
+
} })), zoomSlider && (jsxRuntime.jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-zoom ${wrapperClass}`, children: [jsxRuntime.jsx("button", { className: buttonClass, onClick: () => setZoom(+(zoom - ZOOM_STEP).toFixed(1)), disabled: zoom - ZOOM_STEP < minZoom, children: "\uFF0D" }), jsxRuntime.jsx(AntSlider, { className: sliderClass, min: minZoom, max: maxZoom, step: ZOOM_STEP, value: zoom, onChange: setZoom }), jsxRuntime.jsx("button", { className: buttonClass, onClick: () => setZoom(+(zoom + ZOOM_STEP).toFixed(1)), disabled: zoom + ZOOM_STEP > maxZoom, children: "\uFF0B" })] })), rotationSlider && (jsxRuntime.jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-rotation ${wrapperClass}`, children: [jsxRuntime.jsx("button", { className: `${buttonClass} [font-size:16px]`, onClick: () => setRotation(rotation - ROTATION_STEP), disabled: rotation === ROTATION_MIN, children: "\u21BA" }), jsxRuntime.jsx(AntSlider, { className: sliderClass, min: ROTATION_MIN, max: ROTATION_MAX, step: ROTATION_STEP, value: rotation, onChange: setRotation }), jsxRuntime.jsx("button", { className: `${buttonClass} [font-size:16px]`, onClick: () => setRotation(rotation + ROTATION_STEP), disabled: rotation === ROTATION_MAX, children: "\u21BB" })] })), aspectSlider && (jsxRuntime.jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-aspect ${wrapperClass}`, children: [jsxRuntime.jsx("button", { className: buttonClass, onClick: () => setAspect(+(aspect - ASPECT_STEP).toFixed(2)), disabled: aspect - ASPECT_STEP < minAspect, children: "\u2195" }), jsxRuntime.jsx(AntSlider, { className: sliderClass, min: minAspect, max: maxAspect, step: ASPECT_STEP, value: aspect, onChange: setAspect }), jsxRuntime.jsx("button", { className: buttonClass, onClick: () => setAspect(+(aspect + ASPECT_STEP).toFixed(2)), disabled: aspect + ASPECT_STEP > maxAspect, children: "\u2194" })] })), showReset && (zoomSlider || rotationSlider || aspectSlider) && (jsxRuntime.jsx(AntButton, { className: "[position:absolute] [bottom:20px]", style: isResetActive ? {} : { opacity: 0.3, pointerEvents: 'none' }, onClick: onReset, children: resetBtnText }))] }));
|
|
55
56
|
});
|
|
56
57
|
var EasyCrop$1 = react.memo(EasyCrop);
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
var style = document.createElement('style');
|
|
66
|
-
style.type = 'text/css';
|
|
67
|
-
|
|
68
|
-
if (insertAt === 'top') {
|
|
69
|
-
if (head.firstChild) {
|
|
70
|
-
head.insertBefore(style, head.firstChild);
|
|
71
|
-
} else {
|
|
72
|
-
head.appendChild(style);
|
|
73
|
-
}
|
|
74
|
-
} else {
|
|
75
|
-
head.appendChild(style);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (style.styleSheet) {
|
|
79
|
-
style.styleSheet.cssText = css;
|
|
80
|
-
} else {
|
|
81
|
-
style.appendChild(document.createTextNode(css));
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
var css_248z = ".\\[align-items\\:center\\]{align-items:center}.\\[background\\:transparent\\]{background:transparent}.\\[border\\:0\\]{border:0}.\\[bottom\\:20px\\]{bottom:20px}.\\[cursor\\:pointer\\]{cursor:pointer}.\\[display\\:flex\\]{display:flex}.\\[flex\\:1\\]{flex:1}.\\[font-family\\:inherit\\]{font-family:inherit}.\\[font-size\\:16px\\]{font-size:16px}.\\[font-size\\:18px\\]{font-size:18px}.\\[height\\:32px\\]{height:32px}.\\[height\\:40vh\\]{height:40vh}.\\[justify-content\\:center\\]{justify-content:center}.\\[margin-inline\\:auto\\]{margin-inline:auto}.\\[position\\:absolute\\]{position:absolute}.\\!\\[position\\:relative\\]{position:relative!important}.\\[width\\:100\\%\\]{width:100%}.\\[width\\:32px\\]{width:32px}.\\[width\\:60\\%\\]{width:60%}.disabled\\:\\[cursor\\:default\\]:disabled{cursor:default}.disabled\\:\\[opacity\\:20\\%\\]:disabled{opacity:20%}.\\[\\&\\~section\\:first-of-type\\]\\:\\[margin-top\\:16px\\]~section:first-of-type{margin-top:16px}.\\[\\&\\~section\\:last-of-type\\]\\:\\[margin-bottom\\:16px\\]~section:last-of-type{margin-bottom:16px}";
|
|
86
|
-
styleInject(css_248z,{"insertAt":"top"});
|
|
59
|
+
var css_248z = "/*! tailwindcss v4.1.14 | MIT License | https://tailwindcss.com */.visible{visibility:visible}.\\!\\[position\\:relative\\]{position:relative!important}.\\[position\\:absolute\\]{position:absolute}.\\[bottom\\:20px\\]{bottom:20px}.container{width:100%}.\\[margin-inline\\:auto\\]{margin-inline:auto}.\\[display\\:flex\\]{display:flex}.grid{display:grid}.\\[height\\:32px\\]{height:32px}.\\[height\\:40vh\\]{height:40vh}.\\[width\\:32px\\]{width:32px}.\\[width\\:60\\%\\]{width:60%}.\\[width\\:100\\%\\]{width:100%}.\\[flex\\:1\\]{flex:1}.\\[cursor\\:pointer\\]{cursor:pointer}.\\[align-items\\:center\\]{align-items:center}.\\[justify-content\\:center\\]{justify-content:center}.\\[font-family\\:inherit\\]{font-family:inherit}.\\[font-size\\:16px\\]{font-size:16px}.\\[font-size\\:18px\\]{font-size:18px}.\\[background\\:transparent\\]{background:transparent}.\\[border\\:0\\]{border:0}.disabled\\:\\[cursor\\:default\\]{&:disabled{cursor:default}}.disabled\\:\\[opacity\\:20\\%\\]{&:disabled{opacity:20%}}.\\[\\&\\~section\\:first-of-type\\]\\:\\[margin-top\\:16px\\]{&~section:first-of-type{margin-top:16px}}.\\[\\&\\~section\\:last-of-type\\]\\:\\[margin-bottom\\:16px\\]{&~section:last-of-type{margin-bottom:16px}}";
|
|
60
|
+
const style = document.createElement('style');
|
|
61
|
+
const meta = document.querySelector('meta[name="csp-nonce"]');
|
|
62
|
+
const nonce = meta && meta.content;
|
|
63
|
+
nonce && style.setAttribute('nonce', nonce);
|
|
64
|
+
style.textContent = css_248z;
|
|
65
|
+
document.head.appendChild(style);
|
|
87
66
|
|
|
88
67
|
const ImgCrop = react.forwardRef((props, cropperRef) => {
|
|
89
68
|
const { quality = 0.4, fillColor = 'white', zoomSlider = true, rotationSlider = false, aspectSlider = false, showReset = false, resetText, aspect = 1, minZoom = 1, maxZoom = 3, minAspect = 0.5, maxAspect = 2, cropShape = 'rect', showGrid = false, cropperProps, modalClassName, modalTitle, modalWidth, modalOk, modalCancel, onModalOk, onModalCancel, modalProps, beforeCrop, children, } = props;
|
|
@@ -143,9 +122,10 @@ const ImgCrop = react.forwardRef((props, cropperRef) => {
|
|
|
143
122
|
/**
|
|
144
123
|
* upload
|
|
145
124
|
*/
|
|
125
|
+
const [modalOpen, setModalOpen] = react.useState(false);
|
|
146
126
|
const [modalImage, setModalImage] = react.useState('');
|
|
147
|
-
const onCancel = react.useRef();
|
|
148
|
-
const onOk = react.useRef();
|
|
127
|
+
const onCancel = react.useRef(undefined);
|
|
128
|
+
const onOk = react.useRef(undefined);
|
|
149
129
|
const runBeforeUpload = react.useCallback((_a) => tslib.__awaiter(void 0, [_a], void 0, function* ({ beforeUpload, file, resolve, reject, }) {
|
|
150
130
|
const rawFile = file;
|
|
151
131
|
if (typeof beforeUpload !== 'function') {
|
|
@@ -189,13 +169,17 @@ const ImgCrop = react.forwardRef((props, cropperRef) => {
|
|
|
189
169
|
const reader = new FileReader();
|
|
190
170
|
reader.addEventListener('load', () => {
|
|
191
171
|
if (typeof reader.result === 'string') {
|
|
192
|
-
|
|
172
|
+
setModalOpen(true);
|
|
173
|
+
setTimeout(() => {
|
|
174
|
+
setModalImage(reader.result);
|
|
175
|
+
}, 10);
|
|
193
176
|
}
|
|
194
177
|
});
|
|
195
178
|
reader.readAsDataURL(processedFile);
|
|
196
179
|
// on modal cancel
|
|
197
180
|
onCancel.current = () => {
|
|
198
181
|
var _a, _b;
|
|
182
|
+
setModalOpen(false);
|
|
199
183
|
setModalImage('');
|
|
200
184
|
easyCropRef.current.onReset();
|
|
201
185
|
let hasResolveCalled = false;
|
|
@@ -209,6 +193,7 @@ const ImgCrop = react.forwardRef((props, cropperRef) => {
|
|
|
209
193
|
};
|
|
210
194
|
// on modal confirm
|
|
211
195
|
onOk.current = (event) => tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
196
|
+
setModalOpen(false);
|
|
212
197
|
setModalImage('');
|
|
213
198
|
easyCropRef.current.onReset();
|
|
214
199
|
const canvas = getCropCanvas(event.target);
|
|
@@ -258,7 +243,7 @@ const ImgCrop = react.forwardRef((props, cropperRef) => {
|
|
|
258
243
|
const isCN = lang === 'zh-CN';
|
|
259
244
|
const title = modalTitle || (isCN ? '编辑图片' : 'Edit image');
|
|
260
245
|
const resetBtnText = resetText || (isCN ? '重置' : 'Reset');
|
|
261
|
-
return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [getNewUpload(children),
|
|
246
|
+
return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [getNewUpload(children), jsxRuntime.jsx(AntModal, Object.assign({}, modalProps, modalBaseProps, { open: modalOpen, title: title, onCancel: onCancel.current, onOk: onOk.current, wrapClassName: wrapClassName, maskClosable: false, destroyOnHidden: true, children: jsxRuntime.jsx(EasyCrop$1, { ref: easyCropRef, cropperRef: cropperRef, zoomSlider: zoomSlider, rotationSlider: rotationSlider, aspectSlider: aspectSlider, showReset: showReset, resetBtnText: resetBtnText, modalImage: modalImage, aspect: aspect, minZoom: minZoom, maxZoom: maxZoom, minAspect: minAspect, maxAspect: maxAspect, cropShape: cropShape, showGrid: showGrid, cropperProps: cropperProps }) }))] }));
|
|
262
247
|
});
|
|
263
248
|
|
|
264
249
|
module.exports = ImgCrop;
|
package/dist/antd-img-crop.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import * as react from 'react';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
2
|
+
import { JSX } from 'react';
|
|
3
|
+
import { UploadProps, ModalProps } from 'antd';
|
|
4
|
+
import { CropperProps } from 'react-easy-crop';
|
|
4
5
|
|
|
5
6
|
type BeforeUpload = Exclude<UploadProps['beforeUpload'], undefined>;
|
|
6
7
|
type BeforeUploadReturnType = ReturnType<BeforeUpload>;
|
|
7
|
-
type ImgCropProps = {
|
|
8
|
+
type ImgCropProps$1 = {
|
|
8
9
|
quality?: number;
|
|
9
10
|
fillColor?: string;
|
|
10
11
|
zoomSlider?: boolean;
|
|
@@ -19,7 +20,7 @@ type ImgCropProps = {
|
|
|
19
20
|
maxAspect?: number;
|
|
20
21
|
cropShape?: 'rect' | 'round';
|
|
21
22
|
showGrid?: boolean;
|
|
22
|
-
cropperProps?: Omit<CropperProps, 'image' | 'crop' | 'zoom' | 'rotation' | 'aspect' | 'minZoom' | 'maxZoom' | 'minAspect' | 'maxAspect' | 'zoomWithScroll' | 'cropShape' | 'showGrid' | 'onCropChange' | 'onZoomChange' | 'onRotationChange' | 'onCropComplete' | 'classes'
|
|
23
|
+
cropperProps?: Omit<CropperProps, 'image' | 'crop' | 'zoom' | 'rotation' | 'aspect' | 'minZoom' | 'maxZoom' | 'minAspect' | 'maxAspect' | 'zoomWithScroll' | 'cropShape' | 'showGrid' | 'onCropChange' | 'onZoomChange' | 'onRotationChange' | 'onCropComplete' | 'classes' | 'keyboardStep'> & Partial<Pick<CropperProps, 'keyboardStep'>>;
|
|
23
24
|
modalClassName?: string;
|
|
24
25
|
modalTitle?: string;
|
|
25
26
|
modalWidth?: number | string;
|
|
@@ -27,11 +28,12 @@ type ImgCropProps = {
|
|
|
27
28
|
modalCancel?: string;
|
|
28
29
|
onModalOk?: (value: BeforeUploadReturnType) => void;
|
|
29
30
|
onModalCancel?: (resolve: (value: BeforeUploadReturnType) => void) => void;
|
|
30
|
-
modalProps?: Omit<ModalProps, 'className' | 'title' | 'width' | 'okText' | 'cancelText' | 'onOk' | 'onCancel' | 'open' | 'visible' | 'wrapClassName' | 'maskClosable' | '
|
|
31
|
+
modalProps?: Omit<ModalProps, 'className' | 'title' | 'width' | 'okText' | 'cancelText' | 'onOk' | 'onCancel' | 'open' | 'visible' | 'wrapClassName' | 'maskClosable' | 'destroyOnHidden'>;
|
|
31
32
|
beforeCrop?: BeforeUpload;
|
|
32
33
|
children: JSX.Element;
|
|
33
34
|
};
|
|
34
35
|
|
|
35
|
-
declare const ImgCrop: react.ForwardRefExoticComponent<ImgCropProps & react.RefAttributes<
|
|
36
|
+
declare const ImgCrop: react.ForwardRefExoticComponent<Omit<ImgCropProps, "ref"> & react.RefAttributes<CropperRef>>;
|
|
36
37
|
|
|
37
|
-
export {
|
|
38
|
+
export { ImgCrop as default };
|
|
39
|
+
export type { ImgCropProps$1 as ImgCropProps };
|
|
@@ -17,23 +17,28 @@ const ROTATION_STEP = 1;
|
|
|
17
17
|
const ASPECT_STEP = 0.01;
|
|
18
18
|
|
|
19
19
|
const EasyCrop = forwardRef((props, ref) => {
|
|
20
|
-
const { cropperRef, zoomSlider, rotationSlider, aspectSlider, showReset, resetBtnText, modalImage, aspect:
|
|
20
|
+
const { cropperRef, zoomSlider, rotationSlider, aspectSlider, showReset, resetBtnText, modalImage, aspect: propAspect, minZoom, maxZoom, minAspect, maxAspect, cropShape, showGrid, cropperProps, } = props;
|
|
21
|
+
const [crop, setCrop] = useState({ x: 0, y: 0 });
|
|
21
22
|
const [zoom, setZoom] = useState(ZOOM_INITIAL);
|
|
22
23
|
const [rotation, setRotation] = useState(ROTATION_INITIAL);
|
|
23
|
-
const [aspect, setAspect] = useState(
|
|
24
|
+
const [aspect, setAspect] = useState(propAspect);
|
|
25
|
+
const cropPixelsRef = useRef({ width: 0, height: 0, x: 0, y: 0 });
|
|
26
|
+
const onCropComplete = useCallback((_, croppedAreaPixels) => {
|
|
27
|
+
cropPixelsRef.current = croppedAreaPixels;
|
|
28
|
+
}, []);
|
|
29
|
+
const prevPropAspect = useRef(propAspect);
|
|
30
|
+
if (prevPropAspect.current !== propAspect) {
|
|
31
|
+
prevPropAspect.current = propAspect;
|
|
32
|
+
setAspect(propAspect);
|
|
33
|
+
}
|
|
24
34
|
const isResetActive = zoom !== ZOOM_INITIAL ||
|
|
25
35
|
rotation !== ROTATION_INITIAL ||
|
|
26
|
-
aspect !==
|
|
36
|
+
aspect !== propAspect;
|
|
27
37
|
const onReset = () => {
|
|
28
38
|
setZoom(ZOOM_INITIAL);
|
|
29
39
|
setRotation(ROTATION_INITIAL);
|
|
30
|
-
setAspect(
|
|
40
|
+
setAspect(propAspect);
|
|
31
41
|
};
|
|
32
|
-
const [crop, onCropChange] = useState({ x: 0, y: 0 });
|
|
33
|
-
const cropPixelsRef = useRef({ width: 0, height: 0, x: 0, y: 0 });
|
|
34
|
-
const onCropComplete = useCallback((_, croppedAreaPixels) => {
|
|
35
|
-
cropPixelsRef.current = croppedAreaPixels;
|
|
36
|
-
}, []);
|
|
37
42
|
useImperativeHandle(ref, () => ({
|
|
38
43
|
rotation,
|
|
39
44
|
cropPixelsRef,
|
|
@@ -42,46 +47,20 @@ const EasyCrop = forwardRef((props, ref) => {
|
|
|
42
47
|
const wrapperClass = '[display:flex] [align-items:center] [width:60%] [margin-inline:auto]';
|
|
43
48
|
const buttonClass = '[display:flex] [align-items:center] [justify-content:center] [height:32px] [width:32px] [background:transparent] [border:0] [font-family:inherit] [font-size:18px] [cursor:pointer] disabled:[opacity:20%] disabled:[cursor:default]';
|
|
44
49
|
const sliderClass = '[flex:1]';
|
|
45
|
-
return (jsxs(Fragment, { children: [jsx(Cropper, Object.assign({}, cropperProps, { ref: cropperRef, image: modalImage, crop: crop,
|
|
46
|
-
//
|
|
47
|
-
zoom: zoom, rotation: rotation, aspect: aspect, minZoom: minZoom, maxZoom: maxZoom, zoomWithScroll: zoomSlider,
|
|
48
|
-
//
|
|
49
|
-
cropShape: cropShape, showGrid: showGrid, onCropChange: onCropChange, onZoomChange: setZoom, onRotationChange: setRotation, onCropComplete: onCropComplete, classes: {
|
|
50
|
+
return (jsxs(Fragment, { children: [jsx(Cropper, Object.assign({}, cropperProps, { ref: cropperRef, image: modalImage, crop: crop, zoom: zoom, rotation: rotation, aspect: aspect, minZoom: minZoom, maxZoom: maxZoom, zoomWithScroll: zoomSlider, cropShape: cropShape, showGrid: showGrid, onCropChange: setCrop, onZoomChange: setZoom, onRotationChange: setRotation, onCropComplete: onCropComplete, classes: {
|
|
50
51
|
containerClassName: `${PREFIX}-container ![position:relative] [width:100%] [height:40vh] [&~section:first-of-type]:[margin-top:16px] [&~section:last-of-type]:[margin-bottom:16px]`,
|
|
51
52
|
mediaClassName: `${PREFIX}-media`,
|
|
52
|
-
} })), zoomSlider && (jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-zoom ${wrapperClass}`, children: [jsx("button", { className: buttonClass, onClick: () => setZoom(+(zoom - ZOOM_STEP).toFixed(1)), disabled: zoom - ZOOM_STEP < minZoom, children: "\uFF0D" }), jsx(AntSlider, { className: sliderClass, min: minZoom, max: maxZoom, step: ZOOM_STEP, value: zoom, onChange: setZoom }), jsx("button", { className: buttonClass, onClick: () => setZoom(+(zoom + ZOOM_STEP).toFixed(1)), disabled: zoom + ZOOM_STEP > maxZoom, children: "\uFF0B" })] })), rotationSlider && (jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-rotation ${wrapperClass}`, children: [jsx("button", { className: `${buttonClass} [font-size:16px]`, onClick: () => setRotation(rotation - ROTATION_STEP), disabled: rotation === ROTATION_MIN, children: "\u21BA" }), jsx(AntSlider, { className: sliderClass, min: ROTATION_MIN, max: ROTATION_MAX, step: ROTATION_STEP, value: rotation, onChange: setRotation }), jsx("button", { className: `${buttonClass} [font-size:16px]`, onClick: () => setRotation(rotation + ROTATION_STEP), disabled: rotation === ROTATION_MAX, children: "\u21BB" })] })), aspectSlider && (jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-aspect ${wrapperClass}`, children: [jsx("button", { className: buttonClass, onClick: () => setAspect(+(aspect - ASPECT_STEP).toFixed(2)), disabled: aspect - ASPECT_STEP < minAspect, children: "\u2195" }), jsx(AntSlider, { className: sliderClass, min: minAspect, max: maxAspect, step: ASPECT_STEP, value: aspect, onChange: setAspect }), jsx("button", { className: buttonClass, onClick: () => setAspect(+(aspect + ASPECT_STEP).toFixed(2)), disabled: aspect + ASPECT_STEP > maxAspect, children: "\u2194" })] })), showReset && (zoomSlider || rotationSlider || aspectSlider) && (jsx(AntButton, { className: "[
|
|
53
|
+
} })), zoomSlider && (jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-zoom ${wrapperClass}`, children: [jsx("button", { className: buttonClass, onClick: () => setZoom(+(zoom - ZOOM_STEP).toFixed(1)), disabled: zoom - ZOOM_STEP < minZoom, children: "\uFF0D" }), jsx(AntSlider, { className: sliderClass, min: minZoom, max: maxZoom, step: ZOOM_STEP, value: zoom, onChange: setZoom }), jsx("button", { className: buttonClass, onClick: () => setZoom(+(zoom + ZOOM_STEP).toFixed(1)), disabled: zoom + ZOOM_STEP > maxZoom, children: "\uFF0B" })] })), rotationSlider && (jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-rotation ${wrapperClass}`, children: [jsx("button", { className: `${buttonClass} [font-size:16px]`, onClick: () => setRotation(rotation - ROTATION_STEP), disabled: rotation === ROTATION_MIN, children: "\u21BA" }), jsx(AntSlider, { className: sliderClass, min: ROTATION_MIN, max: ROTATION_MAX, step: ROTATION_STEP, value: rotation, onChange: setRotation }), jsx("button", { className: `${buttonClass} [font-size:16px]`, onClick: () => setRotation(rotation + ROTATION_STEP), disabled: rotation === ROTATION_MAX, children: "\u21BB" })] })), aspectSlider && (jsxs("section", { className: `${PREFIX}-control ${PREFIX}-control-aspect ${wrapperClass}`, children: [jsx("button", { className: buttonClass, onClick: () => setAspect(+(aspect - ASPECT_STEP).toFixed(2)), disabled: aspect - ASPECT_STEP < minAspect, children: "\u2195" }), jsx(AntSlider, { className: sliderClass, min: minAspect, max: maxAspect, step: ASPECT_STEP, value: aspect, onChange: setAspect }), jsx("button", { className: buttonClass, onClick: () => setAspect(+(aspect + ASPECT_STEP).toFixed(2)), disabled: aspect + ASPECT_STEP > maxAspect, children: "\u2194" })] })), showReset && (zoomSlider || rotationSlider || aspectSlider) && (jsx(AntButton, { className: "[position:absolute] [bottom:20px]", style: isResetActive ? {} : { opacity: 0.3, pointerEvents: 'none' }, onClick: onReset, children: resetBtnText }))] }));
|
|
53
54
|
});
|
|
54
55
|
var EasyCrop$1 = memo(EasyCrop);
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
var style = document.createElement('style');
|
|
64
|
-
style.type = 'text/css';
|
|
65
|
-
|
|
66
|
-
if (insertAt === 'top') {
|
|
67
|
-
if (head.firstChild) {
|
|
68
|
-
head.insertBefore(style, head.firstChild);
|
|
69
|
-
} else {
|
|
70
|
-
head.appendChild(style);
|
|
71
|
-
}
|
|
72
|
-
} else {
|
|
73
|
-
head.appendChild(style);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (style.styleSheet) {
|
|
77
|
-
style.styleSheet.cssText = css;
|
|
78
|
-
} else {
|
|
79
|
-
style.appendChild(document.createTextNode(css));
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
var css_248z = ".\\[align-items\\:center\\]{align-items:center}.\\[background\\:transparent\\]{background:transparent}.\\[border\\:0\\]{border:0}.\\[bottom\\:20px\\]{bottom:20px}.\\[cursor\\:pointer\\]{cursor:pointer}.\\[display\\:flex\\]{display:flex}.\\[flex\\:1\\]{flex:1}.\\[font-family\\:inherit\\]{font-family:inherit}.\\[font-size\\:16px\\]{font-size:16px}.\\[font-size\\:18px\\]{font-size:18px}.\\[height\\:32px\\]{height:32px}.\\[height\\:40vh\\]{height:40vh}.\\[justify-content\\:center\\]{justify-content:center}.\\[margin-inline\\:auto\\]{margin-inline:auto}.\\[position\\:absolute\\]{position:absolute}.\\!\\[position\\:relative\\]{position:relative!important}.\\[width\\:100\\%\\]{width:100%}.\\[width\\:32px\\]{width:32px}.\\[width\\:60\\%\\]{width:60%}.disabled\\:\\[cursor\\:default\\]:disabled{cursor:default}.disabled\\:\\[opacity\\:20\\%\\]:disabled{opacity:20%}.\\[\\&\\~section\\:first-of-type\\]\\:\\[margin-top\\:16px\\]~section:first-of-type{margin-top:16px}.\\[\\&\\~section\\:last-of-type\\]\\:\\[margin-bottom\\:16px\\]~section:last-of-type{margin-bottom:16px}";
|
|
84
|
-
styleInject(css_248z,{"insertAt":"top"});
|
|
57
|
+
var css_248z = "/*! tailwindcss v4.1.14 | MIT License | https://tailwindcss.com */.visible{visibility:visible}.\\!\\[position\\:relative\\]{position:relative!important}.\\[position\\:absolute\\]{position:absolute}.\\[bottom\\:20px\\]{bottom:20px}.container{width:100%}.\\[margin-inline\\:auto\\]{margin-inline:auto}.\\[display\\:flex\\]{display:flex}.grid{display:grid}.\\[height\\:32px\\]{height:32px}.\\[height\\:40vh\\]{height:40vh}.\\[width\\:32px\\]{width:32px}.\\[width\\:60\\%\\]{width:60%}.\\[width\\:100\\%\\]{width:100%}.\\[flex\\:1\\]{flex:1}.\\[cursor\\:pointer\\]{cursor:pointer}.\\[align-items\\:center\\]{align-items:center}.\\[justify-content\\:center\\]{justify-content:center}.\\[font-family\\:inherit\\]{font-family:inherit}.\\[font-size\\:16px\\]{font-size:16px}.\\[font-size\\:18px\\]{font-size:18px}.\\[background\\:transparent\\]{background:transparent}.\\[border\\:0\\]{border:0}.disabled\\:\\[cursor\\:default\\]{&:disabled{cursor:default}}.disabled\\:\\[opacity\\:20\\%\\]{&:disabled{opacity:20%}}.\\[\\&\\~section\\:first-of-type\\]\\:\\[margin-top\\:16px\\]{&~section:first-of-type{margin-top:16px}}.\\[\\&\\~section\\:last-of-type\\]\\:\\[margin-bottom\\:16px\\]{&~section:last-of-type{margin-bottom:16px}}";
|
|
58
|
+
const style = document.createElement('style');
|
|
59
|
+
const meta = document.querySelector('meta[name="csp-nonce"]');
|
|
60
|
+
const nonce = meta && meta.content;
|
|
61
|
+
nonce && style.setAttribute('nonce', nonce);
|
|
62
|
+
style.textContent = css_248z;
|
|
63
|
+
document.head.appendChild(style);
|
|
85
64
|
|
|
86
65
|
const ImgCrop = forwardRef((props, cropperRef) => {
|
|
87
66
|
const { quality = 0.4, fillColor = 'white', zoomSlider = true, rotationSlider = false, aspectSlider = false, showReset = false, resetText, aspect = 1, minZoom = 1, maxZoom = 3, minAspect = 0.5, maxAspect = 2, cropShape = 'rect', showGrid = false, cropperProps, modalClassName, modalTitle, modalWidth, modalOk, modalCancel, onModalOk, onModalCancel, modalProps, beforeCrop, children, } = props;
|
|
@@ -141,9 +120,10 @@ const ImgCrop = forwardRef((props, cropperRef) => {
|
|
|
141
120
|
/**
|
|
142
121
|
* upload
|
|
143
122
|
*/
|
|
123
|
+
const [modalOpen, setModalOpen] = useState(false);
|
|
144
124
|
const [modalImage, setModalImage] = useState('');
|
|
145
|
-
const onCancel = useRef();
|
|
146
|
-
const onOk = useRef();
|
|
125
|
+
const onCancel = useRef(undefined);
|
|
126
|
+
const onOk = useRef(undefined);
|
|
147
127
|
const runBeforeUpload = useCallback((_a) => __awaiter(void 0, [_a], void 0, function* ({ beforeUpload, file, resolve, reject, }) {
|
|
148
128
|
const rawFile = file;
|
|
149
129
|
if (typeof beforeUpload !== 'function') {
|
|
@@ -187,13 +167,17 @@ const ImgCrop = forwardRef((props, cropperRef) => {
|
|
|
187
167
|
const reader = new FileReader();
|
|
188
168
|
reader.addEventListener('load', () => {
|
|
189
169
|
if (typeof reader.result === 'string') {
|
|
190
|
-
|
|
170
|
+
setModalOpen(true);
|
|
171
|
+
setTimeout(() => {
|
|
172
|
+
setModalImage(reader.result);
|
|
173
|
+
}, 10);
|
|
191
174
|
}
|
|
192
175
|
});
|
|
193
176
|
reader.readAsDataURL(processedFile);
|
|
194
177
|
// on modal cancel
|
|
195
178
|
onCancel.current = () => {
|
|
196
179
|
var _a, _b;
|
|
180
|
+
setModalOpen(false);
|
|
197
181
|
setModalImage('');
|
|
198
182
|
easyCropRef.current.onReset();
|
|
199
183
|
let hasResolveCalled = false;
|
|
@@ -207,6 +191,7 @@ const ImgCrop = forwardRef((props, cropperRef) => {
|
|
|
207
191
|
};
|
|
208
192
|
// on modal confirm
|
|
209
193
|
onOk.current = (event) => __awaiter(void 0, void 0, void 0, function* () {
|
|
194
|
+
setModalOpen(false);
|
|
210
195
|
setModalImage('');
|
|
211
196
|
easyCropRef.current.onReset();
|
|
212
197
|
const canvas = getCropCanvas(event.target);
|
|
@@ -256,7 +241,7 @@ const ImgCrop = forwardRef((props, cropperRef) => {
|
|
|
256
241
|
const isCN = lang === 'zh-CN';
|
|
257
242
|
const title = modalTitle || (isCN ? '编辑图片' : 'Edit image');
|
|
258
243
|
const resetBtnText = resetText || (isCN ? '重置' : 'Reset');
|
|
259
|
-
return (jsxs(Fragment, { children: [getNewUpload(children),
|
|
244
|
+
return (jsxs(Fragment, { children: [getNewUpload(children), jsx(AntModal, Object.assign({}, modalProps, modalBaseProps, { open: modalOpen, title: title, onCancel: onCancel.current, onOk: onOk.current, wrapClassName: wrapClassName, maskClosable: false, destroyOnHidden: true, children: jsx(EasyCrop$1, { ref: easyCropRef, cropperRef: cropperRef, zoomSlider: zoomSlider, rotationSlider: rotationSlider, aspectSlider: aspectSlider, showReset: showReset, resetBtnText: resetBtnText, modalImage: modalImage, aspect: aspect, minZoom: minZoom, maxZoom: maxZoom, minAspect: minAspect, maxAspect: maxAspect, cropShape: cropShape, showGrid: showGrid, cropperProps: cropperProps }) }))] }));
|
|
260
245
|
});
|
|
261
246
|
|
|
262
247
|
export { ImgCrop as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "antd-img-crop",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.26.0",
|
|
4
4
|
"description": "An image cropper for Ant Design Upload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -22,42 +22,51 @@
|
|
|
22
22
|
"files": [
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "rm -rf dist && rollup -c --configPlugin @rollup/plugin-typescript"
|
|
27
|
+
},
|
|
25
28
|
"peerDependencies": {
|
|
26
29
|
"antd": ">=4.0.0",
|
|
27
30
|
"react": ">=16.8.0",
|
|
28
31
|
"react-dom": ">=16.8.0"
|
|
29
32
|
},
|
|
30
33
|
"dependencies": {
|
|
31
|
-
"react-easy-crop": "^5.
|
|
34
|
+
"react-easy-crop": "^5.5.3",
|
|
32
35
|
"tslib": "^2.8.1"
|
|
33
36
|
},
|
|
34
37
|
"devDependencies": {
|
|
38
|
+
"@eslint/compat": "^1.4.0",
|
|
39
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
40
|
+
"@eslint/js": "^9.37.0",
|
|
35
41
|
"@rollup/plugin-replace": "^6.0.2",
|
|
36
|
-
"@rollup/plugin-typescript": "^12.1.
|
|
37
|
-
"@
|
|
38
|
-
"@types/
|
|
39
|
-
"@types/react
|
|
40
|
-
"@
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
42
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
43
|
+
"@tailwindcss/postcss": "^4.1.14",
|
|
44
|
+
"@types/node": "^24.7.1",
|
|
45
|
+
"@types/react": "19.2.2",
|
|
46
|
+
"@types/react-dom": "19.2.1",
|
|
47
|
+
"@typescript-eslint/eslint-plugin": "^8.46.0",
|
|
48
|
+
"@vitejs/plugin-react": "^5.0.4",
|
|
49
|
+
"antd": "^5.27.4",
|
|
50
|
+
"autoprefixer": "^10.4.21",
|
|
51
|
+
"eslint": "^9.37.0",
|
|
52
|
+
"eslint-config-prettier": "^10.1.8",
|
|
45
53
|
"eslint-config-react-app": "^7.0.1",
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"react": "
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
+
"eslint-plugin-flowtype": "^8.0.3",
|
|
55
|
+
"eslint-plugin-import": "^2.32.0",
|
|
56
|
+
"eslint-plugin-jsx-a11y": "^6.10.2",
|
|
57
|
+
"eslint-plugin-react": "^7.37.5",
|
|
58
|
+
"eslint-plugin-react-hooks": "^7.0.0",
|
|
59
|
+
"postcss": "^8.5.6",
|
|
60
|
+
"prettier": "^3.6.2",
|
|
61
|
+
"prettier-plugin-organize-imports": "^4.3.0",
|
|
62
|
+
"prettier-plugin-tailwindcss": "^0.6.14",
|
|
63
|
+
"react": "19.2.0",
|
|
64
|
+
"react-dom": "19.2.0",
|
|
65
|
+
"rollup": "4.52.4",
|
|
66
|
+
"rollup-plugin-dts": "^6.2.3",
|
|
54
67
|
"rollup-plugin-postcss": "^4.0.2",
|
|
55
|
-
"tailwindcss": "^
|
|
56
|
-
"typescript": "^5.
|
|
57
|
-
"vite": "^
|
|
58
|
-
},
|
|
59
|
-
"scripts": {
|
|
60
|
-
"build": "rm -rf dist && rollup -c --configPlugin @rollup/plugin-typescript",
|
|
61
|
-
"u": "pnpm update --latest !eslint !react !react-dom !@types/react !@types/react-dom"
|
|
68
|
+
"tailwindcss": "^4.1.14",
|
|
69
|
+
"typescript": "^5.9.3",
|
|
70
|
+
"vite": "^7.1.9"
|
|
62
71
|
}
|
|
63
|
-
}
|
|
72
|
+
}
|