@pdfme/ui 2.2.0 → 3.0.0-beta.2

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 (94) hide show
  1. package/README.md +33 -35
  2. package/__mocks__/form-render.js +7 -0
  3. package/dist/index.js +1 -1
  4. package/dist/index.js.LICENSE.txt +42 -4
  5. package/dist/index.js.map +1 -1
  6. package/dist/types/Designer.d.ts +3 -0
  7. package/dist/types/builtInPropPanel.d.ts +3 -0
  8. package/dist/types/builtInRenderer.d.ts +3 -0
  9. package/dist/types/class.d.ts +18 -38
  10. package/dist/types/components/CtlBar/Pager.d.ts +3 -2
  11. package/dist/types/components/CtlBar/Zoom.d.ts +3 -2
  12. package/dist/types/components/CtlBar/index.d.ts +3 -2
  13. package/dist/types/components/Designer/{Main → Canvas}/Guides.d.ts +2 -2
  14. package/dist/types/components/Designer/Canvas/Mask.d.ts +4 -0
  15. package/dist/types/components/Designer/Canvas/Moveable.d.ts +37 -0
  16. package/dist/types/components/Designer/{Main → Canvas}/Selecto.d.ts +2 -1
  17. package/dist/types/components/Designer/{Main → Canvas}/index.d.ts +3 -6
  18. package/dist/types/components/Designer/Sidebar/DetailView/AlignWidget.d.ts +4 -0
  19. package/dist/types/components/Designer/Sidebar/DetailView/WidgetRenderer.d.ts +7 -0
  20. package/dist/types/components/Designer/Sidebar/DetailView/index.d.ts +5 -4
  21. package/dist/types/components/Designer/Sidebar/ListView/SelectableSortableContainer.d.ts +3 -2
  22. package/dist/types/components/Designer/Sidebar/ListView/SelectableSortableItem.d.ts +1 -1
  23. package/dist/types/components/Designer/Sidebar/ListView/index.d.ts +3 -2
  24. package/dist/types/components/Designer/Sidebar/index.d.ts +3 -23
  25. package/dist/types/components/Designer/index.d.ts +6 -107
  26. package/dist/types/components/Divider.d.ts +2 -1
  27. package/dist/types/components/ErrorScreen.d.ts +7 -0
  28. package/dist/types/components/Paper.d.ts +3 -2
  29. package/dist/types/components/Preview.d.ts +10 -2
  30. package/dist/types/components/Renderer.d.ts +10 -0
  31. package/dist/types/components/Root.d.ts +1 -1
  32. package/dist/types/components/Spinner.d.ts +2 -1
  33. package/dist/types/components/UnitPager.d.ts +3 -2
  34. package/dist/types/constants.d.ts +3 -3
  35. package/dist/types/contexts.d.ts +4 -1
  36. package/dist/types/helper.d.ts +4 -46
  37. package/dist/types/hooks.d.ts +2 -2
  38. package/dist/types/i18n.d.ts +4 -2
  39. package/dist/types/index.d.ts +1 -4
  40. package/dist/types/types.d.ts +25 -0
  41. package/package.json +19 -8
  42. package/src/Designer.tsx +69 -21
  43. package/src/Form.tsx +18 -14
  44. package/src/Viewer.tsx +6 -2
  45. package/src/builtInPropPanel.ts +5 -0
  46. package/src/builtInRenderer.ts +5 -0
  47. package/src/class.ts +25 -2
  48. package/src/components/CtlBar/index.tsx +4 -7
  49. package/src/components/Designer/{Main → Canvas}/Guides.tsx +2 -2
  50. package/src/components/Designer/{Main → Canvas}/Moveable.tsx +23 -19
  51. package/src/components/Designer/{Main → Canvas}/index.tsx +77 -30
  52. package/src/components/Designer/Sidebar/DetailView/AlignWidget.tsx +182 -0
  53. package/src/components/Designer/Sidebar/DetailView/WidgetRenderer.tsx +28 -0
  54. package/src/components/Designer/Sidebar/DetailView/index.tsx +153 -22
  55. package/src/components/Designer/Sidebar/ListView/Item.tsx +1 -1
  56. package/src/components/Designer/Sidebar/ListView/SelectableSortableContainer.tsx +6 -6
  57. package/src/components/Designer/Sidebar/ListView/index.tsx +1 -4
  58. package/src/components/Designer/Sidebar/index.tsx +26 -60
  59. package/src/components/Designer/index.tsx +53 -32
  60. package/src/components/{Error.tsx → ErrorScreen.tsx} +2 -2
  61. package/src/components/Paper.tsx +35 -9
  62. package/src/components/Preview.tsx +48 -50
  63. package/src/components/Renderer.tsx +90 -0
  64. package/src/components/Root.tsx +5 -1
  65. package/src/constants.ts +4 -4
  66. package/src/contexts.ts +7 -0
  67. package/src/helper.ts +19 -122
  68. package/src/hooks.ts +6 -5
  69. package/src/i18n.ts +48 -11
  70. package/src/index.ts +1 -76
  71. package/src/types.ts +36 -0
  72. package/tsconfig.json +2 -1
  73. package/webpack.config.js +6 -1
  74. package/dist/types/components/Designer/Main/Mask.d.ts +0 -3
  75. package/dist/types/components/Designer/Main/Moveable.d.ts +0 -31
  76. package/dist/types/components/Designer/Sidebar/DetailView/ExampleInputEditor.d.ts +0 -6
  77. package/dist/types/components/Designer/Sidebar/DetailView/PositionAndSizeEditor.d.ts +0 -6
  78. package/dist/types/components/Designer/Sidebar/DetailView/TextPropEditor.d.ts +0 -6
  79. package/dist/types/components/Designer/Sidebar/DetailView/TypeAndKeyEditor.d.ts +0 -6
  80. package/dist/types/components/Error.d.ts +0 -6
  81. package/dist/types/components/Schemas/BarcodeSchema.d.ts +0 -15
  82. package/dist/types/components/Schemas/ImageSchema.d.ts +0 -15
  83. package/dist/types/components/Schemas/SchemaUI.d.ts +0 -15
  84. package/dist/types/components/Schemas/TextSchema.d.ts +0 -28
  85. package/src/components/Designer/Sidebar/DetailView/ExampleInputEditor.tsx +0 -85
  86. package/src/components/Designer/Sidebar/DetailView/PositionAndSizeEditor.tsx +0 -275
  87. package/src/components/Designer/Sidebar/DetailView/TextPropEditor.tsx +0 -357
  88. package/src/components/Designer/Sidebar/DetailView/TypeAndKeyEditor.tsx +0 -87
  89. package/src/components/Schemas/BarcodeSchema.tsx +0 -124
  90. package/src/components/Schemas/ImageSchema.tsx +0 -87
  91. package/src/components/Schemas/SchemaUI.tsx +0 -62
  92. package/src/components/Schemas/TextSchema.tsx +0 -175
  93. /package/src/components/Designer/{Main → Canvas}/Mask.tsx +0 -0
  94. /package/src/components/Designer/{Main → Canvas}/Selecto.tsx +0 -0
