taro-bluetooth-print 2.2.1 → 2.3.1

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +57 -165
  2. package/README.md +142 -285
  3. package/dist/index.cjs.js +1 -1
  4. package/dist/index.es.js +1 -81644
  5. package/dist/index.umd.js +1 -1
  6. package/dist/types/adapters/AdapterFactory.d.ts +0 -1
  7. package/dist/types/adapters/AlipayAdapter.d.ts +0 -1
  8. package/dist/types/adapters/BaiduAdapter.d.ts +0 -1
  9. package/dist/types/adapters/BaseAdapter.d.ts +0 -1
  10. package/dist/types/adapters/ByteDanceAdapter.d.ts +0 -1
  11. package/dist/types/adapters/TaroAdapter.d.ts +0 -1
  12. package/dist/types/adapters/WebBluetoothAdapter.d.ts +0 -1
  13. package/dist/types/config/PrinterConfig.d.ts +0 -1
  14. package/dist/types/core/BluetoothPrinter.d.ts +1 -2
  15. package/dist/types/core/EventEmitter.d.ts +6 -26
  16. package/dist/types/core/index.d.ts +6 -0
  17. package/dist/types/drivers/CpclDriver.d.ts +304 -0
  18. package/dist/types/drivers/EscPos.d.ts +0 -1
  19. package/dist/types/drivers/GPrinterDriver.d.ts +63 -0
  20. package/dist/types/drivers/TsplDriver.d.ts +251 -0
  21. package/dist/types/drivers/ZplDriver.d.ts +325 -0
  22. package/dist/types/drivers/index.d.ts +9 -0
  23. package/dist/types/encoding/gbk-lite.d.ts +8 -0
  24. package/dist/types/encoding/gbk-table.d.ts +8 -30
  25. package/dist/types/index.d.ts +10 -5
  26. package/dist/types/plugins/PluginManager.d.ts +87 -0
  27. package/dist/types/plugins/builtin/LoggingPlugin.d.ts +14 -0
  28. package/dist/types/plugins/builtin/RetryPlugin.d.ts +18 -0
  29. package/dist/types/plugins/index.d.ts +7 -0
  30. package/dist/types/plugins/types.d.ts +97 -0
  31. package/dist/types/services/CommandBuilder.d.ts +0 -1
  32. package/dist/types/services/ConnectionManager.d.ts +1 -2
  33. package/dist/types/services/PrintJobManager.d.ts +0 -1
  34. package/dist/types/services/index.d.ts +8 -0
  35. package/dist/types/services/interfaces/index.d.ts +0 -1
  36. package/dist/types/template/TemplateEngine.d.ts +0 -1
  37. package/package.json +36 -20
  38. package/src/adapters/BaseAdapter.ts +6 -8
  39. package/src/core/BluetoothPrinter.ts +15 -15
  40. package/src/core/EventEmitter.ts +15 -15
  41. package/src/core/index.ts +7 -0
  42. package/src/drivers/CpclDriver.ts +549 -0
  43. package/src/drivers/GPrinterDriver.ts +115 -0
  44. package/src/drivers/TsplDriver.ts +405 -0
  45. package/src/drivers/ZplDriver.ts +543 -0
  46. package/src/drivers/index.ts +37 -0
  47. package/src/encoding/gbk-lite.ts +108 -0
  48. package/src/encoding/gbk-table.ts +80 -58
  49. package/src/index.ts +27 -23
  50. package/src/plugins/PluginManager.ts +195 -0
  51. package/src/plugins/builtin/LoggingPlugin.ts +99 -0
  52. package/src/plugins/builtin/RetryPlugin.ts +103 -0
  53. package/src/plugins/index.ts +10 -0
  54. package/src/plugins/types.ts +119 -0
  55. package/src/services/ConnectionManager.ts +22 -22
  56. package/src/services/index.ts +16 -0
@@ -1,6 +1,5 @@
1
1
  import { IPrinterAdapter } from '../types';
2
2
  import { PlatformType } from '../utils/platform';
3
-
4
3
  /**
5
4
  * Adapter factory class
6
5
  */
@@ -1,5 +1,4 @@
1
1
  import { MiniProgramAdapter, MiniProgramBLEApi } from './BaseAdapter';
