|
1 | 1 | import { registerPlugin } from '@capacitor/core'; |
2 | 2 |
|
3 | | -import type { CapacitorBarcodeScannerPlugin } from './definitions'; // Importing the interface for type checking. |
| 3 | +import { |
| 4 | + CapacitorBarcodeScannerCameraDirection, |
| 5 | + CapacitorBarcodeScannerScanOrientation, |
| 6 | + type CapacitorBarcodeScannerOptions, |
| 7 | + type CapacitorBarcodeScannerPlugin, |
| 8 | + type CapacitorBarcodeScannerScanResult, |
| 9 | +} from './definitions'; // Importing the interface for type checking. |
4 | 10 | import { applyCss, barcodeScannerCss } from './utils'; // Import utilities for applying CSS. |
5 | 11 |
|
6 | 12 | /** |
7 | 13 | * Registers the `OSBarcode` plugin with Capacitor. |
8 | 14 | * For web platforms, it applies necessary CSS for the barcode scanner and dynamically imports the web implementation. |
9 | 15 | * This allows for lazy loading of the web code only when needed, optimizing overall bundle size. |
10 | 16 | */ |
11 | | -const CapacitorBarcodeScanner = registerPlugin<CapacitorBarcodeScannerPlugin>('CapacitorBarcodeScanner', { |
| 17 | +const CapacitorBarcodeScannerImpl = registerPlugin<CapacitorBarcodeScannerPlugin>('CapacitorBarcodeScanner', { |
12 | 18 | web: () => { |
13 | 19 | applyCss(barcodeScannerCss); // Apply the CSS styles necessary for the web implementation of the barcode scanner. |
14 | 20 | return import('./web').then((m) => new m.CapacitorBarcodeScannerWeb()); // Dynamically import the web implementation and instantiate it. |
15 | 21 | }, |
16 | 22 | }); |
17 | 23 |
|
| 24 | +class CapacitorBarcodeScanner { |
| 25 | + public static async scanBarcode(options: CapacitorBarcodeScannerOptions): Promise<CapacitorBarcodeScannerScanResult> { |
| 26 | + options.scanInstructions = options.scanInstructions || ' '; // Ensure scanInstructions is at least a space. |
| 27 | + options.scanButton = options.scanButton || false; // Set scanButton to false if not provided. |
| 28 | + options.scanText = options.scanText || ' '; // Ensure scanText is at least a space. |
| 29 | + options.cameraDirection = options.cameraDirection || CapacitorBarcodeScannerCameraDirection.BACK; // Set cameraDirection to 'BACK' if not provided. |
| 30 | + options.scanOrientation = options.scanOrientation || CapacitorBarcodeScannerScanOrientation.ADAPTIVE; // Set scanOrientation to 'ADAPTIVE' if not provided. |
| 31 | + return CapacitorBarcodeScannerImpl.scanBarcode(options); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export { CapacitorBarcodeScanner }; // Export the `CapacitorBarcodeScanner` class. |
18 | 36 | export * from './definitions'; // Re-export all exports from the definitions file. |
19 | | -export { CapacitorBarcodeScanner }; // Export the OSBarcode plugin for use in Capacitor projects. |
|
0 commit comments