taro-bluetooth-print 2.8.4 → 2.9.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/CHANGELOG.md +18 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/types/core/BluetoothPrinter.d.ts +21 -4
- package/dist/types/errors/CommandBuildError.d.ts +40 -0
- package/dist/types/errors/ConnectionError.d.ts +45 -0
- package/dist/types/errors/PrintJobError.d.ts +42 -0
- package/dist/types/errors/index.d.ts +9 -0
- package/dist/types/factory/PrinterFactory.d.ts +108 -0
- package/dist/types/factory/index.d.ts +6 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/services/interfaces/ICommandBuilder.d.ts +123 -0
- package/dist/types/services/interfaces/IConnectionManager.d.ts +49 -0
- package/dist/types/services/interfaces/IPrintJobManager.d.ts +67 -0
- package/dist/types/services/interfaces/index.d.ts +6 -233
- package/package.json +1 -1
- package/src/core/BluetoothPrinter.ts +47 -50
- package/src/errors/CommandBuildError.ts +72 -0
- package/src/errors/ConnectionError.ts +78 -0
- package/src/errors/PrintJobError.ts +75 -0
- package/src/errors/index.ts +10 -0
- package/src/factory/PrinterFactory.ts +139 -0
- package/src/factory/index.ts +12 -0
- package/src/index.ts +11 -0
- package/src/services/interfaces/ICommandBuilder.ts +145 -0
- package/src/services/interfaces/IConnectionManager.ts +58 -0
- package/src/services/interfaces/IPrintJobManager.ts +83 -0
- package/src/services/interfaces/index.ts +5 -265
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Printer Factory
|
|
3
|
+
*
|
|
4
|
+
* Factory for creating properly configured BluetoothPrinter instances.
|
|
5
|
+
* This is the recommended way to create printer instances - avoid direct
|
|
6
|
+
* constructor calls unless you need custom dependency injection.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { BluetoothPrinter } from '@/core/BluetoothPrinter';
|
|
10
|
+
import { ConnectionManager } from '@/services/ConnectionManager';
|
|
11
|
+
import { PrintJobManager } from '@/services/PrintJobManager';
|
|
12
|
+
import { CommandBuilder } from '@/services/CommandBuilder';
|
|
13
|
+
import type { IPrinterAdapter } from '@/types';
|
|
14
|
+
import type { IConnectionManager, IPrintJobManager, ICommandBuilder } from '@/services/interfaces';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Options for creating a BluetoothPrinter via the factory
|
|
18
|
+
*/
|
|
19
|
+
export interface PrinterFactoryOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Printer adapter to use for Bluetooth communication.
|
|
22
|
+
* Required if not providing custom connectionManager.
|
|
23
|
+
*
|
|
24
|
+
* Common adapters:
|
|
25
|
+
* - WebBluetoothAdapter (browser Web Bluetooth API)
|
|
26
|
+
* - TaroAdapter (Taro framework)
|
|
27
|
+
* - AlipayAdapter (Alipay mini-program)
|
|
28
|
+
* - ReactNativeAdapter (React Native)
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { WebBluetoothAdapter } from 'taro-bluetooth-print';
|
|
33
|
+
*
|
|
34
|
+
* const printer = createBluetoothPrinter({
|
|
35
|
+
* adapter: new WebBluetoothAdapter()
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
adapter?: IPrinterAdapter;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Custom connection manager instance.
|
|
43
|
+
* If provided, overrides the default ConnectionManager.
|
|
44
|
+
* Useful for testing or custom connection handling.
|
|
45
|
+
*/
|
|
46
|
+
connectionManager?: IConnectionManager;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Custom print job manager instance.
|
|
50
|
+
* If provided, overrides the default PrintJobManager.
|
|
51
|
+
* Useful for custom job scheduling or queue management.
|
|
52
|
+
*/
|
|
53
|
+
printJobManager?: IPrintJobManager;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Custom command builder instance.
|
|
57
|
+
* If provided, overrides the default CommandBuilder.
|
|
58
|
+
* Useful for custom command formatting or driver selection.
|
|
59
|
+
*/
|
|
60
|
+
commandBuilder?: ICommandBuilder;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Creates a new BluetoothPrinter instance with properly configured services.
|
|
65
|
+
*
|
|
66
|
+
* This is the recommended factory function for creating printer instances.
|
|
67
|
+
* It handles dependency injection and ensures all services are properly wired.
|
|
68
|
+
*
|
|
69
|
+
* @param options - Factory options for configuring the printer
|
|
70
|
+
* @returns A fully configured BluetoothPrinter instance
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // Basic usage with WebBluetooth
|
|
75
|
+
* import { createBluetoothPrinter, WebBluetoothAdapter } from 'taro-bluetooth-print';
|
|
76
|
+
*
|
|
77
|
+
* const printer = createBluetoothPrinter({
|
|
78
|
+
* adapter: new WebBluetoothAdapter()
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* await printer.connect('device-id');
|
|
82
|
+
* await printer.text('Hello').feed(2).cut().print();
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // Advanced usage with custom services
|
|
88
|
+
* import { createBluetoothPrinter, TaroAdapter } from 'taro-bluetooth-print';
|
|
89
|
+
*
|
|
90
|
+
* const printer = createBluetoothPrinter({
|
|
91
|
+
* adapter: new TaroAdapter(),
|
|
92
|
+
* printJobManager: customPrintJobManager,
|
|
93
|
+
* });
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export function createBluetoothPrinter(options: PrinterFactoryOptions = {}): BluetoothPrinter {
|
|
97
|
+
const { adapter, connectionManager, printJobManager, commandBuilder } = options;
|
|
98
|
+
|
|
99
|
+
// Build dependency chain: CommandBuilder -> PrintJobManager -> ConnectionManager -> Adapter
|
|
100
|
+
const finalConnectionManager = connectionManager ?? new ConnectionManager(adapter);
|
|
101
|
+
const finalPrintJobManager = printJobManager ?? new PrintJobManager(finalConnectionManager);
|
|
102
|
+
const finalCommandBuilder = commandBuilder ?? new CommandBuilder();
|
|
103
|
+
|
|
104
|
+
return new BluetoothPrinter(finalConnectionManager, finalPrintJobManager, finalCommandBuilder);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Creates a BluetoothPrinter instance for the Web Bluetooth API.
|
|
109
|
+
*
|
|
110
|
+
* This is a convenience function specifically for browser environments
|
|
111
|
+
* using the Web Bluetooth API.
|
|
112
|
+
*
|
|
113
|
+
* @param adapterOptions - Options to pass to the WebBluetoothAdapter
|
|
114
|
+
* @returns A BluetoothPrinter instance configured for Web Bluetooth
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* import { createWebBluetoothPrinter } from 'taro-bluetooth-print';
|
|
119
|
+
*
|
|
120
|
+
* const printer = createWebBluetoothPrinter();
|
|
121
|
+
* await printer.connect('device-id');
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export async function createWebBluetoothPrinter(): Promise<BluetoothPrinter> {
|
|
125
|
+
const { WebBluetoothAdapter } = await import('@/adapters/WebBluetoothAdapter');
|
|
126
|
+
const adapter = new WebBluetoothAdapter();
|
|
127
|
+
return createBluetoothPrinter({ adapter });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Default printer factory instance
|
|
132
|
+
*
|
|
133
|
+
* @deprecated Use the factory functions directly. This export exists
|
|
134
|
+
* for backward compatibility only.
|
|
135
|
+
*/
|
|
136
|
+
export const PrinterFactory = {
|
|
137
|
+
create: createBluetoothPrinter,
|
|
138
|
+
createWebBluetooth: createWebBluetoothPrinter,
|
|
139
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -87,6 +87,17 @@ export { PlatformType, detectPlatform, isPlatformSupported } from './utils/platf
|
|
|
87
87
|
|
|
88
88
|
// Error handling - 错误处理
|
|
89
89
|
export { BluetoothPrintError, ErrorCode } from './errors/BluetoothError';
|
|
90
|
+
export { ConnectionError, ConnectionErrorCode } from './errors/ConnectionError';
|
|
91
|
+
export { PrintJobError, PrintJobErrorCode } from './errors/PrintJobError';
|
|
92
|
+
export { CommandBuildError, CommandBuildErrorCode } from './errors/CommandBuildError';
|
|
93
|
+
|
|
94
|
+
// Factory - 工厂模式
|
|
95
|
+
export {
|
|
96
|
+
createBluetoothPrinter,
|
|
97
|
+
createWebBluetoothPrinter,
|
|
98
|
+
PrinterFactory,
|
|
99
|
+
type PrinterFactoryOptions,
|
|
100
|
+
} from './factory';
|
|
90
101
|
|
|
91
102
|
// Configuration - 配置
|
|
92
103
|
export { DEFAULT_CONFIG, mergeConfig } from './config/PrinterConfig';
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command Builder Interface
|
|
3
|
+
*
|
|
4
|
+
* Builds print commands using the printer driver
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { IQrOptions } from '@/types';
|
|
8
|
+
import { TextAlign, TextStyle } from '@/formatter';
|
|
9
|
+
import { BarcodeOptions } from '@/barcode';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Command Builder Interface
|
|
13
|
+
*
|
|
14
|
+
* Builds print commands using the printer driver
|
|
15
|
+
*/
|
|
16
|
+
export interface ICommandBuilder {
|
|
17
|
+
/**
|
|
18
|
+
* Adds text to the print queue
|
|
19
|
+
*
|
|
20
|
+
* @param content - Text content
|
|
21
|
+
* @param encoding - Text encoding
|
|
22
|
+
* @returns this - For method chaining
|
|
23
|
+
*/
|
|
24
|
+
text(content: string, encoding?: string): this;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Adds line feeds to the print queue
|
|
28
|
+
*
|
|
29
|
+
* @param lines - Number of lines to feed
|
|
30
|
+
* @returns this - For method chaining
|
|
31
|
+
*/
|
|
32
|
+
feed(lines?: number): this;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Adds a paper cut command to the print queue
|
|
36
|
+
*
|
|
37
|
+
* @returns this - For method chaining
|
|
38
|
+
*/
|
|
39
|
+
cut(): this;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Adds an image to the print queue
|
|
43
|
+
*
|
|
44
|
+
* @param data - Image data as Uint8Array
|
|
45
|
+
* @param width - Image width
|
|
46
|
+
* @param height - Image height
|
|
47
|
+
* @returns this - For method chaining
|
|
48
|
+
*/
|
|
49
|
+
image(data: Uint8Array, width: number, height: number): this;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Adds a QR code to the print queue
|
|
53
|
+
*
|
|
54
|
+
* @param content - QR code content
|
|
55
|
+
* @param options - QR code options
|
|
56
|
+
* @returns this - For method chaining
|
|
57
|
+
*/
|
|
58
|
+
qr(content: string, options?: IQrOptions): this;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Clears the print queue
|
|
62
|
+
*
|
|
63
|
+
* @returns this - For method chaining
|
|
64
|
+
*/
|
|
65
|
+
clear(): this;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Sets text alignment
|
|
69
|
+
*
|
|
70
|
+
* @param alignment - Text alignment (left, center, right)
|
|
71
|
+
* @returns this - For method chaining
|
|
72
|
+
*/
|
|
73
|
+
align(alignment: TextAlign): this;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Sets character size (width and height scale)
|
|
77
|
+
*
|
|
78
|
+
* @param width - Width scale factor (1-8)
|
|
79
|
+
* @param height - Height scale factor (1-8)
|
|
80
|
+
* @returns this - For method chaining
|
|
81
|
+
*/
|
|
82
|
+
setSize(width: number, height: number): this;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Sets bold text mode
|
|
86
|
+
*
|
|
87
|
+
* @param enabled - Enable or disable bold
|
|
88
|
+
* @returns this - For method chaining
|
|
89
|
+
*/
|
|
90
|
+
setBold(enabled: boolean): this;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Sets underline text mode
|
|
94
|
+
*
|
|
95
|
+
* @param enabled - Enable or disable underline
|
|
96
|
+
* @returns this - For method chaining
|
|
97
|
+
*/
|
|
98
|
+
setUnderline(enabled: boolean): this;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Sets inverse printing mode (white on black)
|
|
102
|
+
*
|
|
103
|
+
* @param enabled - Enable or disable inverse
|
|
104
|
+
* @returns this - For method chaining
|
|
105
|
+
*/
|
|
106
|
+
setInverse(enabled: boolean): this;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Sets multiple text style properties at once
|
|
110
|
+
*
|
|
111
|
+
* @param style - Text style configuration
|
|
112
|
+
* @returns this - For method chaining
|
|
113
|
+
*/
|
|
114
|
+
setStyle(style: TextStyle): this;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Resets all text formatting to default
|
|
118
|
+
*
|
|
119
|
+
* @returns this - For method chaining
|
|
120
|
+
*/
|
|
121
|
+
resetStyle(): this;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Adds a 1D barcode to the print queue
|
|
125
|
+
*
|
|
126
|
+
* @param content - Barcode content/data
|
|
127
|
+
* @param options - Barcode options
|
|
128
|
+
* @returns this - For method chaining
|
|
129
|
+
*/
|
|
130
|
+
barcode(content: string, options: BarcodeOptions): this;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Gets the current buffer
|
|
134
|
+
*
|
|
135
|
+
* @returns Uint8Array - Current print buffer
|
|
136
|
+
*/
|
|
137
|
+
getBuffer(): Uint8Array;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Gets the total number of bytes in the buffer
|
|
141
|
+
*
|
|
142
|
+
* @returns number - Total bytes
|
|
143
|
+
*/
|
|
144
|
+
getTotalBytes(): number;
|
|
145
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connection Manager Interface
|
|
3
|
+
*
|
|
4
|
+
* Manages Bluetooth device connections
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { PrinterState, IPrinterAdapter } from '@/types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Connection Manager Interface
|
|
11
|
+
*
|
|
12
|
+
* Manages Bluetooth device connections
|
|
13
|
+
*/
|
|
14
|
+
export interface IConnectionManager {
|
|
15
|
+
/**
|
|
16
|
+
* Connects to a Bluetooth device
|
|
17
|
+
*
|
|
18
|
+
* @param deviceId - Bluetooth device ID
|
|
19
|
+
* @param options - Connection options
|
|
20
|
+
* @returns Promise<void>
|
|
21
|
+
*/
|
|
22
|
+
connect(deviceId: string, options?: { retries?: number; timeout?: number }): Promise<void>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Disconnects from the current device
|
|
26
|
+
*
|
|
27
|
+
* @returns Promise<void>
|
|
28
|
+
*/
|
|
29
|
+
disconnect(): Promise<void>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Checks if a device is connected
|
|
33
|
+
*
|
|
34
|
+
* @returns boolean - True if connected, false otherwise
|
|
35
|
+
*/
|
|
36
|
+
isConnected(): boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Gets the current device ID
|
|
40
|
+
*
|
|
41
|
+
* @returns string | null - Device ID or null if not connected
|
|
42
|
+
*/
|
|
43
|
+
getDeviceId(): string | null;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Gets the current connection state
|
|
47
|
+
*
|
|
48
|
+
* @returns PrinterState - Current state
|
|
49
|
+
*/
|
|
50
|
+
getState(): PrinterState;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Gets the printer adapter instance
|
|
54
|
+
*
|
|
55
|
+
* @returns IPrinterAdapter - Printer adapter
|
|
56
|
+
*/
|
|
57
|
+
getAdapter(): IPrinterAdapter;
|
|
58
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Print Job Manager Interface
|
|
3
|
+
*
|
|
4
|
+
* Manages print jobs, including pause/resume/cancel functionality
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { IAdapterOptions } from '@/types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Print Job Manager Interface
|
|
11
|
+
*
|
|
12
|
+
* Manages print jobs, including pause/resume/cancel functionality
|
|
13
|
+
*/
|
|
14
|
+
export interface IPrintJobManager {
|
|
15
|
+
/**
|
|
16
|
+
* Starts a print job
|
|
17
|
+
*
|
|
18
|
+
* @param buffer - Print data buffer
|
|
19
|
+
* @returns Promise<void>
|
|
20
|
+
*/
|
|
21
|
+
start(buffer: Uint8Array, options?: { jobId?: string }): Promise<void>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Pauses the current print job
|
|
25
|
+
*/
|
|
26
|
+
pause(): void;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Resumes a paused print job
|
|
30
|
+
*
|
|
31
|
+
* @returns Promise<void>
|
|
32
|
+
*/
|
|
33
|
+
resume(jobId?: string): Promise<void>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Cancels the current print job
|
|
37
|
+
*/
|
|
38
|
+
cancel(): void;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Gets the number of bytes remaining to print
|
|
42
|
+
*
|
|
43
|
+
* @returns number - Bytes remaining
|
|
44
|
+
*/
|
|
45
|
+
remaining(): number;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Checks if the print job is paused
|
|
49
|
+
*
|
|
50
|
+
* @returns boolean - True if paused, false otherwise
|
|
51
|
+
*/
|
|
52
|
+
isPaused(): boolean;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Checks if a print job is in progress
|
|
56
|
+
*
|
|
57
|
+
* @returns boolean - True if in progress, false otherwise
|
|
58
|
+
*/
|
|
59
|
+
isInProgress(): boolean;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Sets adapter options for write operations
|
|
63
|
+
*
|
|
64
|
+
* @param options - Adapter options
|
|
65
|
+
*/
|
|
66
|
+
setOptions(options: IAdapterOptions): void;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Sets the progress callback
|
|
70
|
+
*
|
|
71
|
+
* @param callback - Progress callback function
|
|
72
|
+
*/
|
|
73
|
+
setProgressCallback(callback?: (sent: number, total: number) => void): void;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Sets the job state change callback
|
|
77
|
+
*
|
|
78
|
+
* @param callback - Job state change callback function
|
|
79
|
+
*/
|
|
80
|
+
setJobStateCallback(
|
|
81
|
+
callback?: (state: 'in-progress' | 'paused' | 'completed' | 'cancelled') => void
|
|
82
|
+
): void;
|
|
83
|
+
}
|