@pdfme/ui 1.0.4 → 1.0.7

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 (35) hide show
  1. package/README.md +30 -2
  2. package/dist/index.js +1 -1
  3. package/dist/index.js.LICENSE.txt +1 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/types/class.d.ts +1 -1
  6. package/dist/types/components/{Preview/Pager/Page.d.ts → CtlBar/Pager.d.ts} +0 -0
  7. package/dist/types/components/CtlBar/Zoom.d.ts +6 -0
  8. package/dist/types/components/CtlBar/index.d.ts +11 -0
  9. package/dist/types/components/Paper.d.ts +2 -2
  10. package/dist/types/components/{Preview/index.d.ts → Preview.d.ts} +0 -0
  11. package/dist/types/components/UnitPager.d.ts +9 -0
  12. package/dist/types/contexts.d.ts +1 -1
  13. package/dist/types/helper.d.ts +4 -0
  14. package/dist/types/hooks.d.ts +4 -4
  15. package/dist/types/i18n.d.ts +2 -0
  16. package/package.json +9 -1
  17. package/src/assets/icons/add.svg +3 -0
  18. package/src/assets/icons/remove.svg +3 -0
  19. package/src/components/CtlBar/Pager.tsx +52 -0
  20. package/src/components/CtlBar/Zoom.tsx +57 -0
  21. package/src/components/CtlBar/index.tsx +50 -0
  22. package/src/components/Designer/Main/index.tsx +2 -2
  23. package/src/components/Designer/Sidebar/index.tsx +9 -4
  24. package/src/components/Designer/index.tsx +35 -12
  25. package/src/components/Error.tsx +1 -1
  26. package/src/components/Paper.tsx +4 -2
  27. package/src/components/{Preview/index.tsx → Preview.tsx} +50 -38
  28. package/src/components/Root.tsx +1 -1
  29. package/src/components/UnitPager.tsx +101 -0
  30. package/src/helper.ts +14 -0
  31. package/src/hooks.ts +16 -18
  32. package/src/i18n.ts +4 -0
  33. package/dist/types/components/Preview/Pager/Unit.d.ts +0 -7
  34. package/src/components/Preview/Pager/Page.tsx +0 -85
  35. package/src/components/Preview/Pager/Unit.tsx +0 -87
