From 4eed3b8ae83dc57c46f6b9331eb6a8813767cdca Mon Sep 17 00:00:00 2001 From: Aliaksei Astafyeu Date: Tue, 30 Jul 2024 09:59:42 +0200 Subject: [PATCH] feat(added): add addFeedLine method, add docs (#164) --- .../EscPosPrinterModule.java | 15 +++++++++ .../reactnativeescposprinter/ThePrinter.java | 6 ++++ docs/printer/Printer.md | 11 +++++++ docs/printer/addLineSpace.md | 32 +++++++++++++++++++ example/ios/Podfile.lock | 4 +-- ios/EscPosPrinter.m | 22 +++++++++++++ ios/ThePrinter.h | 2 ++ ios/ThePrinter.m | 12 +++++++ src/printer/Printer.ts | 4 +++ src/printer/PrinterWrapper.ts | 12 +++++++ 10 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 docs/printer/addLineSpace.md diff --git a/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterModule.java b/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterModule.java index 20f943c..e4fd5b0 100644 --- a/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterModule.java +++ b/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterModule.java @@ -177,6 +177,21 @@ synchronized public void addFeedLine(String target, int line, Promise promise) { } } + @ReactMethod + synchronized public void addLineSpace(String target, int linespc, Promise promise) { + ThePrinter thePrinter = thePrinterManager_.getObject(target); + if (thePrinter == null) { + promise.reject(EposStringHelper.getErrorTextData(ERR_INIT, "")); + } else { + try { + thePrinter.addLineSpace(linespc); + promise.resolve(null); + } catch(Exception e) { + processError(promise, e, ""); + } + } + } + @ReactMethod synchronized public void addCut(String target, int type, Promise promise) { ThePrinter thePrinter = thePrinterManager_.getObject(target); diff --git a/android/src/main/java/com/reactnativeescposprinter/ThePrinter.java b/android/src/main/java/com/reactnativeescposprinter/ThePrinter.java index 0634c09..b78944e 100644 --- a/android/src/main/java/com/reactnativeescposprinter/ThePrinter.java +++ b/android/src/main/java/com/reactnativeescposprinter/ThePrinter.java @@ -250,6 +250,12 @@ synchronized public void addFeedLine(int line) throws Epos2Exception { epos2Printer_.addFeedLine(line); } + synchronized public void addLineSpace(int linespc) throws Epos2Exception { + if (epos2Printer_ == null) throw new Epos2Exception(Epos2Exception.ERR_MEMORY); + + epos2Printer_.addLineSpace(linespc); + } + synchronized public void addCut(int type) throws Epos2Exception { if (epos2Printer_ == null) throw new Epos2Exception(Epos2Exception.ERR_MEMORY); diff --git a/docs/printer/Printer.md b/docs/printer/Printer.md index bf4dd42..81dd3c5 100644 --- a/docs/printer/Printer.md +++ b/docs/printer/Printer.md @@ -58,6 +58,17 @@ Adds a paper-feed-by-line command to the command buffer. await printerInstance.addFeedLine(3); ``` +--- +### [addLineSpace(`linespc: number`): `Promise`](./addLineSpace.md) + +Adds line spacing setting to the command buffer. + +#### Example + +```typescript +await printerInstance.addLineSpace(50); +``` + --- ### [sendData(`timeout?: number`): `Promise`](./sendData.md) diff --git a/docs/printer/addLineSpace.md b/docs/printer/addLineSpace.md new file mode 100644 index 0000000..74da4e8 --- /dev/null +++ b/docs/printer/addLineSpace.md @@ -0,0 +1,32 @@ +## addLineSpace + +Adds line spacing setting to the command buffer. + +### Parameters + +#### linespc + +- `number` + +Specifies the line spacing (in dots). + +| **Value** | **Description** | +| --- | --- | +| 0 to 255 | Line spacing (in dots) | + +### Returns + +`Promise` + +### Errors + +| **Error status** | **Description** | +| --- | --- | +| ERR_PARAM | An invalid parameter was passed. | +| ERR_TIMEOUT | Failed to communicate with the devices within the specified time. | +| ERR_MEMORY | Necessary memory could not be allocated. | + +### Supplementary explanation + +- If the line spacing for a single line is set smaller than the print character size, paper may be fed for a larger quantity than the set amount to ensure proper printing. + diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index b2d5359..6db8574 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -315,7 +315,7 @@ PODS: - React-jsinspector (0.72.6) - React-logger (0.72.6): - glog - - react-native-esc-pos-printer (4.0.0): + - react-native-esc-pos-printer (4.0.1): - RCT-Folly (= 2021.07.22.00) - React-Core - react-native-image-picker (7.1.0): @@ -605,7 +605,7 @@ SPEC CHECKSUMS: React-jsiexecutor: 3bf18ff7cb03cd8dfdce08fbbc0d15058c1d71ae React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072 React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289 - react-native-esc-pos-printer: 7d9eadb4e6a43b01515defe2bc4ad897e1d83145 + react-native-esc-pos-printer: 71506d7f9081fe378901ad0e685cd118831eaf6d react-native-image-picker: 5e076db26cd81660cfb6db5bcf517cfa12054d45 react-native-safe-area-context: 2cd91d532de12acdb0a9cbc8d43ac72a8e4c897c React-NativeModulesApple: 02e35e9a51e10c6422f04f5e4076a7c02243fff2 diff --git a/ios/EscPosPrinter.m b/ios/EscPosPrinter.m index 9286104..2b0bdd8 100644 --- a/ios/EscPosPrinter.m +++ b/ios/EscPosPrinter.m @@ -193,6 +193,28 @@ + (BOOL)requiresMainQueueSetup } } +RCT_EXPORT_METHOD(addLineSpace: (nonnull NSString*) target + linespc: (int) linespc + withResolver:(RCTPromiseResolveBlock)resolve + withRejecter:(RCTPromiseRejectBlock)reject) +{ + int result = EPOS2_SUCCESS; + @synchronized (self) { + ThePrinter* thePrinter = [objManager_ getObject:target]; + if (thePrinter == nil) { + result = [EposStringHelper getInitErrorResultCode]; + } else { + result = [thePrinter addLineSpace:linespc]; + } + + if(result == EPOS2_SUCCESS) { + resolve(nil); + } else { + reject(@"event_failure", [@(result) stringValue], nil); + } + } +} + RCT_EXPORT_METHOD(addCut: (nonnull NSString*) target type: (int)type withResolver:(RCTPromiseResolveBlock)resolve diff --git a/ios/ThePrinter.h b/ios/ThePrinter.h index 095ec6a..f9d3eb4 100644 --- a/ios/ThePrinter.h +++ b/ios/ThePrinter.h @@ -62,6 +62,8 @@ -(int) addFeedLine: (int)line; +-(int) addLineSpace: (int)linespc; + -(int) addCut: (int)type; -(int) addCommand: (nonnull NSString* )base64string; diff --git a/ios/ThePrinter.m b/ios/ThePrinter.m index 0ab004d..4d90909 100644 --- a/ios/ThePrinter.m +++ b/ios/ThePrinter.m @@ -270,6 +270,18 @@ -(int) addFeedLine: (int)line; } } +-(int) addLineSpace: (int)linespc; +{ + @synchronized (self) { + if (epos2Printer_ == nil) { + return EPOS2_ERR_MEMORY; + } + + int result = [epos2Printer_ addLineSpace: linespc]; + return result; + } +} + -(int) addCommand: (NSString* )base64string; { @synchronized (self) { diff --git a/src/printer/Printer.ts b/src/printer/Printer.ts index 732fac5..a22151d 100644 --- a/src/printer/Printer.ts +++ b/src/printer/Printer.ts @@ -86,6 +86,10 @@ export class Printer { return this.printerWrapper.addFeedLine(line); }; + addLineSpace = (linespc: number) => { + return this.printerWrapper.addLineSpace(linespc); + }; + sendData = (timeout?: number) => { return this.printerWrapper.sendData(timeout); }; diff --git a/src/printer/PrinterWrapper.ts b/src/printer/PrinterWrapper.ts index a232461..421b306 100644 --- a/src/printer/PrinterWrapper.ts +++ b/src/printer/PrinterWrapper.ts @@ -138,6 +138,18 @@ export class PrinterWrapper { } }; + addLineSpace = async (linespc: number) => { + try { + await EscPosPrinter.addLineSpace(this.target, linespc); + } catch (error) { + throwProcessedError({ + methodName: 'addLineSpace', + errorCode: error.message, + messagesMapping: CommonOperationErrorMessageMapping, + }); + } + }; + addCut = async (type: AddCutTypeParam = PrinterConstants.PARAM_DEFAULT) => { try { await EscPosPrinter.addCut(this.target, type);