@pdfme/schemas 6.0.3 → 6.0.5-dev.1

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 (116) hide show
  1. package/dist/barcodes/constants.d.ts +4 -0
  2. package/dist/barcodes/helper.d.ts +21 -0
  3. package/dist/barcodes/index.d.ts +4 -0
  4. package/dist/barcodes/pdfRender.d.ts +3 -0
  5. package/dist/barcodes/propPanel.d.ts +3 -0
  6. package/{src/barcodes/types.ts → dist/barcodes/types.d.ts} +5 -7
  7. package/dist/barcodes/uiRender.d.ts +3 -0
  8. package/dist/builtins-CgaZ0UX3.js +613 -0
  9. package/dist/builtins-CgaZ0UX3.js.map +1 -0
  10. package/dist/builtins.d.ts +4 -0
  11. package/dist/builtins.js +2 -0
  12. package/dist/checkbox/index.d.ts +6 -0
  13. package/dist/constants.d.ts +2 -0
  14. package/dist/date/date.d.ts +2 -0
  15. package/dist/date/dateTime.d.ts +2 -0
  16. package/dist/date/helper.d.ts +20 -0
  17. package/dist/date/time.d.ts +2 -0
  18. package/dist/date/types.d.ts +17 -0
  19. package/dist/dynamicTemplate-D_DHR3-X.js +1128 -0
  20. package/dist/dynamicTemplate-D_DHR3-X.js.map +1 -0
  21. package/dist/graphics/image.d.ts +5 -0
  22. package/dist/graphics/imagehelper.d.ts +4 -0
  23. package/dist/graphics/signature.d.ts +4 -0
  24. package/dist/graphics/svg.d.ts +4 -0
  25. package/{src/index.ts → dist/index.d.ts} +1 -22
  26. package/dist/index.js +5383 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/multiVariableText/helper.d.ts +3 -0
  29. package/dist/multiVariableText/index.d.ts +4 -0
  30. package/dist/multiVariableText/pdfRender.d.ts +3 -0
  31. package/dist/multiVariableText/propPanel.d.ts +3 -0
  32. package/{src/multiVariableText/types.ts → dist/multiVariableText/types.d.ts} +2 -3
  33. package/dist/multiVariableText/uiRender.d.ts +3 -0
  34. package/dist/multiVariableText/variables.d.ts +10 -0
  35. package/dist/radioGroup/index.d.ts +7 -0
  36. package/dist/sanitize.d.ts +1 -0
  37. package/dist/select/index.d.ts +7 -0
  38. package/dist/shapes/line.d.ts +6 -0
  39. package/dist/shapes/rectAndEllipse.d.ts +11 -0
  40. package/dist/tables/cell.d.ts +4 -0
  41. package/dist/tables/classes.d.ts +69 -0
  42. package/dist/tables/dynamicTemplate.d.ts +7 -0
  43. package/dist/tables/helper.d.ts +265 -0
  44. package/dist/tables/index.d.ts +4 -0
  45. package/dist/tables/pdfRender.d.ts +3 -0
  46. package/dist/tables/propPanel.d.ts +3 -0
  47. package/dist/tables/tableHelper.d.ts +10 -0
  48. package/dist/tables/types.d.ts +88 -0
  49. package/dist/tables/uiRender.d.ts +3 -0
  50. package/dist/tables.js +2 -0
  51. package/dist/text/constants.d.ts +23 -0
  52. package/dist/text/extraFormatter.d.ts +25 -0
  53. package/dist/text/helper.d.ts +40 -0
  54. package/dist/text/icons/index.d.ts +9 -0
  55. package/dist/text/index.d.ts +4 -0
  56. package/dist/text/pdfRender.d.ts +3 -0
  57. package/dist/text/propPanel.d.ts +3 -0
  58. package/dist/text/types.d.ts +28 -0
  59. package/dist/text/uiRender.d.ts +11 -0
  60. package/dist/utils.d.ts +40 -0
  61. package/dist/utils.js +215 -0
  62. package/dist/utils.js.map +1 -0
  63. package/package.json +5 -1
  64. package/src/barcodes/constants.ts +0 -20
  65. package/src/barcodes/helper.ts +0 -187
  66. package/src/barcodes/index.ts +0 -23
  67. package/src/barcodes/pdfRender.ts +0 -37
  68. package/src/barcodes/propPanel.ts +0 -249
  69. package/src/barcodes/uiRender.ts +0 -94
  70. package/src/builtins.ts +0 -8
  71. package/src/checkbox/index.ts +0 -70
  72. package/src/constants.ts +0 -2
  73. package/src/date/date.ts +0 -9
  74. package/src/date/dateTime.ts +0 -9
  75. package/src/date/helper.ts +0 -544
  76. package/src/date/time.ts +0 -9
  77. package/src/date/types.ts +0 -19
  78. package/src/graphics/image.ts +0 -201
  79. package/src/graphics/imagehelper.ts +0 -156
  80. package/src/graphics/signature.ts +0 -136
  81. package/src/graphics/svg.ts +0 -121
  82. package/src/multiVariableText/helper.ts +0 -65
  83. package/src/multiVariableText/index.ts +0 -16
  84. package/src/multiVariableText/pdfRender.ts +0 -21
  85. package/src/multiVariableText/propPanel.ts +0 -169
  86. package/src/multiVariableText/uiRender.ts +0 -157
  87. package/src/multiVariableText/variables.ts +0 -63
  88. package/src/radioGroup/index.ts +0 -115
  89. package/src/sanitize.ts +0 -50
  90. package/src/select/index.ts +0 -205
  91. package/src/shapes/line.ts +0 -94
  92. package/src/shapes/rectAndEllipse.ts +0 -152
  93. package/src/tables/cell.ts +0 -152
  94. package/src/tables/classes.ts +0 -402
  95. package/src/tables/dynamicTemplate.ts +0 -88
  96. package/src/tables/helper.ts +0 -216
  97. package/src/tables/index.ts +0 -15
  98. package/src/tables/pdfRender.ts +0 -144
  99. package/src/tables/propPanel.ts +0 -111
  100. package/src/tables/tableHelper.ts +0 -289
  101. package/src/tables/types.ts +0 -87
  102. package/src/tables/uiRender.ts +0 -436
  103. package/src/text/constants.ts +0 -104
  104. package/src/text/extraFormatter.ts +0 -83
  105. package/src/text/helper.ts +0 -573
  106. package/src/text/icons/index.ts +0 -30
  107. package/src/text/index.ts +0 -16
  108. package/src/text/pdfRender.ts +0 -240
  109. package/src/text/propPanel.ts +0 -184
  110. package/src/text/types.ts +0 -30
  111. package/src/text/uiRender.ts +0 -292
  112. package/src/utils.ts +0 -354
  113. package/tsconfig.build.json +0 -14
  114. package/tsconfig.json +0 -16
  115. package/vite.config.mts +0 -51
  116. /package/{src/tables.ts → dist/tables.d.ts} +0 -0
