@skalfa/skalfa-printer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Skalfa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ <p align="center">
2
+ <img src="https://skalfa.sejedigital.com/images/logo-skalfa.png" alt="Skalfa Logo" width="300" />
3
+ </p>
4
+
5
+ # @skalfa/skalfa-printer
6
+
7
+ > Thermal printer bridge for Skalfa App.
8
+
9
+ ---
10
+
11
+ ## About this Package
12
+
13
+ This package is part of the **Skalfa Framework**, a premium development ecosystem designed to build high-performance, modular web applications and APIs.
14
+
15
+ ### Usage Scope & Standalone Status
16
+ > 🔒 **Skalfa Ecosystem Integration:** This package is designed to run **integrated within the Skalfa ecosystem** (such as Skalfa API or Skalfa App). It relies on the global service registry and core framework abstractions to operate.
17
+
18
+ ---
19
+
20
+ ## Documentation
21
+
22
+ See the usage documentation at [Documentation](https://skalfa.sejedigital.com).
23
+
24
+ ---
25
+
26
+ ## Installation
27
+
28
+ You can install this package using your preferred package manager:
29
+
30
+ ```bash
31
+ # Using npm
32
+ npm install @skalfa/skalfa-printer
33
+
34
+ # Using bun
35
+ bun add @skalfa/skalfa-printer
36
+ ```
37
+
38
+ ---
39
+
40
+ ## Pre-installed Dependencies
41
+
42
+ The following key dependencies are packaged and managed within this project:
43
+
44
+ | Dependency | Scope | Version |
45
+ | :--- | :--- | :--- |
46
+ | `@skalfa/skalfa-app-core` | runtime (peer) | `^1.0.0` |
47
+ | `@tauri-apps/api` | runtime (peer) | `^2.0.0` |
48
+ | `react` | runtime (peer) | `^19.0.0` |
49
+ | `@types/react` | development | `^19.0.0` |
50
+ | `typescript` | development | `^6.0.3` |
51
+
52
+ ---
53
+
54
+ ## License
55
+
56
+ This package is licensed under the **MIT License**. For full license text, see the [LICENSE](LICENSE) file.
@@ -0,0 +1,7 @@
1
+ export interface PrinterSelectProps {
2
+ name?: string;
3
+ placeholder?: string;
4
+ onChange?: (printerName: string) => void;
5
+ value?: string;
6
+ }
7
+ export declare function PrinterSelect({ name, placeholder, onChange, value: controlledValue, }: PrinterSelectProps): import("react").JSX.Element;
@@ -0,0 +1,51 @@
1
+ "use client";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.PrinterSelect = PrinterSelect;
5
+ const jsx_runtime_1 = require("react/jsx-runtime");
6
+ const react_1 = require("react");
7
+ const skalfa_app_core_1 = require("@skalfa/skalfa-app-core");
8
+ const commands_js_1 = require("./commands.js");
9
+ const STORAGE_KEY = "skalfa-printer-selected";
10
+ const SelectComponent = (props) => {
11
+ const Comp = skalfa_app_core_1.registry.get("SelectComponent");
12
+ return Comp ? (0, jsx_runtime_1.jsx)(Comp, { ...props }) : null;
13
+ };
14
+ function PrinterSelect({ name = "printer", placeholder = "-- PILIH PRINTER --", onChange, value: controlledValue, }) {
15
+ const [printers, setPrinters] = (0, react_1.useState)([]);
16
+ const [internalValue, setInternalValue] = (0, react_1.useState)("");
17
+ const isControlled = controlledValue !== undefined;
18
+ const currentValue = isControlled ? controlledValue : internalValue;
19
+ (0, react_1.useEffect)(() => {
20
+ (async () => {
21
+ try {
22
+ const list = await (0, commands_js_1.listPrinters)();
23
+ setPrinters(list);
24
+ if (!isControlled) {
25
+ const saved = localStorage.getItem(STORAGE_KEY);
26
+ if (saved && list.some((p) => p.name === saved)) {
27
+ setInternalValue(saved);
28
+ }
29
+ }
30
+ }
31
+ catch {
32
+ setPrinters([]);
33
+ }
34
+ })();
35
+ }, [isControlled]);
36
+ const handleChange = (val) => {
37
+ const selected = Array.isArray(val) ? val[0] : val;
38
+ if (!selected)
39
+ return;
40
+ localStorage.setItem(STORAGE_KEY, selected);
41
+ if (!isControlled) {
42
+ setInternalValue(selected);
43
+ }
44
+ onChange?.(selected);
45
+ };
46
+ const options = printers.map((p) => ({
47
+ label: `${p.name}${p.is_default ? " ★" : ""} — ${p.status_text}`,
48
+ value: p.name,
49
+ }));
50
+ return ((0, jsx_runtime_1.jsx)(SelectComponent, { name: name, placeholder: placeholder, options: options, value: currentValue, onChange: handleChange }));
51
+ }
@@ -0,0 +1,11 @@
1
+ export interface PrinterInfo {
2
+ name: string;
3
+ port: string;
4
+ driver: string;
5
+ is_default: boolean;
6
+ status: number;
7
+ status_text: string;
8
+ }
9
+ export declare function listPrinters(): Promise<PrinterInfo[]>;
10
+ export declare function printRaw(printer: string, content: string): Promise<void>;
11
+ export declare function getPrinterStatus(printer: string): Promise<PrinterInfo>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listPrinters = listPrinters;
4
+ exports.printRaw = printRaw;
5
+ exports.getPrinterStatus = getPrinterStatus;
6
+ const core_1 = require("@tauri-apps/api/core");
7
+ async function listPrinters() {
8
+ return (0, core_1.invoke)("plugin:skalfa-printer|list_printers");
9
+ }
10
+ async function printRaw(printer, content) {
11
+ return (0, core_1.invoke)("plugin:skalfa-printer|print_raw", { printer, content });
12
+ }
13
+ async function getPrinterStatus(printer) {
14
+ return (0, core_1.invoke)("plugin:skalfa-printer|get_printer_status", {
15
+ printer,
16
+ });
17
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./commands.js";
2
+ export * from "./usePrinter.hook.js";
3
+ export * from "./PrinterSelect.component.js";
4
+ export * from "./utils.js";
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ const skalfa_app_core_1 = require("@skalfa/skalfa-app-core");
18
+ const PrinterSelect_component_js_1 = require("./PrinterSelect.component.js");
19
+ __exportStar(require("./commands.js"), exports);
20
+ __exportStar(require("./usePrinter.hook.js"), exports);
21
+ __exportStar(require("./PrinterSelect.component.js"), exports);
22
+ __exportStar(require("./utils.js"), exports);
23
+ skalfa_app_core_1.registry.register("PrinterSelect", PrinterSelect_component_js_1.PrinterSelect);
@@ -0,0 +1,13 @@
1
+ import type { PrinterInfo } from "./commands.js";
2
+ export interface UsePrinterReturn {
3
+ printers: PrinterInfo[];
4
+ selectedPrinter: string | null;
5
+ selectPrinter: (name: string) => void;
6
+ print: (content: string) => Promise<void>;
7
+ printTo: (printer: string, content: string) => Promise<void>;
8
+ status: PrinterInfo | null;
9
+ isLoading: boolean;
10
+ error: string | null;
11
+ refresh: () => Promise<void>;
12
+ }
13
+ export declare function usePrinter(): UsePrinterReturn;
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePrinter = usePrinter;
4
+ const react_1 = require("react");
5
+ const commands_js_1 = require("./commands.js");
6
+ const STORAGE_KEY = "skalfa-printer-selected";
7
+ function usePrinter() {
8
+ const [printers, setPrinters] = (0, react_1.useState)([]);
9
+ const [selectedPrinter, setSelectedPrinter] = (0, react_1.useState)(null);
10
+ const [status, setStatus] = (0, react_1.useState)(null);
11
+ const [isLoading, setIsLoading] = (0, react_1.useState)(true);
12
+ const [error, setError] = (0, react_1.useState)(null);
13
+ (0, react_1.useEffect)(() => {
14
+ const saved = localStorage.getItem(STORAGE_KEY);
15
+ if (saved) {
16
+ setSelectedPrinter(saved);
17
+ }
18
+ }, []);
19
+ const fetchPrinters = (0, react_1.useCallback)(async () => {
20
+ setIsLoading(true);
21
+ setError(null);
22
+ try {
23
+ const list = await (0, commands_js_1.listPrinters)();
24
+ setPrinters(list);
25
+ if (!selectedPrinter) {
26
+ const defaultPrinter = list.find((p) => p.is_default);
27
+ if (defaultPrinter) {
28
+ setSelectedPrinter(defaultPrinter.name);
29
+ localStorage.setItem(STORAGE_KEY, defaultPrinter.name);
30
+ }
31
+ }
32
+ }
33
+ catch (e) {
34
+ setError(typeof e === "string" ? e : String(e));
35
+ }
36
+ finally {
37
+ setIsLoading(false);
38
+ }
39
+ }, [selectedPrinter]);
40
+ (0, react_1.useEffect)(() => {
41
+ fetchPrinters();
42
+ }, []);
43
+ (0, react_1.useEffect)(() => {
44
+ if (!selectedPrinter) {
45
+ setStatus(null);
46
+ return;
47
+ }
48
+ let cancelled = false;
49
+ (0, commands_js_1.getPrinterStatus)(selectedPrinter)
50
+ .then((info) => {
51
+ if (!cancelled)
52
+ setStatus(info);
53
+ })
54
+ .catch(() => {
55
+ if (!cancelled)
56
+ setStatus(null);
57
+ });
58
+ return () => {
59
+ cancelled = true;
60
+ };
61
+ }, [selectedPrinter]);
62
+ const selectPrinter = (0, react_1.useCallback)((name) => {
63
+ setSelectedPrinter(name);
64
+ localStorage.setItem(STORAGE_KEY, name);
65
+ }, []);
66
+ const print = (0, react_1.useCallback)(async (content) => {
67
+ if (!selectedPrinter) {
68
+ throw new Error("No printer selected. Call selectPrinter() first.");
69
+ }
70
+ setError(null);
71
+ try {
72
+ await (0, commands_js_1.printRaw)(selectedPrinter, content);
73
+ }
74
+ catch (e) {
75
+ const msg = typeof e === "string" ? e : String(e);
76
+ setError(msg);
77
+ throw new Error(msg);
78
+ }
79
+ }, [selectedPrinter]);
80
+ const printTo = (0, react_1.useCallback)(async (printer, content) => {
81
+ setError(null);
82
+ try {
83
+ await (0, commands_js_1.printRaw)(printer, content);
84
+ }
85
+ catch (e) {
86
+ const msg = typeof e === "string" ? e : String(e);
87
+ setError(msg);
88
+ throw new Error(msg);
89
+ }
90
+ }, []);
91
+ const refresh = (0, react_1.useCallback)(async () => {
92
+ await fetchPrinters();
93
+ if (selectedPrinter) {
94
+ try {
95
+ const info = await (0, commands_js_1.getPrinterStatus)(selectedPrinter);
96
+ setStatus(info);
97
+ }
98
+ catch {
99
+ setStatus(null);
100
+ }
101
+ }
102
+ }, [fetchPrinters, selectedPrinter]);
103
+ return {
104
+ printers,
105
+ selectedPrinter,
106
+ selectPrinter,
107
+ print,
108
+ printTo,
109
+ status,
110
+ isLoading,
111
+ error,
112
+ refresh,
113
+ };
114
+ }
@@ -0,0 +1,33 @@
1
+ export declare const ESC = "\u001B";
2
+ export declare const GS = "\u001D";
3
+ export declare const RESET = "\u001B@";
4
+ export declare const CUT_PARTIAL = "\u001DV\u0001";
5
+ export declare const CUT_FULL = "\u001DV\0";
6
+ export declare const FORM_FEED = "\f";
7
+ export declare const BOLD_ON = "\u001BE\u0001";
8
+ export declare const BOLD_OFF = "\u001BE\0";
9
+ export declare const UNDERLINE_ON = "\u001B-\u0001";
10
+ export declare const UNDERLINE_OFF = "\u001B-\0";
11
+ export declare const DOUBLE_HEIGHT_ON = "\u001D!\u0001";
12
+ export declare const DOUBLE_WIDTH_ON = "\u001D!\u0010";
13
+ export declare const DOUBLE_SIZE_ON = "\u001D!\u0011";
14
+ export declare const SIZE_NORMAL = "\u001D!\0";
15
+ export declare const ALIGN_LEFT = "\u001Ba\0";
16
+ export declare const ALIGN_CENTER = "\u001Ba\u0001";
17
+ export declare const ALIGN_RIGHT = "\u001Ba\u0002";
18
+ export declare function feedLines(n: number): string;
19
+ export declare const LINE_SPACING_DEFAULT = "\u001B2";
20
+ export declare function lineSpacing(n: number): string;
21
+ export declare const PAPER_WIDTH: {
22
+ readonly "58mm": 32;
23
+ readonly "76mm": 42;
24
+ readonly "80mm": 48;
25
+ readonly LX310: 80;
26
+ };
27
+ export type PaperSize = keyof typeof PAPER_WIDTH;
28
+ export declare function divider(width?: number, char?: string): string;
29
+ export declare function centerText(text: string, width: number): string;
30
+ export declare function rowLR(left: string, right: string, width?: number): string;
31
+ export declare function row2Col(col1: string, col2: string, width?: number): string;
32
+ export declare function wrapReceipt(content: string, feedCount?: number): string;
33
+ export declare function wrapDotMatrix(content: string): string;
package/dist/utils.js ADDED
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PAPER_WIDTH = exports.LINE_SPACING_DEFAULT = exports.ALIGN_RIGHT = exports.ALIGN_CENTER = exports.ALIGN_LEFT = exports.SIZE_NORMAL = exports.DOUBLE_SIZE_ON = exports.DOUBLE_WIDTH_ON = exports.DOUBLE_HEIGHT_ON = exports.UNDERLINE_OFF = exports.UNDERLINE_ON = exports.BOLD_OFF = exports.BOLD_ON = exports.FORM_FEED = exports.CUT_FULL = exports.CUT_PARTIAL = exports.RESET = exports.GS = exports.ESC = void 0;
4
+ exports.feedLines = feedLines;
5
+ exports.lineSpacing = lineSpacing;
6
+ exports.divider = divider;
7
+ exports.centerText = centerText;
8
+ exports.rowLR = rowLR;
9
+ exports.row2Col = row2Col;
10
+ exports.wrapReceipt = wrapReceipt;
11
+ exports.wrapDotMatrix = wrapDotMatrix;
12
+ exports.ESC = "\x1B";
13
+ exports.GS = "\x1D";
14
+ exports.RESET = `${exports.ESC}@`;
15
+ exports.CUT_PARTIAL = `${exports.GS}V\x01`;
16
+ exports.CUT_FULL = `${exports.GS}V\x00`;
17
+ exports.FORM_FEED = "\f";
18
+ exports.BOLD_ON = `${exports.ESC}E\x01`;
19
+ exports.BOLD_OFF = `${exports.ESC}E\x00`;
20
+ exports.UNDERLINE_ON = `${exports.ESC}-\x01`;
21
+ exports.UNDERLINE_OFF = `${exports.ESC}-\x00`;
22
+ exports.DOUBLE_HEIGHT_ON = `${exports.GS}!\x01`;
23
+ exports.DOUBLE_WIDTH_ON = `${exports.GS}!\x10`;
24
+ exports.DOUBLE_SIZE_ON = `${exports.GS}!\x11`;
25
+ exports.SIZE_NORMAL = `${exports.GS}!\x00`;
26
+ exports.ALIGN_LEFT = `${exports.ESC}a\x00`;
27
+ exports.ALIGN_CENTER = `${exports.ESC}a\x01`;
28
+ exports.ALIGN_RIGHT = `${exports.ESC}a\x02`;
29
+ function feedLines(n) {
30
+ return `${exports.ESC}d${String.fromCharCode(Math.min(255, Math.max(0, n)))}`;
31
+ }
32
+ exports.LINE_SPACING_DEFAULT = `${exports.ESC}2`;
33
+ function lineSpacing(n) {
34
+ return `${exports.ESC}3${String.fromCharCode(Math.min(255, Math.max(0, n)))}`;
35
+ }
36
+ exports.PAPER_WIDTH = {
37
+ "58mm": 32,
38
+ "76mm": 42,
39
+ "80mm": 48,
40
+ LX310: 80,
41
+ };
42
+ function divider(width = 48, char = "-") {
43
+ return char.repeat(width);
44
+ }
45
+ function centerText(text, width) {
46
+ if (text.length >= width)
47
+ return text.slice(0, width);
48
+ const left = Math.floor((width - text.length) / 2);
49
+ return " ".repeat(left) + text;
50
+ }
51
+ function rowLR(left, right, width = 48) {
52
+ const space = width - left.length - right.length;
53
+ return left + " ".repeat(Math.max(1, space)) + right;
54
+ }
55
+ function row2Col(col1, col2, width = 48) {
56
+ const w = Math.floor(width / 2);
57
+ return col1.padEnd(w) + col2.padEnd(w);
58
+ }
59
+ function wrapReceipt(content, feedCount = 3) {
60
+ return exports.RESET + content + feedLines(feedCount) + exports.CUT_PARTIAL;
61
+ }
62
+ function wrapDotMatrix(content) {
63
+ return "\r\n" + content + "\r\n" + exports.FORM_FEED;
64
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@skalfa/skalfa-printer",
3
+ "version": "1.0.0",
4
+ "description": "Thermal printer bridge for Skalfa Framework.",
5
+ "license": "MIT",
6
+ "keywords": ["skalfa", "skalfa-app", "skalfa-printer", "next-framework", "next-printer-thermal"],
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "files": [
10
+ "dist",
11
+ "src-rust",
12
+ "!src-rust/target"
13
+ ],
14
+ "scripts": {
15
+ "build": "tsc -p tsconfig.build.json"
16
+ },
17
+ "peerDependencies": {
18
+ "@skalfa/skalfa-app-core": "*",
19
+ "@tauri-apps/api": "^2.0.0",
20
+ "react": "^19.0.0"
21
+ },
22
+ "devDependencies": {
23
+ "@skalfa/skalfa-app-core": "*",
24
+ "@tauri-apps/api": "^2.0.0",
25
+ "@types/react": "^19.0.0",
26
+ "react": "^19.2.1",
27
+ "typescript": "^6.0.3"
28
+ }
29
+ }