import React, { useRef } from "react"; import { Modal, Slider, Button, Select, Row, Col, Form, Divider, RadioChangeEvent, Radio, InputNumber, Collapse, } from "antd"; import ReactToPrint from "react-to-print"; import { useSavedState } from "../../utils/saveload"; import { useTranslate } from "@refinedev/core"; interface PrintingDialogProps { items: JSX.Element[]; style?: string; extraSettings?: JSX.Element; visible: boolean; onCancel: () => void; title?: string; } interface PaperDimensions { width: number; height: number; } const paperDimensions: { [key: string]: PaperDimensions } = { A3: { width: 297, height: 420, }, A4: { width: 210, height: 297, }, A5: { width: 148, height: 210, }, Letter: { width: 216, height: 279, }, Legal: { width: 216, height: 356, }, Tabloid: { width: 279, height: 432, }, }; const PrintingDialog: React.FC = ({ items, style, extraSettings, visible, onCancel, title }) => { const t = useTranslate(); const [collapseState, setCollapseState] = useSavedState("print-collapseState", []); const [marginLeft, setMarginLeft] = useSavedState("print-marginLeft", 10); const [marginTop, setMarginTop] = useSavedState("print-marginTop", 10); const [marginRight, setMarginRight] = useSavedState("print-marginRight", 10); const [marginBottom, setMarginBottom] = useSavedState("print-marginBottom", 10); const [horizontalSpacing, setHorizontalSpacing] = useSavedState("print-horizontalSpacing", 0); const [verticalSpacing, setVerticalSpacing] = useSavedState("print-verticalSpacing", 0); const [printerMarginLeft, setPrinterMarginLeft] = useSavedState("print-printerMarginLeft", 5); const [printerMarginTop, setPrinterMarginTop] = useSavedState("print-printerMarginTop", 5); const [printerMarginRight, setPrinterMarginRight] = useSavedState("print-printerMarginRight", 5); const [printerMarginBottom, setPrinterMarginBottom] = useSavedState("print-printerMarginBottom", 5); const [paperColumns, setPaperColumns] = useSavedState("print-itemsPerRow", 3); const [paperRows, setPaperRows] = useSavedState("print-rowsPerPage", 8); const [skipItems, setSkipItems] = useSavedState("print-skipItems", 0); const [paperSize, setPaperSize] = useSavedState("print-paperSize", "A4"); const [customPaperWidth, setCustomPaperWidth] = useSavedState("print-customPaperWidth", 210); const [customPaperHeight, setCustomPaperHeight] = useSavedState("print-customPaperHeight", 297); const [previewScale, setPreviewScale] = useSavedState("print-previewScale", 0.6); const [borderShowMode, setBorderShowMode] = useSavedState<"none" | "border" | "grid">("print-borderShowMode", "grid"); const paperWidth = paperSize === "custom" ? customPaperWidth : paperDimensions[paperSize].width; const paperHeight = paperSize === "custom" ? customPaperHeight : paperDimensions[paperSize].height; const printRef = useRef(null); const itemWidth = (paperWidth - marginLeft - marginRight - horizontalSpacing) / paperColumns - horizontalSpacing; const itemHeight = (paperHeight - marginTop - marginBottom - verticalSpacing) / paperRows - verticalSpacing; const itemsPerRow = paperColumns; const rowsPerPage = paperRows; const itemsIncludingSkipped = [...Array(skipItems).fill(<>), ...items]; const rowsOfItems = []; for (let row_idx = 0; row_idx <= itemsIncludingSkipped.length / itemsPerRow; row_idx += 1) { rowsOfItems.push(itemsIncludingSkipped.slice(row_idx * itemsPerRow, (row_idx + 1) * itemsPerRow)); } const pageBlocks = []; for (let page_idx = 0; page_idx <= rowsOfItems.length / rowsPerPage; page_idx += 1) { pageBlocks.push(rowsOfItems.slice(page_idx * rowsPerPage, (page_idx + 1) * rowsPerPage)); } const pages = pageBlocks.map(function (rows, pageIdx) { const itemRows = rows.map((row, rowIdx) => { return ( {row.map(function (item, colIdx) { return (
{item}
); })} ); }); return (
{itemRows}
); }); return ( } content={() => printRef.current} />, ]} width={1200} // Set the modal width to accommodate the preview > {t("printing.generic.description")}
{pages}
{ setSkipItems(value); }} /> { setSkipItems(value ?? 1); }} /> { setBorderShowMode(e.target.value); }} value={borderShowMode} optionType="button" buttonStyle="solid" /> { setPreviewScale(value); }} /> { setPreviewScale(value ?? 0.1); }} /> { if (Array.isArray(key)) { setCollapseState(key); } }} > {extraSettings} { setPaperColumns(value); }} /> { setPaperColumns(value ?? 1); }} /> { setPaperRows(value); }} /> { setPaperRows(value ?? 1); }} />

{t("printing.generic.helpMargin")}

`${value} mm` }} value={marginLeft} onChange={(value) => { setMarginLeft(value); }} /> { setMarginLeft(value ?? 0); }} /> `${value} mm` }} value={marginTop} onChange={(value) => { setMarginTop(value); }} /> { setMarginTop(value ?? 0); }} /> `${value} mm` }} value={marginRight} onChange={(value) => { setMarginRight(value); }} /> { setMarginRight(value ?? 0); }} /> `${value} mm` }} value={marginBottom} onChange={(value) => { setMarginBottom(value); }} /> { setMarginBottom(value ?? 0); }} />

{t("printing.generic.helpPrinterMargin")}

`${value} mm` }} value={printerMarginLeft} onChange={(value) => { setPrinterMarginLeft(value); }} /> { setPrinterMarginLeft(value ?? 0); }} /> `${value} mm` }} value={printerMarginTop} onChange={(value) => { setPrinterMarginTop(value); }} /> { setPrinterMarginTop(value ?? 0); }} /> `${value} mm` }} value={printerMarginRight} onChange={(value) => { setPrinterMarginRight(value); }} /> { setPrinterMarginRight(value ?? 0); }} /> `${value} mm` }} value={printerMarginBottom} onChange={(value) => { setPrinterMarginBottom(value); }} /> { setPrinterMarginBottom(value ?? 0); }} /> `${value} mm` }} value={horizontalSpacing} onChange={(value) => { setHorizontalSpacing(value); }} /> { setHorizontalSpacing(value ?? 0); }} /> `${value} mm` }} value={verticalSpacing} onChange={(value) => { setVerticalSpacing(value); }} /> { setVerticalSpacing(value ?? 0); }} />
); }; export default PrintingDialog;