'use client'; import React, { useRef } from 'react'; import { AnimatePresence, motion } from 'framer-motion'; import { createPortal } from 'react-dom'; import { match } from 'ts-pattern'; import { useElementScaleSize } from '@documenso/lib/client-only/hooks/use-element-scale-size'; import { useFieldPageCoords } from '@documenso/lib/client-only/hooks/use-field-page-coords'; import { DEFAULT_HANDWRITING_FONT_SIZE, DEFAULT_STANDARD_FONT_SIZE, MIN_HANDWRITING_FONT_SIZE, MIN_STANDARD_FONT_SIZE, } from '@documenso/lib/constants/pdf'; import { Field, FieldType } from '@documenso/prisma/client'; import { FieldWithSignature } from '@documenso/prisma/types/field-with-signature'; import { cn } from '@documenso/ui/lib/utils'; import { Card, CardContent } from '@documenso/ui/primitives/card'; export type FieldContainerPortalProps = { field: FieldWithSignature; className?: string; children: React.ReactNode; }; export type SinglePlayerModeFieldContainerProps = { field: FieldWithSignature; children: React.ReactNode; }; export function FieldContainerPortal({ field, children, className = '', }: FieldContainerPortalProps) { const coords = useFieldPageCoords(field); return createPortal(
{children}
, document.body, ); } export function SinglePlayerModeFieldCardContainer({ field, children, }: SinglePlayerModeFieldContainerProps) { return ( {children} ); } export function SinglePlayerModeSignatureField({ field }: { field: FieldWithSignature }) { const fontVariable = '--font-signature'; const fontVariableValue = getComputedStyle(document.documentElement).getPropertyValue( fontVariable, ); const minFontSize = MIN_HANDWRITING_FONT_SIZE; const maxFontSize = DEFAULT_HANDWRITING_FONT_SIZE; if (!isSignatureFieldType(field.type)) { throw new Error('Invalid field type'); } const $paragraphEl = useRef(null); const { height, width } = useFieldPageCoords(field); const scalingFactor = useElementScaleSize( { height, width, }, $paragraphEl, maxFontSize, fontVariableValue, ); const fontSize = maxFontSize * scalingFactor; const insertedBase64Signature = field.inserted && field.Signature?.signatureImageAsBase64; const insertedTypeSignature = field.inserted && field.Signature?.typedSignature; return ( {insertedBase64Signature ? ( Your signature ) : insertedTypeSignature ? (

{insertedTypeSignature}

) : (

Signature

)}
); } export function SinglePlayerModeCustomTextField({ field }: { field: Field }) { const fontVariable = '--font-sans'; const fontVariableValue = getComputedStyle(document.documentElement).getPropertyValue( fontVariable, ); const minFontSize = MIN_STANDARD_FONT_SIZE; const maxFontSize = DEFAULT_STANDARD_FONT_SIZE; if (isSignatureFieldType(field.type)) { throw new Error('Invalid field type'); } const $paragraphEl = useRef(null); const { height, width } = useFieldPageCoords(field); const scalingFactor = useElementScaleSize( { height, width, }, $paragraphEl, maxFontSize, fontVariableValue, ); const fontSize = maxFontSize * scalingFactor; return ( {field.inserted ? (

{field.customText}

) : (

{match(field.type) .with(FieldType.DATE, () => 'Date') .with(FieldType.NAME, () => 'Name') .with(FieldType.EMAIL, () => 'Email') .with(FieldType.SIGNATURE, FieldType.FREE_SIGNATURE, () => 'Signature') .otherwise(() => '')}

)}
); } const isSignatureFieldType = (fieldType: Field['type']) => fieldType === FieldType.SIGNATURE || fieldType === FieldType.FREE_SIGNATURE;