'use client'; import React, { useRef } from 'react'; import { AnimatePresence, motion } from 'framer-motion'; 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 type { Field } from '@documenso/prisma/client'; import { FieldType } from '@documenso/prisma/client'; import type { FieldWithSignature } from '@documenso/prisma/types/field-with-signature'; import { FieldRootContainer } from '../../components/field/field'; export type SinglePlayerModeFieldContainerProps = { field: FieldWithSignature; children: React.ReactNode; }; export type SinglePlayerModeFieldProps = { field: T; onClick?: () => void; }; export function SinglePlayerModeFieldCardContainer({ field, children, }: SinglePlayerModeFieldContainerProps) { return ( {children} ); } export function SinglePlayerModeSignatureField({ field, onClick, }: SinglePlayerModeFieldProps) { 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 { height, width } = useFieldPageCoords(field); const insertedBase64Signature = field.inserted && field.Signature?.signatureImageAsBase64; const insertedTypeSignature = field.inserted && field.Signature?.typedSignature; const scalingFactor = useElementScaleSize( { height, width, }, insertedTypeSignature || '', maxFontSize, fontVariableValue, ); const fontSize = maxFontSize * scalingFactor; return ( {insertedBase64Signature ? ( Your signature ) : insertedTypeSignature ? (

{insertedTypeSignature}

) : ( )}
); } export function SinglePlayerModeCustomTextField({ field, onClick, }: SinglePlayerModeFieldProps) { 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, }, field.customText, maxFontSize, fontVariableValue, ); const fontSize = maxFontSize * scalingFactor; return ( {field.inserted ? (

{field.customText}

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