import type { JSXConverters, JSXConvertersFunction } from '@payloadcms/richtext-lexical/react' import type { ReactNode } from 'react' import type { LightboxImageOptions } from '../lightbox/lightboxImage.js' import { lightboxImage } from '../lightbox/lightboxImage.js' import { LightboxTrigger } from '../lightbox/LightboxProvider.js' export type RichTextLightboxOptions = { /** * Images of one rich text browse together under this group. Default * 'richtext' — every rich text on the page is one gallery; pass a different * group per field to keep them apart, or `false` to open each image alone. */ group?: false | string /** Passed to lightboxImage: `size` (src fallback), `sizes`, `srcSet`. */ image?: Pick /** Class on each trigger button (e.g. Tailwind `my-6 block`). */ triggerClassName?: string } type UploadNode = { fields?: { alt?: unknown; caption?: unknown } | null; value?: unknown } /** * Wraps every image upload of a rich text in , on top of the * project's converters: your own `upload` converter still renders the inline * image (Payload's default otherwise); only the click-to-enlarge button is * added around it. Files that aren't images (PDF, video) are left as they are. * * // the shortcut * // your own renderer */ export function withLightboxUploads(converters?: JSXConvertersFunction, options: RichTextLightboxOptions = {}): JSXConvertersFunction { const group = options.group === false ? undefined : (options.group ?? 'richtext') return (args) => { const base: JSXConverters = converters ? converters(args) : args.defaultConverters const inner = base.upload return { ...base, upload: (props: { node: UploadNode }) => { const rendered = typeof inner === 'function' ? (inner as (p: typeof props) => ReactNode)(props) : null const { fields, value } = props.node const alt = typeof fields?.alt === 'string' && fields.alt ? fields.alt : undefined const caption = typeof fields?.caption === 'string' && fields.caption ? fields.caption : undefined const image = lightboxImage(value, { ...options.image, ...(alt ? { alt } : {}), ...(caption ? { caption } : {}) }) if (!image || rendered === null || rendered === undefined) {return rendered} return ( {rendered} ) }, } as JSXConverters } }