@pdfme/ui 0.0.0

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 (97) hide show
  1. package/README.md +9 -0
  2. package/__mocks__/assetsTransformer.js +7 -0
  3. package/__mocks__/form-render.js +7 -0
  4. package/__mocks__/lucide-react.js +19 -0
  5. package/dist/index.es.js +159393 -0
  6. package/dist/index.umd.js +1041 -0
  7. package/dist/types/__tests__/assets/helper.d.ts +3 -0
  8. package/dist/types/__tests__/components/Designer.test.d.ts +1 -0
  9. package/dist/types/__tests__/components/PluginIcon.test.d.ts +1 -0
  10. package/dist/types/__tests__/components/Preview.test.d.ts +1 -0
  11. package/dist/types/__tests__/helper.test.d.ts +1 -0
  12. package/dist/types/src/Designer.d.ts +21 -0
  13. package/dist/types/src/Form.d.ts +24 -0
  14. package/dist/types/src/Viewer.d.ts +15 -0
  15. package/dist/types/src/class.d.ts +89 -0
  16. package/dist/types/src/components/AppContextProvider.d.ts +11 -0
  17. package/dist/types/src/components/CtlBar.d.ts +14 -0
  18. package/dist/types/src/components/Designer/Canvas/Guides.d.ts +9 -0
  19. package/dist/types/src/components/Designer/Canvas/Mask.d.ts +4 -0
  20. package/dist/types/src/components/Designer/Canvas/Moveable.d.ts +37 -0
  21. package/dist/types/src/components/Designer/Canvas/Padding.d.ts +6 -0
  22. package/dist/types/src/components/Designer/Canvas/Selecto.d.ts +10 -0
  23. package/dist/types/src/components/Designer/Canvas/index.d.ts +22 -0
  24. package/dist/types/src/components/Designer/LeftSidebar.d.ts +8 -0
  25. package/dist/types/src/components/Designer/PluginIcon.d.ts +10 -0
  26. package/dist/types/src/components/Designer/RightSidebar/DetailView/AlignWidget.d.ts +4 -0
  27. package/dist/types/src/components/Designer/RightSidebar/DetailView/ButtonGroupWidget.d.ts +4 -0
  28. package/dist/types/src/components/Designer/RightSidebar/DetailView/WidgetRenderer.d.ts +7 -0
  29. package/dist/types/src/components/Designer/RightSidebar/DetailView/index.d.ts +8 -0
  30. package/dist/types/src/components/Designer/RightSidebar/ListView/Item.d.ts +45 -0
  31. package/dist/types/src/components/Designer/RightSidebar/ListView/SelectableSortableContainer.d.ts +4 -0
  32. package/dist/types/src/components/Designer/RightSidebar/ListView/SelectableSortableItem.d.ts +14 -0
  33. package/dist/types/src/components/Designer/RightSidebar/ListView/index.d.ts +4 -0
  34. package/dist/types/src/components/Designer/RightSidebar/index.d.ts +4 -0
  35. package/dist/types/src/components/Designer/RightSidebar/layout.d.ts +15 -0
  36. package/dist/types/src/components/Designer/index.d.ts +11 -0
  37. package/dist/types/src/components/ErrorScreen.d.ts +7 -0
  38. package/dist/types/src/components/Paper.d.ts +20 -0
  39. package/dist/types/src/components/Preview.d.ts +15 -0
  40. package/dist/types/src/components/Renderer.d.ts +13 -0
  41. package/dist/types/src/components/Root.d.ts +9 -0
  42. package/dist/types/src/components/Spinner.d.ts +3 -0
  43. package/dist/types/src/components/StaticSchema.d.ts +10 -0
  44. package/dist/types/src/components/UnitPager.d.ts +10 -0
  45. package/dist/types/src/constants.d.ts +11 -0
  46. package/dist/types/src/contexts.d.ts +10 -0
  47. package/dist/types/src/helper.d.ts +73 -0
  48. package/dist/types/src/hooks.d.ts +46 -0
  49. package/dist/types/src/i18n.d.ts +3 -0
  50. package/dist/types/src/index.d.ts +4 -0
  51. package/dist/types/src/theme.d.ts +2 -0
  52. package/dist/types/src/types.d.ts +19 -0
  53. package/eslint.config.mjs +41 -0
  54. package/package.json +127 -0
  55. package/src/Designer.tsx +107 -0
  56. package/src/Form.tsx +102 -0
  57. package/src/Viewer.tsx +59 -0
  58. package/src/class.ts +188 -0
  59. package/src/components/AppContextProvider.tsx +78 -0
  60. package/src/components/CtlBar.tsx +183 -0
  61. package/src/components/Designer/Canvas/Guides.tsx +49 -0
  62. package/src/components/Designer/Canvas/Mask.tsx +20 -0
  63. package/src/components/Designer/Canvas/Moveable.tsx +91 -0
  64. package/src/components/Designer/Canvas/Padding.tsx +56 -0
  65. package/src/components/Designer/Canvas/Selecto.tsx +45 -0
  66. package/src/components/Designer/Canvas/index.tsx +536 -0
  67. package/src/components/Designer/LeftSidebar.tsx +120 -0
  68. package/src/components/Designer/PluginIcon.tsx +87 -0
  69. package/src/components/Designer/RightSidebar/DetailView/AlignWidget.tsx +229 -0
  70. package/src/components/Designer/RightSidebar/DetailView/ButtonGroupWidget.tsx +78 -0
  71. package/src/components/Designer/RightSidebar/DetailView/WidgetRenderer.tsx +28 -0
  72. package/src/components/Designer/RightSidebar/DetailView/index.tsx +469 -0
  73. package/src/components/Designer/RightSidebar/ListView/Item.tsx +158 -0
  74. package/src/components/Designer/RightSidebar/ListView/SelectableSortableContainer.tsx +204 -0
  75. package/src/components/Designer/RightSidebar/ListView/SelectableSortableItem.tsx +88 -0
  76. package/src/components/Designer/RightSidebar/ListView/index.tsx +116 -0
  77. package/src/components/Designer/RightSidebar/index.tsx +72 -0
  78. package/src/components/Designer/RightSidebar/layout.tsx +75 -0
  79. package/src/components/Designer/index.tsx +389 -0
  80. package/src/components/ErrorScreen.tsx +33 -0
  81. package/src/components/Paper.tsx +117 -0
  82. package/src/components/Preview.tsx +220 -0
  83. package/src/components/Renderer.tsx +165 -0
  84. package/src/components/Root.tsx +38 -0
  85. package/src/components/Spinner.tsx +45 -0
  86. package/src/components/StaticSchema.tsx +50 -0
  87. package/src/components/UnitPager.tsx +119 -0
  88. package/src/constants.ts +21 -0
  89. package/src/contexts.ts +14 -0
  90. package/src/helper.ts +534 -0
  91. package/src/hooks.ts +308 -0
  92. package/src/i18n.ts +903 -0
  93. package/src/index.ts +5 -0
  94. package/src/theme.ts +20 -0
  95. package/src/types.ts +20 -0
  96. package/tsconfig.json +48 -0
  97. package/vite.config.mts +22 -0
