UNPKG

react-native-thermal-receipt-printer

Version:

Fork of react-native-printer. A React Native Library to support USB/BLE/Net printer

196 lines (195 loc) 7.92 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { NativeModules, NativeEventEmitter, Platform } from "react-native"; import * as EPToolkit from "./utils/EPToolkit"; var RNUSBPrinter = NativeModules.RNUSBPrinter; var RNBLEPrinter = NativeModules.RNBLEPrinter; var RNNetPrinter = NativeModules.RNNetPrinter; var textTo64Buffer = function (text, opts) { var defaultOptions = { beep: false, cut: false, tailingLine: false, encoding: "UTF8", }; var options = __assign(__assign({}, defaultOptions), opts); var buffer = EPToolkit.exchange_text(text, options); return buffer.toString("base64"); }; var billTo64Buffer = function (text, opts) { var defaultOptions = { beep: true, cut: true, encoding: "UTF8", tailingLine: true, }; var options = __assign(__assign({}, defaultOptions), opts); var buffer = EPToolkit.exchange_text(text, options); return buffer.toString("base64"); }; var textPreprocessingIOS = function (text) { var options = { beep: true, cut: true, }; return { text: text .replace(/<\/?CB>/g, "") .replace(/<\/?CM>/g, "") .replace(/<\/?CD>/g, "") .replace(/<\/?C>/g, "") .replace(/<\/?D>/g, "") .replace(/<\/?B>/g, "") .replace(/<\/?M>/g, ""), opts: options, }; }; // const imageToBuffer = async (imagePath: string, threshold: number = 60) => { // const buffer = await EPToolkit.exchange_image(imagePath, threshold); // return buffer.toString("base64"); // }; export var USBPrinter = { init: function () { return new Promise(function (resolve, reject) { return RNUSBPrinter.init(function () { return resolve(); }, function (error) { return reject(error); }); }); }, getDeviceList: function () { return new Promise(function (resolve, reject) { return RNUSBPrinter.getDeviceList(function (printers) { return resolve(printers); }, function (error) { return reject(error); }); }); }, connectPrinter: function (vendorId, productId) { return new Promise(function (resolve, reject) { return RNUSBPrinter.connectPrinter(vendorId, productId, function (printer) { return resolve(printer); }, function (error) { return reject(error); }); }); }, closeConn: function () { return new Promise(function (resolve) { RNUSBPrinter.closeConn(); resolve(); }); }, printText: function (text, opts) { if (opts === void 0) { opts = {}; } return RNUSBPrinter.printRawData(textTo64Buffer(text, opts), function (error) { return console.warn(error); }); }, printBill: function (text, opts) { if (opts === void 0) { opts = {}; } return RNUSBPrinter.printRawData(billTo64Buffer(text, opts), function (error) { return console.warn(error); }); }, }; export var BLEPrinter = { init: function () { return new Promise(function (resolve, reject) { return RNBLEPrinter.init(function () { return resolve(); }, function (error) { return reject(error); }); }); }, getDeviceList: function () { return new Promise(function (resolve, reject) { return RNBLEPrinter.getDeviceList(function (printers) { return resolve(printers); }, function (error) { return reject(error); }); }); }, connectPrinter: function (inner_mac_address) { return new Promise(function (resolve, reject) { return RNBLEPrinter.connectPrinter(inner_mac_address, function (printer) { return resolve(printer); }, function (error) { return reject(error); }); }); }, closeConn: function () { return new Promise(function (resolve) { RNBLEPrinter.closeConn(); resolve(); }); }, printText: function (text, opts) { if (opts === void 0) { opts = {}; } if (Platform.OS === "ios") { var processedText = textPreprocessingIOS(text); RNBLEPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); } else { RNBLEPrinter.printRawData(textTo64Buffer(text, opts), function (error) { return console.warn(error); }); } }, printBill: function (text, opts) { if (opts === void 0) { opts = {}; } if (Platform.OS === "ios") { var processedText = textPreprocessingIOS(text); RNBLEPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); } else { RNBLEPrinter.printRawData(billTo64Buffer(text, opts), function (error) { return console.warn(error); }); } }, }; export var NetPrinter = { init: function () { return new Promise(function (resolve, reject) { return RNNetPrinter.init(function () { return resolve(); }, function (error) { return reject(error); }); }); }, getDeviceList: function () { return new Promise(function (resolve, reject) { return RNNetPrinter.getDeviceList(function (printers) { return resolve(printers); }, function (error) { return reject(error); }); }); }, connectPrinter: function (host, port) { return new Promise(function (resolve, reject) { return RNNetPrinter.connectPrinter(host, port, function (printer) { return resolve(printer); }, function (error) { return reject(error); }); }); }, closeConn: function () { return new Promise(function (resolve) { RNNetPrinter.closeConn(); resolve(); }); }, printText: function (text, opts) { if (opts === void 0) { opts = {}; } if (Platform.OS === "ios") { var processedText = textPreprocessingIOS(text); RNNetPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); } else { RNNetPrinter.printRawData(textTo64Buffer(text, opts), function (error) { return console.warn(error); }); } }, printBill: function (text, opts) { if (opts === void 0) { opts = {}; } if (Platform.OS === "ios") { var processedText = textPreprocessingIOS(text); RNNetPrinter.printRawData(processedText.text, processedText.opts, function (error) { return console.warn(error); }); } else { RNNetPrinter.printRawData(billTo64Buffer(text, opts), function (error) { return console.warn(error); }); } }, }; export var NetPrinterEventEmitter = new NativeEventEmitter(RNNetPrinter); export var RN_THERMAL_RECEIPT_PRINTER_EVENTS; (function (RN_THERMAL_RECEIPT_PRINTER_EVENTS) { RN_THERMAL_RECEIPT_PRINTER_EVENTS["EVENT_NET_PRINTER_SCANNED_SUCCESS"] = "scannerResolved"; RN_THERMAL_RECEIPT_PRINTER_EVENTS["EVENT_NET_PRINTER_SCANNING"] = "scannerRunning"; RN_THERMAL_RECEIPT_PRINTER_EVENTS["EVENT_NET_PRINTER_SCANNED_ERROR"] = "registerError"; })(RN_THERMAL_RECEIPT_PRINTER_EVENTS || (RN_THERMAL_RECEIPT_PRINTER_EVENTS = {}));