|
| 1 | +import { defineCustomElements } from "@ionic/pwa-elements/loader"; |
| 2 | +import { WebviewPrint } from "capacitor-webview-print"; |
| 3 | +import { Preferences } from "@capacitor/preferences"; |
| 4 | +import { Clipboard } from "@capacitor/clipboard"; |
| 5 | +import { Browser } from "@capacitor/browser"; |
| 6 | +import { Share } from "@capacitor/share"; |
| 7 | +import { Toast } from "@capacitor/toast"; |
| 8 | +import { Capacitor } from "@capacitor/core"; |
| 9 | +import { Html5Qrcode } from "html5-qrcode"; |
| 10 | +import { |
| 11 | + CapacitorBarcodeScanner, |
| 12 | + CapacitorBarcodeScannerTypeHint, |
| 13 | +} from "@capacitor/barcode-scanner"; |
| 14 | + |
| 15 | +export async function printCurrentPage(name) { |
| 16 | + return await WebviewPrint.print({ name: name }); |
| 17 | +} |
| 18 | + |
| 19 | +export async function selectStorage(key) { |
| 20 | + const { value } = await Preferences.get({ key: key }); |
| 21 | + return value; |
| 22 | +} |
| 23 | + |
| 24 | +export async function insertStorage(key, value) { |
| 25 | + return await Preferences.set({ key: key, value: value }); |
| 26 | +} |
| 27 | + |
| 28 | +export async function selectClipboard() { |
| 29 | + const { value } = await Clipboard.read(); |
| 30 | + return value; |
| 31 | +} |
| 32 | + |
| 33 | +export async function openBrowserPage(url) { |
| 34 | + try { |
| 35 | + return await Browser.open({ url: url, windowName: "_blank" }); |
| 36 | + } catch (e) { |
| 37 | + return window.open(url, "_blank").focus(); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +export async function shareText(text) { |
| 42 | + const { value } = await Share.canShare(); |
| 43 | + if (value) { |
| 44 | + return await Share.share({ text: text }); |
| 45 | + } else { |
| 46 | + return await navigator.clipboard.writeText(text); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export async function popupText(text) { |
| 51 | + return await Toast.show({ text: text }); |
| 52 | +} |
| 53 | + |
| 54 | +export async function selectBarcode() { |
| 55 | + if (!Capacitor.isNativePlatform()) { |
| 56 | + const devices = await Html5Qrcode.getCameras(); |
| 57 | + if (!(devices && devices.length)) { |
| 58 | + throw new Error("Camera not found!"); |
| 59 | + } |
| 60 | + } |
| 61 | + const { ScanResult } = await CapacitorBarcodeScanner.scanBarcode({ |
| 62 | + hint: CapacitorBarcodeScannerTypeHint.ALL, |
| 63 | + }); |
| 64 | + return ScanResult; |
| 65 | +} |
| 66 | + |
| 67 | +defineCustomElements(window); |
0 commit comments