11// SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR)
22// SPDX-License-Identifier: Apache-2.0
33
4- import React from 'react' ;
4+ import React , { useCallback } from 'react' ;
55import Box from '@mui/material/Box' ;
66import useTheme from '@mui/material/styles/useTheme' ;
77import { useTranslation } from 'react-i18next' ;
88import Typography from '@mui/material/Typography' ;
99import Button from '@mui/material/Button' ;
10+ import { useExportingRegistry } from 'context/ExportContext' ;
11+ import type { Content , TDocumentDefinitions , ContentImage , Column } from 'pdfmake/interfaces' ;
12+
13+ const toDataUrl = ( img : unknown ) : string | undefined => {
14+ if ( ! img ) return undefined ;
15+ if ( typeof img === 'string' ) return img ;
16+ if ( typeof img === 'object' && img !== null && 'data' in ( img as Record < string , unknown > ) ) {
17+ const d = ( img as Record < string , unknown > ) . data ;
18+ return typeof d === 'string' ? d : undefined ;
19+ }
20+ return undefined ;
21+ } ;
1022
1123export default function ExportDialog ( ) : JSX . Element {
1224 const { t} = useTranslation ( ) ;
1325 const theme = useTheme ( ) ;
26+ const { get} = useExportingRegistry ( ) ;
27+
28+ const handleExport = useCallback ( ( ) => {
29+ void ( async ( ) => {
30+ const lineExp = get ( 'lineChart' ) ;
31+ const mapExp = get ( 'map' ) ;
32+
33+ const pdfMake =
34+ ( await ( lineExp as unknown as { getPDFMake ?: ( ) => Promise < unknown > } ) ?. getPDFMake ?.( ) ) ||
35+ ( await ( lineExp as unknown as { getPdfmake ?: ( ) => Promise < unknown > } ) ?. getPdfmake ?.( ) ) ||
36+ ( await ( mapExp as unknown as { getPDFMake ?: ( ) => Promise < unknown > } ) ?. getPDFMake ?.( ) ) ||
37+ ( await ( mapExp as unknown as { getPdfmake ?: ( ) => Promise < unknown > } ) ?. getPdfmake ?.( ) ) ;
38+
39+ const [ lineImg , mapImg ] = await Promise . all ( [ lineExp ?. export ?.( 'png' ) , mapExp ?. export ?.( 'png' ) ] ) ;
40+
41+ const lineDataUrl = toDataUrl ( lineImg ) ;
42+ const mapDataUrl = toDataUrl ( mapImg ) ;
43+
44+ const doc : TDocumentDefinitions = {
45+ pageSize : 'A4' ,
46+ pageOrientation : 'portrait' ,
47+ pageMargins : [ 30 , 30 , 30 , 30 ] ,
48+ content : [ ] ,
49+ styles : { header : { fontSize : 18 , bold : true , margin : [ 0 , 0 , 0 , 10 ] } } ,
50+ } ;
51+
52+ ( doc . content as Content [ ] ) . push ( { text : t ( 'export.header' ) , style : 'header' } ) ;
53+
54+ if ( lineDataUrl ) {
55+ ( doc . content as ContentImage [ ] ) . push ( {
56+ image : lineDataUrl ,
57+ width : 500 ,
58+ } ) ;
59+ }
60+
61+ if ( mapDataUrl ) {
62+ ( doc . content as ContentImage [ ] ) . push ( {
63+ image : mapDataUrl ,
64+ fit : [ 300 , 300 ] ,
65+ } ) ;
66+ }
67+
68+ // (doc.content as ContentTable[]).push({
69+ // table: {
70+ // body: [
71+ // [{text: 'Line Chart Data'}, {text: 'Line Chart Data'}],
72+ // [{text: 'Line Chart Data'}, {text: 'Line Chart Data'}],
73+ // ],
74+ // },
75+ // });
76+
77+ const columns : Column [ ] = [
78+ {
79+ text : 'Line Chart Data' ,
80+ } ,
81+ {
82+ text : 'Map Data' ,
83+ } ,
84+ ] ;
85+
86+ ( doc . content as Content [ ] ) . push ( {
87+ columns : columns ,
88+ columnGap : 10 ,
89+ } ) ;
90+
91+ const pdfmake = pdfMake as { createPdf ?: ( doc : unknown ) => { download : ( name : string ) => void } } ;
92+ pdfmake ?. createPdf ?.( doc ) ?. download ( 'ESID-export.pdf' ) ;
93+ } ) ( ) ;
94+ } , [ get , t ] ) ;
1495
1596 return (
1697 < Box
@@ -23,7 +104,7 @@ export default function ExportDialog(): JSX.Element {
23104 < br />
24105 < Typography > { t ( 'export.description' ) } </ Typography >
25106 < br />
26- < Button variant = 'contained' color = 'primary' >
107+ < Button variant = 'contained' color = 'primary' onClick = { handleExport } >
27108 { t ( 'export.button' ) }
28109 </ Button >
29110 </ Box >
0 commit comments