2
-
3
2
  /**
4
3
  * Alipay Bluetooth Low Energy adapter
5
4
  *
@@ -1,5 +1,4 @@
1
1
  import { MiniProgramAdapter, MiniProgramBLEApi } from './BaseAdapter';
2
-
3
2
  /**
4
3
  * Baidu Bluetooth Low Energy adapter
5
4
  *
@@ -1,5 +1,4 @@
1
1
  import { IPrinterAdapter, IAdapterOptions, PrinterState } from '../types';
2
-
3
2
  /**
4
3
  * Service information cache entry
5
4
  */
@@ -1,5 +1,4 @@
1
1
  import { MiniProgramAdapter, MiniProgramBLEApi } from './BaseAdapter';
2
-
3
2
  /**
4
3
  * ByteDance Bluetooth Low Energy adapter
5
4
  *
@@ -1,5 +1,4 @@
1
1
  import { MiniProgramAdapter, MiniProgramBLEApi } from './BaseAdapter';
2
-
3
2
  /**
4
3
  * Taro Bluetooth Low Energy adapter
5
4
  *
@@ -1,6 +1,5 @@
1
1
  import { IAdapterOptions } from '../types';
2
2
  import { BaseAdapter } from './BaseAdapter';
3
-
4
3
  /**
5
4
  * Request device options for Web Bluetooth
6
5
  */
@@ -1,5 +1,4 @@
1
1
  import { LogLevel } from '../utils/logger';
2
-
3
2
  /**
4
3
  * Adapter configuration options
5
4
  */
@@ -2,7 +2,6 @@ import { IAdapterOptions, IQrOptions, PrinterState, IPrinterAdapter } from '../t
2
2
  import { EventEmitter } from './EventEmitter';
3
3
  import { BluetoothPrintError } from '../errors/BluetoothError';
4
4
  import { IConnectionManager, IPrintJobManager, ICommandBuilder } from '../services/interfaces';
5
-
6
5
  /**
7
6
  * Event types emitted by BluetoothPrinter
8
7
  */
