From 78cccfa2aae06904a4a3e34326709848eabd0e02 Mon Sep 17 00:00:00 2001 From: Donkie Date: Sat, 15 Jul 2023 15:24:06 +0200 Subject: [PATCH] Client: Abstracted out the printing dialog --- .../components/printing/printingDialog.tsx | 305 +++++++++++++ .../printing/qrCodePrintingDialog.tsx | 404 ++++-------------- 2 files changed, 381 insertions(+), 328 deletions(-) create mode 100644 client/src/components/printing/printingDialog.tsx diff --git a/client/src/components/printing/printingDialog.tsx b/client/src/components/printing/printingDialog.tsx new file mode 100644 index 0000000..45e2c54 --- /dev/null +++ b/client/src/components/printing/printingDialog.tsx @@ -0,0 +1,305 @@ +import React, { useRef } from "react"; +import { + Modal, + Slider, + Button, + Select, + Row, + Col, + Form, + Divider, + Switch, +} from "antd"; +import ReactToPrint from "react-to-print"; +import { useSavedState } from "../../utils/saveload"; + +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 [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 [itemsPerRow, setItemsPerRow] = useSavedState("print-itemsPerRow", 3); + const [paperSize, setPaperSize] = useSavedState("print-paperSize", "A4"); + const [rowHeight, setRowHeight] = useSavedState("print-rowHeight", 70); + const [previewScale, setPreviewScale] = useSavedState( + "print-previewScale", + 0.6 + ); + const [showBorder, setShowBorder] = useSavedState("print-showBorder", true); + + const paperWidth = paperDimensions[paperSize].width; + const paperHeight = paperDimensions[paperSize].height; + + const printRef = useRef(null); + + const itemsPerPage = + itemsPerRow * + Math.floor((paperHeight - marginTop - marginBottom) / rowHeight); + const pageBlocks = []; + for (let i = 0; i < items.length; i += 1) { + if (i % itemsPerPage === 0) { + pageBlocks.push(items.slice(i, i + itemsPerPage)); + } + } + const rowBlocks = pageBlocks.map((pageBlock, idx) => ( +
+ + {pageBlock.map((item, index) => ( +
+ {item} +
+ ))} +
+
+ )); + + return ( + } + content={() => printRef.current} + />, + ]} + width={1000} // Set the modal width to accommodate the preview + > + + +
+
+ + {rowBlocks} +
+
+ + +
+ + { + setMarginLeft(value); + }} + /> + + + { + setMarginTop(value); + }} + /> + + + { + setMarginRight(value); + }} + /> + + + { + setMarginBottom(value); + }} + /> + + + { + setItemsPerRow(value); + }} + /> + + + + + + { + setRowHeight(value); + }} + /> + + + setShowBorder(checked)} + /> + + {extraSettings && } + {extraSettings} + + + { + setPreviewScale(value); + }} + /> + + + +
+
+ ); +}; + +export default PrintingDialog; diff --git a/client/src/components/printing/qrCodePrintingDialog.tsx b/client/src/components/printing/qrCodePrintingDialog.tsx index 5175a07..13d6aac 100644 --- a/client/src/components/printing/qrCodePrintingDialog.tsx +++ b/client/src/components/printing/qrCodePrintingDialog.tsx @@ -1,358 +1,106 @@ -import React, { useRef } from "react"; -import { - Modal, - Slider, - Button, - Select, - Row, - Col, - QRCode, - Form, - Divider, - Switch, -} from "antd"; -import ReactToPrint from "react-to-print"; +import { Form, QRCode, Slider, Switch } from "antd"; import { useSavedState } from "../../utils/saveload"; +import PrintingDialog from "./printingDialog"; interface QRCodeData { - id: number; - content: string; + value: string; + label?: string; + errorLevel?: "L" | "M" | "Q" | "H"; } interface QRCodePrintingDialogProps { visible: boolean; - onCancel: () => void; + items: QRCodeData[]; } -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 QRCodePrintingDialog: React.FC = ({ visible, - onCancel, + items, }) => { - 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 [codesPerRow, setCodesPerRow] = useSavedState("print-codesPerRow", 3); - const [paperSize, setPaperSize] = useSavedState("print-paperSize", "A4"); - const [rowHeight, setRowHeight] = useSavedState("print-rowHeight", 70); const [showContent, setShowContent] = useSavedState( "print-showContent", true ); - const [previewScale, setPreviewScale] = useSavedState( - "print-previewScale", - 0.6 - ); - const [showBorder, setShowBorder] = useSavedState("print-showBorder", true); - const [textSize, setTextSize] = useSavedState("print-textSize", 7); - - const qrCodeData: QRCodeData[] = Array.from({ length: 10 }).map( - (_, index) => ({ - id: index, - content: `QR Code ${index + 1}`, - }) + const [textSize, setTextSize] = useSavedState("print-textSize", 5); + const [showSpoolmanIcon, setShowSpoolmanIcon] = useSavedState( + "print-showSpoolmanIcon", + true ); - const paperWidth = paperDimensions[paperSize].width; - const paperHeight = paperDimensions[paperSize].height; - - const printRef = useRef(null); - - const codesPerPage = - codesPerRow * - Math.floor((paperHeight - marginTop - marginBottom) / rowHeight); - const pageBlocks = []; - for (let i = 0; i < qrCodeData.length; i += 1) { - if (i % codesPerPage === 0) { - pageBlocks.push(qrCodeData.slice(i, i + codesPerPage)); - } - } - const rowBlocks = pageBlocks.map((pageBlock, idx) => ( -
- - {pageBlock.map((qrCode) => ( + const elements = items.map((item) => { + return ( + <> + + {showContent && (
- - {showContent && ( -
- {qrCode.content} -
- )} + {item.label ?? item.value}
- ))} -
-
- )); + )} + + ); + }); return ( - } - content={() => printRef.current} - />, - ]} - width={1000} // Set the modal width to accommodate the preview - > - - -
-
+ + setShowContent(checked)} + /> + + + { + setTextSize(value); }} - > - - {rowBlocks} -
-
- - -
- - { - setMarginLeft(value); - }} - /> - - - { - setMarginTop(value); - }} - /> - - - { - setMarginRight(value); - }} - /> - - - { - setMarginBottom(value); - }} - /> - - - { - setCodesPerRow(value); - }} - /> - - - - - - { - setRowHeight(value); - }} - /> - - - { - setTextSize(value); - }} - /> - - - setShowContent(checked)} - /> - - - setShowBorder(checked)} - /> - - - - { - setPreviewScale(value); - }} - /> - - - -
-
+ .print-page svg { + display: block; + height: 100%; + width: auto; + } + `} + onCancel={() => true} + /> ); };