@pdfme/schemas 4.2.3 → 4.2.4-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.
- package/dist/cjs/src/barcodes/index.js +8 -1
- package/dist/cjs/src/barcodes/index.js.map +1 -1
- package/dist/cjs/src/barcodes/propPanel.js +0 -12
- package/dist/cjs/src/barcodes/propPanel.js.map +1 -1
- package/dist/cjs/src/graphics/image.js +2 -2
- package/dist/cjs/src/graphics/image.js.map +1 -1
- package/dist/cjs/src/graphics/svg.js +2 -2
- package/dist/cjs/src/graphics/svg.js.map +1 -1
- package/dist/cjs/src/multiVariableText/index.js +7 -1
- package/dist/cjs/src/multiVariableText/index.js.map +1 -1
- package/dist/cjs/src/multiVariableText/propPanel.js +0 -1
- package/dist/cjs/src/multiVariableText/propPanel.js.map +1 -1
- package/dist/cjs/src/shapes/line.js +1 -1
- package/dist/cjs/src/shapes/line.js.map +1 -1
- package/dist/cjs/src/shapes/rectAndEllipse.js +10 -3
- package/dist/cjs/src/shapes/rectAndEllipse.js.map +1 -1
- package/dist/cjs/src/tables/index.js +1 -0
- package/dist/cjs/src/tables/index.js.map +1 -1
- package/dist/cjs/src/tables/propPanel.js +0 -1
- package/dist/cjs/src/tables/propPanel.js.map +1 -1
- package/dist/cjs/src/text/index.js +7 -2
- package/dist/cjs/src/text/index.js.map +1 -1
- package/dist/cjs/src/text/pdfRender.js +1 -1
- package/dist/cjs/src/text/pdfRender.js.map +1 -1
- package/dist/cjs/src/text/propPanel.js +4 -2
- package/dist/cjs/src/text/propPanel.js.map +1 -1
- package/dist/cjs/src/text/uiRender.js +48 -7
- package/dist/cjs/src/text/uiRender.js.map +1 -1
- package/dist/esm/src/barcodes/index.js +8 -1
- package/dist/esm/src/barcodes/index.js.map +1 -1
- package/dist/esm/src/barcodes/propPanel.js +0 -12
- package/dist/esm/src/barcodes/propPanel.js.map +1 -1
- package/dist/esm/src/graphics/image.js +2 -2
- package/dist/esm/src/graphics/image.js.map +1 -1
- package/dist/esm/src/graphics/svg.js +2 -2
- package/dist/esm/src/graphics/svg.js.map +1 -1
- package/dist/esm/src/multiVariableText/index.js +7 -1
- package/dist/esm/src/multiVariableText/index.js.map +1 -1
- package/dist/esm/src/multiVariableText/propPanel.js +0 -1
- package/dist/esm/src/multiVariableText/propPanel.js.map +1 -1
- package/dist/esm/src/shapes/line.js +1 -1
- package/dist/esm/src/shapes/line.js.map +1 -1
- package/dist/esm/src/shapes/rectAndEllipse.js +10 -3
- package/dist/esm/src/shapes/rectAndEllipse.js.map +1 -1
- package/dist/esm/src/tables/index.js +1 -0
- package/dist/esm/src/tables/index.js.map +1 -1
- package/dist/esm/src/tables/propPanel.js +0 -1
- package/dist/esm/src/tables/propPanel.js.map +1 -1
- package/dist/esm/src/text/index.js +7 -2
- package/dist/esm/src/text/index.js.map +1 -1
- package/dist/esm/src/text/pdfRender.js +1 -1
- package/dist/esm/src/text/pdfRender.js.map +1 -1
- package/dist/esm/src/text/propPanel.js +4 -2
- package/dist/esm/src/text/propPanel.js.map +1 -1
- package/dist/esm/src/text/uiRender.js +48 -7
- package/dist/esm/src/text/uiRender.js.map +1 -1
- package/dist/types/src/shapes/rectAndEllipse.d.ts +8 -6
- package/package.json +1 -1
- package/src/barcodes/index.ts +9 -1
- package/src/barcodes/propPanel.ts +0 -13
- package/src/graphics/image.ts +2 -2
- package/src/graphics/svg.ts +2 -2
- package/src/multiVariableText/index.ts +7 -1
- package/src/multiVariableText/propPanel.ts +0 -1
- package/src/shapes/line.ts +1 -1
- package/src/shapes/rectAndEllipse.ts +10 -3
- package/src/tables/index.ts +1 -0
- package/src/tables/propPanel.ts +0 -1
- package/src/text/index.ts +7 -2
- package/src/text/pdfRender.ts +1 -1
- package/src/text/propPanel.ts +4 -2
- package/src/text/uiRender.ts +75 -43
package/src/text/uiRender.ts
CHANGED
@@ -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
|
-
|
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
|
77
|
+
const processedText = replaceUnsupportedChars(value, fontKitFont);
|
39
78
|
|
40
79
|
if (!isEditable(mode, schema)) {
|
41
80
|
// Read-only mode
|
42
|
-
textBlock.innerHTML =
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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)
|
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
|
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
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
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) {
|