UNPKG

4.46 kBJavaScriptView Raw
1var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
2
3var _class, _temp;
4
5function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
6
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
10
11function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
12
13import React, { Component } from 'react';
14import PropTypes from 'prop-types';
15import Instance from 'jcrop';
16import 'jcrop/dist/jcrop.css'; // Jcrop
17
18var Jcrop = (_temp = _class = function (_Component) {
19 _inherits(Jcrop, _Component);
20
21 function Jcrop(props) {
22 _classCallCheck(this, Jcrop);
23
24 var _this = _possibleConstructorReturn(this, _Component.call(this, props));
25
26 _this.formatPos = function (argPos) {
27 var pos = argPos;
28 for (var n in argPos) {
29 if (n === 'h') pos['height'] = argPos[n];else if (n === 'w') pos['width'] = argPos[n];
30 }
31 return pos;
32 };
33
34 _this.update = function () {
35 if (!_this.props.src) return;
36 var _this$props = _this.props,
37 rect = _this$props.rect,
38 scale = _this$props.scale;
39
40 Instance.load(_this.$el).then(function (img) {
41 var jcp = Instance.attach(img, _this.props.options);
42 var frame = null;
43 var pos = {};
44 // 按尺寸创建
45 if (rect) {
46 pos = {
47 x: rect[0],
48 y: rect[1],
49 w: rect[2],
50 h: rect[3]
51 };
52 frame = Instance.Rect.create(pos.x, pos.y, pos.w, pos.h);
53 jcp.newWidget(frame);
54 // 按比例创建
55 } else {
56 var _frame;
57
58 frame = Instance.Rect.sizeOf(jcp.el);
59 pos = (_frame = frame).scale.apply(_frame, scale).center(frame.w, frame.h);
60 jcp.newWidget(pos);
61 }
62 if (_this.props.onChange) _this.props.onChange({ pos: _this.formatPos(pos), src: _this.props.src });
63 jcp.listen('crop.change', function (widget, e) {
64 pos = _this.formatPos(widget.pos);
65 if (_this.props.onChange) _this.props.onChange({ pos: pos, src: _this.props.src });
66 });
67 }).catch(function (err) {
68 console.error(err);
69 });
70 };
71
72 return _this;
73 }
74
75 Jcrop.prototype.componentDidMount = function componentDidMount() {
76 this.update();
77 };
78
79 Jcrop.prototype.render = function render() {
80 var _this2 = this;
81
82 var _props = this.props,
83 rect = _props.rect,
84 scale = _props.scale,
85 options = _props.options,
86 onChange = _props.onChange,
87 src = _props.src,
88 style = _props.style,
89 className = _props.className,
90 others = _objectWithoutProperties(_props, ['rect', 'scale', 'options', 'onChange', 'src', 'style', 'className']);
91
92 return React.createElement('img', _extends({ alt: '', ref: function ref(el) {
93 _this2.$el = el;
94 }, className: className, style: Object.assign({ maxWidth: '100%' }, style), src: src }, others));
95 };
96
97 return Jcrop;
98}(Component), _class.defaultProps = {
99 scale: [.7, .5],
100 options: {
101 multi: false
102 }
103}, _temp);
104export { Jcrop as default };
105Jcrop.propTypes = process.env.NODE_ENV !== "production" ? {
106 style: PropTypes.object,
107 className: PropTypes.string,
108 src: PropTypes.string,
109 rect: PropTypes.array, // [10,10,100,100]
110 scale: PropTypes.array, // [.7,.5]
111 options: PropTypes.object,
112 onChange: PropTypes.func
113} : {};
\No newline at end of file