@pdfme/schemas 4.2.4 → 4.2.5-dev.3

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 (57) hide show
  1. package/dist/cjs/src/barcodes/helper.js +1 -1
  2. package/dist/cjs/src/barcodes/helper.js.map +1 -1
  3. package/dist/cjs/src/barcodes/propPanel.js +2 -2
  4. package/dist/cjs/src/barcodes/propPanel.js.map +1 -1
  5. package/dist/cjs/src/multiVariableText/helper.js +13 -9
  6. package/dist/cjs/src/multiVariableText/helper.js.map +1 -1
  7. package/dist/cjs/src/multiVariableText/propPanel.js +4 -2
  8. package/dist/cjs/src/multiVariableText/propPanel.js.map +1 -1
  9. package/dist/cjs/src/shapes/line.js +1 -1
  10. package/dist/cjs/src/shapes/line.js.map +1 -1
  11. package/dist/cjs/src/shapes/rectAndEllipse.js +2 -2
  12. package/dist/cjs/src/shapes/rectAndEllipse.js.map +1 -1
  13. package/dist/cjs/src/tables/helper.js +4 -4
  14. package/dist/cjs/src/tables/helper.js.map +1 -1
  15. package/dist/cjs/src/tables/propPanel.js +1 -1
  16. package/dist/cjs/src/tables/propPanel.js.map +1 -1
  17. package/dist/cjs/src/text/pdfRender.js +1 -1
  18. package/dist/cjs/src/text/pdfRender.js.map +1 -1
  19. package/dist/cjs/src/text/propPanel.js +6 -3
  20. package/dist/cjs/src/text/propPanel.js.map +1 -1
  21. package/dist/cjs/src/text/uiRender.js +48 -7
  22. package/dist/cjs/src/text/uiRender.js.map +1 -1
  23. package/dist/esm/src/barcodes/helper.js +1 -1
  24. package/dist/esm/src/barcodes/helper.js.map +1 -1
  25. package/dist/esm/src/barcodes/propPanel.js +2 -2
  26. package/dist/esm/src/barcodes/propPanel.js.map +1 -1
  27. package/dist/esm/src/multiVariableText/helper.js +13 -9
  28. package/dist/esm/src/multiVariableText/helper.js.map +1 -1
  29. package/dist/esm/src/multiVariableText/propPanel.js +4 -2
  30. package/dist/esm/src/multiVariableText/propPanel.js.map +1 -1
  31. package/dist/esm/src/shapes/line.js +1 -1
  32. package/dist/esm/src/shapes/line.js.map +1 -1
  33. package/dist/esm/src/shapes/rectAndEllipse.js +2 -2
  34. package/dist/esm/src/shapes/rectAndEllipse.js.map +1 -1
  35. package/dist/esm/src/tables/helper.js +4 -4
  36. package/dist/esm/src/tables/helper.js.map +1 -1
  37. package/dist/esm/src/tables/propPanel.js +1 -1
  38. package/dist/esm/src/tables/propPanel.js.map +1 -1
  39. package/dist/esm/src/text/pdfRender.js +1 -1
  40. package/dist/esm/src/text/pdfRender.js.map +1 -1
  41. package/dist/esm/src/text/propPanel.js +6 -3
  42. package/dist/esm/src/text/propPanel.js.map +1 -1
  43. package/dist/esm/src/text/uiRender.js +48 -7
  44. package/dist/esm/src/text/uiRender.js.map +1 -1
  45. package/dist/types/src/shapes/rectAndEllipse.d.ts +6 -0
  46. package/package.json +1 -1
  47. package/src/barcodes/helper.ts +1 -1
  48. package/src/barcodes/propPanel.ts +2 -2
  49. package/src/multiVariableText/helper.ts +14 -9
  50. package/src/multiVariableText/propPanel.ts +4 -2
  51. package/src/shapes/line.ts +1 -1
  52. package/src/shapes/rectAndEllipse.ts +2 -2
  53. package/src/tables/helper.ts +4 -4
  54. package/src/tables/propPanel.ts +1 -1
  55. package/src/text/pdfRender.ts +1 -1
  56. package/src/text/propPanel.ts +6 -3
  57. package/src/text/uiRender.ts +75 -43
@@ -97,7 +97,9 @@ export const propPanel: PropPanel<MultiVariableTextSchema> = {
97
97
  defaultSchema: {
98
98
  ...parentPropPanel.defaultSchema,
99
99
  type: 'multiVariableText',
100
- text: 'Type something...',
100
+ text: 'Add text here using {} for variables ',
101
+ width: 50,
102
+ height: 15,
101
103
  content: '{}',
102
104
  variables: [],
103
105
  },
@@ -113,7 +115,7 @@ const updateVariablesFromText = (text: string, variables: any): boolean => {
113
115
  // Add any new variables
114
116
  for (const match of matches) {
115
117
  const variableName = match.replace('{', '').replace('}', '');
116
- if (!variables[variableName]) {
118
+ if (!(variableName in variables)) {
117
119
  // NOTE: We upper case the variable name as the default value
118
120
  variables[variableName] = variableName.toUpperCase();
119
121
  changed = true;
@@ -45,7 +45,7 @@ const lineSchema: Plugin<LineSchema> = {
45
45
  type: 'string',
46
46
  widget: 'color',
47
47
  required: true,
48
- rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('hexColorPrompt') }],
48
+ rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('validation.hexColor') }],
49
49
  },
50
50
  }),
