esoftplay-android-print
Advanced tools
Comparing version 0.0.187 to 0.0.188
312
index.ts
//@ts-check | ||
import { NativeEventEmitter, NativeModules } from 'react-native'; | ||
// noPage | ||
import { NativeEventEmitter, NativeModules, Platform } from 'react-native'; | ||
import { PERMISSIONS, RESULTS, check, request } from 'react-native-permissions'; | ||
import { BLEPrinter, COMMANDS, NetPrinter } from 'react-native-thermal-receipt-printer-image-qr'; | ||
const { PrinterAndroidLibrary } = NativeModules; | ||
const isIOS = Platform.OS == 'ios'; | ||
let isIosBluetooth = true | ||
const IOSPRINTER = isIosBluetooth ? BLEPrinter : NetPrinter; | ||
const BOLD_ON = COMMANDS.TEXT_FORMAT.TXT_BOLD_ON; | ||
const BOLD_OFF = COMMANDS.TEXT_FORMAT.TXT_BOLD_OFF; | ||
const CENTER = COMMANDS.TEXT_FORMAT.TXT_ALIGN_CT; | ||
const OFF_CENTER = COMMANDS.TEXT_FORMAT.TXT_ALIGN_LT; | ||
function _printTextBase(text: string, jus: Justification, att: "DEFAULT" | "BOLD", size: number) { | ||
let _text = text | ||
switch (jus) { | ||
case "LEFT": | ||
_text = OFF_CENTER + _text + OFF_CENTER | ||
break; | ||
case "CENTER": | ||
_text = CENTER + _text + OFF_CENTER | ||
break; | ||
case "RIGHT": | ||
_text = COMMANDS.TEXT_FORMAT.TXT_ALIGN_RT + _text + OFF_CENTER | ||
default: | ||
_text = OFF_CENTER + _text + OFF_CENTER | ||
} | ||
switch (att) { | ||
case "DEFAULT": | ||
_text = BOLD_OFF + _text + BOLD_OFF | ||
break | ||
case "BOLD": | ||
_text = BOLD_ON + _text + BOLD_OFF | ||
break | ||
default: | ||
_text = BOLD_OFF + _text + BOLD_OFF | ||
} | ||
IOSPRINTER.printText(_text) | ||
} | ||
(() => { | ||
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) | ||
} | ||
}) | ||
if (isIOS) { | ||
check(PERMISSIONS.IOS.BLUETOOTH).then((res) => { | ||
if (res != RESULTS.GRANTED) { | ||
request(PERMISSIONS.IOS.BLUETOOTH) | ||
} | ||
}) | ||
} else { | ||
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) | ||
} | ||
}) | ||
} | ||
})() | ||
@@ -54,3 +101,6 @@ | ||
function appendEnter(text: string): string { | ||
return isStarPrinter ? (text + "\n") : text | ||
if (isIOS) | ||
return text + '\n' | ||
else | ||
return isStarPrinter ? (text + "\n") : text | ||
} | ||
@@ -60,32 +110,74 @@ | ||
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) | ||
} | ||
}) | ||
if (isIOS) { | ||
check(PERMISSIONS.IOS.BLUETOOTH).then((res) => { | ||
if (res != RESULTS.GRANTED) { | ||
request(PERMISSIONS.IOS.BLUETOOTH) | ||
} | ||
}) | ||
} else { | ||
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); | ||
if (isIOS) { | ||
NetPrinter.init().then(() => { | ||
isIosBluetooth = false; | ||
NetPrinter.connectPrinter(ipAddress, Number(port)).then(() => { | ||
cb(1) | ||
}).catch(() => { | ||
cb(0) | ||
}) | ||
}) | ||
return | ||
} else | ||
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) | ||
if (isIOS) { | ||
BLEPrinter.init().then(() => { | ||
isIosBluetooth = true; | ||
BLEPrinter.connectPrinter(mac).then(() => { | ||
cb(1) | ||
}).catch(() => { | ||
cb(0) | ||
}) | ||
}) | ||
}) | ||
} else { | ||
PRINTER.connectBluetooth(mac, (_status) => { | ||
PRINTER.isStarPrinter((status) => { | ||
isStarPrinter = (status == 1) | ||
cb(_status) | ||
}) | ||
}) | ||
} | ||
}, | ||
closeConnection() { | ||
PRINTER.closeConnection() | ||
if (isIOS) { | ||
try { IOSPRINTER.closeConn() } catch (error) { } | ||
} else { | ||
PRINTER.closeConnection() | ||
} | ||
}, | ||
isOpened(cb: (status: number) => void) { | ||
PRINTER.isOpened(cb); | ||
if (isIOS) { | ||
cb(1) // bypass for ios | ||
} else { | ||
PRINTER.isOpened(cb); | ||
} | ||
}, | ||
isStarPrinter(cb: (status: number) => void) { | ||
PRINTER.isStarPrinter(cb); | ||
if (isIOS) { | ||
cb(0) // bypass for ios | ||
} else { | ||
PRINTER.isStarPrinter(cb); | ||
} | ||
}, | ||
@@ -106,25 +198,41 @@ setPaperSize(size: "57mm" | "80mm") { | ||
startScanWifiPrinter(cb: (printer: any[]) => void) { | ||
const eventEmitter = new NativeEventEmitter(PrinterAndroidLibrary); | ||
if (listener) { | ||
listener?.remove?.() | ||
if (isIOS) { | ||
NetPrinter.getDeviceList().then((dev: any) => { | ||
cb(dev) | ||
}) | ||
} else { | ||
const eventEmitter = new NativeEventEmitter(PrinterAndroidLibrary); | ||
if (listener) { | ||
listener?.remove?.() | ||
} | ||
listener = eventEmitter.addListener('onWifiPrinterScanned', ({ data }) => { | ||
cb(data) | ||
}) | ||
PRINTER.startScanWifiPrinter() | ||
} | ||
listener = eventEmitter.addListener('onWifiPrinterScanned', ({ data }) => { | ||
cb(data) | ||
}) | ||
PRINTER.startScanWifiPrinter() | ||
}, | ||
startScanPrinters(cb: (printer: any[]) => void) { | ||
const eventEmitter = new NativeEventEmitter(PrinterAndroidLibrary); | ||
if (listener) { | ||
listener?.remove?.() | ||
if (isIOS) { | ||
BLEPrinter.getDeviceList().then((dev: any) => { | ||
cb(dev) | ||
}) | ||
} else { | ||
const eventEmitter = new NativeEventEmitter(PrinterAndroidLibrary); | ||
if (listener) { | ||
listener?.remove?.() | ||
} | ||
listener = eventEmitter.addListener('onBluetoothPrinterScanned', ({ data }) => { | ||
cb(data) | ||
}); | ||
PRINTER.listBluetooth() | ||
} | ||
listener = eventEmitter.addListener('onBluetoothPrinterScanned', ({ data }) => { | ||
cb(data) | ||
}); | ||
PRINTER.listBluetooth() | ||
}, | ||
releasePrinterList() { | ||
PRINTER?.releaseListBluetooth?.() | ||
if (listener) { | ||
listener?.remove?.() | ||
if (isIOS) { | ||
} else { | ||
PRINTER?.releaseListBluetooth?.() | ||
if (listener) { | ||
listener?.remove?.() | ||
} | ||
} | ||
@@ -134,14 +242,27 @@ }, | ||
dataPrint.push(`[IMAGE]:${base64}`) | ||
PRINTER.printImage(base64, width, align); | ||
if (isIOS) { | ||
IOSPRINTER.printImageBase64(base64, { imageWidth: width }) | ||
} else | ||
PRINTER.printImage(base64, width, align); | ||
}, | ||
printBarcode(barcodeType: BarcodeType, data: string, height: number, barSize: number, align: string) { | ||
printBarcode(barcodeType: BarcodeType, data: string, height: number, barSize: number, align: Justification) { | ||
dataPrint.push(`[BARCODE]:${data}`) | ||
PRINTER.printBarcode(barcodeType, data, height, barSize, align) | ||
if (isIOS) | ||
_printTextBase(barcodeType + ": " + data, align, "BOLD", 0) | ||
else | ||
PRINTER.printBarcode(barcodeType, data, height, barSize, align) | ||
}, | ||
getPairedBluetooth(cb: (list: any) => void) { | ||
PRINTER.getPairedBluetooth(cb); | ||
if (isIOS) { | ||
BLEPrinter.getDeviceList().then(cb) | ||
} else { | ||
PRINTER.getPairedBluetooth(cb); | ||
} | ||
}, | ||
printQRCode(data: string, level: level, size: qrsize, align: string) { | ||
printQRCode(data: string, level: level, size: qrsize, align: Justification) { | ||
dataPrint.push(`[QR]:${data}`) | ||
PRINTER.printQRCode(data, level, size, align) | ||
if (isIOS) | ||
_printTextBase("QRCODE: " + data, align, "BOLD", 0) | ||
else | ||
PRINTER.printQRCode(data, level, size, align) | ||
}, | ||
@@ -151,20 +272,46 @@ addNewLine(count: number = 1) { | ||
dataPrint.push(enters) | ||
PRINTER.printTextBase(enters, "LEFT", "DEFAULT", 0) | ||
if (isIOS) { | ||
_printTextBase(enters, 'LEFT', "DEFAULT", 0) | ||
} else | ||
PRINTER.printTextBase(enters, "LEFT", "DEFAULT", 0) | ||
}, | ||
openDrawer() { | ||
PRINTER.openCashdrawer() | ||
dataPrint.push(`[OPEN DRAWER]`) | ||
if (isIOS) { | ||
IOSPRINTER.printText(COMMANDS.CASH_DRAWER.CD_KICK_2) | ||
IOSPRINTER.printText(COMMANDS.CASH_DRAWER.CD_KICK_5) | ||
} else { | ||
PRINTER.openCashdrawer() | ||
} | ||
}, | ||
pageStart() { | ||
PRINTER.pageStart() | ||
dataPrint = [] | ||
if (isIOS) { | ||
} else { | ||
PRINTER.pageStart() | ||
} | ||
}, | ||
pageEnd(cb: () => void) { | ||
PRINTER.pageEnd(cb) | ||
if (isIOS) { | ||
} else { | ||
PRINTER.pageEnd(cb) | ||
} | ||
}, | ||
testPrint() { | ||
PRINTER.testPrint() | ||
if (isIOS) { | ||
IOSPRINTER.printText(new Array(CHARSIZE).fill('=').join('')) | ||
_printTextBase("TEST PRINT", "CENTER", "DEFAULT", 0) | ||
IOSPRINTER.printText(new Array(CHARSIZE).fill('=').join('')) | ||
} else { | ||
PRINTER.testPrint() | ||
} | ||
}, | ||
printTextln(text: string, align: Justification) { | ||
dataPrint.push(text) | ||
PRINTER.printTextBase(appendEnter(text), align, "DEFAULT", 0) | ||
if (isIOS) | ||
_printTextBase(appendEnter(text), align, "DEFAULT", 0) | ||
else | ||
PRINTER.printTextBase(appendEnter(text), align, "DEFAULT", 0) | ||
}, | ||
@@ -175,3 +322,6 @@ printTextJustify(text1: string, text2: string) { | ||
dataPrint.push(newText) | ||
PRINTER.printTextBase(appendEnter(newText), "LEFT", "DEFAULT", 0) | ||
if (isIOS) | ||
_printTextBase(appendEnter(newText), "LEFT", "DEFAULT", 0) | ||
else | ||
PRINTER.printTextBase(appendEnter(newText), "LEFT", "DEFAULT", 0) | ||
}, | ||
@@ -182,19 +332,34 @@ printTextJustifyBold(text1: string, text2: string) { | ||
dataPrint.push(newText) | ||
PRINTER.printTextBase(appendEnter(newText), "LEFT", "BOLD", 0) | ||
if (isIOS) | ||
_printTextBase(appendEnter(newText), "LEFT", "BOLD", 0) | ||
else | ||
PRINTER.printTextBase(appendEnter(newText), "LEFT", "BOLD", 0) | ||
}, | ||
printTextlnNormal(text: string, align: Justification) { | ||
dataPrint.push(text) | ||
PRINTER.printTextBase(appendEnter(text), align, "DEFAULT", 0) | ||
if (isIOS) | ||
_printTextBase(appendEnter(text), align, "DEFAULT", 0) | ||
else | ||
PRINTER.printTextBase(appendEnter(text), align, "DEFAULT", 0) | ||
}, | ||
printTextlnBold(text: string, align: Justification) { | ||
dataPrint.push(text) | ||
PRINTER.printTextBase(appendEnter(text), align, "BOLD", 0) | ||
if (isIOS) | ||
_printTextBase(appendEnter(text), align, "BOLD", 0) | ||
else | ||
PRINTER.printTextBase(appendEnter(text), align, "BOLD", 0) | ||
}, | ||
cutPaper() { | ||
dataPrint.push("[CUT]") | ||
PRINTER.cutPaper() | ||
if (isIOS) { | ||
IOSPRINTER.printText(COMMANDS.PAPER.PAPER_CUT_A) | ||
} else | ||
PRINTER.cutPaper() | ||
}, | ||
feed() { | ||
dataPrint.push("\n\n\n") | ||
PRINTER.printTextBase("\n\n\n", "CENTER", "DEFAULT", 0) | ||
if (isIOS) | ||
IOSPRINTER.printText("\n\n\n") | ||
else | ||
PRINTER.printTextBase("\n\n\n", "CENTER", "DEFAULT", 0) | ||
}, | ||
@@ -204,3 +369,6 @@ printDashedLine: () => { | ||
dataPrint.push(chars) | ||
PRINTER.printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
if (isIOS) | ||
_printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
else | ||
PRINTER.printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
}, | ||
@@ -210,3 +378,6 @@ printLine: () => { | ||
dataPrint.push(chars) | ||
PRINTER.printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
if (isIOS) | ||
_printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
else | ||
PRINTER.printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
}, | ||
@@ -216,3 +387,6 @@ printDoubleDashedLine: () => { | ||
dataPrint.push(chars) | ||
PRINTER.printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
if (isIOS) | ||
_printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
else | ||
PRINTER.printTextBase(appendEnter(chars), "CENTER", "DEFAULT", 0) | ||
}, | ||
@@ -219,0 +393,0 @@ getDataPrint: () => { |
@@ -1,1 +0,6 @@ | ||
["react-native-permissions"] | ||
[ | ||
"react-native-permissions", | ||
"react-native-qrcode-svg", | ||
"react-native-thermal-receipt-printer-image-qr", | ||
"react-native-ping" | ||
] |
{ | ||
"name": "esoftplay-android-print", | ||
"version": "0.0.187", | ||
"version": "0.0.188", | ||
"description": "Printing ", | ||
@@ -5,0 +5,0 @@ "main": "./index.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26762878
74
4309
4