taro-bluetooth-print 2.2.1 → 2.3.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +128 -14
  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 +0 -1
  15. package/dist/types/drivers/EscPos.d.ts +0 -1
  16. package/dist/types/drivers/TsplDriver.d.ts +251 -0
  17. package/dist/types/index.d.ts +5 -0
  18. package/dist/types/plugins/PluginManager.d.ts +87 -0
  19. package/dist/types/plugins/builtin/LoggingPlugin.d.ts +14 -0
  20. package/dist/types/plugins/builtin/RetryPlugin.d.ts +18 -0
  21. package/dist/types/plugins/index.d.ts +7 -0
  22. package/dist/types/plugins/types.d.ts +97 -0
  23. package/dist/types/services/CommandBuilder.d.ts +0 -1
  24. package/dist/types/services/ConnectionManager.d.ts +0 -1
  25. package/dist/types/services/PrintJobManager.d.ts +0 -1
  26. package/dist/types/services/interfaces/index.d.ts +0 -1
  27. package/dist/types/template/TemplateEngine.d.ts +0 -1
  28. package/package.json +15 -17
  29. package/src/drivers/TsplDriver.ts +417 -0
  30. package/src/index.ts +14 -0
  31. package/src/plugins/PluginManager.ts +193 -0
  32. package/src/plugins/builtin/LoggingPlugin.ts +97 -0
  33. package/src/plugins/builtin/RetryPlugin.ts +109 -0
  34. package/src/plugins/index.ts +10 -0
  35. package/src/plugins/types.ts +119 -0
package/CHANGELOG.md CHANGED
@@ -5,6 +5,44 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [2.3.0] - 2026-02-11
9
+
10
+ ### Added
11
+
12
+ - **插件系统 (Plugin System)** - 可扩展的插件架构
13
+ - `PluginManager` 管理插件生命周期
14
+ - 支持 beforeConnect/afterConnect/beforePrint/afterPrint 等钩子
15
+ - 内置 `LoggingPlugin` 详细日志记录
16
+ - 内置 `RetryPlugin` 自动重试与指数退避
17
+
18
+ - **TSPL 标签打印驱动 (TsplDriver)** - TSC 打印机语言支持
19
+ - 标签尺寸、间隙、速度、密度设置
20
+ - 文本打印(多字体、旋转、缩放)
21
+ - 条码支持(Code128、Code39、EAN-13/8、UPC-A)
22
+ - 二维码支持
23
+ - 图形绘制(矩形、线条、反白)
24
+
25
+ ### Changed
26
+
27
+ - **升级 Taro 至 v4.x** - 从 3.6.x 升级到 4.1.11
28
+ - **升级 Vite 至 v7.x** - 从 5.x 升级到 7.3.1
29
+ - **测试框架迁移至 Vitest** - 替换 Jest,测试速度提升 ~6x
30
+ - **升级其他依赖**:
31
+ - vite-plugin-dts: 3.9.1 → 4.5.4
32
+ - prettier: 3.2.4 → 3.8.1
33
+ - rimraf: 5.0.0 → 6.1.2
34
+ - terser: 5.24.0 → 5.46.0
35
+
36
+ ### Fixed
37
+
38
+ - 修复 TypeScript 测试配置兼容性问题
39
+ - 修复 breakpoint.test.ts 中的类型错误
40
+ - 修复 package.json exports 条件顺序警告
41
+
42
+ ### Removed
43
+
44
+ - 移除 Jest 相关依赖 (jest, ts-jest, @types/jest, jest-environment-jsdom)
45
+
8
46
  ## [2.2.0] - 2026-01-04
9
47
 
10
48
  ### Added
package/README.md CHANGED
@@ -17,7 +17,7 @@
17
17
  ## ✨ 特性
18
18
 
19
19
  - 🚀 **高性能** - 直接字节缓冲区操作,服务缓存优化
