@oneblink/apps-react 5.3.1-beta.2 → 5.3.1-beta.3
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.
@@ -1,18 +1,10 @@
|
|
1
1
|
import * as React from 'react';
|
2
|
-
import
|
3
|
-
import jsQR from 'jsqr';
|
4
|
-
import OnLoading from '../components/renderer/OnLoading';
|
2
|
+
import { Html5QrcodeScanner } from 'html5-qrcode';
|
5
3
|
import CopyToClipboardButton from '../components/renderer/CopyToClipboardButton';
|
6
|
-
import quaggaReader from '../services/barcode-readers/quagger.js';
|
7
4
|
import useBooleanState from '../hooks/useBooleanState';
|
8
5
|
import LookupButton from '../components/renderer/LookupButton';
|
9
6
|
import FormElementLabelContainer from '../components/renderer/FormElementLabelContainer';
|
10
|
-
import { Sentry } from '@oneblink/apps';
|
11
7
|
import useLookupNotification, { LookupNotificationContext, } from '../hooks/useLookupNotification';
|
12
|
-
const MS_BETWEEN_IMAGE_PROCESSING = 10;
|
13
|
-
const fadedSquareWidthInPixels = 200;
|
14
|
-
const fadedSquareHeightInPixels = 150;
|
15
|
-
const redLineHeightInPixels = 1;
|
16
8
|
function FormElementBarcodeScanner({ id, element, value, onChange, validationMessage, displayValidationMessage, isDirty, setIsDirty, }) {
|
17
9
|
const [isCameraOpen, startBarcodeScanner, stopBarcodeScanner] = useBooleanState(false);
|
18
10
|
const [error, setError] = React.useState(null);
|
@@ -58,7 +50,7 @@ function FormElementBarcodeScanner({ id, element, value, onChange, validationMes
|
|
58
50
|
React.createElement("div", null,
|
59
51
|
React.createElement("h4", { className: "title is-4" }, "Whoops..."),
|
60
52
|
React.createElement("p", null, error.message))))),
|
61
|
-
isCameraOpen ? (React.createElement(BarcodeScanner, {
|
53
|
+
isCameraOpen ? (React.createElement(BarcodeScanner, { id: `${id}==BARCODE_SCANNER`, onScan: handleScan, onClose: stopBarcodeScanner })) : (React.createElement("div", null,
|
62
54
|
React.createElement("div", { className: "field has-addons" },
|
63
55
|
React.createElement("div", { className: "control is-expanded has-icons-right" },
|
64
56
|
React.createElement("input", { type: "text", placeholder: element.placeholderValue, id: id, name: element.name, className: "input ob-input cypress-barcode-scanner-control has-margin-bottom-8", value: text, onChange: (e) => onChange(element, {
|
@@ -74,239 +66,34 @@ function FormElementBarcodeScanner({ id, element, value, onChange, validationMes
|
|
74
66
|
React.createElement("div", { className: "has-text-danger ob-error__text cypress-validation-message" }, validationMessage))))));
|
75
67
|
}
|
76
68
|
export default React.memo(FormElementBarcodeScanner);
|
77
|
-
function BarcodeScanner({
|
78
|
-
const
|
79
|
-
const figureElementRef = React.useRef(null);
|
80
|
-
const [{ isLoading = false, selectedDeviceId, error }, setState] = React.useState({
|
81
|
-
isLoading: true,
|
82
|
-
selectedDeviceId: undefined,
|
83
|
-
error: undefined,
|
84
|
-
});
|
85
|
-
const [camera, setCamera] = React.useState(null);
|
86
|
-
const setError = React.useCallback((error) => {
|
87
|
-
setState({
|
88
|
-
error,
|
89
|
-
isLoading: false,
|
90
|
-
selectedDeviceId: undefined,
|
91
|
-
});
|
92
|
-
}, []);
|
93
|
-
// Create timeout using $timeout outside of the scan function so
|
94
|
-
// so that we can cancel it when navigating away from screen
|
95
|
-
const scanImageForBarcode = React.useCallback((videoElement, waitInMS, options, checkStop) => {
|
96
|
-
const restrictedBarcodeTypes = element.restrictedBarcodeTypes || [];
|
97
|
-
// Using $timeout here instead of $interval as we dont know
|
98
|
-
// exactly how long each processing of the image will take.
|
99
|
-
setTimeout(async () => {
|
100
|
-
if (checkStop())
|
101
|
-
return;
|
102
|
-
const canvasElement = document.createElement('canvas');
|
103
|
-
canvasElement.width = options.sourceWidth;
|
104
|
-
canvasElement.height = options.sourceHeight;
|
105
|
-
const canvasContext = canvasElement.getContext('2d');
|
106
|
-
if (canvasContext) {
|
107
|
-
canvasContext.drawImage(videoElement, options.sourceX, options.sourceY, canvasElement.width, canvasElement.height, 0, 0, canvasElement.width, canvasElement.height);
|
108
|
-
if (!element.restrictBarcodeTypes ||
|
109
|
-
(element.restrictedBarcodeTypes || []).indexOf('qr_reader') > -1) {
|
110
|
-
const imageData = canvasContext.getImageData(0, 0, canvasElement.width, canvasElement.height);
|
111
|
-
const code = jsQR(imageData.data, imageData.width, imageData.height, {
|
112
|
-
inversionAttempts: 'dontInvert',
|
113
|
-
});
|
114
|
-
if (code) {
|
115
|
-
return onScan(code.data);
|
116
|
-
}
|
117
|
-
}
|
118
|
-
}
|
119
|
-
if (!element.restrictBarcodeTypes ||
|
120
|
-
!(restrictedBarcodeTypes.length === 1 &&
|
121
|
-
restrictedBarcodeTypes[0] === 'qr_reader')) {
|
122
|
-
const base64Image = canvasElement.toDataURL('image/png');
|
123
|
-
const quaggaResult = await quaggaReader(base64Image, restrictedBarcodeTypes);
|
124
|
-
if (quaggaResult) {
|
125
|
-
return onScan(quaggaResult);
|
126
|
-
}
|
127
|
-
}
|
128
|
-
if (checkStop())
|
129
|
-
return;
|
130
|
-
scanImageForBarcode(videoElement, MS_BETWEEN_IMAGE_PROCESSING, options, checkStop);
|
131
|
-
}, waitInMS);
|
132
|
-
}, [element.restrictBarcodeTypes, element.restrictedBarcodeTypes, onScan]);
|
133
|
-
const switchCamera = React.useCallback(() => {
|
134
|
-
if (!camera) {
|
135
|
-
return;
|
136
|
-
}
|
137
|
-
// We will just be rotating between the available camera.
|
138
|
-
const nextDeviceIndex = camera.availableDevices.findIndex((mediaDeviceInfo) => mediaDeviceInfo.deviceId === camera.activeDeviceId) + 1;
|
139
|
-
const nextDevice = camera.availableDevices[nextDeviceIndex] || camera.availableDevices[0];
|
140
|
-
setState({
|
141
|
-
error: undefined,
|
142
|
-
isLoading: true,
|
143
|
-
selectedDeviceId: nextDevice.deviceId,
|
144
|
-
});
|
145
|
-
}, [camera]);
|
69
|
+
function BarcodeScanner({ id, onScan, onClose }) {
|
70
|
+
const [html5QrcodeScanner, setHtml5QrcodeScanner] = React.useState(null);
|
146
71
|
React.useEffect(() => {
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
72
|
+
const verbose = true;
|
73
|
+
const newHtml5QrcodeScanner = new Html5QrcodeScanner(id, {
|
74
|
+
fps: 10,
|
75
|
+
qrbox: {
|
76
|
+
width: 250,
|
77
|
+
height: 250,
|
78
|
+
},
|
79
|
+
}, verbose);
|
80
|
+
setHtml5QrcodeScanner(newHtml5QrcodeScanner);
|
152
81
|
return () => {
|
153
|
-
|
82
|
+
newHtml5QrcodeScanner.clear();
|
154
83
|
};
|
155
|
-
}, []);
|
84
|
+
}, [id]);
|
156
85
|
React.useEffect(() => {
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
let ignore = false;
|
165
|
-
(async () => {
|
166
|
-
try {
|
167
|
-
const videoElement = videoElementRef.current;
|
168
|
-
const figureElement = figureElementRef.current;
|
169
|
-
if (!videoElement || !figureElement) {
|
170
|
-
return;
|
171
|
-
}
|
172
|
-
console.log('Opening camera with:', selectedDeviceId || 'UNKNOWN');
|
173
|
-
await camera.open(selectedDeviceId);
|
174
|
-
if (ignore) {
|
175
|
-
return;
|
176
|
-
}
|
177
|
-
setState({
|
178
|
-
error: undefined,
|
179
|
-
isLoading: false,
|
180
|
-
selectedDeviceId,
|
181
|
-
});
|
182
|
-
// @ts-expect-error ???
|
183
|
-
const fadedSquareElement = figureElement.getElementsByClassName('ob-barcode-scanner__square')[0];
|
184
|
-
// @ts-expect-error ???
|
185
|
-
const redLineElement = figureElement.getElementsByClassName('ob-barcode-scanner__line')[0];
|
186
|
-
console.log('videoElement Width pixels', videoElement.clientWidth);
|
187
|
-
console.log('videoElement Height pixels', videoElement.clientHeight);
|
188
|
-
console.log('videoElement Width', videoElement.videoWidth);
|
189
|
-
console.log('videoElement Height', videoElement.videoHeight);
|
190
|
-
// Faded Square needs its values set in pixels
|
191
|
-
const fadedSquareLeftInPixels = (videoElement.clientWidth - fadedSquareWidthInPixels) / 2;
|
192
|
-
console.log('fadedSquareLeftInPixels', fadedSquareLeftInPixels);
|
193
|
-
const fadedSquareTopInPixels = (videoElement.clientHeight - fadedSquareHeightInPixels) / 2;
|
194
|
-
console.log('fadedSquareTopInPixels', fadedSquareTopInPixels);
|
195
|
-
fadedSquareElement.style.borderBottom = `${fadedSquareTopInPixels}px`;
|
196
|
-
fadedSquareElement.style.borderTop = `${fadedSquareTopInPixels}px`;
|
197
|
-
fadedSquareElement.style.borderLeft = `${fadedSquareLeftInPixels}px`;
|
198
|
-
fadedSquareElement.style.borderRight = `${fadedSquareLeftInPixels}px`;
|
199
|
-
fadedSquareElement.style.borderColor = 'rgba(0, 0, 0, 0.25)';
|
200
|
-
fadedSquareElement.style.borderStyle = 'solid';
|
201
|
-
redLineElement.style.height = `${redLineHeightInPixels}px`;
|
202
|
-
redLineElement.style.top = `${(videoElement.clientHeight - redLineHeightInPixels) / 2}px`;
|
203
|
-
redLineElement.style.left = `${fadedSquareLeftInPixels}px`;
|
204
|
-
redLineElement.style.right = `${fadedSquareLeftInPixels}px`;
|
205
|
-
// Need to calculate the actual width, which is not in pixels
|
206
|
-
const ratio = videoElement.videoWidth / videoElement.clientWidth;
|
207
|
-
console.log('pixel to video Ratio', ratio);
|
208
|
-
const left = ratio * fadedSquareLeftInPixels;
|
209
|
-
console.log('left in video measurement', left);
|
210
|
-
const top = ratio * fadedSquareTopInPixels;
|
211
|
-
console.log('top in video measurement', top);
|
212
|
-
const fadedSquareWidth = fadedSquareWidthInPixels * ratio;
|
213
|
-
console.log('red square in video measurement', fadedSquareWidth);
|
214
|
-
// Wait a little before scanning the first image
|
215
|
-
// to prevent image processing staring before
|
216
|
-
// camera is ready.
|
217
|
-
scanImageForBarcode(videoElement, 250, {
|
218
|
-
sourceX: left,
|
219
|
-
sourceY: top,
|
220
|
-
sourceWidth: fadedSquareWidth,
|
221
|
-
sourceHeight: fadedSquareWidth,
|
222
|
-
}, () => ignore);
|
223
|
-
}
|
224
|
-
catch (error) {
|
225
|
-
if (ignore) {
|
226
|
-
return;
|
227
|
-
}
|
228
|
-
console.warn('Error while attempting to open camera', error);
|
229
|
-
Sentry.captureException(error);
|
230
|
-
switch (error.name) {
|
231
|
-
case 'NotSupportedError': {
|
232
|
-
setError(new Error('Your browser does not support accessing your camera. Please click "Cancel" below and type in the barcode value manually.'));
|
233
|
-
break;
|
234
|
-
}
|
235
|
-
case 'NotAllowedError': {
|
236
|
-
setError(new Error('Cannot scan for barcodes without granting the application access to the camera. Please click "Cancel" below to try again.'));
|
237
|
-
break;
|
238
|
-
}
|
239
|
-
default: {
|
240
|
-
setError(new Error('An unknown error has occurred, please click "Cancel" below to try again. If the problem persists, please contact support.'));
|
241
|
-
}
|
242
|
-
}
|
243
|
-
}
|
244
|
-
})();
|
245
|
-
return () => {
|
246
|
-
ignore = true;
|
247
|
-
};
|
248
|
-
}, [camera, error, scanImageForBarcode, selectedDeviceId, setError]);
|
86
|
+
html5QrcodeScanner === null || html5QrcodeScanner === void 0 ? void 0 : html5QrcodeScanner.render(function onScanSuccess(decodedText, decodedResult) {
|
87
|
+
console.log(`Code matched = ${decodedText}`, decodedResult);
|
88
|
+
onScan(decodedText);
|
89
|
+
}, function onScanFailure() {
|
90
|
+
// do nothing and keep scanning
|
91
|
+
});
|
92
|
+
}, [html5QrcodeScanner, onScan]);
|
249
93
|
return (React.createElement("div", null,
|
250
|
-
React.createElement("figure", { className: "ob-figure"
|
251
|
-
React.createElement("div", {
|
252
|
-
isLoading && React.createElement(OnLoading, { small: true }),
|
253
|
-
!!error && (React.createElement("div", null,
|
254
|
-
React.createElement("h4", { className: "title is-4" }, "Whoops..."),
|
255
|
-
React.createElement("p", null, error.message))),
|
256
|
-
React.createElement("div", { className: clsx('is-relative', {
|
257
|
-
'is-hidden': isLoading || error,
|
258
|
-
}) },
|
259
|
-
React.createElement("div", { className: "ob-barcode-scanner__square" }),
|
260
|
-
React.createElement("div", { className: "ob-barcode-scanner__line" }),
|
261
|
-
React.createElement("video", { ref: videoElementRef, autoPlay: true, playsInline: true, className: "ob-barcode-scanner__video" })))),
|
94
|
+
React.createElement("figure", { className: "ob-figure" },
|
95
|
+
React.createElement("div", { id: id })),
|
262
96
|
React.createElement("div", { className: "buttons ob-buttons" },
|
263
|
-
React.createElement("button", { type: "button", className: "button ob-button ob-button__cancel is-light cypress-cancel-scan-barcode-button", onClick: onClose }, "Cancel")
|
264
|
-
((camera === null || camera === void 0 ? void 0 : camera.availableDevices.length) || 1) > 1 && (React.createElement("button", { type: "button", className: "button ob-button ob-button__switch-camera is-primary cypress-switch-camera-button", onClick: switchCamera }, "Switch Camera")))));
|
265
|
-
}
|
266
|
-
class HTML5Camera {
|
267
|
-
constructor(htmlVideoElement) {
|
268
|
-
this.htmlVideoElement = htmlVideoElement;
|
269
|
-
this.availableDevices = [];
|
270
|
-
this.mediaStream = undefined;
|
271
|
-
}
|
272
|
-
get activeDeviceId() {
|
273
|
-
var _a;
|
274
|
-
if (this.mediaStream) {
|
275
|
-
const [activeMediaStreamTrack] = this.mediaStream.getTracks();
|
276
|
-
return (_a = activeMediaStreamTrack === null || activeMediaStreamTrack === void 0 ? void 0 : activeMediaStreamTrack.getSettings()) === null || _a === void 0 ? void 0 : _a.deviceId;
|
277
|
-
}
|
278
|
-
}
|
279
|
-
async open(deviceId) {
|
280
|
-
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
281
|
-
const error = new Error();
|
282
|
-
error.name = 'NotSupportedError';
|
283
|
-
throw error;
|
284
|
-
}
|
285
|
-
this.close();
|
286
|
-
const constraints = {
|
287
|
-
video: {
|
288
|
-
facingMode: deviceId ? undefined : 'environment',
|
289
|
-
deviceId: deviceId ? { exact: deviceId } : undefined,
|
290
|
-
},
|
291
|
-
};
|
292
|
-
const mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
|
293
|
-
this.mediaStream = mediaStream;
|
294
|
-
this.htmlVideoElement.srcObject = mediaStream;
|
295
|
-
if (!this.availableDevices.length) {
|
296
|
-
const availableDevices = await navigator.mediaDevices.enumerateDevices();
|
297
|
-
this.availableDevices = availableDevices.filter((mediaDeviceInfo) => mediaDeviceInfo.kind === 'videoinput' && !!mediaDeviceInfo.deviceId);
|
298
|
-
}
|
299
|
-
await new Promise((resolve) => this.htmlVideoElement.addEventListener('canplay', resolve, {
|
300
|
-
once: true,
|
301
|
-
}));
|
302
|
-
}
|
303
|
-
close() {
|
304
|
-
if (this.mediaStream) {
|
305
|
-
this.mediaStream.getTracks().forEach((track) => {
|
306
|
-
track.stop();
|
307
|
-
});
|
308
|
-
this.mediaStream = undefined;
|
309
|
-
}
|
310
|
-
}
|
97
|
+
React.createElement("button", { type: "button", className: "button ob-button ob-button__cancel is-light cypress-cancel-scan-barcode-button", onClick: onClose }, "Cancel"))));
|
311
98
|
}
|
312
99
|
//# sourceMappingURL=FormElementBarcodeScanner.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"FormElementBarcodeScanner.js","sourceRoot":"","sources":["../../src/form-elements/FormElementBarcodeScanner.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,IAAI,MAAM,MAAM,CAAA;AAEvB,OAAO,SAAS,MAAM,kCAAkC,CAAA;AACxD,OAAO,qBAAqB,MAAM,8CAA8C,CAAA;AAChF,OAAO,YAAY,MAAM,wCAAwC,CAAA;AACjE,OAAO,eAAe,MAAM,0BAA0B,CAAA;AACtD,OAAO,YAAY,MAAM,qCAAqC,CAAA;AAE9D,OAAO,yBAAyB,MAAM,kDAAkD,CAAA;AACxF,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,qBAAqB,EAAE,EAC5B,yBAAyB,GAC1B,MAAM,gCAAgC,CAAA;AAGvC,MAAM,2BAA2B,GAAG,EAAE,CAAA;AACtC,MAAM,wBAAwB,GAAG,GAAG,CAAA;AACpC,MAAM,yBAAyB,GAAG,GAAG,CAAA;AACrC,MAAM,q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AC,KAAK,EAAE,EAAE;gBAC7C,KAAK,CAAC,IAAI,EAAE,CAAA;YACd,CAAC,CAAC,CAAA;YACF,IAAI,CAAC,WAAW,GAAG,SAAS,CAAA;SAC7B;IACH,CAAC;CACF","sourcesContent":["import * as React from 'react'\nimport clsx from 'clsx'\nimport jsQR from 'jsqr'\n\nimport OnLoading from '../components/renderer/OnLoading'\nimport CopyToClipboardButton from '../components/renderer/CopyToClipboardButton'\nimport quaggaReader from '../services/barcode-readers/quagger.js'\nimport useBooleanState from '../hooks/useBooleanState'\nimport LookupButton from '../components/renderer/LookupButton'\nimport { FormTypes } from '@oneblink/types'\nimport FormElementLabelContainer from '../components/renderer/FormElementLabelContainer'\nimport { Sentry } from '@oneblink/apps'\nimport useLookupNotification, {\n LookupNotificationContext,\n} from '../hooks/useLookupNotification'\nimport { FormElementValueChangeHandler, IsDirtyProps } from '../types/form'\n\nconst MS_BETWEEN_IMAGE_PROCESSING = 10\nconst fadedSquareWidthInPixels = 200\nconst fadedSquareHeightInPixels = 150\nconst redLineHeightInPixels = 1\n\ntype Props = {\n id: string\n element: FormTypes.BarcodeScannerElement\n value: unknown | undefined\n onChange: FormElementValueChangeHandler<string>\n displayValidationMessage: boolean\n validationMessage: string | undefined\n} & IsDirtyProps\n\nfunction FormElementBarcodeScanner({\n id,\n element,\n value,\n onChange,\n validationMessage,\n displayValidationMessage,\n isDirty,\n setIsDirty,\n}: Props) {\n const [isCameraOpen, startBarcodeScanner, stopBarcodeScanner] =\n useBooleanState(false)\n const [error, setError] = React.useState<Error | null>(null)\n const { onLookup } = useLookupNotification(value)\n const handleScan = React.useCallback(\n (newValue: string | undefined) => {\n setIsDirty()\n onChange(element, {\n value: newValue,\n })\n stopBarcodeScanner()\n onLookup()\n },\n [element, onChange, onLookup, setIsDirty, stopBarcodeScanner],\n )\n\n const openBarcodeScanner = React.useCallback(() => {\n if (window.cordova) {\n setError(null)\n // @ts-expect-error ???\n window.cordova.plugins.barcodeScanner.scan(\n // @ts-expect-error ???\n (result) => {\n if (!result.cancelled) {\n handleScan(result.text)\n }\n },\n // @ts-expect-error ???\n (error) => {\n setError(\n new Error(\n `An error has occurred: \"${error}\". Please click \"Cancel\" below to try again. If the problem persists, please contact support.`,\n ),\n )\n },\n {\n showFlipCameraButton: true,\n showTorchButton: true,\n },\n )\n } else {\n startBarcodeScanner()\n }\n }, [handleScan, startBarcodeScanner])\n\n const { isLookingUp } = React.useContext(LookupNotificationContext)\n const isDisplayingValidationMessage =\n (isDirty || displayValidationMessage) && !!validationMessage && !isLookingUp\n\n const text = typeof value === 'string' ? value : ''\n return (\n <div className=\"cypress-barcode-scanner-element\">\n <FormElementLabelContainer\n className=\"ob-barcode-scanner\"\n element={element}\n id={id}\n required={element.required}\n >\n {error && (\n <figure className=\"ob-figure\">\n <div className=\"figure-content has-text-centered\">\n <div>\n <h4 className=\"title is-4\">Whoops...</h4>\n <p>{error.message}</p>\n </div>\n </div>\n </figure>\n )}\n\n {isCameraOpen ? (\n <BarcodeScanner\n element={element}\n onScan={handleScan}\n onClose={stopBarcodeScanner}\n />\n ) : (\n <div>\n <div className=\"field has-addons\">\n <div className=\"control is-expanded has-icons-right\">\n <input\n type=\"text\"\n placeholder={element.placeholderValue}\n id={id}\n name={element.name}\n className=\"input ob-input cypress-barcode-scanner-control has-margin-bottom-8\"\n value={text}\n onChange={(e) =>\n onChange(element, {\n value: e.target.value || undefined,\n })\n }\n required={element.required}\n disabled={element.readOnly}\n onBlur={setIsDirty}\n />\n <span className=\"ob-input-icon icon is-small is-right\">\n <i className=\"material-icons is-size-5\">document_scanner</i>\n </span>\n </div>\n {!!element.readOnly && !!value && (\n <div className=\"control\">\n <CopyToClipboardButton\n className=\"button is-input-addon copy-button cypress-copy-to-clipboard-button\"\n text={text}\n />\n </div>\n )}\n <LookupButton\n isInputButton\n value={value}\n validationMessage={validationMessage}\n lookupButtonConfig={element.lookupButton}\n />\n </div>\n\n <button\n type=\"button\"\n className=\"button ob-button ob-button__open is-primary cypress-start-scan-barcode-button\"\n disabled={element.readOnly}\n onClick={openBarcodeScanner}\n >\n Scan Barcode\n </button>\n </div>\n )}\n\n {isDisplayingValidationMessage && (\n <div role=\"alert\" className=\"has-margin-top-8\">\n <div className=\"has-text-danger ob-error__text cypress-validation-message\">\n {validationMessage}\n </div>\n </div>\n )}\n </FormElementLabelContainer>\n </div>\n )\n}\n\nexport default React.memo(FormElementBarcodeScanner)\n\ntype BarcodeScannerProps = {\n element: FormTypes.BarcodeScannerElement\n onScan: (barcode: string | undefined) => void\n onClose: () => void\n}\n\nfunction BarcodeScanner({ element, onScan, onClose }: BarcodeScannerProps) {\n const videoElementRef = React.useRef<HTMLVideoElement>(null)\n const figureElementRef = React.useRef<HTMLDivElement>(null)\n\n const [{ isLoading = false, selectedDeviceId, error }, setState] =\n React.useState<{\n isLoading: boolean\n selectedDeviceId: string | undefined\n error: Error | undefined\n }>({\n isLoading: true,\n selectedDeviceId: undefined,\n error: undefined,\n })\n const [camera, setCamera] = React.useState<HTML5Camera | null>(null)\n\n const setError = React.useCallback((error: Error) => {\n setState({\n error,\n isLoading: false,\n selectedDeviceId: undefined,\n })\n }, [])\n\n // Create timeout using $timeout outside of the scan function so\n // so that we can cancel it when navigating away from screen\n const scanImageForBarcode = React.useCallback(\n (\n videoElement: HTMLVideoElement,\n waitInMS: number,\n options: {\n sourceX: number\n sourceY: number\n sourceWidth: number\n sourceHeight: number\n },\n checkStop: () => boolean,\n ) => {\n const restrictedBarcodeTypes = element.restrictedBarcodeTypes || []\n // Using $timeout here instead of $interval as we dont know\n // exactly how long each processing of the image will take.\n setTimeout(async () => {\n if (checkStop()) return\n const canvasElement = document.createElement('canvas')\n\n canvasElement.width = options.sourceWidth\n canvasElement.height = options.sourceHeight\n\n const canvasContext = canvasElement.getContext('2d')\n if (canvasContext) {\n canvasContext.drawImage(\n videoElement,\n options.sourceX,\n options.sourceY,\n canvasElement.width,\n canvasElement.height,\n 0,\n 0,\n canvasElement.width,\n canvasElement.height,\n )\n\n if (\n !element.restrictBarcodeTypes ||\n (element.restrictedBarcodeTypes || []).indexOf('qr_reader') > -1\n ) {\n const imageData = canvasContext.getImageData(\n 0,\n 0,\n canvasElement.width,\n canvasElement.height,\n )\n\n const code = jsQR(\n imageData.data,\n imageData.width,\n imageData.height,\n {\n inversionAttempts: 'dontInvert',\n },\n )\n\n if (code) {\n return onScan(code.data)\n }\n }\n }\n\n if (\n !element.restrictBarcodeTypes ||\n !(\n restrictedBarcodeTypes.length === 1 &&\n restrictedBarcodeTypes[0] === 'qr_reader'\n )\n ) {\n const base64Image = canvasElement.toDataURL('image/png')\n const quaggaResult = await quaggaReader(\n base64Image,\n restrictedBarcodeTypes,\n )\n if (quaggaResult) {\n return onScan(quaggaResult)\n }\n }\n\n if (checkStop()) return\n\n scanImageForBarcode(\n videoElement,\n MS_BETWEEN_IMAGE_PROCESSING,\n options,\n checkStop,\n )\n }, waitInMS)\n },\n [element.restrictBarcodeTypes, element.restrictedBarcodeTypes, onScan],\n )\n\n const switchCamera = React.useCallback(() => {\n if (!camera) {\n return\n }\n\n // We will just be rotating between the available camera.\n const nextDeviceIndex =\n camera.availableDevices.findIndex(\n (mediaDeviceInfo) => mediaDeviceInfo.deviceId === camera.activeDeviceId,\n ) + 1\n const nextDevice =\n camera.availableDevices[nextDeviceIndex] || camera.availableDevices[0]\n setState({\n error: undefined,\n isLoading: true,\n selectedDeviceId: nextDevice.deviceId,\n })\n }, [camera])\n\n React.useEffect(() => {\n if (!videoElementRef.current) {\n return\n }\n\n const newCamera = new HTML5Camera(videoElementRef.current)\n setCamera(newCamera)\n\n return () => {\n newCamera.close()\n }\n }, [])\n\n React.useEffect(() => {\n if (\n !camera ||\n error ||\n // If attempting to open the device that is currently open,\n // we will not attempt to open again.\n (selectedDeviceId && camera.activeDeviceId === selectedDeviceId)\n ) {\n return\n }\n\n let ignore = false\n\n ;(async () => {\n try {\n const videoElement = videoElementRef.current\n const figureElement = figureElementRef.current\n if (!videoElement || !figureElement) {\n return\n }\n\n console.log('Opening camera with:', selectedDeviceId || 'UNKNOWN')\n await camera.open(selectedDeviceId)\n\n if (ignore) {\n return\n }\n\n setState({\n error: undefined,\n isLoading: false,\n selectedDeviceId,\n })\n\n // @ts-expect-error ???\n const fadedSquareElement: HTMLDivElement =\n figureElement.getElementsByClassName('ob-barcode-scanner__square')[0]\n // @ts-expect-error ???\n const redLineElement: HTMLDivElement =\n figureElement.getElementsByClassName('ob-barcode-scanner__line')[0]\n console.log('videoElement Width pixels', videoElement.clientWidth)\n console.log('videoElement Height pixels', videoElement.clientHeight)\n console.log('videoElement Width', videoElement.videoWidth)\n console.log('videoElement Height', videoElement.videoHeight)\n\n // Faded Square needs its values set in pixels\n const fadedSquareLeftInPixels =\n (videoElement.clientWidth - fadedSquareWidthInPixels) / 2\n console.log('fadedSquareLeftInPixels', fadedSquareLeftInPixels)\n const fadedSquareTopInPixels =\n (videoElement.clientHeight - fadedSquareHeightInPixels) / 2\n console.log('fadedSquareTopInPixels', fadedSquareTopInPixels)\n\n fadedSquareElement.style.borderBottom = `${fadedSquareTopInPixels}px`\n fadedSquareElement.style.borderTop = `${fadedSquareTopInPixels}px`\n fadedSquareElement.style.borderLeft = `${fadedSquareLeftInPixels}px`\n fadedSquareElement.style.borderRight = `${fadedSquareLeftInPixels}px`\n fadedSquareElement.style.borderColor = 'rgba(0, 0, 0, 0.25)'\n fadedSquareElement.style.borderStyle = 'solid'\n\n redLineElement.style.height = `${redLineHeightInPixels}px`\n redLineElement.style.top = `${\n (videoElement.clientHeight - redLineHeightInPixels) / 2\n }px`\n redLineElement.style.left = `${fadedSquareLeftInPixels}px`\n redLineElement.style.right = `${fadedSquareLeftInPixels}px`\n\n // Need to calculate the actual width, which is not in pixels\n const ratio = videoElement.videoWidth / videoElement.clientWidth\n console.log('pixel to video Ratio', ratio)\n\n const left = ratio * fadedSquareLeftInPixels\n console.log('left in video measurement', left)\n const top = ratio * fadedSquareTopInPixels\n console.log('top in video measurement', top)\n\n const fadedSquareWidth = fadedSquareWidthInPixels * ratio\n console.log('red square in video measurement', fadedSquareWidth)\n\n // Wait a little before scanning the first image\n // to prevent image processing staring before\n // camera is ready.\n scanImageForBarcode(\n videoElement,\n 250,\n {\n sourceX: left,\n sourceY: top,\n sourceWidth: fadedSquareWidth,\n sourceHeight: fadedSquareWidth,\n },\n () => ignore,\n )\n } catch (error) {\n if (ignore) {\n return\n }\n console.warn('Error while attempting to open camera', error)\n Sentry.captureException(error)\n switch ((error as Error).name) {\n case 'NotSupportedError': {\n setError(\n new Error(\n 'Your browser does not support accessing your camera. Please click \"Cancel\" below and type in the barcode value manually.',\n ),\n )\n break\n }\n case 'NotAllowedError': {\n setError(\n new Error(\n 'Cannot scan for barcodes without granting the application access to the camera. Please click \"Cancel\" below to try again.',\n ),\n )\n break\n }\n default: {\n setError(\n new Error(\n 'An unknown error has occurred, please click \"Cancel\" below to try again. If the problem persists, please contact support.',\n ),\n )\n }\n }\n }\n })()\n\n return () => {\n ignore = true\n }\n }, [camera, error, scanImageForBarcode, selectedDeviceId, setError])\n\n return (\n <div>\n <figure className=\"ob-figure\" ref={figureElementRef}>\n <div className=\"figure-content has-text-centered\">\n {isLoading && <OnLoading small />}\n\n {!!error && (\n <div>\n <h4 className=\"title is-4\">Whoops...</h4>\n <p>{error.message}</p>\n </div>\n )}\n\n <div\n className={clsx('is-relative', {\n 'is-hidden': isLoading || error,\n })}\n >\n <div className=\"ob-barcode-scanner__square\"></div>\n <div className=\"ob-barcode-scanner__line\"></div>\n <video\n ref={videoElementRef}\n autoPlay\n playsInline\n className=\"ob-barcode-scanner__video\"\n />\n </div>\n </div>\n </figure>\n\n <div className=\"buttons ob-buttons\">\n <button\n type=\"button\"\n className=\"button ob-button ob-button__cancel is-light cypress-cancel-scan-barcode-button\"\n onClick={onClose}\n >\n Cancel\n </button>\n {(camera?.availableDevices.length || 1) > 1 && (\n <button\n type=\"button\"\n className=\"button ob-button ob-button__switch-camera is-primary cypress-switch-camera-button\"\n onClick={switchCamera}\n >\n Switch Camera\n </button>\n )}\n </div>\n </div>\n )\n}\n\nclass HTML5Camera {\n availableDevices: MediaDeviceInfo[]\n htmlVideoElement: HTMLVideoElement\n mediaStream: MediaStream | undefined\n\n constructor(htmlVideoElement: HTMLVideoElement) {\n this.htmlVideoElement = htmlVideoElement\n this.availableDevices = []\n this.mediaStream = undefined\n }\n\n get activeDeviceId(): string | undefined {\n if (this.mediaStream) {\n const [activeMediaStreamTrack] = this.mediaStream.getTracks()\n return activeMediaStreamTrack?.getSettings()?.deviceId\n }\n }\n\n async open(deviceId?: string) {\n if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {\n const error = new Error()\n error.name = 'NotSupportedError'\n throw error\n }\n\n this.close()\n\n const constraints = {\n video: {\n facingMode: deviceId ? undefined : 'environment',\n deviceId: deviceId ? { exact: deviceId } : undefined,\n },\n }\n const mediaStream = await navigator.mediaDevices.getUserMedia(constraints)\n this.mediaStream = mediaStream\n this.htmlVideoElement.srcObject = mediaStream\n\n if (!this.availableDevices.length) {\n const availableDevices = await navigator.mediaDevices.enumerateDevices()\n this.availableDevices = availableDevices.filter(\n (mediaDeviceInfo) =>\n mediaDeviceInfo.kind === 'videoinput' && !!mediaDeviceInfo.deviceId,\n )\n }\n\n await new Promise((resolve) =>\n this.htmlVideoElement.addEventListener('canplay', resolve, {\n once: true,\n }),\n )\n }\n\n close() {\n if (this.mediaStream) {\n this.mediaStream.getTracks().forEach((track) => {\n track.stop()\n })\n this.mediaStream = undefined\n }\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"FormElementBarcodeScanner.js","sourceRoot":"","sources":["../../src/form-elements/FormElementBarcodeScanner.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AACjD,OAAO,qBAAqB,MAAM,8CAA8C,CAAA;AAChF,OAAO,eAAe,MAAM,0BAA0B,CAAA;AACtD,OAAO,YAA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ClipboardButton from '../components/renderer/CopyToClipboardButton'\nimport useBooleanState from '../hooks/useBooleanState'\nimport LookupButton from '../components/renderer/LookupButton'\nimport { FormTypes } from '@oneblink/types'\nimport FormElementLabelContainer from '../components/renderer/FormElementLabelContainer'\nimport useLookupNotification, {\n LookupNotificationContext,\n} from '../hooks/useLookupNotification'\nimport { FormElementValueChangeHandler, IsDirtyProps } from '../types/form'\n\ntype Props = {\n id: string\n element: FormTypes.BarcodeScannerElement\n value: unknown | undefined\n onChange: FormElementValueChangeHandler<string>\n displayValidationMessage: boolean\n validationMessage: string | undefined\n} & IsDirtyProps\n\nfunction FormElementBarcodeScanner({\n id,\n element,\n value,\n onChange,\n validationMessage,\n displayValidationMessage,\n isDirty,\n setIsDirty,\n}: Props) {\n const [isCameraOpen, startBarcodeScanner, stopBarcodeScanner] =\n useBooleanState(false)\n const [error, setError] = React.useState<Error | null>(null)\n const { onLookup } = useLookupNotification(value)\n const handleScan = React.useCallback(\n (newValue: string | undefined) => {\n setIsDirty()\n onChange(element, {\n value: newValue,\n })\n stopBarcodeScanner()\n onLookup()\n },\n [element, onChange, onLookup, setIsDirty, stopBarcodeScanner],\n )\n\n const openBarcodeScanner = React.useCallback(() => {\n if (window.cordova) {\n setError(null)\n // @ts-expect-error ???\n window.cordova.plugins.barcodeScanner.scan(\n // @ts-expect-error ???\n (result) => {\n if (!result.cancelled) {\n handleScan(result.text)\n }\n },\n // @ts-expect-error ???\n (error) => {\n setError(\n new Error(\n `An error has occurred: \"${error}\". Please click \"Cancel\" below to try again. If the problem persists, please contact support.`,\n ),\n )\n },\n {\n showFlipCameraButton: true,\n showTorchButton: true,\n },\n )\n } else {\n startBarcodeScanner()\n }\n }, [handleScan, startBarcodeScanner])\n\n const { isLookingUp } = React.useContext(LookupNotificationContext)\n const isDisplayingValidationMessage =\n (isDirty || displayValidationMessage) && !!validationMessage && !isLookingUp\n\n const text = typeof value === 'string' ? value : ''\n return (\n <div className=\"cypress-barcode-scanner-element\">\n <FormElementLabelContainer\n className=\"ob-barcode-scanner\"\n element={element}\n id={id}\n required={element.required}\n >\n {error && (\n <figure className=\"ob-figure\">\n <div className=\"figure-content has-text-centered\">\n <div>\n <h4 className=\"title is-4\">Whoops...</h4>\n <p>{error.message}</p>\n </div>\n </div>\n </figure>\n )}\n\n {isCameraOpen ? (\n <BarcodeScanner\n id={`${id}==BARCODE_SCANNER`}\n onScan={handleScan}\n onClose={stopBarcodeScanner}\n />\n ) : (\n <div>\n <div className=\"field has-addons\">\n <div className=\"control is-expanded has-icons-right\">\n <input\n type=\"text\"\n placeholder={element.placeholderValue}\n id={id}\n name={element.name}\n className=\"input ob-input cypress-barcode-scanner-control has-margin-bottom-8\"\n value={text}\n onChange={(e) =>\n onChange(element, {\n value: e.target.value || undefined,\n })\n }\n required={element.required}\n disabled={element.readOnly}\n onBlur={setIsDirty}\n />\n <span className=\"ob-input-icon icon is-small is-right\">\n <i className=\"material-icons is-size-5\">document_scanner</i>\n </span>\n </div>\n {!!element.readOnly && !!value && (\n <div className=\"control\">\n <CopyToClipboardButton\n className=\"button is-input-addon copy-button cypress-copy-to-clipboard-button\"\n text={text}\n />\n </div>\n )}\n <LookupButton\n isInputButton\n value={value}\n validationMessage={validationMessage}\n lookupButtonConfig={element.lookupButton}\n />\n </div>\n\n <button\n type=\"button\"\n className=\"button ob-button ob-button__open is-primary cypress-start-scan-barcode-button\"\n disabled={element.readOnly}\n onClick={openBarcodeScanner}\n >\n Scan Barcode\n </button>\n </div>\n )}\n\n {isDisplayingValidationMessage && (\n <div role=\"alert\" className=\"has-margin-top-8\">\n <div className=\"has-text-danger ob-error__text cypress-validation-message\">\n {validationMessage}\n </div>\n </div>\n )}\n </FormElementLabelContainer>\n </div>\n )\n}\n\nexport default React.memo(FormElementBarcodeScanner)\n\ntype BarcodeScannerProps = {\n id: string\n onScan: (barcode: string | undefined) => void\n onClose: () => void\n}\n\nfunction BarcodeScanner({ id, onScan, onClose }: BarcodeScannerProps) {\n const [html5QrcodeScanner, setHtml5QrcodeScanner] =\n React.useState<Html5QrcodeScanner | null>(null)\n\n React.useEffect(() => {\n const verbose = true\n const newHtml5QrcodeScanner = new Html5QrcodeScanner(\n id,\n {\n fps: 10,\n qrbox: {\n width: 250,\n height: 250,\n },\n },\n verbose,\n )\n setHtml5QrcodeScanner(newHtml5QrcodeScanner)\n return () => {\n newHtml5QrcodeScanner.clear()\n }\n }, [id])\n\n React.useEffect(() => {\n html5QrcodeScanner?.render(\n function onScanSuccess(decodedText, decodedResult) {\n console.log(`Code matched = ${decodedText}`, decodedResult)\n onScan(decodedText)\n },\n function onScanFailure() {\n // do nothing and keep scanning\n },\n )\n }, [html5QrcodeScanner, onScan])\n\n return (\n <div>\n <figure className=\"ob-figure\">\n <div id={id} />\n </figure>\n <div className=\"buttons ob-buttons\">\n <button\n type=\"button\"\n className=\"button ob-button ob-button__cancel is-light cypress-cancel-scan-barcode-button\"\n onClick={onClose}\n >\n Cancel\n </button>\n </div>\n </div>\n )\n}\n"]}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@oneblink/apps-react",
|
3
3
|
"description": "Helper functions for OneBlink apps in ReactJS.",
|
4
|
-
"version": "5.3.1-beta.
|
4
|
+
"version": "5.3.1-beta.3",
|
5
5
|
"author": "OneBlink <developers@oneblink.io> (https://oneblink.io)",
|
6
6
|
"bugs": {
|
7
7
|
"url": "https://github.com/oneblink/apps-react/issues"
|
@@ -24,12 +24,11 @@
|
|
24
24
|
"escape-string-regexp": "^5.0.0",
|
25
25
|
"file-saver": "^2.0.5",
|
26
26
|
"flatpickr": "^4.6.13",
|
27
|
-
"
|
27
|
+
"html5-qrcode": "^2.3.8",
|
28
28
|
"lodash.clonedeep": "^4.5.0",
|
29
29
|
"lodash.debounce": "^4.0.8",
|
30
30
|
"lodash.throttle": "^4.1.1",
|
31
31
|
"morph-expressions": "^1.1.1",
|
32
|
-
"quagga": "^0.12.1",
|
33
32
|
"query-string": "^8.1.0",
|
34
33
|
"react-google-recaptcha": "^3.1.0",
|
35
34
|
"react-input-mask": "^2.0.4",
|