@plevands/epson-thermal-printer 0.1.2 → 0.2.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/README.md CHANGED
@@ -77,7 +77,7 @@ configurePdfWorker('/assets/pdf.worker.min.mjs');
77
77
  import { useEpsonPrinter, usePrinterConfig } from '@plevands/epson-thermal-printer';
78
78
 
79
79
  function MyPrintComponent() {
80
- const { config } = usePrinterConfig();
80
+ const { config, isConfigured } = usePrinterConfig();
81
81
  const { print, isLoading, error } = useEpsonPrinter(config);
82
82
 
83
83
  const handlePrint = async () => {
@@ -93,7 +93,8 @@ function MyPrintComponent() {
93
93
 
94
94
  return (
95
95
  <div>
96
- <button onClick={handlePrint} disabled={isLoading}>
96
+ {!isConfigured && <p>Please configure the printer first</p>}
97
+ <button onClick={handlePrint} disabled={isLoading || !isConfigured}>
97
98
  {isLoading ? 'Printing...' : 'Print'}
98
99
  </button>
99
100
  {error && <p>Error: {error}</p>}
@@ -102,6 +103,41 @@ function MyPrintComponent() {
102
103
  }
103
104
  ```
104
105
 
106
+ ### Text-Only Printing with Builder (Hooks API)
107
+
108
+ ```typescript
109
+ import { useEpsonPrinter, usePrinterConfig } from '@plevands/epson-thermal-printer';
110
+
111
+ function TextReceipt() {
112
+ const { config, isConfigured } = usePrinterConfig({
113
+ initialConfig: { printerIP: '192.168.1.100' },
114
+ });
115
+ const { printWithBuilder, isLoading, error } = useEpsonPrinter(config, { align: 'left' });
116
+
117
+ const handlePrintText = async () => {
118
+ const result = await printWithBuilder((builder) => {
119
+ builder.addTextAlign('left');
120
+ builder.addText('TICKET #123\n');
121
+ builder.addText('Producto A x1 10.00\n');
122
+ builder.addText('TOTAL 10.00\n');
123
+ builder.addFeedLine(2);
124
+ builder.addCut('feed');
125
+ });
126
+
127
+ if (!result.success) {
128
+ console.error(result.message);
129
+ }
130
+ };
131
+
132
+ return (
133
+ <button onClick={handlePrintText} disabled={isLoading || !isConfigured}>
134
+ Imprimir texto
135
+ {error ? ` (${error})` : ''}
136
+ </button>
137
+ );
138
+ }
139
+ ```
140
+
105
141
  ### Service API (No React)
106
142
 
107
143
  ```typescript
@@ -135,16 +171,21 @@ import { useEpsonPrinter, usePrinterConfig } from '@plevands/epson-thermal-print
135
171
  import { useEffect, useState } from 'react';
136
172
 
137
173
  function PrinterPanel() {
138
- const { config } = usePrinterConfig();
174
+ const { config, isConfigured } = usePrinterConfig();
139
175
  const { checkConnection, print, isLoading, error } = useEpsonPrinter(config);
140
176
  const [isOnline, setIsOnline] = useState<boolean | null>(null);
141
177
 
142
178
  // Check connection when component mounts or config changes
143
179
  useEffect(() => {
180
+ if (!isConfigured) return;
144
181
  checkConnection().then(result => {
145
182
  setIsOnline(result.success);
146
183
  });
147
- }, [checkConnection]);
184
+ }, [checkConnection, isConfigured]);
185
+
186
+ if (!isConfigured) {
187
+ return <span>⚙️ Please configure the printer</span>;
188
+ }
148
189
 
149
190
  if (isOnline === null) {
150
191
  return <span>Checking printer connection...</span>;
@@ -205,7 +246,7 @@ import {
205
246
  } from '@plevands/epson-thermal-printer';
206
247
 
207
248
  function PdfPrinter() {
208
- const { config } = usePrinterConfig();
249
+ const { config, isConfigured } = usePrinterConfig();
209
250
  const { processFile } = usePdfProcessor({
210
251
  enabled: true,
211
252
  trimMargins: { top: 10, bottom: 10, left: 5, right: 5 },
@@ -287,23 +328,51 @@ interface EpsonPrinterConfig {
287
328
 
288
329
  Main hook for printer operations with automatic SDK loading.
289
330
 
331
+ **Parameters:**
332
+ - `config` - `EpsonPrinterConfig | null` — printer configuration (pass `null` or unconfigured hook value; operations will return `NOT_CONFIGURED` error)
333
+ - `options?` - `PrintOptions` — default print options
334
+
290
335
  **Returns:**
291
336
  - `print(canvas)` - Print a single canvas
292
337
  - `printPages(canvases, options?)` - Print multiple pages
338
+ - `printWithBuilder(builder => ...)` - Print custom ePOSBuilder commands (ideal for text receipts)
339
+ - `checkConnection()` - Check printer connection without printing
293
340
  - `testConnection()` - Test printer connection
294
341
  - `isLoading` - Loading state
295
342
  - `error` - Error message if any
296
343
  - `sdkStatus` - SDK loading status
297
344
 
298
- #### `usePrinterConfig()`
345
+ #### `usePrinterConfig(options?)`
299
346
 
300
347
  Manages printer configuration with localStorage persistence.
301
348
 
349
+ Without arguments, `config` starts as `null` until the user calls `updateConfig`. Pass `initialConfig` to provide defaults for new users. Pass `storageKey` to use a custom localStorage key — useful when managing multiple printer configurations (e.g. one per network).
350
+
351
+ **Options (`UsePrinterConfigOptions`):**
352
+ | Option | Type | Default | Description |
353
+ |--------|------|---------|-------------|
354
+ | `initialConfig` | `EpsonPrinterConfig` | `undefined` | Defaults for new users. localStorage values take priority for returning users. |
355
+ | `storageKey` | `string` | `'epson-printer-config'` | localStorage key for this configuration instance. |
356
+
357
+ ```typescript
358
+ // No defaults — config is null until user configures
359
+ const { config, isConfigured } = usePrinterConfig();
360
+
361
+ // With defaults — config starts pre-filled
362
+ const { config, isConfigured } = usePrinterConfig({
363
+ initialConfig: { printerIP: '10.0.0.50' },
364
+ });
365
+
366
+ // Multiple configs for different networks
367
+ const office = usePrinterConfig({ storageKey: 'printer-office' });
368
+ const warehouse = usePrinterConfig({ storageKey: 'printer-warehouse' });
369
+ ```
370
+
302
371
  **Returns:**
303
- - `config` - Current configuration
304
- - `updateConfig(partial)` - Update configuration
305
- - `resetConfig()` - Reset to defaults
306
- - `isConfigured` - Boolean
372
+ - `config` - Current configuration (`EpsonPrinterConfig | null`)
373
+ - `updateConfig(partial)` - Update configuration (persists to localStorage)
374
+ - `resetConfig()` - Reset to initial value (`initialConfig` if provided, otherwise `null`) and clear localStorage
375
+ - `isConfigured` - `true` when config is not null and has a valid printer IP
307
376
 
308
377
  #### `usePdfProcessor(config?)`
309
378
 
@@ -1,2 +1,2 @@
1
1
  import { EpsonPrinterConfig, PrintOptions, UseEpsonPrinterReturn } from '../types';
2
- export declare function useEpsonPrinter(config: EpsonPrinterConfig, options?: PrintOptions): UseEpsonPrinterReturn;
2
+ export declare function useEpsonPrinter(config: EpsonPrinterConfig | null, options?: PrintOptions): UseEpsonPrinterReturn;
@@ -1,2 +1,2 @@
1
- import { UsePrinterConfigReturn } from '../types';
2
- export declare function usePrinterConfig(): UsePrinterConfigReturn;
1
+ import { UsePrinterConfigOptions, UsePrinterConfigReturn } from '../types';
2
+ export declare function usePrinterConfig(options?: UsePrinterConfigOptions): UsePrinterConfigReturn;