UNPKG

react-cropper

Version:
92 lines (84 loc) 4.62 kB
import React, { useState, useEffect } from 'react'; import Cropper from 'cropperjs'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } var applyDefaultOptions = function (cropper, options) { if (options === void 0) { options = {}; } var _a = options.enable, enable = _a === void 0 ? true : _a, _b = options.scaleX, scaleX = _b === void 0 ? 1 : _b, _c = options.scaleY, scaleY = _c === void 0 ? 1 : _c, _d = options.zoomTo, zoomTo = _d === void 0 ? 1 : _d, _e = options.rotateTo, rotateTo = _e === void 0 ? 0 : _e; enable ? cropper.enable() : cropper.disable(); cropper.scaleX(scaleX); cropper.scaleY(scaleY); cropper.rotateTo(rotateTo); cropper.zoomTo(zoomTo); }; var ReactCropper = React.forwardRef(function (_a, ref) { var props = __rest(_a, []); var _b = props.dragMode, dragMode = _b === void 0 ? 'crop' : _b, src = props.src, style = props.style, className = props.className, crossOrigin = props.crossOrigin, scaleX = props.scaleX, scaleY = props.scaleY, enable = props.enable, zoomTo = props.zoomTo, rotateTo = props.rotateTo, _c = props.alt, alt = _c === void 0 ? 'picture' : _c, ready = props.ready, onInitialized = props.onInitialized, rest = __rest(props, ["dragMode", "src", "style", "className", "crossOrigin", "scaleX", "scaleY", "enable", "zoomTo", "rotateTo", "alt", "ready", "onInitialized"]); var _d = useState(undefined), cropper = _d[0], setCropper = _d[1]; var defaultOptions = { scaleY: scaleY, scaleX: scaleX, enable: enable, zoomTo: zoomTo, rotateTo: rotateTo }; var cropperRef = ref; useEffect(function () { if (cropperRef !== null && cropperRef.current !== null && typeof cropperRef !== 'undefined' && cropperRef) { var cropper_1 = new Cropper(cropperRef.current, __assign(__assign({ dragMode: dragMode }, rest), { ready: function (e) { if (e.target !== null) { var target = e.target; applyDefaultOptions(target.cropper, defaultOptions); } ready && ready(e); } })); onInitialized && onInitialized(cropper_1); setCropper(cropper_1); } /** * destroy cropper on un-mount */ return function () { if (cropperRef !== null) { cropper === null || cropper === void 0 ? void 0 : cropper.destroy(); } }; }, [cropperRef]); /** * re-render when src changes */ useEffect(function () { if (typeof cropper !== 'undefined' && typeof src !== 'undefined') { cropper.reset().clear().replace(src); } }, [src]); return (React.createElement("div", { style: style, className: className }, React.createElement("img", { crossOrigin: crossOrigin, src: src, alt: alt, style: { opacity: 0, maxWidth: '100%' }, ref: ref }))); }); export default ReactCropper; export { ReactCropper as Cropper };