@pdfme/ui 3.4.3-dev.2 → 3.4.3-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 (51) hide show
  1. package/dist/index.es.js +60216 -59315
  2. package/dist/index.umd.js +116 -117
  3. package/dist/types/class.d.ts +60 -10
  4. package/dist/types/components/AppContextProvider.d.ts +2 -2
  5. package/dist/types/components/CtlBar.d.ts +2 -0
  6. package/dist/types/components/Designer/Canvas/Padding.d.ts +6 -0
  7. package/dist/types/components/Designer/Canvas/index.d.ts +2 -1
  8. package/dist/types/components/Designer/LeftSidebar.d.ts +8 -0
  9. package/dist/types/components/Designer/index.d.ts +2 -1
  10. package/dist/types/components/Preview.d.ts +1 -1
  11. package/dist/types/components/Renderer.d.ts +4 -3
  12. package/dist/types/constants.d.ts +1 -1
  13. package/dist/types/contexts.d.ts +12 -3
  14. package/dist/types/helper.d.ts +21 -33
  15. package/dist/types/hooks.d.ts +1 -0
  16. package/dist/types/types.d.ts +0 -1
  17. package/package.json +1 -1
  18. package/src/Designer.tsx +8 -3
  19. package/src/Form.tsx +6 -3
  20. package/src/Viewer.tsx +0 -1
  21. package/src/class.ts +28 -5
  22. package/src/components/AppContextProvider.tsx +3 -1
  23. package/src/components/CtlBar.tsx +57 -6
  24. package/src/components/Designer/Canvas/Padding.tsx +54 -0
  25. package/src/components/Designer/Canvas/index.tsx +89 -22
  26. package/src/components/Designer/LeftSidebar.tsx +81 -0
  27. package/src/components/Designer/{Sidebar → RightSidebar}/DetailView/index.tsx +8 -13
  28. package/src/components/Designer/{Sidebar → RightSidebar}/ListView/index.tsx +3 -11
  29. package/src/components/Designer/{Sidebar → RightSidebar}/index.tsx +5 -20
  30. package/src/components/Designer/index.tsx +166 -101
  31. package/src/components/Paper.tsx +1 -2
  32. package/src/components/Preview.tsx +72 -22
  33. package/src/components/Renderer.tsx +12 -11
  34. package/src/constants.ts +1 -1
  35. package/src/helper.ts +114 -118
  36. package/src/hooks.ts +46 -14
  37. package/src/i18n.ts +143 -8
  38. package/src/types.ts +0 -1
  39. /package/dist/types/components/Designer/{Sidebar → RightSidebar}/DetailView/AlignWidget.d.ts +0 -0
  40. /package/dist/types/components/Designer/{Sidebar → RightSidebar}/DetailView/WidgetRenderer.d.ts +0 -0
  41. /package/dist/types/components/Designer/{Sidebar → RightSidebar}/DetailView/index.d.ts +0 -0
  42. /package/dist/types/components/Designer/{Sidebar → RightSidebar}/ListView/Item.d.ts +0 -0
  43. /package/dist/types/components/Designer/{Sidebar → RightSidebar}/ListView/SelectableSortableContainer.d.ts +0 -0
  44. /package/dist/types/components/Designer/{Sidebar → RightSidebar}/ListView/SelectableSortableItem.d.ts +0 -0
  45. /package/dist/types/components/Designer/{Sidebar → RightSidebar}/ListView/index.d.ts +0 -0
  46. /package/dist/types/components/Designer/{Sidebar → RightSidebar}/index.d.ts +0 -0
  47. /package/src/components/Designer/{Sidebar → RightSidebar}/DetailView/AlignWidget.tsx +0 -0
  48. /package/src/components/Designer/{Sidebar → RightSidebar}/DetailView/WidgetRenderer.tsx +0 -0
  49. /package/src/components/Designer/{Sidebar → RightSidebar}/ListView/Item.tsx +0 -0
  50. /package/src/components/Designer/{Sidebar → RightSidebar}/ListView/SelectableSortableContainer.tsx +0 -0
  51. /package/src/components/Designer/{Sidebar → RightSidebar}/ListView/SelectableSortableItem.tsx +0 -0
