esoftplay-android-print
Advanced tools
Comparing version 0.0.215 to 0.0.216
216
index.ts
@@ -1,4 +0,212 @@ | ||
import { Platform } from "react-native"; | ||
import ANDROID from './index.android'; | ||
import IOS from './index.ios'; | ||
export default Platform.OS == 'ios' ? IOS : ANDROID | ||
//@ts-check | ||
import { NativeEventEmitter, NativeModules } from 'react-native'; | ||
import { PERMISSIONS, RESULTS, check, request } from 'react-native-permissions'; | ||
const { PrinterAndroidLibrary } = NativeModules; | ||
(() => { | ||
check(PERMISSIONS.ANDROID.BLUETOOTH_SCAN).then((res) => { | ||
if (res != RESULTS.GRANTED) { | ||
request(PERMISSIONS.ANDROID.BLUETOOTH_SCAN) | ||
} | ||
}) | ||
check(PERMISSIONS.ANDROID.BLUETOOTH_CONNECT).then((res) => { | ||
if (res != RESULTS.GRANTED) { | ||
request(PERMISSIONS.ANDROID.BLUETOOTH_CONNECT) | ||
} | ||
}) | ||
})() | ||
export type BarcodeType = "UPC-A" | "UPC-E" | "EAN13" | "EAN8" | "CODE39" | "ITF" | "CODABAR" | "CODE93" | "CODE128" | ||
export type Justification = "LEFT" | "CENTER" | "RIGHT" | ||
export type level = "L" | "M" | "Q" | "H" | ||
export type attr = "DEFAULT" | "BOLD" | "UNDERLINE" | "MINI" | "WHITE" | "DOUBLEWIDTH" | "DOUBLEHEIGHT" | ||
export type qrsize = 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | ||
let CHARSIZE = 32; | ||
let listener: any | ||
let dataPrint: any[] = [] | ||
let isStarPrinter = false | ||
const PRINTER = { | ||
connectWifi: (ipAddress: string, port: string, cb: (status: 1 | 0) => void) => PrinterAndroidLibrary.connectWifi(ipAddress, port, cb), | ||
connectBluetooth: (macAdress: string, cb: (status: 1 | 0) => void) => PrinterAndroidLibrary.connectBluetooth(macAdress, cb), | ||
closeConnection: () => PrinterAndroidLibrary.closeConnection(), | ||
listBluetooth: () => PrinterAndroidLibrary.listBluetooth(), | ||
getPairedBluetooth: (cb: (list: any) => void) => PrinterAndroidLibrary.getPairedBluetooth(cb), | ||
releaseListBluetooth: () => PrinterAndroidLibrary.releaseListBluetooth(), | ||
getPrintStatus: (cb: (status: string) => void) => PrinterAndroidLibrary.getPrintStatus(cb), | ||
isOpened: (cb: (status: number) => void) => PrinterAndroidLibrary.isOpened(cb), | ||
isStarPrinter: (cb: (status: number) => void) => PrinterAndroidLibrary.isStarPrinter(cb), | ||
openCashdrawer: () => PrinterAndroidLibrary.openCashdrawer(), | ||
startScanWifiPrinter: () => PrinterAndroidLibrary.startScanWifiPrinter(), | ||
testPrint: () => PrinterAndroidLibrary.testPrint(), | ||
pageStart: () => PrinterAndroidLibrary.pageStart(), | ||
pageEnd: (cb: () => void) => PrinterAndroidLibrary.pageEnd(cb), | ||
printBarcode: (barcodeType: BarcodeType, data: string, height: number, barSize: number, align: string) => PrinterAndroidLibrary.printBarcode(barcodeType, data, height, barSize, align), | ||
printQRCode: (data: string, level: level, size: qrsize, align: string) => PrinterAndroidLibrary.printQRCode(data, level, size, align), | ||
printTextBase: (text: string, align: string, IAttribute: attr, size: number) => PrinterAndroidLibrary.printTextBase(text, align, IAttribute, size), | ||
printImage: (base64: string, width: number, align: string) => PrinterAndroidLibrary.printImage(base64, width, align), | ||
setPaperSize: (size: number) => PrinterAndroidLibrary.setPaperSize(String(size)), | ||
cutPaper: () => PrinterAndroidLibrary.cutPaper(), | ||
feed: () => PrinterAndroidLibrary.feed() | ||
} | ||
function appendEnter(text: string): string { | ||
return isStarPrinter ? (text + "\n") : text | ||
} | ||
export default { | ||
permission() { | ||
check(PERMISSIONS.ANDROID.BLUETOOTH_SCAN).then((res) => { | ||
if (res != RESULTS.GRANTED) { | ||
request(PERMISSIONS.ANDROID.BLUETOOTH_SCAN) | ||
} | ||
}) | ||
check(PERMISSIONS.ANDROID.BLUETOOTH_CONNECT).then((res) => { | ||
if (res != RESULTS.GRANTED) { | ||
request(PERMISSIONS.ANDROID.BLUETOOTH_CONNECT) | ||
} | ||
}) | ||
}, | ||
initWifiPrinter(ipAddress: string, port: null | string = "9100", cb: (status: 0 | 1) => void) { | ||
PRINTER.connectWifi(ipAddress, port || "9100", cb); | ||
}, | ||
initPrinter(mac: string, cb: (status: 0 | 1) => void) { | ||
PRINTER.connectBluetooth(mac, (_status) => { | ||
PRINTER.isStarPrinter((status) => { | ||
isStarPrinter = (status == 1) | ||
cb(_status) | ||
}) | ||
}) | ||
}, | ||
closeConnection() { | ||
PRINTER.closeConnection() | ||
}, | ||
isOpened(cb: (status: number) => void) { | ||
PRINTER.isOpened(cb); | ||
}, | ||
isStarPrinter(cb: (status: number) => void) { | ||
PRINTER.isStarPrinter(cb); | ||
}, | ||
setPaperSize(size: "57mm" | "80mm") { | ||
let innerSize = 32 | ||
switch (size) { | ||
case '57mm': | ||
innerSize = 32; | ||
break; | ||
case '80mm': | ||
innerSize = 48; | ||
break; | ||
} | ||
PRINTER.setPaperSize(innerSize) | ||
CHARSIZE = innerSize; | ||
}, | ||
startScanWifiPrinter(cb: (printer: any[]) => void) { | ||
const eventEmitter = new NativeEventEmitter(PrinterAndroidLibrary); | ||
if (listener) { | ||
listener?.remove?.() | ||
} | ||
listener = eventEmitter.addListener('onWifiPrinterScanned', ({ data }) => { | ||
cb(data) | ||
}) | ||
PRINTER.startScanWifiPrinter() | ||
}, | ||
startScanPrinters(cb: (printer: any[]) => void) { | ||
const eventEmitter = new NativeEventEmitter(PrinterAndroidLibrary); | ||
if (listener) { | ||
listener?.remove?.() | ||
} | ||
listener = eventEmitter.addListener('onBluetoothPrinterScanned', ({ data }) => { | ||
cb(data) | ||
}); | ||
PRINTER.listBluetooth() | ||
}, | ||
releasePrinterList() { | ||
PRINTER?.releaseListBluetooth?.() | ||
if (listener) { | ||
listener?.remove?.() | ||
} | ||
}, | ||
printImage(base64: string, width: number, align: Justification) { | ||
dataPrint.push(`[IMAGE]:${base64}`) | ||
PRINTER.printImage(base64, width, align); | ||
}, | ||
printBarcode(barcodeType: BarcodeType, data: string, height: number, barSize: number, align: string) { | ||
dataPrint.push(`[BARCODE]:${data}`) | ||
PRINTER.printBarcode(barcodeType, data, height, barSize, align) | ||
}, | ||
getPairedBluetooth(cb: (list: any) => void) { | ||
PRINTER.getPairedBluetooth(cb); | ||
}, | ||
printQRCode(data: string, level: level, size: qrsize, align: string) { | ||
dataPrint.push(`[QR]:${data}`) | ||
PRINTER.printQRCode(data, level, size, align) | ||
}, | ||
addNewLine(count: number = 1) { | ||
let enters = new Array(count).fill('\n').join('') | ||
dataPrint.push(enters) | ||
PRINTER.printTextBase(enters, "LEFT", "DEFAULT", 0) | ||
}, | ||
openDrawer() { | ||
PRINTER.openCashdrawer() | ||
}, | ||
pageStart() { | ||
PRINTER.pageStart() | ||
dataPrint = [] | ||
}, | ||
pageEnd(cb: () => void) { | ||
PRINTER.pageEnd(cb) | ||
}, | ||
testPrint() { | ||
PRINTER.testPrint() | ||
}, | ||
printTextln(text: string, align: Justification) { | ||
dataPrint.push(text) | ||
PRINTER.printTextBase(appendEnter(text), align, "DEFAULT", 0) | ||
}, | ||
printTextJustify(text1: string, text2: string) { | ||
let adder = CHARSIZE - (text1.length + text2.length) | ||
let newText = text1 + new Array(adder).fill(" ").join("") + text2 | ||
dataPrint.push(newText) | ||
PRINTER.printTextBase(appendEnter(newText), "LEFT", "DEFAULT", 0) | ||
}, | ||
printTextJustifyBold(text1: string, text2: string) { | ||
let adder = CHARSIZE - (text1.length + text2.length) | ||
let newText = text1 + new Array(adder).fill(" ").join("") + text2 | ||
dataPrint.push(newText) | ||
PRINTER.printTextBase(appendEnter(newText), "LEFT", "BOLD", 0) | ||
}, | ||
printTextlnNormal(text: string, align: Justification) { | ||
dataPrint.push(text) | ||
PRINTER.printTextBase(appendEnter(text), align, "DEFAULT", 0) | ||
}, | ||
printTextlnBold(text: string, align: Justification) { | ||
dataPrint.push(text) | ||
PRINTER.printTextBase(appendEnter(text), align, "BOLD", 0) | ||
}, | ||
cutPaper() { | ||
dataPrint.push("[CUT]") | ||
PRINTER.cutPaper() | ||
}, | ||
feed() { | ||
dataPrint.push("\n\n\n") | ||
PRINTER.printTextBase("\n\n\n", "CENTER", "DEFAULT", 0) | ||
}, | ||
printDashedLine: () => { | ||
let chars = new Array(CHARSIZE).fill('-').join("") | ||
dataPrint.push(chars) | ||
PRINTER.printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
}, | ||
printLine: () => { | ||
let chars = new Array(CHARSIZE).fill('_').join("") | ||
dataPrint.push(chars) | ||
PRINTER.printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
}, | ||
printDoubleDashedLine: () => { | ||
let chars = new Array(CHARSIZE).fill('=').join("") | ||
dataPrint.push(chars) | ||
PRINTER.printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
}, | ||
getDataPrint: () => { | ||
return dataPrint.join("\n") | ||
} | ||
} |
{ | ||
"name": "esoftplay-android-print", | ||
"version": "0.0.215", | ||
"version": "0.0.216", | ||
"description": "Printing ", | ||
@@ -5,0 +5,0 @@ "main": "./index.ts", |
26416836
48
4177