@@ -0,0 +1,101 @@
1
+ import React, { useContext } from 'react';
2
+ import left from '../assets/icons/left.svg';
3
+ import right from '../assets/icons/right.svg';
4
+ import doubleLeft from '../assets/icons/double-left.svg';
5
+ import doubleRight from '../assets/icons/double-right.svg';
6
+ import { I18nContext } from '../contexts';
7
+ import { Size } from '@pdfme/common';
8
+
9
+ const buttonWrapStyle: React.CSSProperties = {
10
+ position: 'sticky',
11
+ top: '45%',
12
+ zIndex: 1,
13
+ backgroundColor: '#777777bd',
14
+ borderRadius: 2,
15
+ padding: '0.5rem',
16
+ display: 'flex',
17
+ alignItems: 'center',
18
+ justifyContent: 'space-around',
19
+ width: 110,
20
+ };
21
+
22
+ const btnStyle: React.CSSProperties = {
23
+ cursor: 'pointer',
24
+ border: 'none',
25
+ background: 'none',
26
+ display: 'flex',
27
+ alignItems: 'center',
28
+ };
29
+
30
+ type Props = {
31
+ size: Size;
32
+ unitCursor: number;
33
+ unitNum: number;
34
+ setUnitCursor: (page: number) => void;
35
+ };
36
+
37
+ const UnitPager = ({ size, unitCursor, unitNum, setUnitCursor }: Props) => {
38
+ const i18n = useContext(I18nContext);
39
+
40
+ if (unitNum <= 1) return <></>;
41
+
42
+ return (
43
+ <div style={{ position: 'absolute', ...size }}>
44
+ <div
45
+ style={{
46
+ position: 'sticky',
47
+ width: '100%',
48
+ zIndex: 1,
49
+ top: '50%',
50
+ display: 'flex',
51
+ alignItems: 'center',
52
+ }}
53
+ >
54
+ {unitCursor > 0 && (
55
+ <div style={{ marginLeft: '1rem', ...buttonWrapStyle }}>
56
+ <button
57
+ style={{ paddingLeft: '0.5rem', ...btnStyle }}
58
+ disabled={unitCursor <= 0}
59
+ onClick={() => setUnitCursor(0)}
60
+ >
61
+ <img src={doubleLeft} alt={i18n('goToFirst')} style={{ width: 20 }} />
62
+ </button>
63
+ <button
64
+ style={{ paddingLeft: '0.5rem', ...btnStyle }}
65
+ disabled={unitCursor <= 0}
66
+ onClick={() => setUnitCursor(unitCursor - 1)}
67
+ >
68
+ <img src={left} alt={i18n('goToPrevious')} style={{ width: 20 }} />
69
+ </button>
70
+ <strong style={{ color: 'white', fontSize: '0.9rem' }}>
71
+ {unitCursor + 1}/{unitNum}
72
+ </strong>
73
+ </div>
74
+ )}
75
+ {unitCursor + 1 < unitNum && (
76
+ <div style={{ marginLeft: 'auto', marginRight: '1rem', ...buttonWrapStyle }}>
77
+ <strong style={{ color: 'white', fontSize: '0.9rem' }}>
78
+ {unitCursor + 1}/{unitNum}
79
+ </strong>
80
+ <button
81
+ style={{ paddingRight: '0.5rem', ...btnStyle }}
82
+ disabled={unitCursor + 1 >= unitNum}
83
+ onClick={() => setUnitCursor(unitCursor + 1)}
84
+ >
85
+ <img src={right} alt={i18n('goToNext')} style={{ width: 20 }} />
86
+ </button>
87
+ <button
88
+ style={{ paddingRight: '0.5rem', ...btnStyle }}
89
+ disabled={unitCursor + 1 >= unitNum}
90
+ onClick={() => setUnitCursor(unitNum - 1)}
91
+ >
92
+ <img src={doubleRight} alt={i18n('goToFirst')} style={{ width: 20 }} />
93
+ </button>
94
+ </div>
95
+ )}
96
+ </div>
97
+ </div>
98
+ );
99
+ };
100
+
101
+ export default UnitPager;
package/src/helper.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  DEFAULT_CHARACTER_SPACING,
16
16
  DEFAULT_LINE_HEIGHT,
17
17
  } from '@pdfme/common';
18
+ import { ZOOM, RULER_HEIGHT } from './constants';
18
19
 
19
20
  export const uuid = () =>
20
21
  'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
@@ -527,3 +528,16 @@ export const moveCommandToChangeSchemasArg = (props: {
527
528
  return { key: `position.${key}`, value, schemaId: as.id };
528
529
  });
529
530
  };
531
+
532
+ export const getPagesScrollTopByIndex = (
533
+ pageSizes: {
534
+ width: number;
535
+ height: number;
536
+ }[],
537
+ index: number,
538
+ scale: number
539
+ ) => {
540
+ return pageSizes
541
+ .slice(0, index)
542
+ .reduce((acc, cur) => acc + (cur.height * ZOOM + RULER_HEIGHT * scale) * scale, 0);
543
+ };
package/src/hooks.ts CHANGED
@@ -14,9 +14,9 @@ export const usePrevious = <T>(value: T) => {
14
14
 
15
15
  const getScale = (n: number, paper: number) => (n / paper > 1 ? 1 : n / paper);
16
16
 
17
- type UIPreProcessorProps = { template: Template; size: Size; offset?: number };
17
+ type UIPreProcessorProps = { template: Template; size: Size; zoomLevel: number };
18
18
 
19
- export const useUIPreProcessor = ({ template, size, offset = 0 }: UIPreProcessorProps) => {
19
+ export const useUIPreProcessor = ({ template, size, zoomLevel }: UIPreProcessorProps) => {
20
20
  const [backgrounds, setBackgrounds] = useState<string[]>([]);
21
21
  const [pageSizes, setPageSizes] = useState<Size[]>([]);
22
22
  const [scale, setScale] = useState(0);
@@ -32,18 +32,16 @@ export const useUIPreProcessor = ({ template, size, offset = 0 }: UIPreProcessor
32
32
 
33
33
  const _scale = Math.min(
34
34
  getScale(size.width, paperWidth),
35
- getScale(size.height - offset, paperHeight)
35
+ getScale(size.height - RULER_HEIGHT, paperHeight)
36
36
  );
37
37
 
38
38
  return { backgrounds: _backgrounds, pageSizes: _pageSizes, scale: _scale };
39
- }, [template, size, offset]);
39
+ }, [template, size]);
40
40
 
