-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.jsx
190 lines (170 loc) · 4.32 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import React from "react";
import Core from "./index.js";
export class SimpleCrop extends React.Component {
_instance; //组件实例
constructor(props) {
super(props);
}
//初次渲染
componentDidMount() {
let cropParams = JSON.parse(JSON.stringify(this.props));
cropParams.cropCallback = () => {
this.cropCallback();
};
cropParams.closeCallback = () => {
this.closeCallback();
};
cropParams.uploadCallback = () => {
this.uploadCallback();
};
this._instance = new Core(cropParams);
}
//选取裁剪图片成功回调
uploadCallback() {
if (this.props.uploadCallback) {
let src = this._instance ? this._instance.src : this.props.src;
this.props.uploadCallback(src);
}
}
//关闭回调
closeCallback() {
if (this.props.closeCallback) {
this.props.closeCallback();
}
}
//裁剪回调
cropCallback() {
if (this.props.cropCallback) {
let $resultCanvas = this._instance ? this._instance.$resultCanvas : null;
this.props.cropCallback($resultCanvas);
}
}
//更新
componentDidUpdate(prevProps) {
if (this._instance) {
if (this.hasChanged(["src"], prevProps)) {
this._instance.setImage(this.props.src);
}
if (
this.hasChanged(
[
"rotateSlider",
"startAngle",
"endAngle",
"gapAngle",
"lineationItemWidth",
],
prevProps
)
) {
this._instance.initRotateSlider(this.props);
}
if (
this.hasChanged(["cropSizePercent"], prevProps) ||
!this.isEquivalent(
this.props.positionOffset,
prevProps.positionOffset,
["top", "left"]
) ||
!this.isEquivalent(this.props.size, prevProps.size, ["width", "height"])
) {
this._instance.updateBox(this.props);
}
if (
this.hasChanged(
[
"borderWidth",
"borderColor",
"boldCornerLen",
"coverColor",
"borderDraw",
"coverDraw",
],
prevProps
)
) {
this._instance.initBoxBorder(this.props);
}
if (
!this.isEquivalent(this.props.funcBtns, prevProps.funcBtns, /^[0-9]*$/)
) {
this._instance.initFuncBtns(this.props);
}
if (this.hasChanged(["title"], prevProps)) {
this._instance.initTitle(this.props);
}
if (this.props.visible == false) {
this._instance.hide();
} else {
this._instance.show();
}
}
}
//属性是否发生变化
hasChanged(names, props) {
let cur = {};
let prev = {};
for (let i = 0; i < names.length; i++) {
let item = names[i];
cur[item] = this.props[item];
prev[item] = props[item];
}
return !this.isEquivalent(cur, prev, names);
}
//根据两个简单对象的值比较它们是否相同
isEquivalent(a, b, include) {
if (a != null && b != null) {
let aProps = [];
let aTemps = Object.getOwnPropertyNames(a);
let bProps = [];
let bTemps = Object.getOwnPropertyNames(b);
//只考虑 include
if (include instanceof Array) {
// 数组
for (let i = 0; i < include.length; i++) {
let item = include[i];
if (aTemps.includes(item)) {
aProps.push(item);
}
if (bTemps.includes(item)) {
bProps.push(item);
}
}
} else if (include instanceof RegExp) {
//正则
for (let i = 0; i < aTemps.length; i++) {
let item = aTemps[i];
if (include.test(item)) {
aProps.push(item);
}
}
for (let i = 0; i < bTemps.length; i++) {
let item = bTemps[i];
if (include.test(item)) {
bProps.push(item);
}
}
} else {
aProps = aTemps;
bTemps = bTemps;
}
if (aProps.length != bProps.length) {
return false;
}
for (let i = 0; i < aProps.length; i++) {
let propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
} else if (a === b) {
return true;
} else {
return false;
}
}
render() {
return <div />;
}
}