react-ink-scripter 0.0.21 → 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,74 +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
- var _useState = react.useState({
24
- width: pageConfig.width || 0,
25
- height: pageConfig.height || 0,
26
- marginLeft: pageConfig.marginLeft || 0,
27
- marginRight: pageConfig.marginRight || 0,
28
- marginTop: pageConfig.marginTop || 0,
29
- marginBottom: pageConfig.marginBottom || 0
30
- }),
31
- _useState2 = _slicedToArray(_useState, 2),
32
- page = _useState2[0],
33
- setPage = _useState2[1];
34
- var _useState3 = react.useState(),
35
- _useState4 = _slicedToArray(_useState3, 2),
36
- pageNode = _useState4[0],
37
- setPageNode = _useState4[1];
38
- var setPageNodeRef = react.useCallback(function (node) {
39
- setPageNode(node);
40
- }, []);
41
- react.useLayoutEffect(function () {
42
- if (!open) {
43
- return;
44
- }
45
- var rect = pageNode === null || pageNode === void 0 ? void 0 : pageNode.getBoundingClientRect();
46
- if (!rect) {
47
- return;
48
- }
49
- var width = pageConfig.width,
50
- height = pageConfig.height;
51
- var elmHeight = rect.height,
52
- elmWidth = rect.width;
53
- var pageWidth = width || pix2mm(elmWidth);
54
- var pageHeight = height ? height : getPageHeight(pix2mm(elmHeight), pageConfig);
55
- setPage(function (old) {
56
- return _extends({}, old, {
57
- width: pageWidth,
58
- height: pageHeight
59
- });
60
- });
61
- }, [pageNode, open, pageConfig]);
62
- var getPageContent = react.useCallback(function () {
63
- return (pageNode === null || pageNode === void 0 ? void 0 : pageNode.innerHTML) || '';
64
- }, [pageNode]);
65
- return {
66
- setPageNodeRef: setPageNodeRef,
67
- page: page,
68
- getPageContent: getPageContent,
69
- pageNode: pageNode
70
- };
71
- };
8
+ var _extends = require('@babel/runtime/helpers/extends');
72
9
 
73
10
  var IFrame = react.forwardRef(function (props, ref) {
74
11
  var _iframeRef$current, _iframeRef$current$co, _iframeRef$current$co2, _iframeRef$current2, _iframeRef$current2$c, _iframeRef$current2$c2;
@@ -141,6 +78,84 @@ var PrinterFrame = react.forwardRef(function (props, ref) {
141
78
  });
142
79
  });
143
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
+
144
159
  var Container = function Container(_ref) {
145
160
  var id = _ref.id,
146
161
  _ref$content = _ref.content,
@@ -328,4 +343,5 @@ exports.InkScripter = InkScripter;
328
343
  exports.PrinterFrame = PrinterFrame;
329
344
  exports.getPageHeight = getPageHeight;
330
345
  exports.pix2mm = pix2mm;
346
+ exports.useFrameStyle = useFrameStyle;
331
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,8 +1,53 @@
1
- import { useState, useCallback, useLayoutEffect, forwardRef, useRef, useMemo, 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;
@@ -46,50 +91,31 @@ const usePagePrint = ({ open, pageConfig, }) => {
46
91
  return { setPageNodeRef, page, getPageContent, pageNode };
47
92
  };
48
93
 
49
- const IFrame = forwardRef((props, ref) => {
50
- const { styleCss, className, children, width, height } = props;
51
- const iframeRef = useRef(null);
52
- const [frameWidth, frameHeight] = useMemo(() => {
53
- return [width, height];
54
- }, [width, height]);
55
- const attachBody = iframeRef?.current?.contentWindow?.document?.body;
56
- const attachHead = iframeRef?.current?.contentWindow?.document?.head;
57
- useImperativeHandle(ref, () => {
58
- return iframeRef?.current;
59
- }, []);
60
- return (jsxs("iframe", { ref: iframeRef, className: className, width: frameWidth, height: frameHeight, children: [attachBody && createPortal(children, attachBody), attachHead && createPortal(jsx("style", { children: styleCss }), attachHead)] }));
61
- });
94
+ const useFrameStyle = ({ page, style }) => {
95
+ const pageStyle = useMemo(() => {
96
+ const { width, height, marginLeft, marginRight, marginTop } = page;
97
+ const extraStyle = `
98
+ html {
99
+ margin: 0;
100
+ padding: 0;
101
+ width: ${width}mm;
102
+ height: ${height}mm
103
+ }
62
104
 
63
- const PrinterFrame = forwardRef((props, ref) => {
64
- const { styleCss, open, className, children, actionRef, src, containerId, width, height, } = props;
65
- const iframeRef = useRef(null);
66
- useImperativeHandle(actionRef, () => {
67
- return {
68
- print() {
69
- const cw = iframeRef?.current?.contentWindow;
70
- if (cw) {
71
- cw.focus();
72
- cw.print();
73
- }
74
- },
75
- };
76
- }, []);
77
- useImperativeHandle(ref, () => {
78
- return iframeRef?.current;
79
- });
80
- const attachNode = containerId
81
- ? iframeRef?.current?.contentWindow?.document?.getElementById(containerId)
82
- : iframeRef?.current?.contentWindow?.document?.body;
83
- const attachStyle = iframeRef?.current?.contentWindow?.document?.head;
84
- return (jsxs("iframe", { ref: iframeRef, className: clsx('inks-iframe-root', !!open && 'inks-iframe-root-open', className), src: src, width: width, height: height, children: [!!attachStyle &&
85
- createPortal(jsxs("style", { children: [`
86
- @media print {
87
- .hidden-print {
88
- display: none !important;
89
- }
105
+ body {
106
+ width: ${width}mm;
107
+ margin: 0;
108
+ padding: 0;
109
+ box-sizing: border-box;
110
+ padding-left: ${marginLeft}mm;
111
+ padding-right: ${marginRight}mm;
112
+ padding-top: ${marginTop}mm;
90
113
  }
91
- `, styleCss] }), attachStyle), !!attachNode && createPortal(children, attachNode)] }));
92
- });
114
+ `;
115
+ return style + '\n' + extraStyle;
116
+ }, [page, style, page]);
117
+ return pageStyle;
118
+ };
93
119
 
94
120
  const Container = ({ id, content = [], className, }) => {
95
121
  return (jsx("div", { id: id, className: clsx('inks-grid', className), children: content.map((item, idx) => {
@@ -170,4 +196,4 @@ const InkScripter = forwardRef((props, ref) => {
170
196
  }) }));
171
197
  });
172
198
 
173
- 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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-ink-scripter",
3
- "version": "0.0.21",
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",