41
41
  useEffect(() => {
42
42
  init()
43
- .then((data) => {
44
- setPageSizes(data.pageSizes);
45
- setScale(data.scale);
46
- setBackgrounds(data.backgrounds);
43
+ .then(({ pageSizes, scale, backgrounds }) => {
44
+ setPageSizes(pageSizes), setScale(scale), setBackgrounds(backgrounds);
47
45
  })
48
46
  .catch((e: Error) => {
49
47
  console.error(e);
@@ -51,11 +49,11 @@ export const useUIPreProcessor = ({ template, size, offset = 0 }: UIPreProcessor
51
49
  });
52
50
  }, [init]);
53
51
 
54
- return { backgrounds, pageSizes, scale, error };
52
+ return { backgrounds, pageSizes, scale: scale * zoomLevel, error };
55
53
  };
56
54
 
57
55
  type ScrollPageCursorProps = {
58
- rootRef: RefObject<HTMLDivElement>;
56
+ ref: RefObject<HTMLDivElement>;
59
57
  pageSizes: Size[];
60
58
  scale: number;
61
59
  pageCursor: number;
@@ -63,19 +61,19 @@ type ScrollPageCursorProps = {
63
61
  };
64
62
 
65
63
  export const useScrollPageCursor = ({
66
- rootRef,
64
+ ref,
67
65
  pageSizes,
68
66
  scale,
69
67
  pageCursor,
70
68
  onChangePageCursor,
71
69
  }: ScrollPageCursorProps) => {
72
70
  const onScroll = useCallback(() => {
73
- if (!pageSizes[0] || !rootRef.current) {
71
+ if (!pageSizes[0] || !ref.current) {
74
72
  return;
75
73
  }
76
74
 
77
- const scroll = rootRef.current.scrollTop;
78
- const { top } = rootRef.current.getBoundingClientRect();
75
+ const scroll = ref.current.scrollTop;
76
+ const { top } = ref.current.getBoundingClientRect();
79
77
  const pageHeights = pageSizes.reduce((acc, cur, i) => {
80
78
  let value = (cur.height * ZOOM + RULER_HEIGHT) * scale;
81
79
  if (i === 0) {
@@ -95,15 +93,15 @@ export const useScrollPageCursor = ({
95
93
  if (_pageCursor !== pageCursor) {
96
94
  onChangePageCursor(_pageCursor);
97
95
  }
98
- }, [onChangePageCursor, pageCursor, pageSizes, rootRef, scale]);
96
+ }, [onChangePageCursor, pageCursor, pageSizes, ref, scale]);
99
97
 
100
98
  useEffect(() => {
101
- rootRef.current?.addEventListener('scroll', onScroll);
99
+ ref.current?.addEventListener('scroll', onScroll);
102
100
 
103
101
  return () => {
104
- rootRef.current?.removeEventListener('scroll', onScroll);
102
+ ref.current?.removeEventListener('scroll', onScroll);
105
103
  };
106
- }, [rootRef, onScroll]);
104
+ }, [ref, onScroll]);
107
105
  };
108
106
 
109
107
  export const useMountStatus = () => {
package/src/i18n.ts CHANGED
@@ -26,6 +26,8 @@ const dictEn = {
26
26
  goToNext: 'Next',
27
27
  goToEnd: 'Go to end',
28
28
  select: 'Select',
29
+ zoomIn: 'Zoom in',
30
+ zoomOut: 'Zoom out',
29
31
  errorOccurred: 'An error occurred',
30
32
  errorBulkUpdateFieldName:
31
33
  'Cannot commit the change because the number of items has been changed.',
@@ -56,6 +58,8 @@ const dictJa: { [key in keyof DictEn]: string } = {
56
58
  goToNext: '1つ進む',
57
59
  goToEnd: '最後に進む',
58
60
  select: '選択',
61
+ zoomIn: '拡大',
62
+ zoomOut: '縮小',
59
63
  errorOccurred: 'エラーが発生しました',
60
64
  errorBulkUpdateFieldName: '項目数が変更されているため変更をコミットできません。',
61
65
  commitBulkUpdateFieldName: '変更を反映',
@@ -1,7 +0,0 @@
1
- declare type Props = {
2
- unitCursor: number;
3
- unitNum: number;
4
- setUnitCursor: (page: number) => void;
5
- };
6
- declare const UnitPager: ({ unitCursor, unitNum, setUnitCursor }: Props) => JSX.Element;
7
- export default UnitPager;
@@ -1,85 +0,0 @@
1
- import React, { useContext } from 'react';
2
- import left from '../../../assets/icons/left.svg';
3
- import right from '../../../assets/icons/right.svg';
4
- import doubleLeft from '../../../assets/icons/double-left.svg';
5
- import doubleRight from '../../../assets/icons/double-right.svg';
6
- import { I18nContext } from '../../../contexts';
7
-
8
- const btnStyle: React.CSSProperties = {
9
- cursor: 'pointer',
10
- border: 'none',
11
- background: 'none',
12
- display: 'flex',
13
- alignItems: 'center',
14
- };
15
-
16
- type Props = {
17
- pageCursor: number;
18
- pageNum: number;
19
- setPageCursor: (page: number) => void;
20
- };
21
-
22
- const Pager = ({ pageCursor, pageNum, setPageCursor }: Props) => {
23
- const i18n = useContext(I18nContext);
24
-
25
- if (pageNum <= 1) return <></>;
26
-
27
- return (
28
- <div
29
- style={{
30
- position: 'sticky',
31
- top: '90%',
32
- zIndex: 1,
33
- display: 'flex',
34
- alignItems: 'center',
35
- justifyContent: 'center',
36
- width: '100%',
37
- }}
38
- >
39
- <div
40
- style={{
41
- display: 'flex',
42
- alignItems: 'center',
43
- justifyContent: 'center',
44
- background: '#777777e6',
45
- borderRadius: 2,
46
- padding: '0.5rem',
47
- }}
48
- >
49
- <button
50
- style={{ paddingLeft: '0.5rem', ...btnStyle }}
51
- disabled={pageCursor <= 0}
52
- onClick={() => setPageCursor(0)}
53
- >
54
- <img src={doubleLeft} alt={i18n('goToFirst')} style={{ width: 20 }} />
55
- </button>
56
- <button
57
- style={{ paddingLeft: '0.5rem', ...btnStyle }}
58
- disabled={pageCursor <= 0}
59
- onClick={() => setPageCursor(pageCursor - 1)}
60
- >
61
- <img src={left} alt={i18n('goToPrevious')} style={{ width: 20 }} />
62
- </button>
63
- <strong style={{ color: 'white' }}>
64
- {pageCursor + 1}/{pageNum}
65
- </strong>
66
- <button
67
- style={{ paddingRight: '0.5rem', ...btnStyle }}
68
- disabled={pageCursor + 1 >= pageNum}
69
- onClick={() => setPageCursor(pageCursor + 1)}
70
- >
71
- <img src={right} alt={i18n('goToNext')} style={{ width: 20 }} />
72
- </button>
73
- <button
74
- style={{ paddingRight: '0.5rem', ...btnStyle }}
75
- disabled={pageCursor + 1 >= pageNum}
76
- onClick={() => setPageCursor(pageNum - 1)}
77
- >
78
- <img src={doubleRight} alt={i18n('goToFirst')} style={{ width: 20 }} />
79
- </button>
80
- </div>
81
- </div>
82
- );
83
- };
84
-
85
- export default Pager;
@@ -1,87 +0,0 @@
1
- import React, { useContext } from 'react';
2
- import left from '../../../assets/icons/left.svg';
3
- import right from '../../../assets/icons/right.svg';
4
- import doubleLeft from '../../../assets/icons/double-left.svg';
5
- import doubleRight from '../../../assets/icons/double-right.svg';
6
- import { I18nContext } from '../../../contexts';
7
-
8
- const buttonWrapStyle: React.CSSProperties = {
9
- position: 'sticky',
10
- top: '45%',
11
- zIndex: 1,
12
- backgroundColor: '#777777bd',
13
- borderRadius: 2,
14
- padding: '0.5rem',
15
- display: 'flex',
16
- alignItems: 'center',
17
- width: 100,
18
- };
19
-
20
- const btnStyle: React.CSSProperties = {
21
- cursor: 'pointer',
22
- border: 'none',
23
- background: 'none',
24
- display: 'flex',
25
- alignItems: 'center',
26
- };
27
-
28
- type Props = {
29
- unitCursor: number;
30
- unitNum: number;
31
- setUnitCursor: (page: number) => void;
32
- };
33
-
34
- const UnitPager = ({ unitCursor, unitNum, setUnitCursor }: Props) => {
35
- const i18n = useContext(I18nContext);
36
-
37
- if (unitNum <= 1) return <></>;
38
-
39
- return (
40
- <>
41
- {unitCursor > 0 && (
42
- <div style={{ marginLeft: '1rem', ...buttonWrapStyle }}>
43
- <button
44
- style={{ paddingLeft: '0.5rem', ...btnStyle }}
45
- disabled={unitCursor <= 0}
46
- onClick={() => setUnitCursor(0)}
47
- >
48
- <img src={doubleLeft} alt={i18n('goToFirst')} style={{ width: 20 }} />
49
- </button>
50
- <button
51
- style={{ paddingLeft: '0.5rem', ...btnStyle }}
52
- disabled={unitCursor <= 0}
53
- onClick={() => setUnitCursor(unitCursor - 1)}
54
- >
55
- <img src={left} alt={i18n('goToPrevious')} style={{ width: 20 }} />
56
- </button>
57
- <strong style={{ color: 'white' }}>
58
- {unitCursor + 1}/{unitNum}
59
- </strong>
60
- </div>
61
- )}
62
- {unitCursor + 1 < unitNum && (
63
- <div style={{ marginLeft: 'auto', marginRight: '1rem', ...buttonWrapStyle }}>
64
- <strong style={{ color: 'white' }}>
65
- {unitCursor + 1}/{unitNum}
66
- </strong>
67
- <button
68
- style={{ paddingRight: '0.5rem', ...btnStyle }}
69
- disabled={unitCursor + 1 >= unitNum}
70
- onClick={() => setUnitCursor(unitCursor + 1)}
71
- >
72
- <img src={right} alt={i18n('goToNext')} style={{ width: 20 }} />
73
- </button>
74
- <button
75
- style={{ paddingRight: '0.5rem', ...btnStyle }}
76
- disabled={unitCursor + 1 >= unitNum}
77
- onClick={() => setUnitCursor(unitNum - 1)}
78
- >
79
- <img src={doubleRight} alt={i18n('goToFirst')} style={{ width: 20 }} />
80
- </button>
81
- </div>
82
- )}
83
- </>
84
- );
85
- };
86
-
87
- export default UnitPager;