20
- - 📱 **跨平台** - 基于 Taro 蓝牙 API,支持微信小程序、H5、React Native 等
20
+ - 📱 **跨平台** - 基于 Taro 4.x 蓝牙 API,支持微信小程序、H5、React Native 等
21
21
  - 🎨 **简洁 API** - 链式调用,易于使用
22
22
  - 🖼️ **图片打印** - 内置 Floyd-Steinberg 抖动算法,高质量图片转换
23
23
  - 📲 **二维码支持** - 原生 ESC/POS 二维码指令
@@ -31,8 +31,10 @@
31
31
  - 🔍 **打印预览** - ESC/POS 命令渲染为图像预览
32
32
  - 🌐 **Web Bluetooth** - 支持 H5 环境的 Web Bluetooth API
33
33
  - 💓 **连接稳定** - 心跳检测和自动重连机制
34
+ - 🏷️ **标签打印** - TSPL 协议支持,适用于 TSC 等标签打印机 (v2.3+)
35
+ - 🔌 **插件系统** - 可扩展架构,支持自定义钩子 (v2.3+)
34
36
  - 🛠️ **TypeScript** - 完整的类型定义和 JSDoc 文档
35
- - 🧪 **高测试覆盖** - 76%+ 代码覆盖率
37
+ - 🧪 **高测试覆盖** - Vitest 驱动,69+ 测试用例
36
38
 
37
39
  ## 📦 安装
38
40
 
@@ -246,6 +248,85 @@ await cache.save({ id: 'job-1', data: printData });
246
248
  await cache.sync();
247
249
  ```
248
250
 
251
+ ### TSPL 标签打印 (v2.3+)
252
+
253
+ ```typescript
254
+ import { TsplDriver } from 'taro-bluetooth-print';
255
+
256
+ const tspl = new TsplDriver();
257
+
258
+ // 生成标签打印指令
259
+ const buffer = tspl
260
+ .size(60, 40) // 标签尺寸 60x40mm
261
+ .gap(3) // 标签间隙 3mm
262
+ .clear() // 清除缓冲区
263
+ .text('商品名称', { x: 50, y: 30, font: 3 })
264
+ .text('¥99.00', { x: 50, y: 80, font: 4, xMultiplier: 2 })
265
+ .barcode('6901234567890', {
266
+ x: 50,
267
+ y: 120,
268
+ type: 'EAN13',
269
+ height: 60
270
+ })
271
+ .qrcode('https://example.com', {
272
+ x: 300,
273
+ y: 30,
274
+ cellWidth: 4
275
+ })
276
+ .print(1) // 打印1份
277
+ .getBuffer();
278
+
279
+ // 发送到打印机
280
+ await printer.connect(deviceId);
281
+ await printer.setOptions({ chunkSize: 100 });
282
+ // 直接发送 TSPL 指令需要自定义适配器
283
+ ```
284
+
285
+ ### 插件系统 (v2.3+)
286
+
287
+ ```typescript
288
+ import {
289
+ PluginManager,
290
+ createLoggingPlugin,
291
+ createRetryPlugin
292
+ } from 'taro-bluetooth-print';
293
+
294
+ // 创建插件管理器
295
+ const plugins = new PluginManager();
296
+
297
+ // 注册日志插件
298
+ await plugins.register(createLoggingPlugin({
299
+ logProgress: true, // 记录进度
300
+ }));
301
+
302
+ // 注册重试插件
303
+ await plugins.register(createRetryPlugin({
304
+ maxRetries: 5,
305
+ initialDelay: 1000,
306
+ }));
307
+
308
+ // 自定义插件
309
+ const myPlugin = {
310
+ name: 'my-plugin',
311
+ hooks: {
312
+ beforePrint: (buffer) => {
313
+ console.log(`即将打印 ${buffer.length} 字节`);
314
+ return buffer; // 可以修改数据
315
+ },
316
+ afterPrint: (bytesSent) => {
317
+ console.log(`打印完成: ${bytesSent} 字节`);
318
+ },
319
+ onError: (error) => {
320
+ // 上报错误
321
+ reportError(error);
322
+ return false; // false = 不抑制错误
323
+ },
324
+ },
325
+ };
326
+
327
+ await plugins.register(myPlugin);
328
+ ```
329
+
249
330
  ## 📚 文档
250
331
 
251
332
  完整文档请访问:**[https://agions.github.io/taro-bluetooth-print/](https://agions.github.io/taro-bluetooth-print/)**
@@ -283,6 +364,35 @@ await cache.sync();
283
364
  | `cancel()` | 取消打印 | `void` |
284
365
  | `remaining()` | 获取剩余字节数 | `number` |
285
366
 
367
+ ### TsplDriver (v2.3+)
368
+
369
+ | 方法 | 说明 | 返回值 |
370
+ | ------------------------------ | ------------------ | ------------ |
371
+ | `size(width, height)` | 设置标签尺寸(mm) | `this` |
372
+ | `gap(gap, offset?)` | 设置标签间隙 | `this` |
373
+ | `speed(speed)` | 设置打印速度(1-10) | `this` |
374
+ | `density(density)` | 设置打印浓度(0-15) | `this` |
375
+ | `clear()` | 清除图像缓冲区 | `this` |
376
+ | `text(content, options)` | 添加文本 | `this` |
377
+ | `barcode(content, options)` | 添加条码 | `this` |
378
+ | `qrcode(content, options)` | 添加二维码 | `this` |
379
+ | `box(options)` | 绘制矩形框 | `this` |
380
+ | `line(options)` | 绘制线条 | `this` |
381
+ | `print(copies?, sets?)` | 打印 | `this` |
382
+ | `getBuffer()` | 获取指令缓冲区 | `Uint8Array` |
383
+ | `getCommands()` | 获取指令字符串 | `string` |
384
+
385
+ ### PluginManager (v2.3+)
386
+
387
+ | 方法 | 说明 | 返回值 |
388
+ | --------------------------- | -------------- | --------------- |
389
+ | `register(plugin, options)` | 注册插件 | `Promise<void>` |
390
+ | `unregister(name)` | 注销插件 | `Promise<void>` |
391
+ | `get(name)` | 获取插件 | `Plugin` |
392
+ | `has(name)` | 检查插件是否存在 | `boolean` |
393
+ | `getNames()` | 获取所有插件名 | `string[]` |
394
+ | `clear()` | 清除所有插件 | `Promise<void>` |
395
+
286
396
  ### 事件
287
397
 
288
398
  | 事件名 | 数据类型 | 说明 |
@@ -324,18 +434,22 @@ interface IQrOptions {
324
434
  ## 🏗️ 架构设计
325
435
 
326
436
  ```
