import type { ExportSettings } from '@zakodium/nmrium-core'; import type { ReactNode } from 'react'; import { useState } from 'react'; import { createPortal } from 'react-dom'; import { transferStylesToDocument } from '../utilities/transferStyles.js'; import { ExportOptionsModal } from './ExportOptionsModal.js'; import { ExportSettingsProvider } from './ExportSettingsProvider.js'; import { RenderDetector } from './RenderDetector.js'; import { INITIAL_BASIC_EXPORT_OPTIONS } from './utilities/getExportOptions.js'; import { getSizeInPixel } from './utilities/getSizeInPixel.js'; export interface BaseExportProps { onExportOptionsChange: (options: ExportSettings) => void; defaultExportOptions?: ExportSettings; } interface RenderSizeOption { width: number; minWidth?: number; preserveAspectRatio?: boolean; } interface BaseExportFrameProps { children: ReactNode; onExportReady: ( documentElement: HTMLElement, options: ExportSettings, ) => void; renderOptions: RenderSizeOption; } interface InnerExportFrameProps extends BaseExportFrameProps { exportOptions: ExportSettings; } interface ExportFrameProps extends BaseExportFrameProps, Partial { exportOptions?: ExportSettings; onExportDialogClose?: () => void; confirmButtonText?: string; } export function ExportContent(props: ExportFrameProps) { const [innerExportOptions, setInnerExportOptions] = useState(); const { children, exportOptions, defaultExportOptions, onExportOptionsChange, onExportReady, onExportDialogClose, confirmButtonText = 'Save', renderOptions, } = props; if (!innerExportOptions && exportOptions?.useDefaultSettings === false) { return ( { onExportDialogClose?.(); }} onExportOptionsChange={(options) => { onExportOptionsChange?.(options); setInnerExportOptions(options); }} defaultExportOptions={defaultExportOptions} confirmButtonText={confirmButtonText} /> ); } const options = innerExportOptions || exportOptions || defaultExportOptions || INITIAL_BASIC_EXPORT_OPTIONS; return ( <>
{children} ); } function InnerPrintFrame(props: InnerExportFrameProps) { const { children, exportOptions, onExportReady, renderOptions } = props; const { width, height } = getSizeInPixel(exportOptions); const [iframeDocument, setIframeDocument] = useState(); function refHandler(frame: HTMLIFrameElement | null) { if (!frame) return; const document = frame.contentWindow?.document; if (!document) return; setIframeDocument(document); transferStylesToDocument(document); } const exportWidthInPixel = Math.round(width); const exportHeightInPixel = Math.round(height); const { width: baseRenderWidth, preserveAspectRatio = true, minWidth = 0, } = renderOptions; const renderWidth = Math.round(Math.max(baseRenderWidth, minWidth)); const widthInPixel = preserveAspectRatio ? renderWidth : exportWidthInPixel; const heightInPixel = preserveAspectRatio ? Math.round((height / width) * renderWidth) : exportHeightInPixel; return ( ); }