148 lines
4.3 KiB
TypeScript
148 lines
4.3 KiB
TypeScript
import { FONT_SIZES, ALIGNMENT, FONT, STYLE, BARCODE_CONFIG, PAPER_CONFIG } from './printer-constants';
|
|
import { BarcodeUtils } from './barcode-utils';
|
|
|
|
// Command types for printer operations
|
|
export enum Command {
|
|
// Text commands
|
|
TEXT = 'TEXT',
|
|
HEADER = 'HEADER',
|
|
BANNER = 'BANNER',
|
|
NEWLINE = 'NEWLINE',
|
|
|
|
// Barcode commands
|
|
BARCODE = 'BARCODE',
|
|
|
|
// Formatting commands
|
|
FONT_SIZE = 'FONT_SIZE',
|
|
ALIGN = 'ALIGN',
|
|
FONT_FAMILY = 'FONT_FAMILY',
|
|
STYLE = 'STYLE',
|
|
|
|
// Paper commands
|
|
CUT = 'CUT',
|
|
|
|
// Section formatting
|
|
SECTION = 'SECTION',
|
|
STEP_HEADER = 'STEP_HEADER',
|
|
CHECKBOX = 'CHECKBOX',
|
|
|
|
// List formatting
|
|
LIST = 'LIST',
|
|
}
|
|
|
|
// Command parameter types
|
|
export type FontSize = typeof FONT_SIZES[keyof typeof FONT_SIZES];
|
|
export type Alignment = typeof ALIGNMENT[keyof typeof ALIGNMENT];
|
|
export type FontFamily = typeof FONT[keyof typeof FONT];
|
|
export type TextStyle = typeof STYLE[keyof typeof STYLE];
|
|
|
|
// Command tuple type - [Command, ...parameters]
|
|
export type CommandTuple =
|
|
| [Command.TEXT, string]
|
|
| [Command.HEADER, string]
|
|
| [Command.BANNER, string, number?]
|
|
| [Command.NEWLINE]
|
|
| [Command.BARCODE, string]
|
|
| [Command.FONT_SIZE, FontSize]
|
|
| [Command.ALIGN, Alignment]
|
|
| [Command.FONT_FAMILY, FontFamily]
|
|
| [Command.STYLE, TextStyle]
|
|
| [Command.CUT, boolean, number?]
|
|
| [Command.SECTION, string, string, string?, boolean?]
|
|
| [Command.STEP_HEADER, string, number, string?, boolean?]
|
|
| [Command.CHECKBOX, string]
|
|
| [Command.LIST, string[], number?, string?];
|
|
|
|
// Command array type
|
|
export type CommandArray = CommandTuple[];
|
|
|
|
// Command builder utility
|
|
export class CommandBuilder {
|
|
static text(text: string): CommandTuple {
|
|
return [Command.TEXT, text];
|
|
}
|
|
|
|
static header(text: string): CommandTuple {
|
|
return [Command.HEADER, text];
|
|
}
|
|
|
|
static banner(char: string = '=', length?: number): CommandTuple {
|
|
return [Command.BANNER, char, length];
|
|
}
|
|
|
|
static newline(): CommandTuple {
|
|
return [Command.NEWLINE];
|
|
}
|
|
|
|
static barcode(data: string): CommandTuple {
|
|
return [Command.BARCODE, data];
|
|
}
|
|
|
|
static taskBarcode(taskId: number): CommandTuple {
|
|
return [Command.BARCODE, BarcodeUtils.generateTaskBarcode(taskId)];
|
|
}
|
|
|
|
static stepBarcode(stepId: number): CommandTuple {
|
|
return [Command.BARCODE, BarcodeUtils.generateStepBarcode(stepId)];
|
|
}
|
|
|
|
static barcodeWithAlignment(data: string, alignment: Alignment = ALIGNMENT.CENTER): CommandTuple[] {
|
|
return [
|
|
[Command.ALIGN, alignment],
|
|
[Command.BARCODE, data],
|
|
[Command.ALIGN, ALIGNMENT.LEFT], // Reset to left alignment
|
|
];
|
|
}
|
|
|
|
static taskBarcodeWithAlignment(taskId: number, alignment: Alignment = ALIGNMENT.CENTER): CommandTuple[] {
|
|
return [
|
|
[Command.ALIGN, alignment],
|
|
[Command.BARCODE, BarcodeUtils.generateTaskBarcode(taskId)],
|
|
[Command.ALIGN, ALIGNMENT.LEFT], // Reset to left alignment
|
|
];
|
|
}
|
|
|
|
static stepBarcodeWithAlignment(stepId: number, alignment: Alignment = ALIGNMENT.CENTER): CommandTuple[] {
|
|
return [
|
|
[Command.ALIGN, alignment],
|
|
[Command.BARCODE, BarcodeUtils.generateStepBarcode(stepId)],
|
|
[Command.ALIGN, ALIGNMENT.LEFT], // Reset to left alignment
|
|
];
|
|
}
|
|
|
|
static fontSize(size: FontSize): CommandTuple {
|
|
return [Command.FONT_SIZE, size];
|
|
}
|
|
|
|
static align(alignment: Alignment): CommandTuple {
|
|
return [Command.ALIGN, alignment];
|
|
}
|
|
|
|
static font(font: FontFamily): CommandTuple {
|
|
return [Command.FONT_FAMILY, font];
|
|
}
|
|
|
|
static style(style: TextStyle): CommandTuple {
|
|
return [Command.STYLE, style];
|
|
}
|
|
|
|
static cut(partial: boolean = true, lines: number = PAPER_CONFIG.CUT_LINES): CommandTuple {
|
|
return [Command.CUT, partial, lines];
|
|
}
|
|
|
|
static section(header: string, content: string, bannerChar: string = '=', trailingNewline: boolean = false): CommandTuple {
|
|
return [Command.SECTION, header, content, bannerChar, trailingNewline];
|
|
}
|
|
|
|
static stepHeader(stepName: string, stepNumber: number, taskName?: string, isTaskView: boolean = false): CommandTuple {
|
|
return [Command.STEP_HEADER, stepName, stepNumber, taskName, isTaskView];
|
|
}
|
|
|
|
static checkbox(text: string): CommandTuple {
|
|
return [Command.CHECKBOX, text];
|
|
}
|
|
|
|
static list(items: string[], startIndex: number = 0, prefix: string = ''): CommandTuple {
|
|
return [Command.LIST, items, startIndex, prefix];
|
|
}
|
|
} |