import { PropsDefs, PropsDocument, PropsG, PropsImage, PropsLine, PropsLinearGradient, PropsLink, PropsPage, PropsPath, PropsRect, PropsSVG, PropsStop, PropsText, PropsView, fontWeight, } from './Types'; import { Defs, Document, G, Image, Line, LinearGradient, Link, Page, Path, Rect, Stop, Svg, Text, View, } from '@react-pdf/renderer'; import { Style } from '@react-pdf/types'; import { FC, useState } from 'react'; let isHtml = true; export const usePDFComponentsAreHTML = () => { const [html, setHtml] = useState(isHtml); isHtml = html; return { isHTML: html, setHtml, }; }; const fontWeightConverter = (fontWeight?: fontWeight) => { if (typeof fontWeight === 'number') return fontWeight; switch (fontWeight) { case 'thin': return 100; case 'hairline': return 100; case 'ultralight': return 200; case 'extralight': return 200; case 'light': return 300; case 'normal': return 400; case 'medium': return 500; case 'semibold': return 600; case 'demibold': return 600; case 'bold': return 700; case 'ultrabold': return 800; case 'extrabold': return 800; case 'heavy': return 900; case 'black': return 900; default: return 400; } }; const adjustStyles = (style: Style) => { if (!style) return; Object.keys(style).forEach(key => { if (key === 'paddingVertical') { style.paddingTop = style[key]; style.paddingBottom = style[key]; } else if (key === 'paddingHorizontal') { style.paddingLeft = style[key]; style.paddingRight = style[key]; } else if (key === 'fontWeight') { style.fontWeight = fontWeightConverter(style[key]); } return style; }); }; const mergeStylesIntoOne = (styles: Style[]) => { const mergedStyle: Style = {}; if (!styles[0]) return mergedStyle; styles.forEach(style => { Object.keys(style).forEach(key => { mergedStyle[key as keyof Style] = style[key as keyof Style]; }); }); return mergedStyle; }; export const CustomView: FC = ({ children, style, ...rest }) => { if (isHtml) { let newStyle = style; if (Array.isArray(style)) { newStyle = mergeStylesIntoOne(style) as { [key: string]: string; }; } adjustStyles(newStyle as { [key: string]: string }); return (
{children}
); } if (Array.isArray(style)) { style = [ { display: 'flex', flexDirection: 'column', position: 'relative', left: 0, right: 0, }, ...style, ]; } else { style = { display: 'flex', flexDirection: 'column', position: 'relative', left: 0, right: 0, ...style, }; } return ( {children} ); }; export const CustomText: FC = ({ children, style, ...rest }) => { let newStyle = style; if (Array.isArray(style)) { newStyle = mergeStylesIntoOne(style) as { [key: string]: string; }; } if (isHtml) { adjustStyles(newStyle as { [key: string]: string }); return (
{children}
); } return ( {children} ); }; export const CustomImage: FC = ({ style, ...rest }) => { if (isHtml) { let newStyle = style; if (Array.isArray(style)) { newStyle = mergeStylesIntoOne(style) as { [key: string]: string; }; } adjustStyles(newStyle as { [key: string]: string }); return ( ); } return ; }; export const CustomPage: FC = ({ style, children, ...rest }) => { if (isHtml) { let newStyle = style; if (Array.isArray(style)) { newStyle = mergeStylesIntoOne(style) as { [key: string]: string; }; } adjustStyles(newStyle as { [key: string]: string }); return (
{children}
); } return ( {children} ); }; export const CustomLink: FC = ({ children, style, ...rest }) => { if (isHtml) { let newStyle = style; if (Array.isArray(style)) { newStyle = mergeStylesIntoOne(style) as { [key: string]: string; }; } adjustStyles(newStyle as { [key: string]: string }); return (
{children}
); } return ( {children} ); }; export const CustomG: FC = ({ children, ...rest }) => { if (isHtml) { return {children}; } return {children}; }; export const CustomPath: FC = ({ children, ...rest }) => { if (isHtml) { return {children}; } return {children}; }; export const CustomRect: FC = ({ children, ...rest }) => { if (isHtml) { return {children}; } return {children}; }; export const CustomSVG: FC = ({ children, ...rest }) => { if (isHtml) { const style = { left: 0, right: 0, ...rest.style, }; return ( {children} ); } return {children}; }; export const CustomDefs: FC = ({ children, ...rest }) => { if (isHtml) { return {children}; } return {children}; }; export const CustomLine: FC = ({ children, ...rest }) => { if (isHtml) { return {children}; } return {children}; }; export const CustomStop: FC = ({ children, ...rest }) => { if (isHtml) { return {children}; } return {children}; }; export const CustomLinearGradient: FC = ({ children, ...rest }) => { if (isHtml) { return {children}; } return {children}; }; export const CustomDocument: FC = ({ children, ...rest }) => { if (isHtml) { return (
{children}
); } return {children}; }; export { CustomDefs as Defs, CustomDocument as Document, CustomG as G, CustomImage as Image, CustomLine as Line, CustomLinearGradient as LinearGradient, CustomLink as Link, CustomPage as Page, CustomPath as Path, CustomRect as Rect, CustomStop as Stop, CustomSVG as Svg, CustomText as Text, CustomView as View, };