|
| 1 | +import { defineCustomElements } from "@ionic/pwa-elements/loader"; |
| 2 | +import { Filesystem, Directory } from "@capacitor/filesystem"; |
| 3 | +import { WebviewPrint } from "capacitor-webview-print"; |
| 4 | +import { Preferences } from "@capacitor/preferences"; |
| 5 | +import { Clipboard } from "@capacitor/clipboard"; |
| 6 | +import { Browser } from "@capacitor/browser"; |
| 7 | +import { Share } from "@capacitor/share"; |
| 8 | +import { Toast } from "@capacitor/toast"; |
| 9 | +import { Capacitor } from "@capacitor/core"; |
| 10 | +import { Html5Qrcode } from "html5-qrcode"; |
| 11 | +import { saveAs } from "file-saver"; |
| 12 | +import { |
| 13 | + CapacitorBarcodeScanner, |
| 14 | + CapacitorBarcodeScannerTypeHint, |
| 15 | +} from "@capacitor/barcode-scanner"; |
| 16 | + |
| 17 | +export async function printCurrentPage(name) { |
| 18 | + return await WebviewPrint.print({ name: name }); |
| 19 | +} |
| 20 | + |
| 21 | +export async function selectStorage(key) { |
| 22 | + const { value } = await Preferences.get({ key: key }); |
| 23 | + return value; |
| 24 | +} |
| 25 | + |
| 26 | +export async function insertStorage(key, value) { |
| 27 | + return await Preferences.set({ key: key, value: value }); |
| 28 | +} |
| 29 | + |
| 30 | +export async function selectClipboard() { |
| 31 | + const { value } = await Clipboard.read(); |
| 32 | + return value; |
| 33 | +} |
| 34 | + |
| 35 | +export async function openBrowserPage(url) { |
| 36 | + try { |
| 37 | + return await Browser.open({ url: url, windowName: "_blank" }); |
| 38 | + } catch (e) { |
| 39 | + return window.open(url, "_blank").focus(); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export async function shareText(text) { |
| 44 | + const { value } = await Share.canShare(); |
| 45 | + if (value) { |
| 46 | + return await Share.share({ text: text }); |
| 47 | + } else { |
| 48 | + return await navigator.clipboard.writeText(text); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export async function popupText(text) { |
| 53 | + return await Toast.show({ text: text }); |
| 54 | +} |
| 55 | + |
| 56 | +export async function selectBarcode() { |
| 57 | + if (!Capacitor.isNativePlatform()) { |
| 58 | + const devices = await Html5Qrcode.getCameras(); |
| 59 | + if (!(devices && devices.length)) { |
| 60 | + throw new Error("Camera not found!"); |
| 61 | + } |
| 62 | + } |
| 63 | + const { ScanResult } = await CapacitorBarcodeScanner.scanBarcode({ |
| 64 | + hint: CapacitorBarcodeScannerTypeHint.ALL, |
| 65 | + }); |
| 66 | + return ScanResult; |
| 67 | +} |
| 68 | + |
| 69 | +export async function saveFile(name, mime, bs) { |
| 70 | + const u8a = Uint8Array.from(bs); |
| 71 | + if (Capacitor.isNativePlatform()) { |
| 72 | + const b64 = btoa(String.fromCharCode.apply(null, u8a)); |
| 73 | + const { uri } = await Filesystem.writeFile({ |
| 74 | + path: name, |
| 75 | + data: b64, |
| 76 | + directory: Directory.Documents, |
| 77 | + }); |
| 78 | + return uri; |
| 79 | + } else { |
| 80 | + const blob = new Blob([u8a, { type: mime }]); |
| 81 | + await saveAs(blob, name); |
| 82 | + return null; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export async function shareFiles(files) { |
| 87 | + if (Capacitor.isNativePlatform()) { |
| 88 | + const { value } = await Share.share({ files: files }); |
| 89 | + return value; |
| 90 | + } else { |
| 91 | + return null; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export function isNativePlatform() { |
| 96 | + return Capacitor.isNativePlatform(); |
| 97 | +} |
| 98 | + |
| 99 | +defineCustomElements(window); |
0 commit comments