@pdfme/schemas 3.1.4 → 3.1.5-dev.10

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 (88) hide show
  1. package/README.md +303 -0
  2. package/dist/cjs/src/barcodes/propPanel.js +27 -29
  3. package/dist/cjs/src/barcodes/propPanel.js.map +1 -1
  4. package/dist/cjs/src/barcodes/uiRender.js +8 -10
  5. package/dist/cjs/src/barcodes/uiRender.js.map +1 -1
  6. package/dist/cjs/src/image/pdfRender.js +2 -0
  7. package/dist/cjs/src/image/pdfRender.js.map +1 -1
  8. package/dist/cjs/src/image/uiRender.js +3 -3
  9. package/dist/cjs/src/image/uiRender.js.map +1 -1
  10. package/dist/cjs/src/index.js +3 -1
  11. package/dist/cjs/src/index.js.map +1 -1
  12. package/dist/cjs/src/line/constants.js +5 -0
  13. package/dist/cjs/src/line/constants.js.map +1 -0
  14. package/dist/cjs/src/line/index.js +8 -0
  15. package/dist/cjs/src/line/index.js.map +1 -0
  16. package/dist/cjs/src/line/pdfRender.js +20 -0
  17. package/dist/cjs/src/line/pdfRender.js.map +1 -0
  18. package/dist/cjs/src/line/propPanel.js +35 -0
  19. package/dist/cjs/src/line/propPanel.js.map +1 -0
  20. package/dist/cjs/src/line/types.js +3 -0
  21. package/dist/cjs/src/line/types.js.map +1 -0
  22. package/dist/cjs/src/line/uiRender.js +14 -0
  23. package/dist/cjs/src/line/uiRender.js.map +1 -0
  24. package/dist/cjs/src/renderUtils.js +28 -1
  25. package/dist/cjs/src/renderUtils.js.map +1 -1
  26. package/dist/cjs/src/text/index.js.map +1 -1
  27. package/dist/cjs/src/text/pdfRender.js +6 -29
  28. package/dist/cjs/src/text/pdfRender.js.map +1 -1
  29. package/dist/cjs/src/text/propPanel.js +2 -2
  30. package/dist/cjs/src/text/propPanel.js.map +1 -1
  31. package/dist/cjs/src/text/uiRender.js +11 -17
  32. package/dist/cjs/src/text/uiRender.js.map +1 -1
  33. package/dist/esm/src/barcodes/propPanel.js +27 -29
  34. package/dist/esm/src/barcodes/propPanel.js.map +1 -1
  35. package/dist/esm/src/barcodes/uiRender.js +9 -11
  36. package/dist/esm/src/barcodes/uiRender.js.map +1 -1
  37. package/dist/esm/src/image/pdfRender.js +2 -0
  38. package/dist/esm/src/image/pdfRender.js.map +1 -1
  39. package/dist/esm/src/image/uiRender.js +4 -4
  40. package/dist/esm/src/image/uiRender.js.map +1 -1
  41. package/dist/esm/src/index.js +2 -1
  42. package/dist/esm/src/index.js.map +1 -1
  43. package/dist/esm/src/line/constants.js +2 -0
  44. package/dist/esm/src/line/constants.js.map +1 -0
  45. package/dist/esm/src/line/index.js +6 -0
  46. package/dist/esm/src/line/index.js.map +1 -0
  47. package/dist/esm/src/line/pdfRender.js +16 -0
  48. package/dist/esm/src/line/pdfRender.js.map +1 -0
  49. package/dist/esm/src/line/propPanel.js +32 -0
  50. package/dist/esm/src/line/propPanel.js.map +1 -0
  51. package/dist/esm/src/line/types.js +2 -0
  52. package/dist/esm/src/line/types.js.map +1 -0
  53. package/dist/esm/src/line/uiRender.js +10 -0
  54. package/dist/esm/src/line/uiRender.js.map +1 -0
  55. package/dist/esm/src/renderUtils.js +27 -2
  56. package/dist/esm/src/renderUtils.js.map +1 -1
  57. package/dist/esm/src/text/index.js.map +1 -1
  58. package/dist/esm/src/text/pdfRender.js +6 -29
  59. package/dist/esm/src/text/pdfRender.js.map +1 -1
  60. package/dist/esm/src/text/propPanel.js +2 -2
  61. package/dist/esm/src/text/propPanel.js.map +1 -1
  62. package/dist/esm/src/text/uiRender.js +12 -18
  63. package/dist/esm/src/text/uiRender.js.map +1 -1
  64. package/dist/types/src/index.d.ts +2 -1
  65. package/dist/types/src/line/constants.d.ts +1 -0
  66. package/dist/types/src/line/index.d.ts +4 -0
  67. package/dist/types/src/line/pdfRender.d.ts +3 -0
  68. package/dist/types/src/line/propPanel.d.ts +3 -0
  69. package/dist/types/src/line/types.d.ts +4 -0
  70. package/dist/types/src/line/uiRender.d.ts +3 -0
  71. package/dist/types/src/renderUtils.d.ts +3 -1
  72. package/package.json +1 -1
  73. package/src/barcodes/propPanel.ts +27 -29
  74. package/src/barcodes/uiRender.ts +9 -11
  75. package/src/image/pdfRender.ts +1 -0
  76. package/src/image/uiRender.ts +4 -4
  77. package/src/index.ts +2 -1
  78. package/src/line/constants.ts +1 -0
  79. package/src/line/index.ts +8 -0
  80. package/src/line/pdfRender.ts +24 -0
  81. package/src/line/propPanel.ts +34 -0
  82. package/src/line/types.ts +5 -0
  83. package/src/line/uiRender.ts +12 -0
  84. package/src/renderUtils.ts +34 -2
  85. package/src/text/index.ts +0 -1
  86. package/src/text/pdfRender.ts +6 -42
  87. package/src/text/propPanel.ts +2 -2
  88. package/src/text/uiRender.ts +14 -29
