打包支持
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { BarcodeDetector as BarcodeDetectorPonyfill, prepareZXingModule } from "barcode-detector/dist/cjs/index.js";
|
||||
import { createUUID } from "../../utils/uuid";
|
||||
import { getConfig } from "../config";
|
||||
import scanBeepAudio from "../../../res/scan_beep.ogg";
|
||||
@@ -8,9 +9,9 @@ const scanWeb = {
|
||||
}
|
||||
|
||||
const DEFAULT_SCAN_BEEP_AUDIO = scanBeepAudio;
|
||||
const BARCODE_DETECTOR_POLYFILL_URL = "https://fastly.jsdelivr.net/npm/barcode-detector@3/dist/iife/index.min.js";
|
||||
const ZXING_READER_WASM_URL = "./lib/reader.wasm";
|
||||
|
||||
let barcodeDetectorPolyfillPromise = null;
|
||||
let barcodeDetectorPreparePromise = null;
|
||||
|
||||
function removeEl(id) {
|
||||
try {
|
||||
@@ -70,48 +71,32 @@ function getBarcodeDetectorClass() {
|
||||
if (typeof BarcodeDetector !== 'undefined') {
|
||||
return BarcodeDetector;
|
||||
}
|
||||
if (typeof window !== 'undefined'
|
||||
&& window.BarcodeDetectionAPI
|
||||
&& window.BarcodeDetectionAPI.BarcodeDetector) {
|
||||
return window.BarcodeDetectionAPI.BarcodeDetector;
|
||||
}
|
||||
return null;
|
||||
return BarcodeDetectorPonyfill;
|
||||
}
|
||||
|
||||
function loadBarcodeDetectorPolyfill() {
|
||||
if (getBarcodeDetectorClass()) {
|
||||
return Promise.resolve(getBarcodeDetectorClass());
|
||||
function prepareBarcodeDetector() {
|
||||
const BarcodeDetectorClass = getBarcodeDetectorClass();
|
||||
if (typeof BarcodeDetector !== 'undefined' || !prepareZXingModule) {
|
||||
return Promise.resolve(BarcodeDetectorClass);
|
||||
}
|
||||
if (barcodeDetectorPolyfillPromise) {
|
||||
return barcodeDetectorPolyfillPromise;
|
||||
}
|
||||
barcodeDetectorPolyfillPromise = new Promise((resolve, reject) => {
|
||||
if (typeof document === 'undefined') {
|
||||
reject(new Error("BarcodeDetector is not supported"));
|
||||
return;
|
||||
}
|
||||
const scriptUrl = getConfig("webBarcodeDetectorPolyfillUrl") || BARCODE_DETECTOR_POLYFILL_URL;
|
||||
const script = document.createElement("script");
|
||||
script.src = scriptUrl;
|
||||
script.onload = () => {
|
||||
const BarcodeDetectorClass = getBarcodeDetectorClass();
|
||||
if (BarcodeDetectorClass) {
|
||||
resolve(BarcodeDetectorClass);
|
||||
} else {
|
||||
reject(new Error("BarcodeDetector polyfill is not ready"));
|
||||
if (!barcodeDetectorPreparePromise) {
|
||||
prepareZXingModule({
|
||||
overrides: {
|
||||
locateFile: path => {
|
||||
if (path && path.indexOf(".wasm") !== -1) {
|
||||
return ZXING_READER_WASM_URL;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
};
|
||||
script.onerror = reject;
|
||||
document.head.appendChild(script);
|
||||
}).catch(err => {
|
||||
barcodeDetectorPolyfillPromise = null;
|
||||
throw err;
|
||||
});
|
||||
return barcodeDetectorPolyfillPromise;
|
||||
});
|
||||
barcodeDetectorPreparePromise = Promise.resolve(BarcodeDetectorClass);
|
||||
}
|
||||
return barcodeDetectorPreparePromise;
|
||||
}
|
||||
|
||||
function createBarcodeDetector(scanType) {
|
||||
return loadBarcodeDetectorPolyfill().then(BarcodeDetectorClass => {
|
||||
return prepareBarcodeDetector().then(BarcodeDetectorClass => {
|
||||
if (!BarcodeDetectorClass) {
|
||||
throw new Error("BarcodeDetector is not supported");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user