@@ -1,87 +0,0 @@
1
- import React, { useState, forwardRef, ChangeEvent, Ref } from 'react';
2
- import { ImageSchema } from '@pdfme/common';
3
- import { SchemaUIProps } from './SchemaUI';
4
- import { readFiles } from '../../helper';
5
- import { ZOOM } from '../../constants';
6
- import { XMarkIcon } from '@heroicons/react/24/outline';
7
-
8
- type Props = SchemaUIProps & { schema: ImageSchema };
9
-
10
- const ImageSchemaUI = (props: Props, ref: Ref<HTMLInputElement>) => {
11
- const { editable, placeholder, tabIndex, schema, onChange, onStopEditing } = props;
12
- const [fileName, setFileName] = useState<string>('');
13
- const hasData = Boolean(schema.data);
14
-
15
- const size: React.CSSProperties = { width: schema.width * ZOOM, height: schema.height * ZOOM };
16
-
17
- return (
18
- <>
19
- <div
20
- style={{
21
- ...size,
22
- opacity: hasData ? 1 : 0.5,
23
- backgroundImage: hasData || !editable ? 'none' : `url(${placeholder})`,
24
- backgroundSize: `${size.width}px ${size.height}px`,
25
- }}
26
- onClick={(e) => {
27
- if (editable) {
28
- e.stopPropagation();
29
- }
30
- }}
31
- >
32
- {hasData && <img style={{ ...size, borderRadius: 0 }} src={schema.data} />}
33
- {hasData && editable && (
34
- <button
35
- tabIndex={tabIndex}
36
- style={{
37
- position: 'absolute',
38
- top: 0,
39
- left: 0,
40
- zIndex: 1,
41
- display: 'flex',
42
- justifyContent: 'center',
43
- alignItems: 'center',
44
- color: '#333',
45
- background: '#f2f2f2',
46
- borderRadius: 2,
47
- border: '1px solid #767676',
48
- cursor: 'pointer',
49
- height: 24,
50
- width: 24,
51
- }}
52
- aria-label="close"
53
- onClick={() => {
54
- setFileName('');
55
- onChange('');
56
- }}
57
- >
58
- <XMarkIcon width={10} height={10} />
59
- </button>
60
- )}
61
- </div>
62
- <label
63
- style={{
64
- display: editable ? 'flex' : 'none',
65
- position: 'absolute',
66
- top: '50%',
67
- width: '100%',
68
- cursor: 'pointer',
69
- }}
70
- >
71
- <input
72
- ref={ref}
73
- tabIndex={tabIndex}
74
- value={fileName}
75
- onChange={(event: ChangeEvent<HTMLInputElement>) =>
76
- readFiles(event.target.files, 'dataURL').then((result) => onChange(result as string))
77
- }
78
- onBlur={onStopEditing}
79
- type="file"
80
- accept="image/jpeg, image/png"
81
- />
82
- </label>
83
- </>
84
- );
85
- };
86
-
87
- export default forwardRef<HTMLInputElement, Props>(ImageSchemaUI);
@@ -1,62 +0,0 @@
1
- import React, { forwardRef, RefObject, Ref, ReactNode } from 'react';
2
- import { SchemaForUI, isTextSchema, isImageSchema, isBarcodeSchema } from '@pdfme/common';
3
- import { ZOOM, SELECTABLE_CLASSNAME } from '../../constants';
4
- import TextSchema from './TextSchema';
5
- import ImageSchema from './ImageSchema';
6
- import BarcodeSchema from './BarcodeSchema';
7
-
8
- export interface SchemaUIProps {
9
- schema: SchemaForUI;
10
- editable: boolean;
11
- onChange: (value: string) => void;
12
- onStopEditing: () => void;
13
- tabIndex?: number;
14
- placeholder?: string;
15
- }
16
-
17
- type Props = SchemaUIProps & {
18
- outline: string;
19
- onChangeHoveringSchemaId?: (id: string | null) => void;
20
- };
21
-
22
- const Wrapper = ({
23
- children,
24
- outline,
25
- onChangeHoveringSchemaId,
26
- schema,
27
- }: Props & { children: ReactNode }) => (
28
- <div
29
- title={schema.key}
30
- onMouseEnter={() => onChangeHoveringSchemaId && onChangeHoveringSchemaId(schema.id)}
31
- onMouseLeave={() => onChangeHoveringSchemaId && onChangeHoveringSchemaId(null)}
32
- className={SELECTABLE_CLASSNAME}
33
- id={schema.id}
34
- style={{
35
- position: 'absolute',
36
- cursor: 'pointer',
37
- height: schema.height * ZOOM,
38
- width: schema.width * ZOOM,
39
- top: schema.position.y * ZOOM,
40
- left: schema.position.x * ZOOM,
41
- outline,
42
- }}
43
- >
44
- {children}
45
- </div>
46
- );
47
-
48
- const SchemaUI = (props: Props, ref: Ref<HTMLTextAreaElement | HTMLInputElement>) => {
49
- const r = {
50
- [props.editable ? 'ref' : '']: ref as RefObject<HTMLTextAreaElement | HTMLInputElement>,
51
- };
52
- const { schema } = props;
53
-
54
- return (
55
- <Wrapper {...props}>
56
- {isTextSchema(schema) && <TextSchema {...r} {...props} schema={schema} />}
57
- {isImageSchema(schema) && <ImageSchema {...r} {...props} schema={schema} />}
58
- {isBarcodeSchema(schema) && <BarcodeSchema {...r} {...props} schema={schema} />}
59
- </Wrapper>
60
- );
61
- };
62
- export default forwardRef<HTMLTextAreaElement | HTMLInputElement, Props>(SchemaUI);
@@ -1,175 +0,0 @@
1
- import React, { useContext, forwardRef, Ref, useState, useEffect } from 'react';
2
- import {
3
- DEFAULT_FONT_SIZE,
4
- DEFAULT_ALIGNMENT,
5
- VERTICAL_ALIGN_TOP,
6
- VERTICAL_ALIGN_MIDDLE,
7
- VERTICAL_ALIGN_BOTTOM,
8
- DEFAULT_VERTICAL_ALIGNMENT,
9
- DEFAULT_LINE_HEIGHT,
10
- DEFAULT_CHARACTER_SPACING,
11
- DEFAULT_FONT_COLOR,
12
- TextSchema,
13
- calculateDynamicFontSize,
14
- getFontKitFont,
15
- getBrowserVerticalFontAdjustments,
16
- } from '@pdfme/common';
17
- import { SchemaUIProps } from './SchemaUI';
18
- import { ZOOM } from '../../constants';
19
- import { FontContext } from '../../contexts';
20
-
21
- const mapVerticalAlignToFlex = (verticalAlignmentValue: string | undefined) => {
22
- switch (verticalAlignmentValue) {
23
- case VERTICAL_ALIGN_TOP:
24
- return 'flex-start';
25
- case VERTICAL_ALIGN_MIDDLE:
26
- return 'center';
27
- case VERTICAL_ALIGN_BOTTOM:
28
- return 'flex-end';
29
- }
30
- return 'flex-start';
31
- };
32
-
33
- type Props = SchemaUIProps & { schema: TextSchema };
34
-
35
- const TextSchemaUI = (
36
- { schema, editable, placeholder, tabIndex, onChange, onStopEditing }: Props,
37
- ref: Ref<HTMLTextAreaElement>
38
- ) => {
39
- const font = useContext(FontContext);
40
- const [dynamicFontSize, setDynamicFontSize] = useState<number | undefined>(undefined);
41
- const [topAdjustment, setTopAdjustment] = useState<number>(0);
42
- const [bottomAdjustment, setBottomAdjustment] = useState<number>(0);
43
-
44
- useEffect(() => {
45
- if (schema.dynamicFontSize && schema.data) {
46
- calculateDynamicFontSize({
47
- textSchema: schema,
48
- font,
49
- input: schema.data,
50
- startingFontSize: dynamicFontSize,
51
- }).then(setDynamicFontSize);
52
- } else {
53
- setDynamicFontSize(undefined);
54
- }
55
- }, [
56
- schema.data,
57
- schema.width,
58
- schema.height,
59
- schema.dynamicFontSize?.min,
60
- schema.dynamicFontSize?.max,
61
- schema.dynamicFontSize?.fit,
62
- schema.characterSpacing,
63
- schema.lineHeight,
64
- font
65
- ]);
66
-
67
- useEffect(() => {
68
- getFontKitFont(schema, font).then(fontKitFont => {
69
- // Depending on vertical alignment, we need to move the top or bottom of the font to keep
70
- // it within it's defined box and align it with the generated pdf.
71
- const { topAdj, bottomAdj } = getBrowserVerticalFontAdjustments(
72
- fontKitFont,
73
- dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
74
- schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
75
- schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
76
- );
77
- setTopAdjustment(topAdj);
78
- setBottomAdjustment(bottomAdj);
79
- });
80
-
81
- if (ref && 'current' in ref) {
82
- const textarea = ref.current;
83
-
84
- if (textarea) {
85
- // Textareas cannot be vertically aligned, so we need to adjust the height of the textarea
86
- // to exactly fit the height of it's content, whilst aligned within it's container.
87
- // This gives the appearance of being vertically aligned.
88
- textarea.style.height = 'auto'; // Reset the height to auto to ensure we get the correct height.
89
- textarea.style.height = `${textarea.scrollHeight}px`;
90
- }
91
- }
92
- }, [
93
- schema.data,
94
- schema.width,
95
- schema.height,
96
- schema.fontName,
97
- schema.fontSize,
98
- schema.dynamicFontSize?.max,
99
- schema.dynamicFontSize?.min,
100
- schema.dynamicFontSize?.fit,
101
- schema.characterSpacing,
102
- schema.lineHeight,
103
- schema.verticalAlignment,
104
- font,
105
- dynamicFontSize,
106
- editable,
107
- ]);
108
-
109
- const containerStyle: React.CSSProperties = {
110
- position: 'absolute',
111
- top: 0,
112
- padding: 0,
113
- height: schema.height * ZOOM,
114
- width: schema.width * ZOOM,
115
- resize: 'none',
116
- backgroundColor: schema.data && schema.backgroundColor ? schema.backgroundColor : 'rgb(242 244 255 / 75%)',
117
- border: 'none',
118
- display: 'flex',
119
- flexDirection: 'column',
120
- justifyContent: mapVerticalAlignToFlex(schema.verticalAlignment),
121
- };
122
-
123
- const textareaStyle: React.CSSProperties = {
124
- padding: 0,
125
- resize: 'none',
126
- border: 'none',
127
- outline: 'none',
128
- paddingTop: topAdjustment,
129
- background: 'none',
130
- };
131
-
132
- const fontStyles: React.CSSProperties = {
133
- fontFamily: schema.fontName ? `'${schema.fontName}'` : 'inherit',
134
- color: schema.fontColor ? schema.fontColor : DEFAULT_FONT_COLOR,
135
- fontSize: `${dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE}pt`,
136
- letterSpacing: `${schema.characterSpacing ?? DEFAULT_CHARACTER_SPACING}pt`,
137
- lineHeight: `${schema.lineHeight ?? DEFAULT_LINE_HEIGHT}em`,
138
- textAlign: schema.alignment ?? DEFAULT_ALIGNMENT,
139
- whiteSpace: 'pre-wrap',
140
- wordBreak: 'break-word',
141
- };
142
-
143
- return editable ? (
144
- <div style={containerStyle}>
145
- <textarea
146
- ref={ref}
147
- placeholder={placeholder}
148
- tabIndex={tabIndex}
149
- style={{ ...textareaStyle, ...fontStyles }}
150
- onChange={(e) => onChange(e.target.value)}
151
- onBlur={onStopEditing}
152
- value={schema.data}
153
- ></textarea>
154
- </div>
155
- ) : (
156
- <div style={containerStyle}>
157
- <div
158
- style={{
159
- ...fontStyles,
160
- marginBottom: bottomAdjustment,
161
- paddingTop: topAdjustment,
162
- }}
163
- >
164
- {/* Set the letterSpacing of the last character to 0. */}
165
- {schema.data.split('').map((l, i) => (
166
- <span key={i} style={{ letterSpacing: String(schema.data).length === i + 1 ? 0 : 'inherit' }}>
167
- {l}
168
- </span>
169
- ))}
170
- </div>
171
- </div>
172
- );
173
- };
174
-
175
- export default forwardRef<HTMLTextAreaElement, Props>(TextSchemaUI);