51
51
  defaultSchema: {
@@ -77,13 +77,13 @@ const shape: Plugin<ShapeSchema> = {
77
77
  title: i18n('schemas.borderColor'),
78
78
  type: 'string',
79
79
  widget: 'color',
80
- rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('hexColorPrompt') }],
80
+ rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('validation.hexColor') }],
81
81
  },
82
82
  color: {
83
83
  title: i18n('schemas.color'),
84
84
  type: 'string',
85
85
  widget: 'color',
86
- rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('hexColorPrompt') }],
86
+ rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('validation.hexColor') }],
87
87
  },
88
88
  }),
89
89
  defaultSchema: {
@@ -110,19 +110,19 @@ export const getCellPropPanelSchema = (arg: {
110
110
  title: i18n('schemas.textColor'),
111
111
  type: 'string',
112
112
  widget: 'color',
113
- rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('hexColorPrompt') }],
113
+ rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('validation.hexColor') }],
114
114
  },
115
115
  borderColor: {
116
116
  title: i18n('schemas.borderColor'),
117
117
  type: 'string',
118
118
  widget: 'color',
119
- rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('hexColorPrompt') }],
119
+ rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('validation.hexColor') }],
120
120
  },
121
121
  backgroundColor: {
122
122
  title: i18n('schemas.backgroundColor'),
123
123
  type: 'string',
124
124
  widget: 'color',
125
- rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('hexColorPrompt') }],
125
+ rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('validation.hexColor') }],
126
126
  },
127
127
  ...(isBody
128
128
  ? {
@@ -130,7 +130,7 @@ export const getCellPropPanelSchema = (arg: {
130
130
  title: i18n('schemas.table.alternateBackgroundColor'),
131
131
  type: 'string',
132
132
  widget: 'color',
133
- rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('hexColorPrompt') }],
133
+ rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('validation.hexColor') }],
134
134
  },
135
135
  }
136
136
  : {}),
@@ -33,7 +33,7 @@ export const propPanel: PropPanel<TableSchema> = {
33
33
  title: i18n('schemas.borderColor'),
34
34
  type: 'string',
35
35
  widget: 'color',
36
- rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('hexColorPrompt') }],
36
+ rules: [{ pattern: HEX_COLOR_PATTERN, message: i18n('validation.hexColor') }],
37
37
  },
38
38
  },
39
39
  },
@@ -97,7 +97,7 @@ export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
97
97
  const [pdfFontObj, fontKitFont, fontProp] = await Promise.all([
98
98
  embedAndGetFontObj({ pdfDoc, font, _cache }),
99
99
  getFontKitFont(schema.fontName, font, _cache),
100
- getFontProp({ value, font, schema, _cache }),
100
+ getFontProp({ value, font, schema, _cache, colorType }),
101
101
  ]);
102
102
 
103
103
  const { fontSize, color, alignment, verticalAlignment, lineHeight, characterSpacing } = fontProp;
@@ -39,9 +39,12 @@ const UseDynamicFontSize = (props: PropPanelWidgetProps) => {
39
39
  changeSchemas([{ key: 'dynamicFontSize', value: val, schemaId: activeSchema.id }]);
40
40
  };
41
41
  const label = document.createElement('label');
42
- label.innerText = i18n('schemas.text.dynamicFontSize') || '';
42
+ const span = document.createElement('span');
43
+ span.innerText = i18n('schemas.text.dynamicFontSize') || '';
44
+ span.style.cssText = 'margin-left: 0.5rem';
43
45
  label.style.cssText = 'display: flex; width: 100%;';
44
46
  label.appendChild(checkbox);
47
+ label.appendChild(span);
45
48
  rootElement.appendChild(label);
46
49
  };
47
50
 
@@ -126,7 +129,7 @@ export const propPanel: PropPanel<TextSchema> = {
126
129
  rules: [
127
130
  {
128
131
  pattern: HEX_COLOR_PATTERN,
129
- message: i18n('hexColorPrompt'),
132
+ message: i18n('validation.hexColor'),
130
133
  },
131
134
  ],
132
135
  },
@@ -137,7 +140,7 @@ export const propPanel: PropPanel<TextSchema> = {
137
140
  rules: [
138
141
  {
139
142
  pattern: HEX_COLOR_PATTERN,
140
- message: i18n('hexColorPrompt'),
143
+ message: i18n('validation.hexColor'),
141
144
  },
142
145
  ],
143
146
  },
@@ -1,4 +1,5 @@
1
1
  import type * as CSS from 'csstype';
2
+ import type { Font as FontKitFont } from 'fontkit';
2
3
  import { UIRenderProps, getDefaultFont } from '@pdfme/common';