@@ -0,0 +1,24 @@
1
+ import type { PDFRenderProps } from '@pdfme/common';
2
+ import type { LineSchema } from './types';
3
+ import { rotatePoint, convertForPdfLayoutProps, hex2RgbColor } from '../renderUtils.js';
4
+ import { DEFAULT_LINE_COLOR } from './constants.js';
5
+
6
+ export const pdfRender = (arg: PDFRenderProps<LineSchema>) => {
7
+ const { page, schema } = arg;
8
+ const pageHeight = page.getHeight();
9
+ const {
10
+ width,
11
+ height,
12
+ rotate,
13
+ position: { x, y },
14
+ opacity,
15
+ } = convertForPdfLayoutProps({ schema, pageHeight, applyRotateTranslate: false });
16
+ const pivot = { x: x + width / 2, y: y + height / 2 };
17
+ page.drawLine({
18
+ start: rotatePoint({ x, y: y + height / 2 }, pivot, rotate.angle),
19
+ end: rotatePoint({ x: x + width, y: y + height / 2 }, pivot, rotate.angle),
20
+ thickness: height,
21
+ color: hex2RgbColor(schema.color ?? DEFAULT_LINE_COLOR),
22
+ opacity: opacity,
23
+ });
24
+ };
@@ -0,0 +1,34 @@
1
+ import { PropPanel } from '@pdfme/common';
2
+ import type { LineSchema } from './types';
3
+ import { HEX_COLOR_PATTERN } from '../constants.js';
4
+ import { DEFAULT_LINE_COLOR } from './constants.js';
5
+
6
+ export const propPanel: PropPanel<LineSchema> = {
7
+ schema: ({ i18n }) => {
8
+ return {
9
+ color: {
10
+ title: i18n('schemas.line.color'),
11
+ type: 'string',
12
+ widget: 'color',
13
+ required: true,
14
+ rules: [
15
+ {
16
+ pattern: HEX_COLOR_PATTERN,
17
+ message: i18n('hexColorPrompt'),
18
+ },
19
+ ],
20
+ },
21
+ };
22
+ },
23
+ defaultValue: '',
24
+ defaultSchema: {
25
+ type: 'line',
26
+ position: { x: 0, y: 0 },
27
+ width: 50,
28
+ height: 1,
29
+ rotate: 0,
30
+ opacity: 1,
31
+ readOnly: true,
32
+ color: DEFAULT_LINE_COLOR,
33
+ },
34
+ };
@@ -0,0 +1,5 @@
1
+ import { Schema } from '@pdfme/common';
2
+
3
+ export interface LineSchema extends Schema {
4
+ color: string;
5
+ }
@@ -0,0 +1,12 @@
1
+ import type { LineSchema } from './types';
2
+ import { UIRenderProps } from '@pdfme/common';
3
+ import { DEFAULT_LINE_COLOR } from './constants.js';
4
+
5
+ export const uiRender = (arg: UIRenderProps<LineSchema>) => {
6
+ const { schema, rootElement } = arg;
7
+ const div = document.createElement('div');
8
+ div.style.backgroundColor = schema.color ?? DEFAULT_LINE_COLOR;
9
+ div.style.width = '100%';
10
+ div.style.height = '100%';
11
+ rootElement.appendChild(div);
12
+ };
@@ -1,5 +1,5 @@
1
- import { degrees, degreesToRadians } from '@pdfme/pdf-lib';
2
- import { Schema, mm2pt } from '@pdfme/common';
1
+ import { degrees, degreesToRadians, rgb } from '@pdfme/pdf-lib';
2
+ import { Schema, mm2pt, Mode, isHexValid } from '@pdfme/common';
3
3
 
