react-ink-scripter 0.0.13 → 0.0.16

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.
@@ -4,17 +4,18 @@
4
4
  flex-direction: column;
5
5
  overflow: hidden;
6
6
  padding: 4px;
7
+ font-size: 14px;
7
8
  }
8
9
 
9
10
  .inks-root > #title {
10
11
  text-align: center;
11
12
  flex: 1 0;
12
13
  padding: 16px;
13
- font-size: 20px;
14
+ font-size: 28px;
14
15
  }
15
16
 
16
17
  .inks-root > #footer {
17
- margin-left: -8px;
18
+ margin: 0;
18
19
  }
19
20
 
20
21
  .inks-grid {
@@ -96,9 +97,8 @@
96
97
  padding: 0;
97
98
  text-align: left;
98
99
  border-spacing: 0;
99
-
100
- border-top: var(--Border-Props);
101
- border-left: var(--Border-Props);
100
+ border-collapse: collapse;
101
+ border: var(--Border-Props);
102
102
  }
103
103
 
104
104
 
@@ -115,7 +115,6 @@
115
115
  .inks-cell {
116
116
  padding: 8px 4px;
117
117
  margin: 0;
118
- border-right: var(--Border-Props);
119
- border-bottom: var(--Border-Props);
118
+ border: var(--Border-Props);
120
119
  }
121
120
 
package/dist/cjs/index.js CHANGED
@@ -1,11 +1,69 @@
1
1
  'use strict';
2
2
 
3
+ var _extends = require('@babel/runtime/helpers/extends');
3
4
  var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
4
5
  var react = require('react');
5
6
  var reactDom = require('react-dom');
6
7
  var jsxRuntime = require('react/jsx-runtime');
7
8
  var clsx = require('clsx');
8
- var _extends = require('@babel/runtime/helpers/extends');
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: 0,
27
+ marginRight: 0,
28
+ marginTop: 0,
29
+ 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 setNodeRef = 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
+ return {
63
+ setNodeRef: setNodeRef,
64
+ page: page
65
+ };
66
+ };
9
67
 
