react-ink-scripter 0.0.22 → 0.0.24

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/index.js CHANGED
@@ -1,85 +1,11 @@
1
1
  'use strict';
2
2
 
3
- var _extends = require('@babel/runtime/helpers/extends');
4
3
  var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
5
4
  var react = require('react');
6
5
  var reactDom = require('react-dom');
7
6
  var jsxRuntime = require('react/jsx-runtime');
8
7
  var clsx = require('clsx');
9
-
10
- var getPageHeight = function getPageHeight(height, page) {
11
- var _page$marginTop = page.marginTop,
12
- marginTop = _page$marginTop === void 0 ? 0 : _page$marginTop,
13
- _page$marginBottom = page.marginBottom,
14
- marginBottom = _page$marginBottom === void 0 ? 0 : _page$marginBottom;
15
- return height + marginTop + marginBottom;
16
- };
17
- var pix2mm = function pix2mm(pix) {
18
- return Math.round(pix * 0.2645833333);
19
- };
20
- var usePagePrint = function usePagePrint(_ref) {
21
- var open = _ref.open,
22
- pageConfig = _ref.pageConfig,
23
- style = _ref.style;
24
- var _useState = react.useState({
25
- width: pageConfig.width || 0,
26
- height: pageConfig.height || 0,
27
- marginLeft: pageConfig.marginLeft || 0,
28
- marginRight: pageConfig.marginRight || 0,
29
- marginTop: pageConfig.marginTop || 0,
30
- marginBottom: pageConfig.marginBottom || 0
31
- }),
32
- _useState2 = _slicedToArray(_useState, 2),
33
- page = _useState2[0],
34
- setPage = _useState2[1];
35
- var _useState3 = react.useState(),
36
- _useState4 = _slicedToArray(_useState3, 2),
37
- pageNode = _useState4[0],
38
- setPageNode = _useState4[1];
39
- var setPageNodeRef = react.useCallback(function (node) {
40
- setPageNode(node);
41
- }, []);
42
- react.useLayoutEffect(function () {
43
- if (!open) {
44
- return;
45
- }
46
- var rect = pageNode === null || pageNode === void 0 ? void 0 : pageNode.getBoundingClientRect();
47
- if (!rect) {
48
- return;
49
- }
50
- var width = pageConfig.width,
51
- height = pageConfig.height;
52
- var elmHeight = rect.height,
53
- elmWidth = rect.width;
54
- var pageWidth = width || pix2mm(elmWidth);
55
- var pageHeight = height ? height : getPageHeight(pix2mm(elmHeight), pageConfig);
56
- setPage(function (old) {
57
- return _extends({}, old, {
58
- width: pageWidth,
59
- height: pageHeight
60
- });
61
- });
62
- }, [pageNode, open, pageConfig]);
63
- var getPageContent = react.useCallback(function () {
64
- return (pageNode === null || pageNode === void 0 ? void 0 : pageNode.innerHTML) || '';
65
- }, [pageNode]);
66
- var pageStyle = react.useMemo(function () {
67
- var width = page.width,
68
- height = page.height,
69
- marginLeft = page.marginLeft,
70
- marginRight = page.marginRight,
71
- marginTop = page.marginTop;
72
- var extraStyle = "\nhtml {\nmargin: 0;\npadding: 0;\nwidth: ".concat(width, "mm;\nheight: ").concat(height, "mm\n}\n\nbody {\nwidth: ").concat(width, "mm;\nmargin: 0;\npadding: 0;\nbox-sizing: border-box;\npadding-left: ").concat(marginLeft, "mm;\npadding-right: ").concat(marginRight, "mm;\npadding-top: ").concat(marginTop, "mm;\n}\n");
73
- return style + '\n' + extraStyle;
74
- }, [page, style, page]);
75
- return {
76
- setPageNodeRef: setPageNodeRef,
77
- page: page,
78
- getPageContent: getPageContent,
79
- pageNode: pageNode,
80
- pageStyle: pageStyle
81
- };
82
- };
8
+ var _extends = require('@babel/runtime/helpers/extends');
83
9
 
84
10
  var IFrame = react.forwardRef(function (props, ref) {
85
11
  var _iframeRef$current, _iframeRef$current$co, _iframeRef$current$co2, _iframeRef$current2, _iframeRef$current2$c, _iframeRef$current2$c2;
@@ -152,6 +78,84 @@ var PrinterFrame = react.forwardRef(function (props, ref) {
152
78
  });
153
79
  });
