forked from wonsikin/react-native-barcode-builder
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.cjs
218 lines (176 loc) · 6.28 KB
/
index.cjs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var React = require('react');
var reactNative = require('react-native');
var barcodes = require('jsbarcode/src/barcodes');
var Svg = require('react-native-svg');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var barcodes__default = /*#__PURE__*/_interopDefaultLegacy(barcodes);
var Svg__default = /*#__PURE__*/_interopDefaultLegacy(Svg);
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
_setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
var ErrorBoundary = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(ErrorBoundary, _React$Component);
function ErrorBoundary(props) {
var _this;
_this = _React$Component.call(this, props) || this;
_this.state = {
hasError: false
};
return _this;
}
ErrorBoundary.getDerivedStateFromError = function getDerivedStateFromError(error) {
return {
hasError: true
};
};
var _proto = ErrorBoundary.prototype;
_proto.componentDidCatch = function componentDidCatch(error, errorInfo) {
console.log(error, errorInfo);
};
_proto.render = function render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return /*#__PURE__*/React__default["default"].createElement("h1", null, "Something went wrong.");
}
return this.props.children;
};
return ErrorBoundary;
}(React__default["default"].Component);
var Barcode = function Barcode(_ref) {
var value = _ref.value,
_ref$format = _ref.format,
format = _ref$format === void 0 ? 'CODE128' : _ref$format,
_ref$width = _ref.width,
width = _ref$width === void 0 ? 2 : _ref$width,
_ref$height = _ref.height,
height = _ref$height === void 0 ? 100 : _ref$height,
text = _ref.text,
_ref$textColor = _ref.textColor,
textColor = _ref$textColor === void 0 ? '#000000' : _ref$textColor,
_ref$lineColor = _ref.lineColor,
lineColor = _ref$lineColor === void 0 ? '#000000' : _ref$lineColor,
_ref$background = _ref.background,
background = _ref$background === void 0 ? '#ffffff' : _ref$background,
onError = _ref.onError;
var _useState = React.useState([]),
bars = _useState[0],
setBars = _useState[1];
var _useState2 = React.useState(0),
barCodeWidth = _useState2[0],
setBarCodeWidth = _useState2[1];
var props = {
value: value,
format: format,
width: width,
height: height,
text: text,
textColor: textColor,
lineColor: lineColor,
background: background,
onError: onError
};
React.useEffect(function () {
update();
}, [value]);
var update = function update() {
var encoder = barcodes__default["default"][format];
var encoded = encode(value, encoder, props);
if (encoded) {
setBars(drawSvgBarCode(encoded, props));
setBarCodeWidth(encoded.data.length * width);
}
};
var drawSvgBarCode = function drawSvgBarCode(encoding, options) {
var rects = []; // binary data of barcode
var binary = encoding.data;
var barWidth = 0;
var x = 0;
var yFrom = 0;
for (var b = 0; b < binary.length; b++) {
x = b * options.width;
if (binary[b] === '1') {
barWidth++;
} else if (barWidth > 0) {
rects[rects.length] = drawRect(x - options.width * barWidth, yFrom, options.width * barWidth, options.height);
barWidth = 0;
}
} // Last draw is needed since the barcode ends with 1
if (barWidth > 0) {
rects[rects.length] = drawRect(x - options.width * (barWidth - 1), yFrom, options.width * barWidth, options.height);
}
return rects;
};
var drawRect = function drawRect(x, y, width, height) {
return "M" + x + "," + y + "h" + width + "v" + height + "h-" + width + "z";
}; // encode() handles the Encoder call and builds the binary string to be rendered
var encode = function encode(text, Encoder, options) {
// If text is not a non-empty string, throw error.
if (typeof text !== 'string' || text.length === 0) {
if (options.onError) {
options.onError(new Error('Barcode value must be a non-empty string'));
return;
}
throw new Error('Barcode value must be a non-empty string');
}
var encoder;
try {
encoder = new Encoder(text, options);
} catch (error) {
// If the encoder could not be instantiated, throw error.
if (options.onError) {
options.onError(new Error('Invalid barcode format.'));
return;
}
throw new Error('Invalid barcode format.');
} // If the input is not valid for the encoder, throw error.
if (!encoder.valid()) {
if (options.onError) {
options.onError(new Error('Invalid barcode for selected format.'));
return;
}
throw new Error('Invalid barcode for selected format.');
} // Make a request for the binary data (and other infromation) that should be rendered
// encoded stucture is {
// text: 'xxxxx',
// data: '110100100001....'
// }
var encoded = encoder.encode();
return encoded;
};
var backgroundStyle = {
backgroundColor: background
};
return /*#__PURE__*/React__default["default"].createElement(ErrorBoundary, null, /*#__PURE__*/React__default["default"].createElement(reactNative.View, {
style: [styles.svgContainer, backgroundStyle]
}, /*#__PURE__*/React__default["default"].createElement(Svg__default["default"], {
height: height,
width: barCodeWidth,
fill: lineColor
}, /*#__PURE__*/React__default["default"].createElement(Svg.Path, {
d: bars.join(' ')
})), typeof text !== 'undefined' && /*#__PURE__*/React__default["default"].createElement(reactNative.Text, {
style: {
color: textColor,
width: barCodeWidth,
textAlign: 'center'
}
}, text)));
};
var styles = reactNative.StyleSheet.create({
svgContainer: {
alignItems: 'center',
padding: 10
}
});
module.exports = Barcode;
//# sourceMappingURL=index.cjs.map