@@ -0,0 +1,73 @@
1
+ import { Template, BasePdf, SchemaForUI, Size, PluginRegistry } from '@pdfme/common';
2
+ export declare const uuid: () => string;
3
+ export declare const debounce: <T extends (...args: unknown[]) => unknown>(cb: T, wait?: number) => T;
4
+ export declare const round: (number: number, precision: number) => number;
5
+ export declare const flatten: <T>(arr: T[][]) => T[];
6
+ export declare const initShortCuts: (arg: {
7
+ move: (command: "up" | "down" | "left" | "right", isShift: boolean) => void;
8
+ remove: () => void;
9
+ esc: () => void;
10
+ copy: () => void;
11
+ paste: () => void;
12
+ redo: () => void;
13
+ undo: () => void;
14
+ save: () => void;
15
+ selectAll: () => void;
16
+ }) => void;
17
+ export declare const destroyShortCuts: () => void;
18
+ export declare const arrayBufferToBase64: (arrayBuffer: ArrayBuffer) => string;
19
+ export declare const template2SchemasList: (_template: Template) => Promise<{
20
+ name: string;
21
+ type: string;
22
+ position: {
23
+ x: number;
24
+ y: number;
25
+ };
26
+ width: number;
27
+ height: number;
28
+ id: string;
29
+ content?: string | undefined;
30
+ rotate?: number | undefined;
31
+ opacity?: number | undefined;
32
+ readOnly?: boolean | undefined;
33
+ required?: boolean | undefined;
34
+ __bodyRange?: {
35
+ start: number;
36
+ end?: number | undefined;
37
+ } | undefined;
38
+ __isSplit?: boolean | undefined;
39
+ }[][]>;
40
+ export declare const schemasList2template: (schemasList: SchemaForUI[][], basePdf: BasePdf) => Template;
41
+ export declare const getUniqueSchemaName: (arg: {
42
+ copiedSchemaName: string;
43
+ schema: SchemaForUI[];
44
+ stackUniqueSchemaNames: string[];
45
+ }) => string;
46
+ export declare const moveCommandToChangeSchemasArg: (props: {
47
+ command: "up" | "down" | "left" | "right";
48
+ activeSchemas: SchemaForUI[];
49
+ isShift: boolean;
50
+ pageSize: Size;
51
+ }) => {
52
+ key: string;
53
+ value: number;
54
+ schemaId: string;
55
+ }[];
56
+ export declare const getPagesScrollTopByIndex: (pageSizes: Size[], index: number, scale: number) => number;
57
+ export declare const changeSchemas: (args: {
58
+ objs: {
59
+ key: string;
60
+ value: unknown;
61
+ schemaId: string;
62
+ }[];
63
+ schemas: SchemaForUI[];
64
+ basePdf: BasePdf;
65
+ pluginsRegistry: PluginRegistry;
66
+ pageSize: {
67
+ width: number;
68
+ height: number;
69
+ };
70
+ commitSchemas: (newSchemas: SchemaForUI[]) => void;
71
+ }) => void;
72
+ export declare const useMaxZoom: () => number;
73
+ export declare const setFontNameRecursively: (obj: Record<string, unknown>, fontName: string, seen?: WeakSet<WeakKey>) => void;
@@ -0,0 +1,46 @@
1
+ import { RefObject } from 'react';
2
+ import { Template, Size, SchemaForUI, ChangeSchemas } from '@pdfme/common';
3
+ export declare const usePrevious: <T>(value: T) => T | null;
4
+ type UIPreProcessorProps = {
5
+ template: Template;
6
+ size: Size;
7
+ zoomLevel: number;
8
+ maxZoom: number;
9
+ };
10
+ export declare const useUIPreProcessor: ({ template, size, zoomLevel, maxZoom }: UIPreProcessorProps) => {
11
+ backgrounds: string[];
12
+ pageSizes: {
13
+ height: number;
14
+ width: number;
15
+ }[];
16
+ scale: number;
17
+ error: Error | null;
18
+ refresh: (template: Template) => Promise<void>;
19
+ };
20
+ type ScrollPageCursorProps = {
21
+ ref: RefObject<HTMLDivElement>;
22
+ pageSizes: Size[];
23
+ scale: number;
24
+ pageCursor: number;
25
+ onChangePageCursor: (page: number) => void;
26
+ };
27
+ export declare const useScrollPageCursor: ({ ref, pageSizes, scale, pageCursor, onChangePageCursor, }: ScrollPageCursorProps) => void;
28
+ export declare const useMountStatus: () => boolean;
29
+ interface UseInitEventsParams {
30
+ pageCursor: number;
31
+ pageSizes: Size[];
32
+ activeElements: HTMLElement[];
33
+ template: Template;
34
+ schemasList: SchemaForUI[][];
35
+ changeSchemas: ChangeSchemas;
36
+ commitSchemas: (newSchemas: SchemaForUI[]) => void;
37
+ removeSchemas: (ids: string[]) => void;
38
+ onSaveTemplate: (t: Template) => void;
39
+ past: React.MutableRefObject<SchemaForUI[][]>;
40
+ future: React.MutableRefObject<SchemaForUI[][]>;
41
+ setSchemasList: React.Dispatch<React.SetStateAction<SchemaForUI[][]>>;
42
+ onEdit: (targets: HTMLElement[]) => void;
43
+ onEditEnd: () => void;
44
+ }
45
+ export declare const useInitEvents: ({ pageCursor, pageSizes, activeElements, template, schemasList, changeSchemas, commitSchemas, removeSchemas, onSaveTemplate, past, future, setSchemasList, onEdit, onEditEnd, }: UseInitEventsParams) => void;
46
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { Lang, Dict } from '@pdfme/common';
2
+ export declare const getDict: (lang: Lang) => Dict;
3
+ export declare const i18n: (key: keyof Dict, dict?: Dict) => string;
@@ -0,0 +1,4 @@
1
+ import Designer from './Designer.js';
2
+ import Form from './Form.js';
3
+ import Viewer from './Viewer.js';
4
+ export { Designer, Viewer, Form };
@@ -0,0 +1,2 @@
1
+ import type { ThemeConfig } from 'antd';
2
+ export declare const defaultTheme: ThemeConfig;
@@ -0,0 +1,19 @@
1
+ import type { SchemaForUI, Size, ChangeSchemas, BasePdf } from '@pdfme/common';
2
+ export type SidebarProps = {
3
+ height: number;
4
+ hoveringSchemaId: string | null;
5
+ onChangeHoveringSchemaId: (id: string | null) => void;
6
+ size: Size;
7
+ pageSize: Size;
8
+ basePdf: BasePdf;
9
+ activeElements: HTMLElement[];
10
+ schemas: SchemaForUI[];
11
+ schemasList: SchemaForUI[][];
12
+ onSortEnd: (sortedSchemas: SchemaForUI[]) => void;
13
+ onEdit: (id: string) => void;
14
+ onEditEnd: () => void;
15
+ changeSchemas: ChangeSchemas;
16
+ deselectSchema: () => void;
17
+ sidebarOpen: boolean;
18
+ setSidebarOpen: (sidebarOpen: boolean) => void;
19
+ };
@@ -0,0 +1,41 @@
1
+ import { fileURLToPath } from 'url';
2
+ import { dirname, resolve } from 'path';
3
+ import reactPlugin from 'eslint-plugin-react';
4
+ import reactHooksPlugin from 'eslint-plugin-react-hooks';
5
+
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = dirname(__filename);
8
+
9
+ // Import the root config
10
+ const rootConfigPath = resolve(__dirname, '../../eslint.config.mjs');
11
+ const rootConfig = await import(rootConfigPath);
12
+
13
+ export default [
14
+ ...rootConfig.default,
15
+ {
16
+ files: ['src/**/*.ts', 'src/**/*.tsx'],
17
+ languageOptions: {
18
+ parserOptions: {
19
+ project: ['./tsconfig.json'],
20
+ tsconfigRootDir: __dirname,
21
+ ecmaFeatures: {
22
+ jsx: true,
23
+ },
24
+ },
25
+ },
26
+ plugins: {
27
+ react: reactPlugin,
28
+ 'react-hooks': reactHooksPlugin,
29
+ },
30
+ settings: {
31
+ react: {
32
+ version: 'detect',
33
+ },
34
+ },
35
+ rules: {
36
+ ...reactPlugin.configs.recommended.rules,
37
+ 'react-hooks/rules-of-hooks': 'error',
38
+ 'react-hooks/exhaustive-deps': 'warn',
39
+ },
40
+ },
41
+ ];
package/package.json ADDED
@@ -0,0 +1,127 @@
1
+ {
2
+ "name": "@pdfme/ui",
3
+ "version": "0.0.0",
4
+ "sideEffects": false,
5
+ "author": "hand-dot",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "pdf",
9
+ "pdf-generation",
10
+ "pdf-designer",
11
+ "pdf-viewer",
12
+ "typescript",
13
+ "react"
14
+ ],
15
+ "description": "TypeScript base PDF generator and React base UI. Open source, developed by the community, and completely free to use under the MIT license!",
16
+ "homepage": "https://pdfme.com",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git@github.com:pdfme/pdfme.git"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/pdfme/pdfme/issues"
23
+ },
24
+ "main": "dist/index.umd.js",
25
+ "module": "dist/index.es.js",
26
+ "types": "dist/types/src/index.d.ts",
27
+ "scripts": {
28
+ "dev": "run-p devBuild:watch devBuildType:watch",
29
+ "devBuild:watch": "esbuild src/index.ts --bundle --outfile=dist/index.es.js --format=esm --watch",
30
+ "devBuildType:watch": "tsc --emitDeclarationOnly --watch",
31
+ "build": "vite build && tsc --emitDeclarationOnly",
32
+ "clean": "rimraf dist",
33
+ "lint": "eslint --ext .ts,.tsx src --config eslint.config.mjs",
34
+ "test": "jest",
35
+ "prune": "ts-prune src",
36
+ "prettier": "prettier --write 'src/**/*.{ts,tsx}'"
37
+ },
38
+ "dependencies": {
39
+ "@dnd-kit/core": "^6.0.8",
40
+ "@dnd-kit/sortable": "^10.0.0",
41
+ "@pdfme/converter": "*",
42
+ "@scena/react-guides": "^0.28.2",
43
+ "antd": "^5.27.4",
44
+ "dompurify": "^3.2.7",
45
+ "form-render": "^2.5.5",
46
+ "globrex": "^0.1.2",
47
+ "hotkeys-js": "^3.13.15",
48
+ "lucide-react": "^0.552.0",
49
+ "react": "^16.14.0",
50
+ "react-dom": "^16.14.0",
51
+ "react-moveable": "^0.56.0",
52
+ "react-refresh": "^0.17.0",
53
+ "react-selecto": "^1.12.0"
54
+ },
55
+ "devDependencies": {
56
+ "@pdfme/common": "*",
57
+ "@pdfme/schemas": "*",
58
+ "@testing-library/jest-dom": "^6.8.0",
59
+ "@testing-library/react": "^12.1.2",
60
+ "@types/dompurify": "^3.2.0",
61
+ "@types/jest": "^30.0.0",
62
+ "@types/react": "^17.0.52",
63
+ "@types/react-dom": "^17.0.18",
64
+ "@ungap/structured-clone": "^1.3.0",
65
+ "@vitejs/plugin-react": "^5.0.4",
66
+ "csstype": "^3.1.2",
67
+ "esbuild": "^0.25.10",
68
+ "eslint-plugin-react": "^7.37.5",
69
+ "eslint-plugin-react-hooks": "^5.2.0",
70
+ "is-path-inside": "^4.0.0",
71
+ "jest-canvas-mock": "^2.3.1",
72
+ "jest-environment-jsdom": "^29.7.0",
73
+ "postcss": "^8.5.6",
74
+ "process": "^0.11.10",
75
+ "rollup": "^4.44.1",
76
+ "vite": "^7.1.7",
77
+ "vite-plugin-css-injected-by-js": "^3.3.0",
78
+ "vite-tsconfig-paths": "^5.1.4"
79
+ },
80
+ "peerDependencies": {
81
+ "@pdfme/common": "latest",
82
+ "@pdfme/schemas": "latest"
83
+ },
84
+ "jest": {
85
+ "testEnvironment": "jest-environment-jsdom",
86
+ "setupFiles": [
87
+ "jest-canvas-mock"
88
+ ],
89
+ "setupFilesAfterEnv": [
90
+ "./__tests__/test-helpers.js"
91
+ ],
92
+ "moduleNameMapper": {
93
+ "\\.(png|css)$": "<rootDir>/__mocks__/assetsTransformer.js",
94
+ "^@pdfme/converter$": "<rootDir>/../converter/src/index.node.ts",
95
+ "^@pdfme/schemas/utils$": "<rootDir>/../schemas/src/utils.ts",
96
+ "^antd/es/": "antd/lib/",
97
+ "^form-render/es/": "form-render/lib/",
98
+ "^rc-picker/es/": "rc-picker/lib/",
99
+ "^lodash-es$": "lodash",
100
+ "\\^lucide-react$": "<rootDir>/__mocks__/lucide-react.js"
101
+ },
102
+ "resolver": "ts-jest-resolver",
103
+ "moduleFileExtensions": [
104
+ "js",
105
+ "ts",
106
+ "tsx"
107
+ ],
108
+ "transform": {
109
+ "^.+\\.tsx?$": [
110
+ "ts-jest",
111
+ {
112
+ "tsconfig": "tsconfig.json"
113
+ }
114
+ ]
115
+ },
116
+ "testMatch": [
117
+ "**/*.test.ts",
118
+ "**/*.test.tsx"
119
+ ],
120
+ "transformIgnorePatterns": [
121
+ "/node_modules/(?!lucide-react)"
122
+ ]
123
+ },
124
+ "publishConfig": {
125
+ "access": "public"
126
+ }
127
+ }
@@ -0,0 +1,107 @@
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom';
3
+ import {
4
+ cloneDeep,
5
+ Template,
6
+ DesignerProps,
7
+ checkDesignerProps,
8
+ checkTemplate,
9
+ PDFME_VERSION,
10
+ } from '@pdfme/common';
11
+ import { BaseUIClass } from './class.js';
12
+ import { DESTROYED_ERR_MSG } from './constants.js';
13
+ import DesignerComponent from './components/Designer/index.js';
14
+ import AppContextProvider from './components/AppContextProvider.js';
15
+
16
+ class Designer extends BaseUIClass {
17
+ private onSaveTemplateCallback?: (template: Template) => void;
18
+ private onChangeTemplateCallback?: (template: Template) => void;
19
+ private onPageChangeCallback?: (pageInfo: { currentPage: number; totalPages: number }) => void;
20
+ private pageCursor: number = 0;
21
+
22
+ constructor(props: DesignerProps) {
23
+ super(props);
24
+ checkDesignerProps(props);
25
+ }
26
+
27
+ public saveTemplate() {
28
+ if (!this.domContainer) throw Error(DESTROYED_ERR_MSG);
29
+ if (this.onSaveTemplateCallback) {
30
+ this.onSaveTemplateCallback(this.template);
31
+ }
32
+ }
33
+
34
+ public updateTemplate(template: Template) {
35
+ checkTemplate(template);
36
+ if (!this.domContainer) throw Error(DESTROYED_ERR_MSG);
37
+ this.template = cloneDeep(template);
38
+ if (this.onChangeTemplateCallback) {
39
+ this.onChangeTemplateCallback(template);
40
+ }
41
+ this.render();
42
+ }
43
+
44
+ public onSaveTemplate(cb: (template: Template) => void) {
45
+ this.onSaveTemplateCallback = cb;
46
+ }
47
+
48
+ public onChangeTemplate(cb: (template: Template) => void) {
49
+ this.onChangeTemplateCallback = cb;
50
+ }
51
+
52
+ public onPageChange(cb: (pageInfo: { currentPage: number; totalPages: number }) => void) {
53
+ this.onPageChangeCallback = cb;
54
+ }
55
+
56
+ public getPageCursor() {
57
+ return this.pageCursor;
58
+ }
59
+
60
+ public getTotalPages() {
61
+ if (!this.domContainer) throw Error(DESTROYED_ERR_MSG);
62
+ return this.template.schemas.length;
63
+ }
64
+
65
+ protected render() {
66
+ if (!this.domContainer) throw Error(DESTROYED_ERR_MSG);
67
+ ReactDOM.render(
68
+ <AppContextProvider
69
+ lang={this.getLang()}
70
+ font={this.getFont()}
71
+ plugins={this.getPluginsRegistry()}
72
+ options={this.getOptions()}
73
+ >
74
+ <DesignerComponent
75
+ template={this.template}
76
+ onSaveTemplate={(template) => {
77
+ this.template = template;
78
+ this.template.pdfmeVersion = PDFME_VERSION;
79
+ if (this.onSaveTemplateCallback) {
80
+ this.onSaveTemplateCallback(template);
81
+ }
82
+ }}
83
+ onChangeTemplate={(template) => {
84
+ this.template = template;
85
+ this.template.pdfmeVersion = PDFME_VERSION;
86
+ if (this.onChangeTemplateCallback) {
87
+ this.onChangeTemplateCallback(template);
88
+ }
89
+ }}
90
+ onPageCursorChange={(newPageCursor: number, totalPages: number) => {
91
+ this.pageCursor = newPageCursor;
92
+ if (this.onPageChangeCallback) {
93
+ this.onPageChangeCallback({
94
+ currentPage: newPageCursor,
95
+ totalPages: totalPages,
96
+ });
97
+ }
98
+ }}
99
+ size={this.size}
100
+ />
101
+ </AppContextProvider>,
102
+ this.domContainer,
103
+ );
104
+ }
105
+ }
106
+
107
+ export default Designer;
package/src/Form.tsx ADDED
@@ -0,0 +1,102 @@
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom';
3
+ import { PreviewProps } from '@pdfme/common';
4
+ import { PreviewUI } from './class.js';
5
+ import { DESTROYED_ERR_MSG } from './constants.js';
6
+ import AppContextProvider from './components/AppContextProvider.js';
7
+ import Preview from './components/Preview.js';
8
+
9
+ class Form extends PreviewUI {
10
+ private onChangeInputCallback?: (arg: { index: number; value: string; name: string }) => void;
11
+ private onPageChangeCallback?: (pageInfo: { currentPage: number; totalPages: number }) => void;
12
+ private pageCursor: number = 0;
13
+
14
+ constructor(props: PreviewProps) {
15
+ super(props);
16
+ }
17
+
18
+ public onChangeInput(cb: (arg: { index: number; value: string; name: string }) => void) {
19
+ this.onChangeInputCallback = cb;
20
+ }
21
+
22
+ public onPageChange(cb: (pageInfo: { currentPage: number; totalPages: number }) => void) {
23
+ this.onPageChangeCallback = cb;
24
+ }
25
+
26
+ public getPageCursor() {
27
+ return this.pageCursor;
28
+ }
29
+
30
+ public getTotalPages() {
31
+ if (!this.domContainer) throw Error(DESTROYED_ERR_MSG);
32
+ return this.template.schemas.length;
33
+ }
34
+
35
+ public setInputs(inputs: { [key: string]: string }[]): void {
36
+ const previousInputs = this.getInputs();
37
+
38
+ super.setInputs(inputs);
39
+
40
+ const changedInputs: Array<{ index: number; name: string; value: string }> = [];
41
+
42
+ inputs.forEach((input, index) => {
43
+ const prevInput = previousInputs[index] || {};
44
+
45
+ const allKeys = new Set([...Object.keys(input), ...Object.keys(prevInput)]);
46
+
47
+ allKeys.forEach((name) => {
48
+ const newValue = input[name];
49
+ const oldValue = prevInput[name];
50
+
51
+ if (newValue !== oldValue) {
52
+ changedInputs.push({ index, name, value: newValue });
53
+ }
54
+ });
55
+ });
56
+
57
+ changedInputs.forEach((input) => {
58
+ if (this.onChangeInputCallback) {
59
+ this.onChangeInputCallback(input);
60
+ }
61
+ });
62
+ }
63
+
64
+ protected render() {
65
+ if (!this.domContainer) throw Error(DESTROYED_ERR_MSG);
66
+ ReactDOM.render(
67
+ <AppContextProvider
68
+ lang={this.getLang()}
69
+ font={this.getFont()}
70
+ plugins={this.getPluginsRegistry()}
71
+ options={this.getOptions()}
72
+ >
73
+ <Preview
74
+ template={this.template}
75
+ size={this.size}
76
+ inputs={this.inputs}
77
+ onChangeInput={(arg: { index: number; value: string; name: string }) => {
78
+ const { index, value, name } = arg;
79
+ if (this.onChangeInputCallback) {
80
+ this.onChangeInputCallback({ index, value, name });
81
+ }
82
+ if (this.inputs && this.inputs[index]) {
83
+ if (this.inputs[index][name] !== value) {
84
+ this.inputs[index][name] = value;
85
+ this.render();
86
+ }
87
+ }
88
+ }}
89
+ onPageChange={(pageInfo) => {
90
+ this.pageCursor = pageInfo.currentPage;
91
+ if (this.onPageChangeCallback) {
92
+ this.onPageChangeCallback(pageInfo);
93
+ }
94
+ }}
95
+ />
96
+ </AppContextProvider>,
97
+ this.domContainer,
98
+ );
99
+ }
100
+ }
101
+
102
+ export default Form;
package/src/Viewer.tsx ADDED
@@ -0,0 +1,59 @@
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom';
3
+ import { PreviewProps } from '@pdfme/common';
4
+ import { PreviewUI } from './class.js';
5
+ import { DESTROYED_ERR_MSG } from './constants.js';
6
+ import Preview from './components/Preview.js';
7
+ import AppContextProvider from './components/AppContextProvider.js';
8
+
9
+ class Viewer extends PreviewUI {
10
+ private onPageChangeCallback?: (pageInfo: { currentPage: number; totalPages: number }) => void;
11
+ private pageCursor: number = 0;
12
+
13
+ constructor(props: PreviewProps) {
14
+ super(props);
15
+ console.warn(
16
+ '[@pdfme/ui] Viewer component is deprecated and will be removed in a future version.',
17
+ );
18
+ }
19
+
20
+ public onPageChange(cb: (pageInfo: { currentPage: number; totalPages: number }) => void) {
21
+ this.onPageChangeCallback = cb;
22
+ }
23
+
24
+ public getPageCursor() {
25
+ return this.pageCursor;
26
+ }
27
+
28
+ public getTotalPages() {
29
+ if (!this.domContainer) throw Error(DESTROYED_ERR_MSG);
30
+ return this.template.schemas.length;
31
+ }
32
+
33
+ protected render() {
34
+ if (!this.domContainer) throw Error(DESTROYED_ERR_MSG);
35
+ ReactDOM.render(
36
+ <AppContextProvider
37
+ lang={this.getLang()}
38
+ font={this.getFont()}
39
+ plugins={this.getPluginsRegistry()}
40
+ options={this.getOptions()}
41
+ >
42
+ <Preview
43
+ template={this.template}
44
+ size={this.size}
45
+ inputs={this.inputs}
46
+ onPageChange={(pageInfo) => {
47
+ this.pageCursor = pageInfo.currentPage;
48
+ if (this.onPageChangeCallback) {
49
+ this.onPageChangeCallback(pageInfo);
50
+ }
51
+ }}
52
+ />
53
+ </AppContextProvider>,
54
+ this.domContainer,
55
+ );
56
+ }
57
+ }
58
+
59
+ export default Viewer;