Skip to content

Commit

Permalink
refactor & doc update
Browse files Browse the repository at this point in the history
  • Loading branch information
yeasir01 committed Feb 3, 2024
1 parent 8ab6362 commit 7859db0
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 119 deletions.
Binary file added .github/images/auto-cut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/images/auto-half-chain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/images/auto-half-cut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/images/chain-print.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/images/half-cut-chain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/images/half-cut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/images/no-cut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 21 additions & 10 deletions docs/options.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Notice
Properties are applicable exclusively to models that support each respective function. If a model does not support a particular function, the configuration becomes invalid, even if a prop is specified.

## Print Config Options
## Supported Ext Types (File Export)
| **Description** | **Extension** |
|---|:---:|
| LBX type | .lbx |
| LBL (P-touch Editor 4.2) | .lbl |
| LBI type | .lbi |
| BMP Image | .bmp |
| PAF Type | .paf |

## Print Option Props
| **Key** | **Type** | **Default** | **Description** |
|---|---|:---:|---|
| copies | number | 1 |Number of copies to print.|
Expand All @@ -21,14 +30,16 @@ Properties are applicable exclusively to models that support each respective fun
| highResolution | boolean | false |High resolution printing.|
| color | boolean | false |Print in color.|
| mono | boolean | false |Print in monochrome.|
| continue | boolean | false |Front margin not output.|
| fitPage | boolean | false |Specify whether to adjust the size and position of objects in the template in accordance with layout changes resulting from media changes. If set to true, adjustments will be made; otherwise, if set to false or undefined, no adjustments will be applied..|

## Supported Export Extensions
| **Description** | **Extension** |
|---|:---:|
| LBX type | .lbx |
| LBL (P-touch Editor 4.2) | .lbl |
| LBI type | .lbi |
| BMP Image | .bmp |
| PAF Type | .paf |
## PT-9500PC / PT-9600 / PT-3600
| **noCut** | **autoCut** | **halfCut** | **cutMark** | **chainPrint** | **cutAtEnd** | **specialTape** | **Result** |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|---:|
|✔️|-|-|-|-|-|-|![no cut](../.github/images/no-cut.png) |
|-|-|-|-|✔️|-|-|![chain print](../.github/images/chain-print.png) |
|-|-|✔️|-|-|-|-|![half cut](../.github/images/half-cut.png) |
|-|-|✔️|-|✔️|-|-|![half cut chain](../.github/images/half-cut-chain.png) |
|-|✔️|-|-|-|-|-|![auto cut](../.github/images/auto-cut.png) |
|-|✔️|✔️|-|-|-|-|![auto cut half cut](../.github/images/auto-half-cut.png) |
|-|✔️|✔️|-|✔️|-|-|![auto half chain](../.github/images/auto-half-chain.png) |

2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Install package
```sh
$ npm install bpac-js
```
Import module - named and defaults ok.
Import - named or defaults ok.