327
- ┌─────────────────────────────────────┐
328
- BluetoothPrinter (Core)
329
- │ - 连接管理
330
- │ - 打印队列 │
331
- - 事件系统
332
- │ - 断点续传 │
333
- └────────┬────────────────┬───────────┘
334
-
335
- ┌────▼─────┐ ┌────▼──────┐
336
- Adapter │ Driver │
337
- │ 层 │ │ 层 │
338
- └──────────┘ └───────────┘
437
+ ┌─────────────────────────────────────────────────┐
438
+ BluetoothPrinter (Core)
439
+ │ - 连接管理 - 打印队列 - 事件系统 - 断点续传
440
+ └──────────┬──────────────────┬───────────────────┘
441
+
442
+ ┌─────▼──────┐ ┌────▼──────┐
443
+ │ Adapter │ │ Driver │
444
+ │ 层 │
445
+ └────────────┘ └───────────┘
446
+
447
+ ┌─────▼──────┐ ┌────▼──────┐ ┌────────────┐
448
+ │ Taro │ │ ESC/POS │ │ Plugin
449
+ │ Web BT │ │ TSPL │ │ System │
450
+ │ Alipay... │ │ (ZPL...) │ │ (v2.3+) │
451
+ └────────────┘ └───────────┘ └────────────┘
452
+ ```
339
453
  │ │
340
454
  ┌────▼─────┐ ┌────▼──────┐
341
455
  │Taro │ │ESC/POS │