3
4
  import type { TextSchema } from './types';
4
5
  import {
@@ -21,41 +22,79 @@ import {
21
22
  } from './helper.js';
22
23
  import { isEditable } from '../utils.js';
23
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
+
24
59
  export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
25
- const {
26
- value,
27
- schema,
28
- mode,
29
- onChange,
30
- stopEditing,
31
- tabIndex,
32
- placeholder,
33
- options,
34
- _cache,
35
- } = arg;
60
+ const { value, schema, mode, onChange, stopEditing, tabIndex, placeholder, options, _cache } =
61
+ arg;
36
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, textBlock] = await Promise.all([
73
+ getFontKitFont(schema.fontName, font, _cache),
74
+ buildStyledTextContainer(arg, usePlaceholder ? placeholder : value),
75
+ ]);
37
76
 
38
- const textBlock = await buildStyledTextContainer(arg, usePlaceholder ? placeholder : value);
77
+ const processedText = replaceUnsupportedChars(value, fontKitFont);
39
78
 
40
79
  if (!isEditable(mode, schema)) {
41
80
  // Read-only mode
42
- textBlock.innerHTML = value
43
- .split('')
44
- .map(
45
- (l: string, i: number) =>
46
- `<span style="letter-spacing:${
47
- String(value).length === i + 1 ? 0 : 'inherit'
48
- };">${l}</span>`
49
- )
50
- .join('');
81
+ textBlock.innerHTML = processedText
82
+ .split('')
83
+ .map(
84
+ (l, i) =>
85
+ `<span style="letter-spacing:${
86
+ String(value).length === i + 1 ? 0 : 'inherit'
87
+ };">${l}</span>`
88
+ )
89
+ .join('');
51
90
  return;
52
91
  }
53
92
 
54
93
  makeElementPlainTextContentEditable(textBlock);
55
94
  textBlock.tabIndex = tabIndex || 0;
56
- textBlock.innerText = value;
95
+ textBlock.innerText = mode === 'designer' ? value : processedText;
57
96
  textBlock.addEventListener('blur', (e: Event) => {
58
- onChange && onChange({ key: 'content', value: (e.target as HTMLDivElement).textContent });
97
+ onChange && onChange({ key: 'content', value: getText(e.target as HTMLDivElement) });
59
98
  stopEditing && stopEditing();
60
99
  });
61
100
 
@@ -71,19 +110,18 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
71
110
  dynamicFontSize = await calculateDynamicFontSize({
72
111
  textSchema: schema,
73
112
  font,
74
- value: textBlock.textContent,
113
+ value: getText(textBlock),
75
114
  startingFontSize: dynamicFontSize,
76
115
  _cache,
77
116
  });
78
117
  textBlock.style.fontSize = `${dynamicFontSize}pt`;
79
118
 
80
- const { topAdj: newTopAdj, bottomAdj: newBottomAdj } =
81
- getBrowserVerticalFontAdjustments(
82
- fontKitFont,
83
- dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
84
- schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
85
- schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
86
- );
119
+ const { topAdj: newTopAdj, bottomAdj: newBottomAdj } = getBrowserVerticalFontAdjustments(
120
+ fontKitFont,
121
+ dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
122
+ schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
123
+ schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
124
+ );
87
125
  textBlock.style.paddingTop = `${newTopAdj}px`;
88
126
  textBlock.style.marginBottom = `${newBottomAdj}px`;
89
127
  })();
@@ -118,13 +156,7 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
118
156
  };
119
157
 
120
158
  export const buildStyledTextContainer = async (arg: UIRenderProps<TextSchema>, value: string) => {
121
- const {
122
- schema,
123
- rootElement,
124
- mode,
125
- options,
126
- _cache,
127
- } = arg;
159
+ const { schema, rootElement, mode, options, _cache } = arg;
128
160
  const font = options?.font || getDefaultFont();
129
161
 
130
162
  let dynamicFontSize: undefined | number = undefined;
@@ -143,10 +175,10 @@ export const buildStyledTextContainer = async (arg: UIRenderProps<TextSchema>, v
143
175
  // Depending on vertical alignment, we need to move the top or bottom of the font to keep
144
176
  // it within it's defined box and align it with the generated pdf.
145
177
  const { topAdj, bottomAdj } = getBrowserVerticalFontAdjustments(
146
- fontKitFont,
147
- dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
148
- schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
149
- schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
178
+ fontKitFont,
179
+ dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
180
+ schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
181
+ schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
150
182
  );
151
183
 
152
184
  const topAdjustment = topAdj.toString();
@@ -231,7 +263,7 @@ export const makeElementPlainTextContentEditable = (element: HTMLElement) => {
231
263
  selection.getRangeAt(0).insertNode(document.createTextNode(paste || ''));
232
264
  selection.collapseToEnd();
233
265
  });
234
- }
266
+ };
235
267
 
236
268
  const mapVerticalAlignToFlex = (verticalAlignmentValue: string | undefined) => {
237
269
  switch (verticalAlignmentValue) {