@@ -1,292 +0,0 @@
1
- import type * as CSS from 'csstype';
2
- import type { Font as FontKitFont } from 'fontkit';
3
- import { UIRenderProps, getDefaultFont } from '@pdfme/common';
4
- import type { TextSchema } from './types.js';
5
- import {
6
- DEFAULT_FONT_SIZE,
7
- DEFAULT_ALIGNMENT,
8
- VERTICAL_ALIGN_TOP,
9
- VERTICAL_ALIGN_MIDDLE,
10
- VERTICAL_ALIGN_BOTTOM,
11
- DEFAULT_VERTICAL_ALIGNMENT,
12
- DEFAULT_LINE_HEIGHT,
13
- DEFAULT_CHARACTER_SPACING,
14
- DEFAULT_FONT_COLOR,
15
- PLACEHOLDER_FONT_COLOR,
16
- } from './constants.js';
17
- import {
18
- calculateDynamicFontSize,
19
- getFontKitFont,
20
- getBrowserVerticalFontAdjustments,
21
- isFirefox,
22
- } from './helper.js';
23
- import { isEditable } from '../utils.js';
24
-
25
- const replaceUnsupportedChars = (text: string, fontKitFont: FontKitFont): string => {
26
- const charSupportCache: { [char: string]: boolean } = {};
27
-
28
- const isCharSupported = (char: string): boolean => {
29
- if (char in charSupportCache) {
30
- return charSupportCache[char];
31
- }
32
- const isSupported = fontKitFont.hasGlyphForCodePoint(char.codePointAt(0) || 0);
33
- charSupportCache[char] = isSupported;
34
- return isSupported;
35
- };
36
-
37
- const segments = text.split(/(\r\n|\n|\r)/);
38
-
39
- return segments
40
- .map((segment) => {
41
- if (/\r\n|\n|\r/.test(segment)) {
42
- return segment;
43
- }
44
-
45
- return segment
46
- .split('')
47
- .map((char) => {
48
- if (/\s/.test(char) || char.charCodeAt(0) < 32) {
49
- return char;
50
- }
51
-
52
- return isCharSupported(char) ? char : '〿';
53
- })
54
- .join('');
55
- })
56
- .join('');
57
- };
58
-
59
- export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
60
- const { value, schema, mode, onChange, stopEditing, tabIndex, placeholder, options, _cache } =
61
- arg;
62
- const usePlaceholder = isEditable(mode, schema) && placeholder && !value;
63
- const getText = (element: HTMLDivElement) => {
64
- let text = element.innerText;
65
- if (text.endsWith('\n')) {
66
- // contenteditable adds additional newline char retrieved with innerText
67
- text = text.slice(0, -1);
68
- }
69
- return text;
70
- };
71
- const font = options?.font || getDefaultFont();
72
- const fontKitFont = await getFontKitFont(
73
- schema.fontName,
74
- font,
75
- _cache as Map<string, import('fontkit').Font>,
76
- );
77
- const textBlock = buildStyledTextContainer(
78
- arg,
79
- fontKitFont,
80
- usePlaceholder ? placeholder : value,
81
- );
82
-
83
- const processedText = replaceUnsupportedChars(value, fontKitFont);
84
-
85
- if (!isEditable(mode, schema)) {
86
- // Read-only mode
87
- textBlock.innerHTML = processedText
88
- .split('')
89
- .map((l, i) => {
90
- const escaped = l
91
- .replace(/&/g, '&amp;')
92
- .replace(/</g, '&lt;')
93
- .replace(/>/g, '&gt;')
94
- .replace(/"/g, '&quot;');
95
- return `<span style="letter-spacing:${
96
- String(value).length === i + 1 ? 0 : 'inherit'
97
- };">${escaped}</span>`;
98
- })
99
- .join('');
100
- return;
101
- }
102
-
103
- makeElementPlainTextContentEditable(textBlock);
104
- textBlock.tabIndex = tabIndex || 0;
105
- textBlock.innerText = mode === 'designer' ? value : processedText;
106
- textBlock.addEventListener('blur', (e: Event) => {
107
- if (onChange) onChange({ key: 'content', value: getText(e.target as HTMLDivElement) });
108
- if (stopEditing) stopEditing();
109
- });
110
-
111
- if (schema.dynamicFontSize) {
112
- let dynamicFontSize: undefined | number = undefined;
113
-
114
- textBlock.addEventListener('keyup', () => {
115
- setTimeout(() => {
116
- // Use a regular function instead of an async one since we don't need await
117
- (() => {
118
- if (!textBlock.textContent) return;
119
- dynamicFontSize = calculateDynamicFontSize({
120
- textSchema: schema,
121
- fontKitFont,
122
- value: getText(textBlock),
123
- startingFontSize: dynamicFontSize,
124
- });
125
- textBlock.style.fontSize = `${dynamicFontSize}pt`;
126
-
127
- const { topAdj: newTopAdj, bottomAdj: newBottomAdj } = getBrowserVerticalFontAdjustments(
128
- fontKitFont,
129
- dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
130
- schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
131
- schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT,
132
- );
133
- textBlock.style.paddingTop = `${newTopAdj}px`;
134
- textBlock.style.marginBottom = `${newBottomAdj}px`;
135
- })();
136
- }, 0);
137
- });
138
- }
139
-
140
- if (usePlaceholder) {
141
- textBlock.style.color = PLACEHOLDER_FONT_COLOR;
142
- textBlock.addEventListener('focus', () => {
143
- if (textBlock.innerText === placeholder) {
144
- textBlock.innerText = '';
145
- textBlock.style.color = schema.fontColor ?? DEFAULT_FONT_COLOR;
146
- }
147
- });
148
- }
149
-
150
- if (mode === 'designer') {
151
- setTimeout(() => {
152
- textBlock.focus();
153
- // Set the focus to the end of the editable element when you focus, as we would for a textarea
154
- const selection = window.getSelection();
155
- const range = document.createRange();
156
- if (selection && range) {
157
- range.selectNodeContents(textBlock);
158
- range.collapse(false); // Collapse range to the end
159
- selection?.removeAllRanges();
160
- selection?.addRange(range);
161
- }
162
- });
163
- }
164
- };
165
-
166
- export const buildStyledTextContainer = (
167
- arg: UIRenderProps<TextSchema>,
168
- fontKitFont: FontKitFont,
169
- value: string,
170
- ) => {
171
- const { schema, rootElement, mode } = arg;
172
-
173
- let dynamicFontSize: undefined | number = undefined;
174
-
175
- if (schema.dynamicFontSize && value) {
176
- dynamicFontSize = calculateDynamicFontSize({
177
- textSchema: schema,
178
- fontKitFont,
179
- value,
180
- startingFontSize: dynamicFontSize,
181
- });
182
- }
183
-
184
- // Depending on vertical alignment, we need to move the top or bottom of the font to keep
185
- // it within it's defined box and align it with the generated pdf.
186
- const { topAdj, bottomAdj } = getBrowserVerticalFontAdjustments(
187
- fontKitFont,
188
- dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
189
- schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
190
- schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT,
191
- );
192
-
193
- const topAdjustment = topAdj.toString();
194
- const bottomAdjustment = bottomAdj.toString();
195
-
196
- const container = document.createElement('div');
197
-
198
- const containerStyle: CSS.Properties = {
199
- padding: 0,
200
- resize: 'none',
201
- backgroundColor: getBackgroundColor(value, schema),
202
- border: 'none',
203
- display: 'flex',
204
- flexDirection: 'column',
205
- justifyContent: mapVerticalAlignToFlex(schema.verticalAlignment),
206
- width: '100%',
207
- height: '100%',
208
- cursor: isEditable(mode, schema) ? 'text' : 'default',
209
- };
210
- Object.assign(container.style, containerStyle);
211
- rootElement.innerHTML = '';
212
- rootElement.appendChild(container);
213
-
214
- // text decoration
215
- const textDecorations = [];
216
- if (schema.strikethrough) textDecorations.push('line-through');
217
- if (schema.underline) textDecorations.push('underline');
218
-
219
- const textBlockStyle: CSS.Properties = {
220
- // Font formatting styles
221
- fontFamily: schema.fontName ? `'${schema.fontName}'` : 'inherit',
222
- color: schema.fontColor ? schema.fontColor : DEFAULT_FONT_COLOR,
223
- fontSize: `${dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE}pt`,
224
- letterSpacing: `${schema.characterSpacing ?? DEFAULT_CHARACTER_SPACING}pt`,
225
- lineHeight: `${schema.lineHeight ?? DEFAULT_LINE_HEIGHT}em`,
226
- textAlign: schema.alignment ?? DEFAULT_ALIGNMENT,
227
- whiteSpace: 'pre-wrap',
228
- wordBreak: 'break-word',
229
- // Block layout styles
230
- resize: 'none',
231
- border: 'none',
232
- outline: 'none',
233
- marginBottom: `${bottomAdjustment}px`,
234
- paddingTop: `${topAdjustment}px`,
235
- backgroundColor: 'transparent',
236
- textDecoration: textDecorations.join(' '),
237
- };
238
-
239
- const textBlock = document.createElement('div');
240
- textBlock.id = 'text-' + String(schema.id);
241
- Object.assign(textBlock.style, textBlockStyle);
242
-
243
- container.appendChild(textBlock);
244
-
245
- return textBlock;
246
- };
247
-
248
- /**
249
- * Firefox doesn't support 'plaintext-only' contentEditable mode, which we want to avoid mark-up.
250
- * This function adds a workaround for Firefox to make the contentEditable element behave like 'plaintext-only'.
251
- */
252
- export const makeElementPlainTextContentEditable = (element: HTMLElement) => {
253
- if (!isFirefox()) {
254
- element.contentEditable = 'plaintext-only';
255
- return;
256
- }
257
-
258
- element.contentEditable = 'true';
259
- element.addEventListener('keydown', (e: KeyboardEvent) => {
260
- if (e.key === 'Enter' && !e.shiftKey) {
261
- e.preventDefault();
262
- document.execCommand('insertLineBreak', false, undefined);
263
- }
264
- });
265
-
266
- element.addEventListener('paste', (e: ClipboardEvent) => {
267
- e.preventDefault();
268
- const paste = e.clipboardData?.getData('text');
269
- const selection = window.getSelection();
270
- if (!selection?.rangeCount) return;
271
- selection.deleteFromDocument();
272
- selection.getRangeAt(0).insertNode(document.createTextNode(paste || ''));
273
- selection.collapseToEnd();
274
- });
275
- };
276
-
277
- export const mapVerticalAlignToFlex = (verticalAlignmentValue: string | undefined) => {
278
- switch (verticalAlignmentValue) {
279
- case VERTICAL_ALIGN_TOP:
280
- return 'flex-start';
281
- case VERTICAL_ALIGN_MIDDLE:
282
- return 'center';
283
- case VERTICAL_ALIGN_BOTTOM:
284
- return 'flex-end';
285
- }
286
- return 'flex-start';
287
- };
288
-
289
- const getBackgroundColor = (value: string, schema: { backgroundColor?: string }) => {
290
- if (!value || !schema.backgroundColor) return 'transparent';
291
- return schema.backgroundColor;
292
- };
package/src/utils.ts DELETED
@@ -1,354 +0,0 @@
1
- import type * as CSS from 'csstype';
2
- import { cmyk, degrees, degreesToRadians, rgb, Color } from '@pdfme/pdf-lib';
3
- import { Schema, mm2pt, Mode, isHexValid, ColorType } from '@pdfme/common';
4
- import { IconNode } from 'lucide';
5
- import { getDynamicHeightsForTable as _getDynamicHeightsForTable } from './tables/dynamicTemplate.js';
6
- export const convertForPdfLayoutProps = ({
7
- schema,
8
- pageHeight,
9
- applyRotateTranslate = true,
10
- }: {
11
- schema: Schema;
12
- pageHeight: number;
13
- applyRotateTranslate?: boolean;
14
- }) => {
15
- const { width: mmWidth, height: mmHeight, position, rotate, opacity } = schema;
16
- const { x: mmX, y: mmY } = position;
17
-
18
- const rotateDegrees = rotate ? -rotate : 0;
19
- const width = mm2pt(mmWidth);
20
- const height = mm2pt(mmHeight);
21
- let x = mm2pt(mmX);
22
- // PDF coordinate system is from bottom left, UI is top left, so we need to flip the y axis
23
- let y = pageHeight - mm2pt(mmY) - height;
24
-
25
- if (rotateDegrees && applyRotateTranslate) {
26
- // If rotating we must pivot around the same point as the UI performs its rotation.
27
- // The UI performs rotation around the objects center point (the pivot point below),
28
- // pdflib rotates around the bottom left corner of the object.
29
- // We must therefore adjust the X and Y by rotating the bottom left corner by this pivot point.
30
- const pivotPoint = { x: x + width / 2, y: pageHeight - mm2pt(mmY) - height / 2 };
31
- const rotatedPoint = rotatePoint({ x, y }, pivotPoint, rotateDegrees);
32
- x = rotatedPoint.x;
33
- y = rotatedPoint.y;
34
- }
35
-
36
- return {
37
- position: { x, y },
38
- height: height,
39
- width: width,
40
- rotate: degrees(rotateDegrees),
41
- opacity,
42
- };
43
- };
44
-
45
- export const rotatePoint = (
46
- point: { x: number; y: number },
47
- pivot: { x: number; y: number },
48
- angleDegrees: number,
49
- ): { x: number; y: number } => {
50
- const angleRadians = degreesToRadians(angleDegrees);
51
-
52
- const x =
53
- Math.cos(angleRadians) * (point.x - pivot.x) -
54
- Math.sin(angleRadians) * (point.y - pivot.y) +
55
- pivot.x;
56
- const y =
57
- Math.sin(angleRadians) * (point.x - pivot.x) +
58
- Math.cos(angleRadians) * (point.y - pivot.y) +
59
- pivot.y;
60
-
61
- return { x, y };
62
- };
63
-
64
- export const getDynamicHeightsForTable = _getDynamicHeightsForTable;
65
-
66
- // ----------------------------------------
67
-
68
- export const addAlphaToHex = (hex: string, alphaPercentage: number) => {
69
- if (!/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i.test(hex)) {
70
- throw new Error('Invalid HEX color code');
71
- }
72
- const alphaValue = Math.round((alphaPercentage / 100) * 255);
73
- let alphaHex = alphaValue.toString(16);
74
- if (alphaHex.length === 1) alphaHex = '0' + alphaHex;
75
- return hex + alphaHex;
76
- };
77
-
78
- export const isEditable = (mode: Mode, schema: Schema) =>
79
- mode === 'designer' || (mode === 'form' && schema.readOnly !== true);
80
-
81
- const hex2rgb = (hex: string) => {
82
- if (hex.slice(0, 1) === '#') hex = hex.slice(1);
83
- if (hex.length === 3)
84
- hex =
85
- hex.slice(0, 1) +
86
- hex.slice(0, 1) +
87
- hex.slice(1, 2) +
88
- hex.slice(1, 2) +
89
- hex.slice(2, 3) +
90
- hex.slice(2, 3);
91
-
92
- return [hex.slice(0, 2), hex.slice(2, 4), hex.slice(4, 6)].map((str) => parseInt(str, 16));
93
- };
94
-
95
- export const hex2RgbColor = (hexString: string | undefined) => {
96
- if (hexString) {
97
- const isValid = isHexValid(hexString);
98
-
99
- if (!isValid) {
100
- throw new Error(`Invalid hex color value ${hexString}`);
101
- }
102
-
103
- const [r, g, b] = hex2rgb(hexString);
104
-
105
- return rgb(r / 255, g / 255, b / 255);
106
- }
107
-
108
- return undefined;
109
- };
110
-
111
- const hex2CmykColor = (hexString: string | undefined) => {
112
- if (hexString) {
113
- const isValid = isHexValid(hexString);
114
-
115
- if (!isValid) {
116
- throw new Error(`Invalid hex color value ${hexString}`);
117
- }
118
-
119
- // Remove the # if it's present
120
- hexString = hexString.replace('#', '');
121
-
122
- // Extract the hexadecimal color code and the opacity
123
- const hexColor = hexString.substring(0, 6);
124
- const opacityColor = hexString.substring(6, 8);
125
- const opacity = opacityColor ? parseInt(opacityColor, 16) / 255 : 1;
126
-
127
- // Convert the hex values to decimal
128
- let r = parseInt(hexColor.substring(0, 2), 16) / 255;
129
- let g = parseInt(hexColor.substring(2, 4), 16) / 255;
130
- let b = parseInt(hexColor.substring(4, 6), 16) / 255;
131
-
132
- // Apply the opacity
133
- r = r * opacity + (1 - opacity);
134
- g = g * opacity + (1 - opacity);
135
- b = b * opacity + (1 - opacity);
136
-
137
- // Calculate the CMYK values
138
- const k = 1 - Math.max(r, g, b);
139
- const c = r === 0 ? 0 : (1 - r - k) / (1 - k);
140
- const m = g === 0 ? 0 : (1 - g - k) / (1 - k);
141
- const y = b === 0 ? 0 : (1 - b - k) / (1 - k);
142
-
143
- return cmyk(c, m, y, k);
144
- }
145
-
146
- return undefined;
147
- };
148
-
149
- export const hex2PrintingColor = (color?: string | Color, colorType?: ColorType) => {
150
- // if color is already CMYK, RGB or Grayscale, does not required to convert
151
- if (typeof color === 'object') return color;
152
- return colorType?.toLowerCase() == 'cmyk' ? hex2CmykColor(color) : hex2RgbColor(color);
153
- };
154
-
155
- export const readFile = (input: File | FileList | null): Promise<string | ArrayBuffer> =>
156
- new Promise((resolve, reject) => {
157
- const fileReader = new FileReader();
158
-
159
- fileReader.onload = (e) => {
160
- if (e.target?.result) {
161
- resolve(e.target.result);
162
- }
163
- };
164
-
165
- fileReader.onerror = () => {
166
- reject(new Error('[@pdfme/schemas] File reading failed'));
167
- };
168
-
169
- let file: File | null = null;
170
- if (input instanceof FileList && input.length > 0) {
171
- file = input[0];
172
- } else if (input instanceof File) {
173
- file = input;
174
- }
175
-
176
- if (file) {
177
- fileReader.readAsDataURL(file);
178
- } else {
179
- reject(new Error('[@pdfme/schemas] No files provided'));
180
- }
181
- });
182
-
183
- export const createErrorElm = () => {
184
- const container = document.createElement('div');
185
- const containerStyle: CSS.Properties = {
186
- display: 'flex',
187
- alignItems: 'center',
188
- justifyContent: 'center',
189
- width: '100%',
190
- height: '100%',
191
- };
192
- Object.assign(container.style, containerStyle);
193
-
194
- const span = document.createElement('span');
195
- const spanStyle: CSS.Properties = {
196
- color: 'white',
197
- background: 'red',
198
- padding: '0.25rem',
199
- fontSize: '12pt',
200
- fontWeight: 'bold',
201
- borderRadius: '2px',
202
- fontFamily: "'Open Sans', sans-serif",
203
- };
204
- Object.assign(span.style, spanStyle);
205
-
206
- span.textContent = 'ERROR';
207
- container.appendChild(span);
208
-
209
- return container;
210
- };
211
-
212
- export const createSvgStr = (icon: IconNode, attrs?: Record<string, string>): string => {
213
- // In lucide 0.475.0, the icon is an array of elements, not a single SVG element
214
- // We need to create an SVG wrapper and add the elements as children
215
-
216
- const safeTagNames = new Set([
217
- 'svg',
218
- 'circle',
219
- 'ellipse',
220
- 'g',
221
- 'line',
222
- 'path',
223
- 'polygon',
224
- 'polyline',
225
- 'rect',
226
- ]);
227
- const safeAttributeNames = new Set([
228
- 'aria-hidden',
229
- 'aria-label',
230
- 'class',
231
- 'clip-rule',
232
- 'cx',
233
- 'cy',
234
- 'd',
235
- 'fill',
236
- 'fill-opacity',
237
- 'fill-rule',
238
- 'focusable',
239
- 'height',
240
- 'id',
241
- 'opacity',
242
- 'points',
243
- 'preserveAspectRatio',
244
- 'r',
245
- 'role',
246
- 'rx',
247
- 'ry',
248
- 'stroke',
249
- 'stroke-dasharray',
250
- 'stroke-dashoffset',
251
- 'stroke-linecap',
252
- 'stroke-linejoin',
253
- 'stroke-miterlimit',
254
- 'stroke-opacity',
255
- 'stroke-width',
256
- 'transform',
257
- 'vector-effect',
258
- 'viewBox',
259
- 'width',
260
- 'x',
261
- 'x1',
262
- 'x2',
263
- 'xmlns',
264
- 'xmlns:xlink',
265
- 'y',
266
- 'y1',
267
- 'y2',
268
- ]);
269
- const escapeHtmlAttribute = (value: string): string =>
270
- value
271
- .replaceAll('&', '&amp;')
272
- .replaceAll('"', '&quot;')
273
- .replaceAll("'", '&#39;')
274
- .replaceAll('<', '&lt;')
275
- .replaceAll('>', '&gt;');
276
- const escapeHtmlText = (value: string): string =>
277
- value.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
278
- const toSafeAttributeString = (attributes: Record<string, string>): string =>
279
- Object.entries(attributes)
280
- .filter(
281
- ([key, value]) =>
282
- safeAttributeNames.has(key) &&
283
- value !== undefined &&
284
- value !== null &&
285
- !key.toLowerCase().startsWith('on'),
286
- )
287
- .map(([key, value]) => `${key}="${escapeHtmlAttribute(String(value))}"`)
288
- .join(' ');
289
- const toSafeTagName = (tag: unknown): string => {
290
- const tagName = String(tag);
291
- if (!safeTagNames.has(tagName)) {
292
- throw new Error(`Invalid SVG tag name: ${tagName}`);
293
- }
294
- return tagName;
295
- };
296
-
297
- // Handle non-array input
298
- if (!Array.isArray(icon)) {
299
- return escapeHtmlText(String(icon));
300
- }
301
-
302
- // Create default SVG attributes
303
- const svgAttrs = {
304
- xmlns: 'http://www.w3.org/2000/svg',
305
- width: '24',
306
- height: '24',
307
- viewBox: '0 0 24 24',
308
- fill: 'none',
309
- stroke: 'currentColor',
310
- 'stroke-width': '2',
311
- 'stroke-linecap': 'round',
312
- 'stroke-linejoin': 'round',
313
- ...attrs,
314
- };
315
- const svgAttrString = toSafeAttributeString(svgAttrs);
316
-
317
- // Helper function to process a single element
318
- const processElement = (element: unknown): string => {
319
- if (!Array.isArray(element)) {
320
- return escapeHtmlText(String(element));
321
- }
322
-
323
- const [tag, attributes = {}, children = []] = element as [
324
- unknown,
325
- Record<string, string>,
326
- unknown[],
327
- ];
328
- const tagName = toSafeTagName(tag);
329
- const attrString = toSafeAttributeString(attributes);
330
-
331
- // Process children recursively
332
- let childrenString = '';
333
-
334
- if (Array.isArray(children) && children.length > 0) {
335
- childrenString = children.map((child) => processElement(child)).join('');
336
- }
337
-
338
- // Return properly formatted element string
339
- if (childrenString) {
340
- return `<${String(tagName)}${attrString ? ' ' + String(attrString) : ''}>${childrenString}</${String(tagName)}>`;
341
- } else {
342
- // Self-closing tag for empty children
343
- return `<${String(tagName)}${attrString ? ' ' + String(attrString) : ''}/>`;
344
- }
345
- };
346
-
347
- // Process all elements and join them
348
- const elementsString = Array.isArray(icon)
349
- ? icon.map((element) => processElement(element)).join('')
350
- : processElement(icon);
351
-
352
- // Return the complete SVG string
353
- return `<svg ${svgAttrString}>${elementsString}</svg>`;
354
- };
@@ -1,14 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.build.base.json",
3
- "compilerOptions": {
4
- "declaration": true,
5
- "declarationDir": "./dist",
6
- "emitDeclarationOnly": true,
7
- "module": "ESNext",
8
- "moduleResolution": "bundler",
9
- "outDir": "./dist",
10
- "rootDir": "./src",
11
- "skipLibCheck": true
12
- },
13
- "include": ["src/**/*.ts"]
14
- }
package/tsconfig.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "composite": true,
5
- "declaration": true,
6
- "declarationDir": "./dist/typecheck",
7
- "emitDeclarationOnly": true,
8
- "module": "ESNext",
9
- "moduleResolution": "bundler",
10
- "outDir": "./dist/typecheck",
11
- "tsBuildInfoFile": "./dist/typecheck/tsconfig.tsbuildinfo",
12
- "skipLibCheck": true
13
- },
14
- "include": ["src/**/*.ts"],
15
- "references": [{ "path": "../common" }, { "path": "../pdf-lib" }]
16
- }