react-ink-scripter 0.0.22 → 0.0.23

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,
@@ -339,4 +343,5 @@ exports.InkScripter = InkScripter;
339
343
  exports.PrinterFrame = PrinterFrame;
340
344
  exports.getPageHeight = getPageHeight;
341
345
  exports.pix2mm = pix2mm;
346
+ exports.useFrameStyle = useFrameStyle;
342
347
  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) {
@@ -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.23",
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",