154
80
 
81
+ var getPageHeight = function getPageHeight(height, page) {
82
+ var _page$marginTop = page.marginTop,
83
+ marginTop = _page$marginTop === void 0 ? 0 : _page$marginTop,
84
+ _page$marginBottom = page.marginBottom,
85
+ marginBottom = _page$marginBottom === void 0 ? 0 : _page$marginBottom;
86
+ return height + marginTop + marginBottom;
87
+ };
88
+ var pix2mm = function pix2mm(pix) {
89
+ return Math.round(pix * 0.2645833333);
90
+ };
91
+ var usePagePrint = function usePagePrint(_ref) {
92
+ var open = _ref.open,
93
+ pageConfig = _ref.pageConfig;
94
+ var _useState = react.useState({
95
+ width: pageConfig.width || 0,
96
+ height: pageConfig.height || 0,
97
+ marginLeft: pageConfig.marginLeft || 0,
98
+ marginRight: pageConfig.marginRight || 0,
99
+ marginTop: pageConfig.marginTop || 0,
100
+ marginBottom: pageConfig.marginBottom || 0
101
+ }),
102
+ _useState2 = _slicedToArray(_useState, 2),
103
+ page = _useState2[0],
104
+ setPage = _useState2[1];
105
+ var _useState3 = react.useState(),
106
+ _useState4 = _slicedToArray(_useState3, 2),
107
+ pageNode = _useState4[0],
108
+ setPageNode = _useState4[1];
109
+ var setPageNodeRef = react.useCallback(function (node) {
110
+ setPageNode(node);
111
+ }, []);
112
+ react.useLayoutEffect(function () {
113
+ if (!open) {
114
+ return;
115
+ }
116
+ var rect = pageNode === null || pageNode === void 0 ? void 0 : pageNode.getBoundingClientRect();
117
+ if (!rect) {
118
+ return;
119
+ }
120
+ var width = pageConfig.width,
121
+ height = pageConfig.height;
122
+ var elmHeight = rect.height,
123
+ elmWidth = rect.width;
124
+ var pageWidth = width || pix2mm(elmWidth);
125
+ var pageHeight = height ? height : getPageHeight(pix2mm(elmHeight), pageConfig);
126
+ setPage(function (old) {
127
+ return _extends({}, old, {
128
+ width: pageWidth,
129
+ height: pageHeight
130
+ });
131
+ });
132
+ }, [pageNode, open, pageConfig]);
133
+ var getPageContent = react.useCallback(function () {
134
+ return (pageNode === null || pageNode === void 0 ? void 0 : pageNode.innerHTML) || '';
135
+ }, [pageNode]);
136
+ return {
137
+ setPageNodeRef: setPageNodeRef,
138
+ page: page,
139
+ getPageContent: getPageContent,
140
+ pageNode: pageNode
141
+ };
142
+ };
143
+
144
+ var useFrameStyle = function useFrameStyle(_ref) {
145
+ var page = _ref.page,
146
+ style = _ref.style;
147
+ var pageStyle = react.useMemo(function () {
148
+ var width = page.width,
149
+ height = page.height,
150
+ marginLeft = page.marginLeft,
151
+ marginRight = page.marginRight,
152
+ marginTop = page.marginTop;
153
+ var extraStyle = "\nhtml {\nmargin: 0;\npadding: 0;\nwidth: ".concat(width, "mm;\nheight: ").concat(height, "mm\n}\n\nbody {\nwidth: ").concat(width, "mm;\nmargin: 0;\npadding: 0;\nbox-sizing: border-box;\npadding-left: ").concat(marginLeft, "mm;\npadding-right: ").concat(marginRight, "mm;\npadding-top: ").concat(marginTop, "mm;\n}\n");
154
+ return style + '\n' + extraStyle;
155
+ }, [page, style, page]);
156
+ return pageStyle;
157
+ };
158
+
155
159
  var Container = function Container(_ref) {
156
160
  var id = _ref.id,
157
161
  _ref$content = _ref.content,
@@ -189,11 +193,12 @@ var TableCell = function TableCell(_ref2) {
189
193
  header = item.header,
190
194
  colSpan = item.colSpan,
191
195
  rowSpan = item.rowSpan,
192
- value = item.value;
196
+ value = item.value,
197
+ className = item.className;
193
198
  if (header) {
194
199
  return jsxRuntime.jsx("th", {
195
200
  id: id,
196
- className: "inks-cell",
201
+ className: clsx('inks-cell', className),
197
202
  colSpan: colSpan,
198
203
  rowSpan: rowSpan,
199
204
  children: value
@@ -201,7 +206,7 @@ var TableCell = function TableCell(_ref2) {
201
206
  }
202
207
  return jsxRuntime.jsx("td", {
203
208
  id: id,
204
- className: "inks-cell",
209
+ className: clsx('inks-cell', className),
205
210
  colSpan: colSpan,
206
211
  rowSpan: rowSpan,
207
212
  children: value
@@ -339,4 +344,5 @@ exports.InkScripter = InkScripter;
339
344
  exports.PrinterFrame = PrinterFrame;
340
345
  exports.getPageHeight = getPageHeight;
341
346
  exports.pix2mm = pix2mm;
347
+ exports.useFrameStyle = useFrameStyle;
342
348
  exports.usePagePrint = usePagePrint;
@@ -1,4 +1,5 @@
1
- export * from './usePagePrint';
2
1
  export * from './IFrame';
3
2
  export * from './PrinterFrame';
3
+ export * from './usePagePrint';
4
+ export * from './useFrameStyle';
4
5
  export * from './InkScripter';
package/dist/esm/index.js CHANGED
@@ -1,14 +1,59 @@
1
- import { useState, useCallback, useLayoutEffect, useMemo, forwardRef, useRef, useImperativeHandle } from 'react';
2
1
  import { jsxs, jsx } from 'react/jsx-runtime';
2
+ import { forwardRef, useRef, useMemo, useImperativeHandle, useState, useCallback, useLayoutEffect } from 'react';
3
3
  import { createPortal } from 'react-dom';
4
4
  import clsx from 'clsx';
5
5
 
6
+ const IFrame = forwardRef((props, ref) => {
7
+ const { styleCss, className, children, width, height } = props;
8
+ const iframeRef = useRef(null);
9
+ const [frameWidth, frameHeight] = useMemo(() => {
10
+ return [width, height];
11
+ }, [width, height]);
12
+ const attachBody = iframeRef?.current?.contentWindow?.document?.body;
13
+ const attachHead = iframeRef?.current?.contentWindow?.document?.head;
14
+ useImperativeHandle(ref, () => {
15
+ return iframeRef?.current;
16
+ }, []);
17
+ return (jsxs("iframe", { ref: iframeRef, className: className, width: frameWidth, height: frameHeight, children: [attachBody && createPortal(children, attachBody), attachHead && createPortal(jsx("style", { children: styleCss }), attachHead)] }));
18
+ });
19
+
20
+ const PrinterFrame = forwardRef((props, ref) => {
21
+ const { styleCss, open, className, children, actionRef, src, containerId, width, height, } = props;
22
+ const iframeRef = useRef(null);
23
+ useImperativeHandle(actionRef, () => {
24
+ return {
25
+ print() {
26
+ const cw = iframeRef?.current?.contentWindow;
27
+ if (cw) {
28
+ cw.focus();
29
+ cw.print();
30
+ }
31
+ },
32
+ };
33
+ }, []);
34
+ useImperativeHandle(ref, () => {
35
+ return iframeRef?.current;
36
+ });
37
+ const attachNode = containerId
38
+ ? iframeRef?.current?.contentWindow?.document?.getElementById(containerId)
39
+ : iframeRef?.current?.contentWindow?.document?.body;
40
+ const attachStyle = iframeRef?.current?.contentWindow?.document?.head;
41
+ return (jsxs("iframe", { ref: iframeRef, className: clsx('inks-iframe-root', !!open && 'inks-iframe-root-open', className), src: src, width: width, height: height, children: [!!attachStyle &&
42
+ createPortal(jsxs("style", { children: [`
43
+ @media print {
44
+ .hidden-print {
45
+ display: none !important;
46
+ }
47
+ }
48
+ `, styleCss] }), attachStyle), !!attachNode && createPortal(children, attachNode)] }));
49
+ });
50
+
6
51
  const getPageHeight = (height, page) => {
7
52
  const { marginTop = 0, marginBottom = 0 } = page;
8
53
  return height + marginTop + marginBottom;
9
54
  };
10
55
  const pix2mm = (pix) => Math.round(pix * 0.2645833333);
11
- const usePagePrint = ({ open, pageConfig, style, }) => {
56
+ const usePagePrint = ({ open, pageConfig, }) => {
12
57
  const [page, setPage] = useState({
13
58
  width: pageConfig.width || 0,
14
59
  height: pageConfig.height || 0,
@@ -43,6 +88,10 @@ const usePagePrint = ({ open, pageConfig, style, }) => {
43
88
  const getPageContent = useCallback(() => {
44
89
  return pageNode?.innerHTML || '';
45
90
  }, [pageNode]);
91
+ return { setPageNodeRef, page, getPageContent, pageNode };
92
+ };
93
+
94
+ const useFrameStyle = ({ page, style }) => {
46
95
  const pageStyle = useMemo(() => {
47
96
  const { width, height, marginLeft, marginRight, marginTop } = page;
48
97
  const extraStyle = `
@@ -65,54 +114,9 @@ padding-top: ${marginTop}mm;
65
114
  `;
66
115
  return style + '\n' + extraStyle;
67
116
  }, [page, style, page]);
68
- return { setPageNodeRef, page, getPageContent, pageNode, pageStyle };
117
+ return pageStyle;
69
118
  };
70
119
 
71
- const IFrame = forwardRef((props, ref) => {
72
- const { styleCss, className, children, width, height } = props;
73
- const iframeRef = useRef(null);
74
- const [frameWidth, frameHeight] = useMemo(() => {
75
- return [width, height];
76
- }, [width, height]);
77
- const attachBody = iframeRef?.current?.contentWindow?.document?.body;
78
- const attachHead = iframeRef?.current?.contentWindow?.document?.head;
79
- useImperativeHandle(ref, () => {
80
- return iframeRef?.current;
81
- }, []);
82
- return (jsxs("iframe", { ref: iframeRef, className: className, width: frameWidth, height: frameHeight, children: [attachBody && createPortal(children, attachBody), attachHead && createPortal(jsx("style", { children: styleCss }), attachHead)] }));
83
- });
84
-
85
- const PrinterFrame = forwardRef((props, ref) => {
86
- const { styleCss, open, className, children, actionRef, src, containerId, width, height, } = props;
87
- const iframeRef = useRef(null);
88
- useImperativeHandle(actionRef, () => {
89
- return {
90
- print() {
91
- const cw = iframeRef?.current?.contentWindow;
92
- if (cw) {
93
- cw.focus();
94
- cw.print();
95
- }
96
- },
97
- };
98
- }, []);
99
- useImperativeHandle(ref, () => {
100
- return iframeRef?.current;
101
- });
102
- const attachNode = containerId
103
- ? iframeRef?.current?.contentWindow?.document?.getElementById(containerId)
104
- : iframeRef?.current?.contentWindow?.document?.body;
105
- const attachStyle = iframeRef?.current?.contentWindow?.document?.head;
106
- return (jsxs("iframe", { ref: iframeRef, className: clsx('inks-iframe-root', !!open && 'inks-iframe-root-open', className), src: src, width: width, height: height, children: [!!attachStyle &&
107
- createPortal(jsxs("style", { children: [`
108
- @media print {
109
- .hidden-print {
110
- display: none !important;
111
- }
112
- }
113
- `, styleCss] }), attachStyle), !!attachNode && createPortal(children, attachNode)] }));
114
- });
115
-
116
120
  const Container = ({ id, content = [], className, }) => {
117
121
  return (jsx("div", { id: id, className: clsx('inks-grid', className), children: content.map((item, idx) => {
118
122
  switch (item.type) {
@@ -130,11 +134,11 @@ const TableCell = ({ item }) => {
130
134
  if (typeof item === 'string') {
131
135
  return jsx("td", { className: "inks-cell", children: item });
132
136
  }
133
- const { id, header, colSpan, rowSpan, value } = item;
137
+ const { id, header, colSpan, rowSpan, value, className } = item;
134
138
  if (header) {
135
- return (jsx("th", { id: id, className: "inks-cell", colSpan: colSpan, rowSpan: rowSpan, children: value }));
139
+ return (jsx("th", { id: id, className: clsx('inks-cell', className), colSpan: colSpan, rowSpan: rowSpan, children: value }));
136
140
  }
137
- return (jsx("td", { id: id, className: "inks-cell", colSpan: colSpan, rowSpan: rowSpan, children: value }));
141
+ return (jsx("td", { id: id, className: clsx('inks-cell', className), colSpan: colSpan, rowSpan: rowSpan, children: value }));
138
142
  };
139
143
  const TableRow = ({ cells }) => {
140
144
  if (!cells.length) {
@@ -192,4 +196,4 @@ const InkScripter = forwardRef((props, ref) => {
192
196
  }) }));
193
197
  });
194
198
 
195
- export { IFrame, InkScripter, PrinterFrame, getPageHeight, pix2mm, usePagePrint };
199
+ export { IFrame, InkScripter, PrinterFrame, getPageHeight, pix2mm, useFrameStyle, usePagePrint };
@@ -0,0 +1,6 @@
1
+ import { type PageConfig } from './usePagePrint';
2
+ export type useFrameStyleProps = {
3
+ page: PageConfig;
4
+ style?: string;
5
+ };
6
+ export declare const useFrameStyle: ({ page, style }: useFrameStyleProps) => string;
@@ -11,13 +11,11 @@ export declare const pix2mm: (pix: number) => number;
11
11
  export type usePagePrintProps = {
12
12
  open?: boolean;
13
13
  pageConfig: Partial<PageConfig>;
14
- style?: string;
15
14
  };
16
- export declare const usePagePrint: <T extends Element>({ open, pageConfig, style, }: usePagePrintProps) => {
15
+ export declare const usePagePrint: <T extends Element>({ open, pageConfig, }: usePagePrintProps) => {
17
16
  setPageNodeRef: (node: T) => void;
18
17
  page: PageConfig;
19
18
  getPageContent: () => string;
20
19
  pageNode: T | undefined;
21
- pageStyle: string;
22
20
  };
23
21
  export default usePagePrint;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-ink-scripter",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "description": "a react component for generate page to print",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -9,7 +9,13 @@
9
9
  "type": "git",
10
10
  "url": "git+https://github.com/lawrsp/react-ink-scripter.git"
11
11
  },
12
- "keywords": ["react", "component", "print", "printer", "iframe"],
12
+ "keywords": [
13
+ "react",
14
+ "component",
15
+ "print",
16
+ "printer",
17
+ "iframe"
18
+ ],
13
19
  "author": "lawrsp",
14
20
  "license": "MIT",
15
21
  "bugs": {
@@ -29,15 +35,13 @@
29
35
  "clsx": "^1.2.1"
30
36
  },
31
37
  "devDependencies": {
32
- "@babel/runtime": "^7.21.0",
33
- "react": "^18.2.0",
34
- "react-dom": "^18.2.0",
35
38
  "@babel/core": "^7.21.0",
36
39
  "@babel/plugin-proposal-object-rest-spread": "^7.20.7",
37
40
  "@babel/plugin-transform-runtime": "^7.21.0",
38
41
  "@babel/preset-env": "^7.20.2",
39
42
  "@babel/preset-react": "^7.18.6",
40
43
  "@babel/preset-typescript": "^7.21.0",
44
+ "@babel/runtime": "^7.21.0",
41
45
  "@rollup/plugin-babel": "^6.0.3",
42
46
  "@rollup/plugin-commonjs": "^24.0.1",
43
47
  "@rollup/plugin-node-resolve": "^15.0.1",
@@ -55,9 +59,12 @@
55
59
  "@vitejs/plugin-react": "^3.1.0",
56
60
  "babel-loader": "^8.3.0",
57
61
  "json": "^11.0.0",
62
+ "react": "^18.2.0",
63
+ "react-dom": "^18.2.0",
58
64
  "rimraf": "^4.1.3",
59
65
  "rollup": "^3.18.0",
60
66
  "rollup-plugin-sizes": "^1.0.5",
67
+ "tslib": "^2.8.1",
61
68
  "typescript": "^4.9.3",
62
69
  "vite": "^4.1.0"
63
70
  },
@@ -75,6 +82,11 @@
75
82
  "optional": true
76
83
  }
77
84
  },
78
- "sideEffects": ["*.css"],
79
- "files": ["dist"]
85
+ "sideEffects": [
86
+ "*.css"
87
+ ],
88
+ "files": [
89
+ "dist"
90
+ ],
91
+ "packageManager": "yarn@4.0.2+sha256.825003a0f561ad09a3b1ac4a3b3ea6207af2796d54f62a9420520915721f5186"
80
92
  }