package/src/helper.ts CHANGED
@@ -1,4 +1,4 @@
1
- // TODO Update pdfjs-dist. (might be able to reduce the bundle size.)
1
+ // Update pdfjs-dist. (might be able to reduce the bundle size.)
2
2
  // @ts-ignore
3
3
  import PDFJSWorker from 'pdfjs-dist/legacy/build/pdf.worker.entry.js';
4
4
  import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist/legacy/build/pdf.js';
@@ -11,9 +11,12 @@ import {
11
11
  b64toUint8Array,
12
12
  pt2mm,
13
13
  Template,
14
+ BasePdf,
14
15
  SchemaForUI,
15
16
  Schema,
16
17
  Size,
18
+ isBlankPdf,
19
+ Plugins,
17
20
  } from '@pdfme/common';
18
21
  import { RULER_HEIGHT } from './constants.js';
19
22
 
@@ -250,39 +253,38 @@ export const b64toBlob = (base64: string) => {
250
253
  return new Blob([uniy8Array.buffer], { type: mimeType });
251
254
  };
252
255
 
253
- const sortSchemasList = (template: Template, pageNum: number): SchemaForUI[][] =>
254
- new Array(pageNum).fill('').reduce((acc, _, i) => {
256
+ const sortSchemasList = (template: Template): SchemaForUI[][] => {
257
+ const { schemas } = template;
258
+ const pageNum = schemas.length;
259
+ const arr = new Array(pageNum).fill('') as SchemaForUI[][];
260
+ return arr.reduce((acc, _, i) => {
255
261
  acc.push(
256
- template.schemas[i]
257
- ? Object.entries(template.schemas[i])
258
- .sort((a, b) => {
259
- const aIndex = (template.columns ?? []).findIndex((c) => c === a[0]);
260
- const bIndex = (template.columns ?? []).findIndex((c) => c === b[0]);
261
-
262
- return aIndex > bIndex ? 1 : -1;
263
- })
264
- .map((e) => {
265
- const [key, value] = e;
266
- const data = template.sampledata?.[0]?.[key] ?? '';
267
-
268
- return Object.assign(value, {
269
- key,
270
- data,
271
- id: uuid(),
272
- });
273
- })
262
+ schemas[i]
263
+ ? Object.entries(schemas[i]).map(([key, schema]) =>
264
+ Object.assign(schema, { key, content: schema.content, id: uuid() })
265
+ )
274
266
  : []
275
267
  );
276
-
277
268
  return acc;
278
269
  }, [] as SchemaForUI[][]);
279
-
280
- export const templateSchemas2SchemasList = async (_template: Template) => {
270
+ };
271
+ export const template2SchemasList = async (_template: Template) => {
281
272
  const template = cloneDeep(_template);
282
- const sortedSchemasList = sortSchemasList(template, template.schemas.length);
283
- const basePdf = await getB64BasePdf(template.basePdf);
284
- const pdfBlob = b64toBlob(basePdf);
285
- const pageSizes = await getPdfPageSizes(pdfBlob);
273
+ const { basePdf, schemas } = template;
274
+ const sortedSchemasList = sortSchemasList(template);
275
+
276
+ let pageSizes: Size[] = [];
277
+ if (isBlankPdf(basePdf)) {
278
+ pageSizes = schemas.map(() => ({
279
+ width: basePdf.width,
280
+ height: basePdf.height,
281
+ }));
282
+ } else {
283
+ const b64BasePdf = await getB64BasePdf(basePdf);
284
+ const pdfBlob = b64toBlob(b64BasePdf);
285
+ pageSizes = await getPdfPageSizes(pdfBlob);
286
+ }
287
+
286
288
  const ssl = sortedSchemasList.length;
287
289
  const psl = pageSizes.length;
288
290
  const schemasList = (
@@ -306,90 +308,24 @@ export const templateSchemas2SchemasList = async (_template: Template) => {
306
308
 
307
309
  return schema;
308
310
  });
309
-
310
311
  return schemasList;
311
312
  };
312
313
 
313
- export const generateColumnsAndSampledataIfNeeded = (template: Template) => {
314
- const { schemas, columns, sampledata } = template;
315
-
316
- const flatSchemaLengthForColumns = schemas
317
- .map((schema) => Object.keys(schema).length)
318
- .reduce((acc, cur) => acc + cur, 0);
319
- const needColumns = !columns || flatSchemaLengthForColumns !== columns.length;
320
-
321
- const flatSchemaLengthForSampleData = schemas
322
- .map((schema) => Object.keys(schema).filter((key) => !schema[key].readOnly).length)
323
- .reduce((acc, cur) => acc + cur, 0);
324
- const needSampledata =
325
- !sampledata || flatSchemaLengthForSampleData !== Object.keys(sampledata[0]).length;
326
-
327
- // columns
328
- if (needColumns) {
329
- template.columns = flatten(schemas.map((schema) => Object.keys(schema)));
330
- }
331
-
332
- // sampledata
333
- if (needSampledata) {
334
- template.sampledata = [
335
- schemas.reduce(
336
- (acc, cur) =>
337
- Object.assign(
338
- acc,
339
- Object.keys(cur).reduce((a, c) => {
340
- const { readOnly } = cur[c];
341
- if (readOnly) {
342
- return a;
343
- }
344
- return Object.assign(a, { [c]: '' });
345
- }, {} as { [key: string]: string })
346
- ),
347
- {} as { [key: string]: string }
348
- ),
349
- ];
350
- }
351
-
352
- return template;
353
- };
354
-
355
- export const fmtTemplate = (template: Template, schemasList: SchemaForUI[][]): Template => {
356
- const schemaAddedTemplate: Template = {
357
- ...template,
358
- schemas: cloneDeep(schemasList).map((schema) =>
359
- schema.reduce((acc, cur) => {
360
- const k = cur.key;
361
- // @ts-ignore
362
- delete cur.id;
363
- // @ts-ignore
364
- delete cur.key;
365
- // @ts-ignore
366
- delete cur.data;
367
- acc[k] = cur;
368
-
369
- return acc;
370
- }, {} as { [key: string]: Schema })
371
- ),
372
- columns: cloneDeep(schemasList).reduce(
373
- (acc, cur) => acc.concat(cur.map((s) => s.key)),
374
- [] as string[]
375
- ),
376
- sampledata: [
377
- cloneDeep(schemasList).reduce((acc, cur) => {
378
- cur.forEach((c) => {
379
- if (c.readOnly) {
380
- return;
381
- }
382
- acc[c.key] = c.data;
383
- });
384
-
385
- return acc;
386
- }, {} as { [key: string]: string }),
387
- ],
388
- basePdf: template.basePdf,
389
- };
390
-
391
- return schemaAddedTemplate;
392
- };
314
+ export const schemasList2template = (schemasList: SchemaForUI[][], basePdf: BasePdf): Template => ({
315
+ schemas: cloneDeep(schemasList).map((schema) =>
316
+ schema.reduce((acc, cur) => {
317
+ const k = cur.key;
318
+ // @ts-ignore
319
+ delete cur.id;
320
+ // @ts-ignore
321
+ delete cur.key;
322
+ acc[k] = cur;
323
+
324
+ return acc;
325
+ }, {} as { [key: string]: Schema })
326
+ ),
327
+ basePdf,
328
+ });
393
329
 
394
330
  export const getUniqSchemaKey = (arg: {
395
331
  copiedSchemaKey: string;
@@ -474,18 +410,78 @@ export const moveCommandToChangeSchemasArg = (props: {
474
410
  });
475
411
  };
476
412
 
477
- export const getPagesScrollTopByIndex = (
478
- pageSizes: {
479
- width: number;
480
- height: number;
481
- }[],
482
- index: number,
483
- scale: number
484
- ) => {
413
+ export const getPagesScrollTopByIndex = (pageSizes: Size[], index: number, scale: number) => {
485
414
  return pageSizes
486
415
  .slice(0, index)
487
416
  .reduce((acc, cur) => acc + (cur.height * ZOOM + RULER_HEIGHT * scale) * scale, 0);
488
417
  };
489
418
 
490
419
  export const getSidebarContentHeight = (sidebarHeight: number) =>
491
- sidebarHeight - RULER_HEIGHT - RULER_HEIGHT / 2 - 115;
420
+ sidebarHeight - RULER_HEIGHT - RULER_HEIGHT / 2 - 30;
421
+
422
+ const handlePositionSizeChange = (
423
+ schema: SchemaForUI,
424
+ key: string,
425
+ value: any,
426
+ basePdf: BasePdf,
427
+ pageSize: Size
428
+ ) => {
429
+ const padding = isBlankPdf(basePdf) ? basePdf.padding : [0, 0, 0, 0];
430
+ const [pt, pr, pb, pl] = padding;
431
+ const { width: pw, height: ph } = pageSize;
432
+ const calcBounds = (v: any, min: number, max: number) => Math.min(Math.max(Number(v), min), max);
433
+ if (key === 'position.x') {
434
+ schema.position.x = calcBounds(value, pl, pw - schema.width - pr);
435
+ } else if (key === 'position.y') {
436
+ schema.position.y = calcBounds(value, pt, ph - schema.height - pb);
437
+ } else if (key === 'width') {
438
+ schema.width = calcBounds(value, 0, pw - schema.position.x - pr);
439
+ } else if (key === 'height') {
440
+ schema.height = calcBounds(value, 0, ph - schema.position.y - pb);
441
+ }
442
+ };
443
+
444
+ const handleTypeChange = (
445
+ schema: SchemaForUI,
446
+ key: string,
447
+ value: any,
448
+ pluginsRegistry: Plugins
449
+ ) => {
450
+ if (key !== 'type') return;
451
+ const keysToKeep = ['id', 'key', 'type', 'position'];
452
+ Object.keys(schema).forEach((key) => {
453
+ if (!keysToKeep.includes(key)) {
454
+ delete schema[key as keyof typeof schema];
455
+ }
456
+ });
457
+ const propPanel = Object.values(pluginsRegistry).find(
458
+ (plugin) => plugin?.propPanel.defaultSchema.type === value
459
+ )?.propPanel;
460
+ Object.assign(schema, propPanel?.defaultSchema || {});
461
+ };
462
+
463
+ export const changeSchemas = (args: {
464
+ objs: { key: string; value: any; schemaId: string }[];
465
+ schemas: SchemaForUI[];
466
+ basePdf: BasePdf;
467
+ pluginsRegistry: Plugins;
468
+ pageSize: { width: number; height: number };
469
+ commitSchemas: (newSchemas: SchemaForUI[]) => void;
470
+ }) => {
471
+ const { objs, schemas, basePdf, pluginsRegistry, pageSize, commitSchemas } = args;
472
+ const newSchemas = objs.reduce((acc, { key, value, schemaId }) => {
473
+ const tgt = acc.find((s) => s.id === schemaId);
474
+ if (!tgt) return acc;
475
+ // Assign to reference
476
+ set(tgt, key, value);
477
+
478
+ if (key === 'type') {
479
+ handleTypeChange(tgt, key, value, pluginsRegistry);
480
+ } else if (['position.x', 'position.y', 'width', 'height'].includes(key)) {
481
+ handlePositionSizeChange(tgt, key, value, basePdf, pageSize);
482
+ }
483
+
484
+ return acc;
485
+ }, cloneDeep(schemas));
486
+ commitSchemas(newSchemas);
487
+ };
package/src/hooks.ts CHANGED
@@ -1,8 +1,16 @@
1
1
  import { RefObject, useRef, useState, useCallback, useEffect } from 'react';
2
- import { ZOOM, Template, Size, getB64BasePdf, SchemaForUI, ChangeSchemas } from '@pdfme/common';
2
+ import {
3
+ ZOOM,
4
+ Template,
5
+ Size,
6
+ getB64BasePdf,
7
+ SchemaForUI,
8
+ ChangeSchemas,
9
+ isBlankPdf,
10
+ } from '@pdfme/common';
3
11
 
4
12
  import {
5
- fmtTemplate,
13
+ schemasList2template,
6
14
  uuid,
7
15
  cloneDeep,
8
16
  getUniqSchemaKey,
@@ -36,14 +44,29 @@ export const useUIPreProcessor = ({ template, size, zoomLevel }: UIPreProcessorP
36
44
  const [error, setError] = useState<Error | null>(null);
37
45
 
38
46
  const init = async (prop: { template: Template; size: Size }) => {
39
- const { template, size } = prop;
40
- const _basePdf = await getB64BasePdf(template.basePdf);
41
- const pdfBlob = b64toBlob(_basePdf);
42
- const _pageSizes = await getPdfPageSizes(pdfBlob);
43
- const paperWidth = _pageSizes[0].width * ZOOM;
44
- const paperHeight = _pageSizes[0].height * ZOOM;
45
- const _backgrounds = await pdf2Pngs(pdfBlob, paperWidth);
47
+ const {
48
+ template: { basePdf, schemas },
49
+ size,
50
+ } = prop;
51
+ let paperWidth, paperHeight, _backgrounds, _pageSizes;
46
52
 
53
+ if (isBlankPdf(basePdf)) {
54
+ const { width, height } = basePdf;
55
+ paperWidth = width * ZOOM;
56
+ paperHeight = height * ZOOM;
57
+ _backgrounds = schemas.map(
58
+ () =>
59
+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdj+P///38ACfsD/QVDRcoAAAAASUVORK5CYII='
60
+ );
61
+ _pageSizes = schemas.map(() => ({ width, height }));
62
+ } else {
63
+ const _basePdf = await getB64BasePdf(basePdf);
64
+ const pdfBlob = b64toBlob(_basePdf);
65
+ _pageSizes = await getPdfPageSizes(pdfBlob);
66
+ paperWidth = _pageSizes[0].width * ZOOM;
67
+ paperHeight = _pageSizes[0].height * ZOOM;
68
+ _backgrounds = await pdf2Pngs(pdfBlob, paperWidth);
69
+ }
47
70
  const _scale = Math.min(
48
71
  getScale(size.width, paperWidth),
49
72
  getScale(size.height - RULER_HEIGHT, paperHeight)
@@ -51,6 +74,7 @@ export const useUIPreProcessor = ({ template, size, zoomLevel }: UIPreProcessorP
51
74
 
52
75
  return { backgrounds: _backgrounds, pageSizes: _pageSizes, scale: _scale };
53
76
  };
77
+
54
78
  useEffect(() => {
55
79
  init({ template, size })
56
80
  .then(({ pageSizes, scale, backgrounds }) => {
@@ -62,7 +86,16 @@ export const useUIPreProcessor = ({ template, size, zoomLevel }: UIPreProcessorP
62
86
  });
63
87
  }, [template, size]);
64
88
 
65
- return { backgrounds, pageSizes, scale: scale * zoomLevel, error };
89
+ return {
90
+ backgrounds,
91
+ pageSizes,
92
+ scale: scale * zoomLevel,
93
+ error,
94
+ refresh: (template: Template) =>
95
+ init({ template, size }).then(({ pageSizes, scale, backgrounds }) => {
96
+ setPageSizes(pageSizes), setScale(scale), setBackgrounds(backgrounds);
97
+ }),
98
+ };
66
99
  };
67
100
 
68
101
  type ScrollPageCursorProps = {
@@ -163,8 +196,6 @@ export const useInitEvents = ({
163
196
  }: UseInitEventsParams) => {
164
197
  const copiedSchemas = useRef<SchemaForUI[] | null>(null);
165
198
 
166
- const modifiedTemplate = fmtTemplate(template, schemasList);
167
-
168
199
  const initEvents = useCallback(() => {
169
200
  const getActiveSchemas = () => {
170
201
  const ids = activeElements.map((ae) => ae.id);
@@ -215,12 +246,14 @@ export const useInitEvents = ({
215
246
  },
216
247
  redo: () => timeTravel('redo'),
217
248
  undo: () => timeTravel('undo'),
218
- save: () => onSaveTemplate && onSaveTemplate(modifiedTemplate),
249
+ save: () =>
250
+ onSaveTemplate && onSaveTemplate(schemasList2template(schemasList, template.basePdf)),
219
251
  remove: () => removeSchemas(getActiveSchemas().map((s) => s.id)),
220
252
  esc: onEditEnd,
221
253
  selectAll: () => onEdit(schemasList[pageCursor].map((s) => document.getElementById(s.id)!)),
222
254
  });
223
255
  }, [
256
+ template,
224
257
  activeElements,
225
258
  pageCursor,
226
259
  pageSizes,
@@ -228,7 +261,6 @@ export const useInitEvents = ({
228
261
  commitSchemas,
229
262
  schemasList,
230
263
  onSaveTemplate,
231
- modifiedTemplate,
232
264
  removeSchemas,
233
265
  past,
234
266
  future,