import { columnsText, fontSize as fs, AlginType, sleep, FontSize } from './print_common'; import HardwareKit from 'qm-hardwarekit'; import { Platform } from 'react-native'; let Sprt = HardwareKit.Sprt; // const PAGE_ROW_SIZE: number = 32; const PAGE_ROW_SIZE: number = 28; var _device = { address: '' }; var isQrCode = false; function startPrint(device: any) { _device = device; isQrCode = false; sleep(2500); return new Promise(resolve => resolve(null)); } const printQrCode = text => { // sleep(3000); // lineWrap(1); isQrCode = true; console.log('------ printQrCode -----', text); return new Promise(async resolve => { if ('android' === Platform.OS) { await Sprt.printQrCode(_device.address, text); } else { await Sprt.printQrCode(text); } resolve(null); }); }; const printBarCode = text => { return new Promise(async resolve => { if (Sprt) await Sprt.printBarCode(text); resolve(null); }); }; function printText(text: String, fontSize: FontSize = 'middle', algin: AlginType = 0) { fontSize = typeof fontSize === 'number' ? fontSize : fs(fontSize); return new Promise(async resolve => { if ('android' === Platform.OS) { switch (algin) { case 0: await Sprt.printAlignLeft(_device.address); break; case 1: await Sprt.printAlignCenter(_device.address); break; case 2: await Sprt.printAlignRight(_device.address); break; } await Sprt.printText(_device.address, text); //打印完成还原 await Sprt.printAlignLeft(_device.address); } else { switch (algin) { case 0: await Sprt.printAlignLeft(); break; case 1: await Sprt.printAlignCenter(); break; case 2: await Sprt.printAlignRight(); break; } await Sprt.printText(text); //打印完成还原 await Sprt.printAlignLeft(); } resolve(null); }); } function printlnText(text: String, fontSize: FontSize = 'middle', algin: AlginType = 0) { return printText(text + '\n', fontSize, algin); } function printColumnsText( texts: Array, weights: Array, algins: Array, zoom: boolean = true, ) { return new Promise(async resolve => { columnsText(texts, weights, algins, PAGE_ROW_SIZE, zoom).map(async text => { await printlnText(text); }); resolve(null); }); } const line = () => { return new Promise(async resolve => { if ('android' === Platform.OS) { await Sprt.lineSplit(_device.address); } else { await Sprt.lineSplit(); } resolve(null); }); }; const lineWrap = (nums: number) => { return new Promise(async resolve => { if ('android' === Platform.OS) { await Sprt.lineWrap(_device.address, nums); } else { await Sprt.lineWrap(nums); } resolve(null); }); }; const endPrint = () => { // setTimeout(() => { return new Promise(async resolve => { if ('android' === Platform.OS) { if (!isQrCode) await Sprt.lineWrap(_device.address, 4); } else { await Sprt.lineWrap(4); } // }, 1000); resolve(null); }); }; export { printText, printlnText, printColumnsText, lineWrap, printQrCode, printBarCode, line, startPrint, endPrint };