10
68
  var IFrame = react.forwardRef(function (props, ref) {
11
69
  var _iframeRef$current, _iframeRef$current$co, _iframeRef$current$co2, _iframeRef$current2, _iframeRef$current2$c, _iframeRef$current2$c2;
@@ -68,7 +126,7 @@ var PrinterFrame = react.forwardRef(function (props, ref) {
68
126
  var attachStyle = iframeRef === null || iframeRef === void 0 ? void 0 : (_iframeRef$current4 = iframeRef.current) === null || _iframeRef$current4 === void 0 ? void 0 : (_iframeRef$current4$c = _iframeRef$current4.contentWindow) === null || _iframeRef$current4$c === void 0 ? void 0 : (_iframeRef$current4$c2 = _iframeRef$current4$c.document) === null || _iframeRef$current4$c2 === void 0 ? void 0 : _iframeRef$current4$c2.head;
69
127
  return jsxRuntime.jsxs("iframe", {
70
128
  ref: iframeRef,
71
- className: clsx("inks-iframe-root", !!open && "inks-iframe-root-open", className),
129
+ className: clsx('inks-iframe-root', !!open && 'inks-iframe-root-open', className),
72
130
  src: src,
73
131
  width: width,
74
132
  height: height,
@@ -85,16 +143,16 @@ var Container = function Container(_ref) {
85
143
  className = _ref.className;
86
144
  return jsxRuntime.jsx("div", {
87
145
  id: id,
88
- className: clsx("inks-grid", className),
146
+ className: clsx('inks-grid', className),
89
147
  children: content.map(function (item, idx) {
90
148
  switch (item.type) {
91
- case "pair":
149
+ case 'pair':
92
150
  return jsxRuntime.jsx(Pair, _extends({}, item, {
93
- className: clsx("inks-grid-item", item.className)
151
+ className: clsx('inks-grid-item', item.className)
94
152
  }), idx);
95
- case "text":
153
+ case 'text':
96
154
  return jsxRuntime.jsx(Text, _extends({}, item, {
97
- className: clsx("inks-grid-item", item.className)
155
+ className: clsx('inks-grid-item', item.className)
98
156
  }), idx);
99
157
  }
100
158
  })
@@ -105,7 +163,7 @@ var Grid = function Grid(props) {
105
163
  };
106
164
  var TableCell = function TableCell(_ref2) {
107
165
  var item = _ref2.item;
108
- if (typeof item === "string") {
166
+ if (typeof item === 'string') {
109
167
  return jsxRuntime.jsx("td", {
110
168
  className: "inks-cell",
111
169
  children: item
@@ -216,7 +274,7 @@ var Text = function Text(_ref4) {
216
274
  className = _ref4.className;
217
275
  return jsxRuntime.jsx("div", {
218
276
  id: id,
219
- className: clsx("inks-text", span && "inks-grid", span && "inks-span-".concat(span), className),
277
+ className: clsx('inks-text', span && "inks-span-".concat(span), className),
220
278
  children: value
221
279
  });
222
280
  };
@@ -228,7 +286,7 @@ var Pair = function Pair(_ref5) {
228
286
  className = _ref5.className;
229
287
  return jsxRuntime.jsxs("div", {
230
288
  id: id,
231
- className: clsx("inks-pair", span && "inks-grid", span && "inks-span-".concat(span), className),
289
+ className: clsx('inks-pair', span && "inks-span-".concat(span), className),
232
290
  children: [jsxRuntime.jsxs("label", {
233
291
  className: "inks-pair-label",
234
292
  children: [label, ":"]
@@ -243,17 +301,17 @@ var InkScripter = react.forwardRef(function (props, ref) {
243
301
  value = _props$value === void 0 ? [] : _props$value,
244
302
  className = props.className;
245
303
  return jsxRuntime.jsx("div", {
246
- className: clsx("inks-root", className),
304
+ className: clsx('inks-root', className),
247
305
  ref: ref,
248
306
  children: value.map(function (item, index) {
249
307
  switch (item.type) {
250
- case "grid":
308
+ case 'grid':
251
309
  return jsxRuntime.jsx(Grid, _extends({}, item), index);
252
- case "table":
310
+ case 'table':
253
311
  return jsxRuntime.jsx(Table, _extends({}, item), index);
254
- case "text":
312
+ case 'text':
255
313
  return jsxRuntime.jsx(Text, _extends({}, item), index);
256
- case "pair":
314
+ case 'pair':
257
315
  return jsxRuntime.jsx(Pair, _extends({}, item), index);
258
316
  }
259
317
  })
@@ -263,3 +321,6 @@ var InkScripter = react.forwardRef(function (props, ref) {
263
321
  exports.IFrame = IFrame;
264
322
  exports.InkScripter = InkScripter;
265
323
  exports.PrinterFrame = PrinterFrame;
324
+ exports.getPageHeight = getPageHeight;
325
+ exports.pix2mm = pix2mm;
326
+ exports.usePagePrint = usePagePrint;
@@ -1,4 +1,4 @@
1
- import type { ReactNode, ForwardRefExoticComponent, RefAttributes } from "react";
1
+ import type { ReactNode, ForwardRefExoticComponent, RefAttributes } from 'react';
2
2
  export interface IFrameProps {
3
3
  className?: string;
4
4
  styleCss?: string;
@@ -1,22 +1,22 @@
1
- import type { ForwardRefExoticComponent, RefAttributes } from "react";
1
+ import type { ForwardRefExoticComponent, RefAttributes } from 'react';
2
2
  export type ContentTextItem = {
3
3
  id?: string;
4
4
  className?: string;
5
- type: "text";
5
+ type: 'text';
6
6
  value: string;
7
7
  span?: number;
8
8
  };
9
9
  export type ContentPairItem = {
10
10
  id?: string;
11
11
  className?: string;
12
- type: "pair";
12
+ type: 'pair';
13
13
  label: string;
14
14
  value: string;
15
15
  span?: number;
16
16
  };
17
17
  export type ContentGridItem = {
18
18
  id?: string;
19
- type: "grid";
19
+ type: 'grid';
20
20
  content: (ContentPairItem | ContentTextItem)[];
21
21
  };
22
22
  export type ContentTableCellItem = {
@@ -29,7 +29,7 @@ export type ContentTableCellItem = {
29
29
  } | string;
30
30
  export type ContentTableItem = {
31
31
  id?: string;
32
- type: "table";
32
+ type: 'table';
33
33
  head?: ContentTableCellItem[][];
34
34
  body?: ContentTableCellItem[][];
35
35
  foot?: ContentTableCellItem[][];
@@ -1,4 +1,4 @@
1
- import type { Ref, ReactNode, ForwardRefExoticComponent, RefAttributes } from "react";
1
+ import type { Ref, ReactNode, ForwardRefExoticComponent, RefAttributes } from 'react';
2
2
  export interface PrinterActions {
3
3
  print: () => void;
4
4
  }
@@ -1,3 +1,4 @@
1
- export * from "./IFrame";
2
- export * from "./PrinterFrame";
3
- export * from "./InkScripter";
1
+ export * from './usePagePrint';
2
+ export * from './IFrame';
3
+ export * from './PrinterFrame';
4
+ export * from './InkScripter';
package/dist/esm/index.js CHANGED
@@ -1,8 +1,48 @@
1
+ import { useState, useCallback, useLayoutEffect, forwardRef, useRef, useMemo, useImperativeHandle } from 'react';
1
2
  import { jsxs, jsx } from 'react/jsx-runtime';
2
- import { forwardRef, useRef, useMemo, useImperativeHandle } from 'react';
3
3
  import { createPortal } from 'react-dom';
4
4
  import clsx from 'clsx';
5
5
 
6
+ const getPageHeight = (height, page) => {
7
+ const { marginTop = 0, marginBottom = 0 } = page;
8
+ return height + marginTop + marginBottom;
9
+ };
10
+ const pix2mm = (pix) => Math.round(pix * 0.2645833333);
11
+ const usePagePrint = ({ open, pageConfig, }) => {
12
+ const [page, setPage] = useState({
13
+ width: pageConfig.width || 0,
14
+ height: pageConfig.height || 0,
15
+ marginLeft: 0,
16
+ marginRight: 0,
17
+ marginTop: 0,
18
+ marginBottom: 0,
19
+ });
20
+ const [pageNode, setPageNode] = useState();
21
+ const setNodeRef = useCallback((node) => {
22
+ setPageNode(node);
23
+ }, []);
24
+ useLayoutEffect(() => {
25
+ if (!open) {
26
+ return;
27
+ }
28
+ const rect = pageNode?.getBoundingClientRect();
29
+ if (!rect) {
30
+ return;
31
+ }
32
+ // height === 0 means auto
33
+ const { width, height } = pageConfig;
34
+ const { height: elmHeight, width: elmWidth } = rect;
35
+ const pageWidth = width || pix2mm(elmWidth);
36
+ const pageHeight = height ? height : getPageHeight(pix2mm(elmHeight), pageConfig);
37
+ setPage((old) => ({
38
+ ...old,
39
+ width: pageWidth,
40
+ height: pageHeight,
41
+ }));
42
+ }, [pageNode, open, pageConfig]);
43
+ return { setNodeRef, page };
44
+ };
45
+
6
46
  const IFrame = forwardRef((props, ref) => {
7
47
  const { styleCss, className, children, width, height } = props;
8
48
  const iframeRef = useRef(null);
@@ -38,7 +78,7 @@ const PrinterFrame = forwardRef((props, ref) => {
38
78
  ? iframeRef?.current?.contentWindow?.document?.getElementById(containerId)
39
79
  : iframeRef?.current?.contentWindow?.document?.body;
40
80
  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 &&
81
+ 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
82
  createPortal(jsxs("style", { children: [`
43
83
  @media print {
44
84
  .hidden-print {
@@ -49,12 +89,12 @@ const PrinterFrame = forwardRef((props, ref) => {
49
89
  });
50
90
 
51
91
  const Container = ({ id, content = [], className, }) => {
52
- return (jsx("div", { id: id, className: clsx("inks-grid", className), children: content.map((item, idx) => {
92
+ return (jsx("div", { id: id, className: clsx('inks-grid', className), children: content.map((item, idx) => {
53
93
  switch (item.type) {
54
- case "pair":
55
- return (jsx(Pair, { ...item, className: clsx("inks-grid-item", item.className) }, idx));
56
- case "text":
57
- return (jsx(Text, { ...item, className: clsx("inks-grid-item", item.className) }, idx));
94
+ case 'pair':
95
+ return (jsx(Pair, { ...item, className: clsx('inks-grid-item', item.className) }, idx));
96
+ case 'text':
97
+ return (jsx(Text, { ...item, className: clsx('inks-grid-item', item.className) }, idx));
58
98
  }
59
99
  }) }));
60
100
  };
@@ -62,7 +102,7 @@ const Grid = (props) => {
62
102
  return jsx(Container, { ...props });
63
103
  };
64
104
  const TableCell = ({ item }) => {
65
- if (typeof item === "string") {
105
+ if (typeof item === 'string') {
66
106
  return jsx("td", { className: "inks-cell", children: item });
67
107
  }
68
108
  const { id, header, colSpan, rowSpan, value } = item;
@@ -106,25 +146,25 @@ const Table = (props) => {
106
146
  return (jsxs("table", { id: id, className: "inks-table", children: [jsx(TableHead, { rows: head }), jsx(TableBody, { rows: body }), jsx(TableFoot, { rows: foot })] }));
107
147
  };
108
148
  const Text = ({ id, value, span, className }) => {
109
- return (jsx("div", { id: id, className: clsx("inks-text", span && "inks-grid", span && `inks-span-${span}`, className), children: value }));
149
+ return (jsx("div", { id: id, className: clsx('inks-text', span && `inks-span-${span}`, className), children: value }));
110
150
  };
111
151
  const Pair = ({ id, value, label, span, className }) => {
112
- return (jsxs("div", { id: id, className: clsx("inks-pair", span && "inks-grid", span && `inks-span-${span}`, className), children: [jsxs("label", { className: "inks-pair-label", children: [label, ":"] }), jsx("div", { className: "inks-pair-value", children: value })] }));
152
+ return (jsxs("div", { id: id, className: clsx('inks-pair', span && `inks-span-${span}`, className), children: [jsxs("label", { className: "inks-pair-label", children: [label, ":"] }), jsx("div", { className: "inks-pair-value", children: value })] }));
113
153
  };
114
154
  const InkScripter = forwardRef((props, ref) => {
115
155
  const { value = [], className } = props;
116
- return (jsx("div", { className: clsx("inks-root", className), ref: ref, children: value.map((item, index) => {
156
+ return (jsx("div", { className: clsx('inks-root', className), ref: ref, children: value.map((item, index) => {
117
157
  switch (item.type) {
118
- case "grid":
158
+ case 'grid':
119
159
  return jsx(Grid, { ...item }, index);
120
- case "table":
160
+ case 'table':
121
161
  return jsx(Table, { ...item }, index);
122
- case "text":
162
+ case 'text':
123
163
  return jsx(Text, { ...item }, index);
124
- case "pair":
164
+ case 'pair':
125
165
  return jsx(Pair, { ...item }, index);
126
166
  }
127
167
  }) }));
128
168
  });
129
169
 
130
- export { IFrame, InkScripter, PrinterFrame };
170
+ export { IFrame, InkScripter, PrinterFrame, getPageHeight, pix2mm, usePagePrint };
@@ -0,0 +1,19 @@
1
+ export type PageConfig = {
2
+ width: number;
3
+ height: number;
4
+ marginLeft: number;
5
+ marginRight: number;
6
+ marginTop: number;
7
+ marginBottom: number;
8
+ };
9
+ export declare const getPageHeight: (height: number, page: Partial<PageConfig>) => number;
10
+ export declare const pix2mm: (pix: number) => number;
11
+ export type usePagePrintProps = {
12
+ open?: boolean;
13
+ pageConfig: Partial<PageConfig>;
14
+ };
15
+ export declare const usePagePrint: <T extends Element>({ open, pageConfig, }: usePagePrintProps) => {
16
+ setNodeRef: (node: T) => void;
17
+ page: PageConfig;
18
+ };
19
+ export default usePagePrint;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-ink-scripter",
3
- "version": "0.0.13",
3
+ "version": "0.0.16",
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",
@@ -29,8 +29,7 @@
29
29
  "@types/react": "^17.0.0 || ^18.0.0",
30
30
  "@types/react-dom": "^17.0.0 || ^18.0.0",
31
31
  "react": "^17.0.0 || ^18.0.0",
32
- "react-dom": "^17.0.0 || ^18.0.0",
33
- "@babel/runtime": "^7.21.0"
32
+ "react-dom": "^17.0.0 || ^18.0.0"
34
33
  },
35
34
  "peerDependenciesMeta": {
36
35
  "@types/react": {