@@ -51,7 +50,7 @@ export interface PrinterEvents {
51
50
  * ```
52
51
  */
53
52
  export declare class BluetoothPrinter extends EventEmitter<PrinterEvents> {
54
- private readonly logger;
53
+ private readonly printerLogger;
55
54
  /** Current printer state */
56
55
  state: PrinterState;
57
56
  /** Connection manager instance */
@@ -1,32 +1,12 @@
1
- /**
2
- * Type-safe event emitter for pub/sub pattern
3
- *
4
- * @template T - Event map type with event names as keys and payload types as values
5
- *
6
- * @example
7
- * ```typescript
8
- * interface MyEvents {
9
- * 'connected': { deviceId: string };
10
- * 'error': Error;
11
- * 'progress': { current: number; total: number };
12
- * 'ready': void;
13
- * }
14
- *
15
- * class MyClass extends EventEmitter<MyEvents> {
16
- * connect() {
17
- * this.emit('connected', { deviceId: '123' });
18
- * }
19
- * }
20
- *
21
- * const instance = new MyClass();
22
- * const unsubscribe = instance.on('connected', (data) => {
23
- * console.log('Connected to:', data.deviceId);
24
- * });
25
- * ```
26
- */
27
1
  export declare class EventEmitter<T> {
28
2
  private listeners;
29
3
  private debugMode;
4
+ protected readonly logger: {
5
+ debug: (message: string, ...args: unknown[]) => void;
6
+ info: (message: string, ...args: unknown[]) => void;
7
+ warn: (message: string, ...args: unknown[]) => void;
8
+ error: (message: string, ...args: unknown[]) => void;
9
+ };
30
10
  /**
31
11
  * Subscribe to an event
32
12
  *
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Core Module - 核心模块
3
+ */
4
+ export { BluetoothPrinter } from './BluetoothPrinter';
5
+ export type { PrinterEvents } from './BluetoothPrinter';
6
+ export { EventEmitter } from './EventEmitter';
@@ -0,0 +1,304 @@
1
+ /**
2
+ * CPCL Driver
3
+ * PCL commands for Compact Print Language - HP/Honeywell/Toddler printers
4
+ *
5
+ * CPCL is commonly used in mobile/portable thermal printers
6
+ */
7
+ /**
8
+ * CPCL page size presets
9
+ */
10
+ export type CPCLPageSize = 'A4' | 'A5' | 'LETTER' | '4X6' | '4X2' | '4X4' | '2.25X1.25' | 'CUSTOM';
11
+ /**
12
+ * CPCL Text options
13
+ */
14
+ export interface CpclTextOptions {
15
+ /** X position in dots */
16
+ x?: number;
17
+ /** Y position in dots */
18
+ y?: number;
19
+ /** Font type: 0-6 (0=OCR, 1=OCR-B, 2=CG Triumvirate, etc.) */
20
+ font?: number;
21
+ /** Horizontal multiplier (1-8) */
22
+ xMulti?: number;
23
+ /** Vertical multiplier (1-8) */
24
+ yMulti?: number;
25
+ /** Rotation: 0, 90, 180, 270 */
26
+ rotation?: 0 | 90 | 180 | 270;
27
+ }
28
+ /**
29
+ * CPCL Barcode options
30
+ */
31
+ export interface CpclBarcodeOptions {
32
+ /** X position in dots */
33
+ x?: number;
34
+ /** Y position in dots */
35
+ y?: number;
36
+ /** Barcode type */
37
+ type: '128' | '39' | 'EAN13' | 'EAN8' | 'UPCA' | 'UPCE' | 'MSI' | 'PLESSEY' | 'PDF417' | 'DATAMATRIX' | 'QR';
38
+ /** Barcode height in dots (default: 50) */
39
+ height?: number;
40
+ /** Wide bar width for 39 (default: 2) */
41
+ wide?: number;
42
+ /** Narrow bar width (default: 1) */
43
+ narrow?: number;
44
+ /** Show human-readable text (default: true) */
45
+ readable?: boolean;
46
+ }
47
+ /**
48
+ * CPCL QR Code options
49
+ */
50
+ export interface CpclQRCodeOptions {
51
+ /** X position in dots */
52
+ x?: number;
53
+ /** Y position in dots */
54
+ y?: number;
55
+ /** Model (default: 2) */
56
+ model?: 1 | 2;
57
+ /** Error correction level: L, M, Q, H */
58
+ errorCorrection?: 'L' | 'M' | 'Q' | 'H';
59
+ /** Cell size (default: 4) */
60
+ cellSize?: number;
61
+ }
62
+ /**
63
+ * CPCL Line options
64
+ */
65
+ export interface CpclLineOptions {
66
+ /** Start X position */
67
+ x1: number;
68
+ /** Start Y position */
69
+ y1: number;
70
+ /** End X position */
71
+ x2: number;
72
+ /** End Y position */
73
+ y2: number;
74
+ /** Line width/thickness */
75
+ width?: number;
76
+ }
77
+ /**
78
+ * CPCL Box options
79
+ */
80
+ export interface CpclBoxOptions {
81
+ /** X position */
82
+ x: number;
83
+ /** Y position */
84
+ y: number;
85
+ /** Width */
86
+ width: number;
87
+ /** Height */
88
+ height: number;
89
+ /** Border thickness */
90
+ thickness?: number;
91
+ }
92
+ /**
93
+ * CPCL Driver for HP, Honeywell, and Toddler portable printers
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * const cpcl = new CpclDriver();
98
+ *
99
+ * const commands = cpcl
100
+ * .pageStart()
101
+ * .text('Hello World!', { x: 50, y: 50 })
102
+ * .barcode('1234567890', { x: 50, y: 150, type: '128' })
103
+ * .qrcode('https://example.com', { x: 300, y: 50 })
104
+ * .pageEnd()
105
+ * .getCommands();
106
+ * ```
107
+ */
108
+ export declare class CpclDriver {
109
+ private commands;
110
+ private readonly logger;
111
+ private pageWidth;
112
+ private pageHeight;
113
+ /**
114
+ * Initialize CPCL driver
115
+ * @param width - Page width in dots
116
+ * @param height - Page height in dots (0 = continuous)
117
+ */
118
+ constructor(width?: number, height?: number);
119
+ /**
120
+ * Set page size
121
+ * @param width - Width in dots
122
+ * @param height - Height in dots (0 = continuous)
123
+ */
124
+ setPageSize(width: number, height?: number): this;
125
+ /**
126
+ * Use standard page size
127
+ * @param size - Preset size name
128
+ */
129
+ usePageSize(size: CPCLPageSize): this;
130
+ /**
131
+ * Start a new page
132
+ */
133
+ pageStart(): this;
134
+ /**
135
+ * End current page
136
+ */
137
+ pageEnd(): this;
138
+ /**
139
+ * Form feed to next label
140
+ */
141
+ formFeed(): this;
142
+ /**
143
+ * Set line print mode
144
+ * @param font - Font number (0-6)
145
+ * @param xMulti - Horizontal multiplier
146
+ * @param yMulti - Vertical multiplier
147
+ */
148
+ setLinePrint(font?: number, xMulti?: number, yMulti?: number): this;
149
+ /**
150
+ * Set text font
151
+ * @param font - Font: 0=OCR, 1=OCR-B, 2=CG Triumvirate Bold, 3-6=Various sizes
152
+ * @param xMulti - Horizontal multiplier (1-8)
153
+ * @param yMulti - Vertical multiplier (1-8)
154
+ * @param rotation - Rotation angle
155
+ */
156
+ setFont(font?: number, xMulti?: number, yMulti?: number, rotation?: 0 | 90 | 180 | 270): this;
157
+ /**
158
+ * Add text at current position
159
+ * @param content - Text content
160
+ */
161
+ text(content: string): this;
162
+ /**
163
+ * Add positioned text
164
+ * @param content - Text content
165
+ * @param options - Text options
166
+ */
167
+ textAt(content: string, options: CpclTextOptions): this;
168
+ /**
169
+ * Add text using legacy command
170
+ * @param content - Text content
171
+ * @param x - X position
172
+ * @param y - Y position
173
+ * @param font - Font number
174
+ */
175
+ legacyText(content: string, x: number, y: number, font?: number): this;
176
+ /**
177
+ * Add barcode at position
178
+ * @param content - Barcode content
179
+ * @param options - Barcode options
180
+ */
181
+ barcode(content: string, options: CpclBarcodeOptions): this;
182
+ /**
183
+ * Add Code 128 barcode (most common)
184
+ * @param content - Barcode content
185
+ * @param x - X position
186
+ * @param y - Y position
187
+ * @param height - Barcode height
188
+ */
189
+ code128(content: string, x?: number, y?: number, height?: number): this;
190
+ /**
191
+ * Add Code 39 barcode
192
+ * @param content - Barcode content
193
+ * @param x - X position
194
+ * @param y - Y position
195
+ * @param height - Barcode height
196
+ */
197
+ code39(content: string, x?: number, y?: number, height?: number): this;
198
+ /**
199
+ * Add QR code
200
+ * @param content - QR code content
201
+ * @param options - QR options
202
+ */
203
+ qrcode(content: string, options?: CpclQRCodeOptions): this;
204
+ /**
205
+ * Add 2D barcode (PDF417 or DataMatrix)
206
+ * @param content - Content
207
+ * @param type - Type: PDF417 or DATAMATRIX
208
+ * @param x - X position
209
+ * @param y - Y position
210
+ * @param height - Height
211
+ */
212
+ twoDBarcode(content: string, type: 'PDF417' | 'DATAMATRIX', x?: number, y?: number, height?: number): this;
213
+ /**
214
+ * Add line
215
+ * @param options - Line options
216
+ */
217
+ line(options: CpclLineOptions): this;
218
+ /**
219
+ * Add box/rectangle
220
+ * @param options - Box options
221
+ */
222
+ box(options: CpclBoxOptions): this;
223
+ /**
224
+ * Add inverse area (white on black)
225
+ * @param x - X position
226
+ * @param y - Y position
227
+ * @param width - Width
228
+ * @param height - Height
229
+ */
230
+ inverse(x: number, y: number, width: number, height: number): this;
231
+ /**
232
+ * Set print density
233
+ * @param density - Density value (-50 to +50)
234
+ */
235
+ setDensity(density: number): this;
236
+ /**
237
+ * Set speed
238
+ * @param speed - Speed: 2-6 (2=50%, 3=75%, 4=100%, 5=125%, 6=150%)
239
+ */
240
+ setSpeed(speed: number): this;
241
+ /**
242
+ * Cut paper (if cutter installed)
243
+ */
244
+ cut(): this;
245
+ /**
246
+ * Partial cut
247
+ */
248
+ partialCut(): this;
249
+ /**
250
+ * Feed and cut
251
+ */
252
+ feedCut(): this;
253
+ /**
254
+ * Sound beep
255
+ * @param count - Number of beeps
256
+ * @param duration - Duration in 10ms units
257
+ */
258
+ beep(count?: number, duration?: number): this;
259
+ /**
260
+ * Add logo/image
261
+ * @param x - X position
262
+ * @param y - Y position
263
+ * @param logoName - Stored logo name
264
+ */
265
+ logo(x: number, y: number, logoName: string): this;
266
+ /**
267
+ * Download logo to printer (store in memory)
268
+ * Note: This is a placeholder for future implementation
269
+ * @param logoName - Name to store logo as
270
+ * @param _bitmap - Logo bitmap data (placeholder)
271
+ */
272
+ downloadLogo(logoName: string, _bitmap: Uint8Array): this;
273
+ /**
274
+ * Print stored logo
275
+ * @param logoName - Logo name
276
+ * @param x - X position
277
+ * @param y - Y position
278
+ */
279
+ printLogo(logoName: string, x?: number, y?: number): this;
280
+ /**
281
+ * Get all commands as string
282
+ */
283
+ getCommands(): string;
284
+ /**
285
+ * Get commands as buffer for sending to printer
286
+ */
287
+ getBuffer(): Uint8Array;
288
+ /**
289
+ * Get raw command list
290
+ */
291
+ getCommandList(): string[];
292
+ /**
293
+ * Clear all commands
294
+ */
295
+ reset(): this;
296
+ /**
297
+ * Get page width
298
+ */
299
+ getPageWidth(): number;
300
+ /**
301
+ * Escape special characters in text
302
+ */
303
+ private escapeText;
304
+ }
@@ -1,5 +1,4 @@
1
1
  import { IPrinterDriver, IQrOptions } from '../types';
2
-
3
2
  /**
4
3
  * ESC/POS driver options
5
4
  */
@@ -0,0 +1,63 @@
1
+ import { EscPos, EscPosOptions } from './EscPos';
2
+ /**
3
+ * GPrinter Driver for GiaoBao (佳博) thermal printers
4
+ *
5
+ * 佳博打印机基于 ESC/POS 指令集,此驱动扩展了标准 EscPos
6
+ * 添加了佳博特定的命令支持
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { GPrinterDriver } from 'taro-bluetooth-print';
11
+ *
12
+ * const driver = new GPrinterDriver();
13
+ * let commands = driver.init();
14
+ * commands = driver.text('Hello 佳博!', 'GBK');
15
+ * commands = driver.cut();
16
+ * ```
17
+ */
18
+ /**
19
+ * GPrinter options (same as EscPosOptions)
20
+ */
21
+ export type GPrinterOptions = EscPosOptions;
22
+ export declare class GPrinterDriver extends EscPos {
23
+ /**
24
+ * 佳博打印机特定: 打开钱箱
25
+ * @param pin 钱箱引脚 (0 或 1, 默认 0)
26
+ */
27
+ openCashDrawer(pin?: number): Uint8Array[];
28
+ /**
29
+ * 佳博打印机特定: 发送声响
30
+ * @param times 次数 (1-9)
31
+ * @param duration 持续时间
32
+ */
33
+ beep(times?: number, duration?: number): Uint8Array[];
34
+ /**
35
+ * 佳博打印机特定: 打印自检页
36
+ */
37
+ selfTest(): Uint8Array[];
38
+ /**
39
+ * 佳博打印机特定: 获取状态
40
+ * 返回打印机状态字节
41
+ */
42
+ getStatus(): Uint8Array[];
43
+ /**
44
+ * 佳博打印机特定: 设置字符代码页
45
+ * @param codePage 代码页编号
46
+ */
47
+ setCodePage(codePage: number): Uint8Array[];
48
+ /**
49
+ * 佳博打印机特定: 打印并走纸
50
+ * @param lines 走纸行数
51
+ */
52
+ printAndFeed(lines: number): Uint8Array[];
53
+ /**
54
+ * 佳博打印机特定: 设置左边界
55
+ * @param n 左边界字符数
56
+ */
57
+ setLeftMargin(n: number): Uint8Array[];
58
+ /**
59
+ * 佳博打印机特定: 设置打印区域宽度
60
+ * @param n 宽度 (字符数)
61
+ */
62
+ setPrintWidth(n: number): Uint8Array[];
63
+ }