```javascript
import BrotherBrotherSdk from "bpac-js";
Expand Down
175 changes: 87 additions & 88 deletions src/BrotherSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,32 @@ import {
} from "./adapter";

export default class BrotherSdk {
#ready: boolean;

templatePath: string;

printer: undefined | string;

exportDir: undefined | string;

// One mutation observer, regardless of instances.
static #observer: MutationObserver | undefined;
static #observer: MutationObserver | null = null;

/**
* **Retrieve The List Of Installed Printers**
*
* asynchronously retrieves the list of installed printers compatible with the bpac SDK.
*
* @returns {Promise<string[]>}
* a promise that resolves to an array of installed printers
* compatible with the 'bpac' SDK.
* @throws {Error}
* will throw an err if the method fails.
*
*/
static async getPrinterList(): Promise<string[]> {
const printers = await getPrinters();
return printers;
}
static #ready: boolean = false;

/**
* @constructor
* Constructs a new instance of the BrotherSdk class, used
* for interacting with Brother SDK functionality.
* **BPAC-JS Class Object**
*
* Create a new instance of this class to interact with the Brother SDK
* in JavaScript. This object facilitates communication and integration
* with the SDK functionalities.
* @param {Object} object
* @param {String} object.templatePath
* Specifies the path to the template file (supports various formats)
* Specifies the path to the template file
* - Win path: "C:\\\path\\\to\\\your\\\template.lbx"
* - Unix path: "/home/templates/template.lbx"
* - UNC path: "\\\server\share\template.lbx"
* - Remote URL: "http://yourserver.com/templates/label.lbx"
* @param {String} [object.exportDir = ""]
* The path for exporting generated templates.
* The path for exporting generated assets.
* - Win path: "C:\\\path\\\to\\\your\\\"
* - Unix path: "/home/templates/"
* @param {String} [object.printer = undefined]
Expand All @@ -76,59 +61,7 @@ export default class BrotherSdk {
this.templatePath = templatePath;
this.exportDir = exportDir;
this.printer = printer;
this.#ready = false;
this.#initialize();
}

#initialize() {
const targetNode = document.body;
const className = "bpac-extension-installed";

if (this.#ready) {
return;
}

if (BrotherSdk.#observer) {
return;
}

if (targetNode.classList.contains(className)) {
this.#ready = true;
return;
}

BrotherSdk.#observer = new MutationObserver(() => {
if (targetNode.classList.contains(className)) {
this.#ready = true;
BrotherSdk.#observer?.disconnect();
BrotherSdk.#observer = undefined;
}
});

BrotherSdk.#observer.observe(targetNode, {
attributes: true,
attributeFilter: ["class"],
});
}

async #isPrintReady(timeout: number = 3000): Promise<boolean> {
if (this.#ready) {
return true;
}

return new Promise((resolve, reject) => {
setTimeout(() => {
if (this.#ready) {
resolve(true);
} else {
reject(
new Error(
"Cannot establish printer communication: b-PAC extension missing or inactive. Install/enable.",
),
);
}
}, timeout);
});
BrotherSdk.#initialize();
}

/**
Expand Down Expand Up @@ -179,9 +112,6 @@ export default class BrotherSdk {
* @param {boolean} [config.mono = false]
* Monochrome printing is performed. Valid only with models supporting
* the color printing function.
* @param {boolean} [config.continue = false]
* Combines with printing for the following DoPrint( ) so that it is a single print job.
* As a result, when the next DoPrints are called up, the front margins are not output.
* @param {boolean} [config.fitPage = false]
* Specify whether to adjust the size and position of objects in the template in accordance
* with layout changes resulting from media changes. If set to true, adjustments
Expand All @@ -195,14 +125,14 @@ export default class BrotherSdk {
...opts
} = config || {};

await this.#isPrintReady();
await BrotherSdk.printerIsReady();

const bitMask = getStartPrintOptions(opts);
const hexValue = getStartPrintOptions(opts);

await openTemplate(this.templatePath);
await setPrinter(this.printer, fitPage);
await populateObjectsInTemplate(data);
await startPrint(printName, bitMask);
await startPrint(printName, hexValue);
await printOut(copies);
await endPrint();
await closeTemplate();
Expand Down Expand Up @@ -236,7 +166,7 @@ export default class BrotherSdk {
const height = options?.height || 0;
const width = options?.width || 0;

await this.#isPrintReady();
await BrotherSdk.printerIsReady();
await openTemplate(this.templatePath);
await populateObjectsInTemplate(data);
const base64Data = await imageData(width, height);
Expand All @@ -257,11 +187,11 @@ export default class BrotherSdk {
*
*/
async getPrinterName(): Promise<string> {
await this.#isPrintReady();
await BrotherSdk.printerIsReady();
await openTemplate(this.templatePath);
const printer = getPrinterName();
const printerName = getPrinterName();
await closeTemplate();
return printer;
return printerName;
}

/**
Expand Down Expand Up @@ -296,7 +226,7 @@ export default class BrotherSdk {
filePathOrFileName,
);

await this.#isPrintReady();
await BrotherSdk.printerIsReady();
await openTemplate(this.templatePath);
await populateObjectsInTemplate(data);
const status = await exportTemplate(fileType, path, resolution);
Expand All @@ -310,4 +240,73 @@ export default class BrotherSdk {

return true;
}

static #initialize():void {
const targetNode = document.body;
const className = "bpac-extension-installed";

if (BrotherSdk.#ready) {
return;
}

if (BrotherSdk.#observer) {
return;
}

if (targetNode.classList.contains(className)) {
BrotherSdk.#ready = true;
return;
}

BrotherSdk.#observer = new MutationObserver(() => {
if (targetNode.classList.contains(className)) {
BrotherSdk.#ready = true;
BrotherSdk.#observer?.disconnect();
BrotherSdk.#observer = null;
}
});

BrotherSdk.#observer.observe(targetNode, {
attributes: true,
attributeFilter: ["class"],
});
}

/**
* **Get List Of Installed Printers**
*
* asynchronously retrieves the list of installed printers compatible with the bpac SDK.
*
* @returns {Promise<string[]>}
* a promise that resolves to an array of installed printers
* compatible with the 'bpac' SDK.
* @throws {Error}
* will throw an err if the method fails.
*
*/
static async getPrinterList(): Promise<string[]> {
await BrotherSdk.printerIsReady();
const printers = await getPrinters();
return printers;
}

static async printerIsReady(timeout: number = 3000): Promise<boolean> {
if (BrotherSdk.#ready) {
return true;
}

return new Promise((resolve, reject) => {
setTimeout(() => {
if (BrotherSdk.#ready) {
resolve(true);
} else {
reject(
new Error(
"Cannot establish printer communication: b-PAC extension missing or inactive. Install/enable.",
),
);
}
}, timeout);
});
}
}
2 changes: 1 addition & 1 deletion src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const setPrinter = async (printer: string | undefined, fitPage: boolean):
if (printer === undefined && fitPage === false) return;

if (printer === undefined && fitPage === true) {
throw new Error("To use fitPage, you must manually set the printer name.");
throw new Error("To use fitPage, you must explicity set the printer name.");
}

const isPrinter:boolean = await Doc.SetPrinter(printer, fitPage);
Expand Down
19 changes: 8 additions & 11 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StartPrintOptions } from "./types";

const optionBitmaskMap: { [key in keyof StartPrintOptions]: number } = {
const optionToHexMap: { [key:string]: number } = {
autoCut: 0x1,
cutPause: 0x1,
cutMark: 0x2,
Expand All @@ -16,7 +16,6 @@ const optionBitmaskMap: { [key in keyof StartPrintOptions]: number } = {
highResolution: 0x02000000,
color: 0x8,
mono: 0x10000000,
continue: 0x40000000,
};

export const getAbsolutePath = (
Expand Down Expand Up @@ -54,22 +53,20 @@ export const getExportType = (fileExt:string) => {
};

export const getStartPrintOptions = (options: StartPrintOptions): number => {
const combinedBitmask: number[] = [];
const combinedHexArray: number[] = [];

Object.entries(options).forEach(([key, value]) => {
const k = key as keyof StartPrintOptions;
Object.entries(options).forEach(([key, value]): void => {
if (value === true && optionToHexMap[key] !== undefined) {
const hexadecimal = optionToHexMap[key];

if (value === true && optionBitmaskMap[k] !== undefined) {
const bitmaskValue: number | undefined = optionBitmaskMap[k];

if (bitmaskValue !== undefined) {
combinedBitmask.push(bitmaskValue);
if (hexadecimal !== undefined) {
combinedHexArray.push(hexadecimal);
}
}
});

// eslint-disable-next-line no-bitwise
return combinedBitmask.reduce((acc, val) => acc | val, 0x0);
return combinedHexArray.reduce((acc, val) => acc | val, 0x0);
};

export const getFileExtension = (filePathOrFileName:string): string => {
Expand Down
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ export type StartPrintOptions = {
highResolution?: boolean,
color?: boolean,
mono?: boolean,
continue?: boolean,
};

export type FitPage = {
fitPage: boolean;
fitPage?: boolean;
};

export type PrintConfig = PrintOptions & StartPrintOptions & FitPage;
Expand Down
1 change: 1 addition & 0 deletions tests/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>bpac test</h1>
<button id="print-btn">Print</button>
<button id="preview-btn">Preview</button>
<button id="export-btn">Export</button>
<button id="get-printers-btn">Get Printers</button>
</div>
<script type="module" src="playground.js"></script>
</body>
Expand Down
Loading

0 comments on commit 7859db0

Please sign in to comment.