-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.ts
202 lines (179 loc) · 6.92 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
PDFDocument,
StandardFonts,
PDFPage,
PDFFont,
rgb,
Color,
Grayscale,
RGB,
CMYK,
} from "pdf-lib";
// Alignment options for text elements
export type Alignment = "left" | "center" | "right";
//Currently internal only, nor exposed to the user.
//Optimal mode will come in a future release, as it needs a bit more testing. May also add a "none" mode.
export enum BreakWordMode {
Essential = "essential", //DEFAULT ON
Optimal = "optimal",
}
export enum ErrorCode {
ERR_CONVERT_VALIDATE, // Error when data validation or conversion fails
ERR_COLUMN_COUNT_MISMATCH, // Error when the column count does not match expected values
ERR_ROW_COUNT_MISMATCH, // Error when the row count does not match expected values
ERR_TABLE_WIDTH_OVERFLOW, // Error when table width exceeds available space
ERR_TABLE_HEIGHT_OVERFLOW, // Error when table height exceeds available space
DRAW_ROW_ERROR, // Error when a row fails to draw correctly
ERR_WRAP_HEADER_INVALID, // Indicates invalid usage of wrapHeader without a header
ERR_NO_SPACE_FOR_HEADERS, // Indicates insufficient space for headers
ERR_INVALID_DISTRIBUTE_MODE, // Indicates an invalid column width distribution mode
}
// Options for distributing column widths in the table
export type GenColumnWidthOptions = {
columnWidthMode: "equal" | "auto" | "wrapHeader"; // Mode for calculating column widths
availableWidth: number; // Total width available for the table
tableData: CellContent[][]; // Data for the table cells
font: PDFFont; // PDFFont for table cells
headerFont?: PDFFont; // PDFFont for the header row (optional)
headerTextSize?: number; // PDFFont size for the header row (optional)
textSize: number; // PDFFont size for table cells
borderWidth: number; // Width of the table borders
horizontalMargin: number; // Margin for wrapping text within cells
headerHorizontalMargin: number;
hasHeader: boolean; // If true, the first row is treated as a header row
};
// Custom styled text with optional font, size, color, and alignment
export interface CustomStyledText {
type: "text";
text: string;
alignment?: Alignment;
font?: PDFFont;
textSize?: number;
textColor?: Color;
}
// Base interface for link elements with optional font, size, color, and alignment
export interface LinkBase {
type: "link";
url?: string;
page?: number;
text: string;
alignment?: Alignment;
font?: PDFFont;
textSize?: number;
textColor?: Color;
}
// Enforcing either a 'url' or a 'page' property but not both
export type Link = LinkBase & ({ url: string } | { page: number });
// Base interface for image elements with optional alignment and horizontal margin
export interface ImageBase {
type: "image";
src?: string;
data?: Uint8Array;
width: number;
height: number;
alignment?: Alignment;
horizontalMargin?: number;
}
// Enforcing either a 'src' or a 'data' property but not both
export type Image = ImageBase & ({ src: string } | { data: Uint8Array });
// Defining a type for cell elements, which can be a string, Link, Image, or CustomStyledText
export type CellElement = string | Link | Image | CustomStyledText;
// Defining a type for cell content, which can be a CellElement or an array of CellElements
export type CellContent = CellElement | Array<CellElement>;
export interface TableObject {
rows: {
[key: string]: CellElement[] | undefined;
}[];
columns: {
title: string;
key: string;
}[];
}
export interface TableDataConverterValidatorInput {
data: CellContent[][] | TableObject;
hasHeader: boolean;
fillEmpty?: boolean;
}
/**
* *SPECIAL* DeepPartial type
*
* This utility type creates a deep partial version of an existing type, T.
* It recursively makes all properties of T and its nested objects optional. It also does the same for specifiedFunctions
*/
export type TableOptionsDeepPartial<T> = {
[K in keyof T]?: T[K] extends (...args: any[]) => any
? T[K] | undefined
: T[K] extends Color
? T[K] | undefined
: T[K] extends PDFFont
? T[K] | undefined
: T[K] extends object
? TableOptionsDeepPartial<T[K]>
: T[K];
};
// Basic options for the table, including font, colors, and alignment
export interface DrawTableOptions {
textSize: number; // PDFFont size for the table text
textColor: Color; // Color for the table text
contentAlignment: Alignment; // Alignment for the table text
font: PDFFont; // PDFFont for the table text
linkColor: Color; // Default color for links
lineHeight: number; // Line height for table rows
column: ColumnOptions; // Options for table columns
row: RowOptions; // Options for table rows
header: HeaderOptions; // Options for the header row
title: TitleOptions; // Options for the table title
border: BorderOptions; // Options for the table borders
pageMargin: PageMarginOptions; // Options for the page margins
contentMargin: ContentMarginOptions; // Options for the content margins
fillUndefCells: boolean;
}
// Header row options
export interface HeaderOptions {
hasHeaderRow: boolean; // If true, the first row will be treated as a header row and styled accordingly
font: PDFFont; // PDFFont for the header row
textSize: number; // PDFFont size for the header row
textColor: Color; // Text color for the header row
backgroundColor: Color; // Background color for the header row
contentAlignment: Alignment; // Alignment for header row content
}
// Options related to column widths
export type ColumnOptions = {
widthMode: "equal" | "auto" | "wrapHeader"; // Column width mode
overrideWidths: number[]; // If provided, these widths will be used for each column
};
// Options related to row heights
export interface RowOptions {
backgroundColors: (Color | undefined)[];
overrideHeights: number[]; // If provided, these heights will be used for each row
}
// Options for the table title
export interface TitleOptions {
text: string; // Text for the title (optional)
textSize: number; // PDFFont size for the title (optional)
font: PDFFont; // PDFFont for the title (optional)
textColor: Color; // Text color for the title (optional)
alignment: Alignment; // Alignment for the title (optional)
}
// Options for the table borders
export interface BorderOptions {
color: Color; // Color for the table borders (optional)
width: number; // Width for the table borders (optional)
}
// Options for the page margins
export interface PageMarginOptions {
bottom: number; // Bottom page margin (optional)
right: number; // Right page margin (optional)
}
// Options for the content margins
export interface ContentMarginOptions {
horizontal: number; // Horizontal content margin (optional)
vertical: number; // Vertical content margin (optional)
}
// Dimensions of the table
export interface TableDimensions {
endX: number; // X-coordinate of the table's bottom-right corner
endY: number; // Y-coordinate of the table's bottom-right corner
width: number; // Width of the table
height: number; // Height of the table
}