4
4
  export const convertForPdfLayoutProps = ({
5
5
  schema,
@@ -71,3 +71,35 @@ export const addAlphaToHex = (hex: string, alphaPercentage: number) => {
71
71
  if (alphaHex.length === 1) alphaHex = '0' + alphaHex;
72
72
  return hex + alphaHex;
73
73
  };
74
+
75
+ export const isEditable = (mode: Mode) => mode === 'form' || mode === 'designer';
76
+
77
+ const hex2rgb = (hex: string) => {
78
+ if (hex.slice(0, 1) === '#') hex = hex.slice(1);
79
+ if (hex.length === 3)
80
+ hex =
81
+ hex.slice(0, 1) +
82
+ hex.slice(0, 1) +
83
+ hex.slice(1, 2) +
84
+ hex.slice(1, 2) +
85
+ hex.slice(2, 3) +
86
+ hex.slice(2, 3);
87
+
88
+ return [hex.slice(0, 2), hex.slice(2, 4), hex.slice(4, 6)].map((str) => parseInt(str, 16));
89
+ };
90
+
91
+ export const hex2RgbColor = (hexString: string | undefined) => {
92
+ if (hexString) {
93
+ const isValid = isHexValid(hexString);
94
+
95
+ if (!isValid) {
96
+ throw new Error(`Invalid hex color value ${hexString}`);
97
+ }
98
+
99
+ const [r, g, b] = hex2rgb(hexString);
100
+
101
+ return rgb(r / 255, g / 255, b / 255);
102
+ }
103
+
104
+ return undefined;
105
+ };
package/src/text/index.ts CHANGED
@@ -5,5 +5,4 @@ import { uiRender } from './uiRender.js';
5
5
  import type { TextSchema } from './types';
6
6
 
7
7
  const schema: Plugin<TextSchema> = { pdf: pdfRender, ui: uiRender, propPanel };
8
-
9
8
  export default schema;
@@ -1,12 +1,5 @@
1
- import { PDFFont, PDFDocument, rgb } from '@pdfme/pdf-lib';
2
- import {
3
- PDFRenderProps,
4
- Font,
5
- getDefaultFont,
6
- getFallbackFontName,
7
- mm2pt,
8
- isHexValid,
9
- } from '@pdfme/common';
1
+ import { PDFFont, PDFDocument } from '@pdfme/pdf-lib';
2
+ import { PDFRenderProps, Font, getDefaultFont, getFallbackFontName, mm2pt } from '@pdfme/common';
10
3
  import type { TextSchema, FontWidthCalcValues } from './types';
11
4
  import {
12
5
  VERTICAL_ALIGN_TOP,
@@ -27,37 +20,7 @@ import {
27
20
  getSplittedLines,
28
21
  widthOfTextAtSize,
29
22
  } from './helper.js';
30
- import { convertForPdfLayoutProps, rotatePoint } from '../renderUtils.js';
31
-
32
- const hex2rgb = (hex: string) => {
33
- if (hex.slice(0, 1) === '#') hex = hex.slice(1);
34
- if (hex.length === 3)
35
- hex =
36
- hex.slice(0, 1) +
37
- hex.slice(0, 1) +
38
- hex.slice(1, 2) +
39
- hex.slice(1, 2) +
40
- hex.slice(2, 3) +
41
- hex.slice(2, 3);
42
-
43
- return [hex.slice(0, 2), hex.slice(2, 4), hex.slice(4, 6)].map((str) => parseInt(str, 16));
44
- };
45
-
46
- const hex2RgbColor = (hexString: string | undefined) => {
47
- if (hexString) {
48
- const isValid = isHexValid(hexString);
49
-
50
- if (!isValid) {
51
- throw new Error(`Invalid hex color value ${hexString}`);
52
- }
53
-
54
- const [r, g, b] = hex2rgb(hexString);
55
-
56
- return rgb(r / 255, g / 255, b / 255);
57
- }
58
-
59
- return undefined;
60
- };
23
+ import { convertForPdfLayoutProps, rotatePoint, hex2RgbColor } from '../renderUtils.js';
61
24
 
62
25
  const embedAndGetFontObj = async (arg: {
63
26
  pdfDoc: PDFDocument;
@@ -118,6 +81,7 @@ const getFontProp = async ({
118
81
 
119
82
  export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
120
83
  const { value, pdfDoc, pdfLib, page, options, schema, _cache } = arg;
84
+ if (!value) return;
121
85
 
122
86
  const { font = getDefaultFont() } = options;
123
87
 
@@ -132,7 +96,7 @@ export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
132
96
  const fontName = (
133
97
  schema.fontName ? schema.fontName : getFallbackFontName(font)
134
98
  ) as keyof typeof pdfFontObj;
135
- const pdfFontValue = pdfFontObj[fontName];
99
+ const pdfFontValue = pdfFontObj && pdfFontObj[fontName];
136
100
 
137
101
  const pageHeight = page.getHeight();
138
102
  const {
@@ -162,7 +126,7 @@ export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
162
126
  };
163
127
 
164
128
  let lines: string[] = [];
165
- value.split(/\r|\n|\r\n/g).forEach((line: string) => {
129
+ value.split(/\r\n|\r|\n/g).forEach((line: string) => {
166
130
  lines = lines.concat(getSplittedLines(line, fontWidthCalcValues));
167
131
  });
168
132
 
@@ -153,7 +153,7 @@ export const propPanel: PropPanel<TextSchema> = {
153
153
  rules: [
154
154
  {
155
155
  pattern: HEX_COLOR_PATTERN,
156
- message: 'Please enter a valid hex color code.',
156
+ message: i18n('hexColorPrompt'),
157
157
  },
158
158
  ],
159
159
  },
@@ -164,7 +164,7 @@ export const propPanel: PropPanel<TextSchema> = {
164
164
  rules: [
165
165
  {
166
166
  pattern: HEX_COLOR_PATTERN,
167
- message: 'Please enter a valid hex color code.',
167
+ message: i18n('hexColorPrompt'),
168
168
  },
169
169
  ],
170
170
  },
@@ -18,8 +18,7 @@ import {
18
18
  getFontKitFont,
19
19
  getBrowserVerticalFontAdjustments,
20
20
  } from './helper.js';
21
- import { addAlphaToHex } from '../renderUtils.js';
22
- import { DEFAULT_OPACITY } from '../constants.js';
21
+ import { addAlphaToHex, isEditable } from '../renderUtils.js';
23
22
 
24
23
  const mapVerticalAlignToFlex = (verticalAlignmentValue: string | undefined) => {
25
24
  switch (verticalAlignmentValue) {
@@ -33,19 +32,10 @@ const mapVerticalAlignToFlex = (verticalAlignmentValue: string | undefined) => {
33
32
  return 'flex-start';
34
33
  };
35
34
 
36
- const getBackgroundColor = (
37
- mode: 'form' | 'viewer' | 'designer',
38
- value: string,
39
- schema: Schema,
40
- defaultBackgroundColor: string
41
- ) => {
42
- if ((mode === 'form' || mode === 'designer') && value && schema.backgroundColor) {
43
- return schema.backgroundColor as string;
44
- } else if (mode === 'viewer') {
45
- return (schema.backgroundColor as string) ?? 'transparent';
46
- } else {
47
- return defaultBackgroundColor;
48
- }
35
+ const getBackgroundColor = (value: string, schema: Schema, defaultBackgroundColor: string) => {
36
+ if (!value) return 'transparent';
37
+ if (schema.backgroundColor) return schema.backgroundColor as string;
38
+ return defaultBackgroundColor;
49
39
  };
50
40
 
51
41
  export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
@@ -94,19 +84,13 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
94
84
  const containerStyle: CSS.Properties = {
95
85
  padding: 0,
96
86
  resize: 'none',
97
- backgroundColor: getBackgroundColor(
98
- mode,
99
- value,
100
- schema,
101
- addAlphaToHex(theme.colorPrimaryBg, 30)
102
- ),
87
+ backgroundColor: getBackgroundColor(value, schema, addAlphaToHex(theme.colorPrimaryBg, 30)),
103
88
  border: 'none',
104
89
  display: 'flex',
105
90
  flexDirection: 'column',
106
91
  justifyContent: mapVerticalAlignToFlex(schema.verticalAlignment),
107
92
  width: '100%',
108
93
  height: '100%',
109
- opacity: schema.opacity ?? DEFAULT_OPACITY,
110
94
  };
111
95
  Object.assign(container.style, containerStyle);
112
96
  rootElement.innerHTML = '';
@@ -126,14 +110,14 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
126
110
  resize: 'none',
127
111
  border: 'none',
128
112
  outline: 'none',
129
- marginBottom: bottomAdjustment + 'px',
130
- paddingTop: topAdjustment + 'px',
113
+ marginBottom: `${bottomAdjustment}px`,
114
+ paddingTop: `${topAdjustment}px`,
131
115
  backgroundColor: 'transparent',
132
116
  };
133
117
  const textBlock = document.createElement('div');
134
118
  Object.assign(textBlock.style, textBlockStyle);
135
119
 
136
- if (mode === 'form' || mode === 'designer') {
120
+ if (isEditable(mode)) {
137
121
  textBlock.contentEditable = 'plaintext-only';
138
122
  textBlock.tabIndex = tabIndex || 0;
139
123
  textBlock.innerText = value;
@@ -150,14 +134,15 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
150
134
  dynamicFontSize = await calculateDynamicFontSize(getCdfArg(textBlock.textContent));
151
135
  textBlock.style.fontSize = `${dynamicFontSize}pt`;
152
136
 
153
- const { topAdj: newTopAdj, bottomAdj: newBottomAdj } = getBrowserVerticalFontAdjustments(
137
+ const { topAdj: newTopAdj, bottomAdj: newBottomAdj } =
138
+ getBrowserVerticalFontAdjustments(
154
139
  fontKitFont,
155
140
  dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
156
141
  schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
157
142
  schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
158
- );
159
- textBlock.style.paddingTop = newTopAdj.toString() + 'px';
160
- textBlock.style.marginBottom = newBottomAdj.toString() + 'px';
143
+ );
144
+ textBlock.style.paddingTop = `${newTopAdj}px`;
145
+ textBlock.style.marginBottom = `${newBottomAdj}px`;
161
146
  })();
162
147
  }, 0);
163
148
  });