(
nodes: FlowNode[],
options: Partial<ExportOptions> = {}
)
| 198 | * Supports PNG (high-res), SVG (vector), and PDF formats |
| 199 | */ |
| 200 | export async function downloadImage( |
| 201 | nodes: FlowNode[], |
| 202 | options: Partial<ExportOptions> = {} |
| 203 | ): Promise<void> { |
| 204 | if (nodes.length === 0) { |
| 205 | console.warn('No nodes to export'); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | const mergedOptions: Required<ExportOptions> = { |
| 210 | ...DEFAULT_OPTIONS, |
| 211 | ...options, |
| 212 | }; |
| 213 | |
| 214 | const element = getViewportElement(); |
| 215 | if (!element) { |
| 216 | console.error('ReactFlow viewport element not found'); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | const { viewport, width, height } = calculateBoundsAndViewport(nodes); |
| 221 | |
| 222 | try { |
| 223 | switch (mergedOptions.format) { |
| 224 | case 'png': { |
| 225 | const dataUrl = await exportToPng(element, width, height, viewport, mergedOptions); |
| 226 | triggerDownload(dataUrl, `${mergedOptions.fileName}.png`); |
| 227 | break; |
| 228 | } |
| 229 | case 'svg': { |
| 230 | const dataUrl = await exportToSvg(element, width, height, viewport, mergedOptions); |
| 231 | triggerDownload(dataUrl, `${mergedOptions.fileName}.svg`); |
| 232 | break; |
| 233 | } |
| 234 | case 'pdf': { |
| 235 | await exportToPdf(element, width, height, viewport, mergedOptions); |
| 236 | break; |
| 237 | } |
| 238 | default: |
| 239 | console.error(`Unsupported format: ${mergedOptions.format}`); |
| 240 | } |
| 241 | } catch (error) { |
| 242 | console.error('Failed to export image:', error); |
| 243 | throw error; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get list of supported export formats |
no test coverage detected