react-native-signature-canvas 4.5.0 → 4.6.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/index.js +258 -186
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, {
|
|
2
|
+
useState,
|
|
3
|
+
useEffect,
|
|
4
|
+
useMemo,
|
|
5
|
+
useRef,
|
|
6
|
+
forwardRef,
|
|
7
|
+
useImperativeHandle,
|
|
8
|
+
} from "react";
|
|
2
9
|
import { View, StyleSheet, ActivityIndicator } from "react-native";
|
|
3
10
|
|
|
4
11
|
import htmlContent from "./h5/html";
|
|
@@ -10,203 +17,268 @@ import { WebView } from "react-native-webview";
|
|
|
10
17
|
const styles = StyleSheet.create({
|
|
11
18
|
webBg: {
|
|
12
19
|
width: "100%",
|
|
13
|
-
backgroundColor: "
|
|
14
|
-
flex: 1
|
|
20
|
+
backgroundColor: "transparent",
|
|
21
|
+
flex: 1,
|
|
22
|
+
},
|
|
23
|
+
loadingOverlayContainer: {
|
|
24
|
+
position: "absolute",
|
|
25
|
+
top: 0,
|
|
26
|
+
bottom: 0,
|
|
27
|
+
left: 0,
|
|
28
|
+
right: 0,
|
|
29
|
+
alignItems: "center",
|
|
30
|
+
justifyContent: "center",
|
|
15
31
|
},
|
|
16
|
-
loadingOverlayContainer: { position: "absolute", top: 0, bottom: 0, left: 0, right: 0, alignItems: "center", justifyContent: "center" },
|
|
17
32
|
});
|
|
18
33
|
|
|
19
|
-
const SignatureView = forwardRef(
|
|
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
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
34
|
+
const SignatureView = forwardRef(
|
|
35
|
+
(
|
|
36
|
+
{
|
|
37
|
+
androidHardwareAccelerationDisabled = false,
|
|
38
|
+
autoClear = false,
|
|
39
|
+
backgroundColor = "",
|
|
40
|
+
bgHeight = 0,
|
|
41
|
+
bgWidth = 0,
|
|
42
|
+
bgSrc = null,
|
|
43
|
+
clearText = "Clear",
|
|
44
|
+
confirmText = "Confirm",
|
|
45
|
+
customHtml = null,
|
|
46
|
+
dataURL = "",
|
|
47
|
+
descriptionText = "Sign above",
|
|
48
|
+
dotSize = null,
|
|
49
|
+
imageType = "",
|
|
50
|
+
minWidth = 0.5,
|
|
51
|
+
maxWidth = 2.5,
|
|
52
|
+
onOK = () => {},
|
|
53
|
+
onEmpty = () => {},
|
|
54
|
+
onClear = () => {},
|
|
55
|
+
onUndo = () => {},
|
|
56
|
+
onRedo = () => {},
|
|
57
|
+
onDraw = () => {},
|
|
58
|
+
onErase = () => {},
|
|
59
|
+
onGetData = () => {},
|
|
60
|
+
onChangePenColor = () => {},
|
|
61
|
+
onChangePenSize = () => {},
|
|
62
|
+
onBegin = () => {},
|
|
63
|
+
onEnd = () => {},
|
|
64
|
+
overlayHeight = 0,
|
|
65
|
+
overlayWidth = 0,
|
|
66
|
+
overlaySrc = null,
|
|
67
|
+
penColor = "",
|
|
68
|
+
rotated = false,
|
|
69
|
+
style = null,
|
|
70
|
+
scrollable = false,
|
|
71
|
+
trimWhitespace = false,
|
|
72
|
+
webStyle = "",
|
|
73
|
+
webviewContainerStyle = null,
|
|
74
|
+
},
|
|
75
|
+
ref
|
|
76
|
+
) => {
|
|
77
|
+
const [loading, setLoading] = useState(true);
|
|
78
|
+
const webViewRef = useRef();
|
|
79
|
+
const source = useMemo(() => {
|
|
80
|
+
let injectedJavaScript = injectedSignaturePad + injectedApplication;
|
|
81
|
+
const htmlContentValue = customHtml ? customHtml : htmlContent;
|
|
82
|
+
injectedJavaScript = injectedJavaScript.replace(
|
|
83
|
+
/<%autoClear%>/g,
|
|
84
|
+
autoClear
|
|
85
|
+
);
|
|
86
|
+
injectedJavaScript = injectedJavaScript.replace(
|
|
87
|
+
/<%trimWhitespace%>/g,
|
|
88
|
+
trimWhitespace
|
|
89
|
+
);
|
|
90
|
+
injectedJavaScript = injectedJavaScript.replace(
|
|
91
|
+
/<%imageType%>/g,
|
|
92
|
+
imageType
|
|
93
|
+
);
|
|
94
|
+
injectedJavaScript = injectedJavaScript.replace(/<%dataURL%>/g, dataURL);
|
|
95
|
+
injectedJavaScript = injectedJavaScript.replace(
|
|
96
|
+
/<%penColor%>/g,
|
|
97
|
+
penColor
|
|
98
|
+
);
|
|
99
|
+
injectedJavaScript = injectedJavaScript.replace(
|
|
100
|
+
/<%backgroundColor%>/g,
|
|
101
|
+
backgroundColor
|
|
102
|
+
);
|
|
103
|
+
injectedJavaScript = injectedJavaScript.replace(/<%dotSize%>/g, dotSize);
|
|
104
|
+
injectedJavaScript = injectedJavaScript.replace(
|
|
105
|
+
/<%minWidth%>/g,
|
|
106
|
+
minWidth
|
|
107
|
+
);
|
|
108
|
+
injectedJavaScript = injectedJavaScript.replace(
|
|
109
|
+
/<%maxWidth%>/g,
|
|
110
|
+
maxWidth
|
|
111
|
+
);
|
|
72
112
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
113
|
+
let html = htmlContentValue(injectedJavaScript);
|
|
114
|
+
html = html.replace(/<%bgWidth%>/g, bgWidth);
|
|
115
|
+
html = html.replace(/<%bgHeight%>/g, bgHeight);
|
|
116
|
+
html = html.replace(/<%bgSrc%>/g, bgSrc);
|
|
117
|
+
html = html.replace(/<%overlayWidth%>/g, overlayWidth);
|
|
118
|
+
html = html.replace(/<%overlayHeight%>/g, overlayHeight);
|
|
119
|
+
html = html.replace(/<%overlaySrc%>/g, overlaySrc);
|
|
120
|
+
html = html.replace(/<%style%>/g, webStyle);
|
|
121
|
+
html = html.replace(/<%description%>/g, descriptionText);
|
|
122
|
+
html = html.replace(/<%confirm%>/g, confirmText);
|
|
123
|
+
html = html.replace(/<%clear%>/g, clearText);
|
|
124
|
+
html = html.replace(/<%orientation%>/g, rotated);
|
|
85
125
|
|
|
86
|
-
|
|
87
|
-
|
|
126
|
+
return { html };
|
|
127
|
+
}, [
|
|
128
|
+
customHtml,
|
|
129
|
+
autoClear,
|
|
130
|
+
trimWhitespace,
|
|
131
|
+
rotated,
|
|
132
|
+
imageType,
|
|
133
|
+
webStyle,
|
|
134
|
+
descriptionText,
|
|
135
|
+
confirmText,
|
|
136
|
+
clearText,
|
|
137
|
+
dataURL,
|
|
138
|
+
bgSrc,
|
|
139
|
+
bgWidth,
|
|
140
|
+
bgHeight,
|
|
141
|
+
]);
|
|
88
142
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
if (webViewRef.current) {
|
|
145
|
+
webViewRef.current.reload();
|
|
146
|
+
}
|
|
147
|
+
}, [source]);
|
|
93
148
|
|
|
94
|
-
|
|
95
|
-
|
|
149
|
+
const isJson = (str) => {
|
|
150
|
+
try {
|
|
96
151
|
JSON.parse(str);
|
|
97
|
-
|
|
152
|
+
} catch (e) {
|
|
98
153
|
return false;
|
|
99
|
-
}
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const getSignature = e => {
|
|
104
|
-
switch (e.nativeEvent.data) {
|
|
105
|
-
case "BEGIN":
|
|
106
|
-
onBegin();
|
|
107
|
-
break;
|
|
108
|
-
case "END":
|
|
109
|
-
onEnd();
|
|
110
|
-
break;
|
|
111
|
-
case "EMPTY":
|
|
112
|
-
onEmpty();
|
|
113
|
-
break;
|
|
114
|
-
case "CLEAR":
|
|
115
|
-
onClear();
|
|
116
|
-
break;
|
|
117
|
-
case "UNDO":
|
|
118
|
-
onUndo();
|
|
119
|
-
break;
|
|
120
|
-
case "REDO":
|
|
121
|
-
onRedo();
|
|
122
|
-
break;
|
|
123
|
-
case "DRAW":
|
|
124
|
-
onDraw();
|
|
125
|
-
break;
|
|
126
|
-
case "ERASE":
|
|
127
|
-
onErase();
|
|
128
|
-
break;
|
|
129
|
-
case "CHANGE_PEN":
|
|
130
|
-
onChangePenColor();
|
|
131
|
-
break;
|
|
132
|
-
case "CHANGE_PEN_SIZE":
|
|
133
|
-
onChangePenSize();
|
|
134
|
-
break;
|
|
135
|
-
default:
|
|
136
|
-
isJson(e.nativeEvent.data)? onGetData(e.nativeEvent.data): onOK(e.nativeEvent.data);
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
useImperativeHandle(ref, () => ({
|
|
141
|
-
readSignature: () => {
|
|
142
|
-
if (webViewRef.current) {
|
|
143
|
-
webViewRef.current.injectJavaScript("readSignature();true;");
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
clearSignature: () => {
|
|
147
|
-
if (webViewRef.current) {
|
|
148
|
-
webViewRef.current.injectJavaScript("clearSignature();true;");
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
undo: () => {
|
|
152
|
-
if (webViewRef.current) {
|
|
153
|
-
webViewRef.current.injectJavaScript("undo();true;");
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
redo: () => {
|
|
157
|
-
if (webViewRef.current) {
|
|
158
|
-
webViewRef.current.injectJavaScript("redo();true;");
|
|
159
154
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
155
|
+
return true;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const getSignature = (e) => {
|
|
159
|
+
switch (e.nativeEvent.data) {
|
|
160
|
+
case "BEGIN":
|
|
161
|
+
onBegin();
|
|
162
|
+
break;
|
|
163
|
+
case "END":
|
|
164
|
+
onEnd();
|
|
165
|
+
break;
|
|
166
|
+
case "EMPTY":
|
|
167
|
+
onEmpty();
|
|
168
|
+
break;
|
|
169
|
+
case "CLEAR":
|
|
170
|
+
onClear();
|
|
171
|
+
break;
|
|
172
|
+
case "UNDO":
|
|
173
|
+
onUndo();
|
|
174
|
+
break;
|
|
175
|
+
case "REDO":
|
|
176
|
+
onRedo();
|
|
177
|
+
break;
|
|
178
|
+
case "DRAW":
|
|
179
|
+
onDraw();
|
|
180
|
+
break;
|
|
181
|
+
case "ERASE":
|
|
182
|
+
onErase();
|
|
183
|
+
break;
|
|
184
|
+
case "CHANGE_PEN":
|
|
185
|
+
onChangePenColor();
|
|
186
|
+
break;
|
|
187
|
+
case "CHANGE_PEN_SIZE":
|
|
188
|
+
onChangePenSize();
|
|
189
|
+
break;
|
|
190
|
+
default:
|
|
191
|
+
isJson(e.nativeEvent.data)
|
|
192
|
+
? onGetData(e.nativeEvent.data)
|
|
193
|
+
: onOK(e.nativeEvent.data);
|
|
184
194
|
}
|
|
185
|
-
}
|
|
186
|
-
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
useImperativeHandle(
|
|
198
|
+
ref,
|
|
199
|
+
() => ({
|
|
200
|
+
readSignature: () => {
|
|
201
|
+
if (webViewRef.current) {
|
|
202
|
+
webViewRef.current.injectJavaScript("readSignature();true;");
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
clearSignature: () => {
|
|
206
|
+
if (webViewRef.current) {
|
|
207
|
+
webViewRef.current.injectJavaScript("clearSignature();true;");
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
undo: () => {
|
|
211
|
+
if (webViewRef.current) {
|
|
212
|
+
webViewRef.current.injectJavaScript("undo();true;");
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
redo: () => {
|
|
216
|
+
if (webViewRef.current) {
|
|
217
|
+
webViewRef.current.injectJavaScript("redo();true;");
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
draw: () => {
|
|
221
|
+
if (webViewRef.current) {
|
|
222
|
+
webViewRef.current.injectJavaScript("draw();true;");
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
erase: () => {
|
|
226
|
+
if (webViewRef.current) {
|
|
227
|
+
webViewRef.current.injectJavaScript("erase();true;");
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
changePenColor: (color) => {
|
|
231
|
+
if (webViewRef.current) {
|
|
232
|
+
webViewRef.current.injectJavaScript(
|
|
233
|
+
"changePenColor('" + color + "');true;"
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
changePenSize: (minW, maxW) => {
|
|
238
|
+
if (webViewRef.current) {
|
|
239
|
+
webViewRef.current.injectJavaScript(
|
|
240
|
+
"changePenSize(" + minW + "," + maxW + ");true;"
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
getData: () => {
|
|
245
|
+
if (webViewRef.current) {
|
|
246
|
+
webViewRef.current.injectJavaScript("getData();true;");
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
}),
|
|
250
|
+
[webViewRef]
|
|
251
|
+
);
|
|
187
252
|
|
|
188
|
-
|
|
253
|
+
const renderError = ({ nativeEvent }) =>
|
|
254
|
+
console.warn("WebView error: ", nativeEvent);
|
|
189
255
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
256
|
+
return (
|
|
257
|
+
<View style={[styles.webBg, style]}>
|
|
258
|
+
<WebView
|
|
259
|
+
bounces={false}
|
|
260
|
+
style={[webviewContainerStyle]}
|
|
261
|
+
scrollEnabled={scrollable}
|
|
262
|
+
androidLayerType="hardware"
|
|
263
|
+
androidHardwareAccelerationDisabled={
|
|
264
|
+
androidHardwareAccelerationDisabled
|
|
265
|
+
}
|
|
266
|
+
ref={webViewRef}
|
|
267
|
+
useWebKit={true}
|
|
268
|
+
source={source}
|
|
269
|
+
onMessage={getSignature}
|
|
270
|
+
javaScriptEnabled={true}
|
|
271
|
+
onError={renderError}
|
|
272
|
+
onLoadEnd={() => setLoading(false)}
|
|
273
|
+
/>
|
|
274
|
+
{loading && (
|
|
275
|
+
<View style={styles.loadingOverlayContainer}>
|
|
276
|
+
<ActivityIndicator color={"transparent"} />
|
|
277
|
+
</View>
|
|
278
|
+
)}
|
|
279
|
+
</View>
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
);
|
|
211
283
|
|
|
212
284
|
export default SignatureView;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-signature-canvas",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"description": "React Native Signature Component based Canvas for Android && IOS && expo",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
},
|
|
30
30
|
"homepage": "https://github.com/YanYuanFE/react-native-signature-canvas#readme",
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"react-native-webview": ">
|
|
32
|
+
"react-native-webview": ">13.3.1"
|
|
33
33
|
}
|
|
34
34
|
}
|