@@ -11,6 +11,7 @@ import { useTimeTicks } from './timeTicks';
1111import TSV2AxesLayer from './TSV2AxesLayer' ;
1212import TSV2CursorLayer from './TSV2CursorLayer' ;
1313import { exportToSVG , downloadSVG } from './svgExport' ;
14+ import { SVGExportCapability } from './SVGExportCapability' ;
1415import GetAppIcon from '@material-ui/icons/GetApp' ;
1516
1617type Props = {
@@ -29,6 +30,7 @@ type Props = {
2930 yMin ?: number
3031 yMax ?: number
3132 }
33+ svgExportCapability ?: SVGExportCapability
3234}
3335
3436const defaultMargins = {
@@ -53,7 +55,7 @@ export const useTimeScrollView2 = ({width, height, hideToolbar}: {width: number,
5355 }
5456}
5557
56- const TimeScrollView2 : FunctionComponent < Props > = ( { width, height, onCanvasElement, gridlineOpts, onKeyDown, onMouseDown, onMouseMove, onMouseOut, onMouseUp, hideToolbar, yAxisInfo} ) => {
58+ const TimeScrollView2 : FunctionComponent < Props > = ( { width, height, onCanvasElement, gridlineOpts, onKeyDown, onMouseDown, onMouseMove, onMouseOut, onMouseUp, hideToolbar, yAxisInfo, svgExportCapability } ) => {
5759 const { visibleStartTimeSec, visibleEndTimeSec, zoomTimeseriesSelection, panTimeseriesSelection } = useTimeRange ( )
5860 const { currentTime, currentTimeInterval } = useTimeseriesSelection ( )
5961 const timeRange = useMemo ( ( ) => (
@@ -174,34 +176,45 @@ const TimeScrollView2: FunctionComponent<Props> = ({width, height, onCanvasEleme
174176 onMouseOut && onMouseOut ( e )
175177 } , [ handleMouseLeave , onMouseOut ] )
176178
177- const handleExportSVG = useCallback ( ( ) => {
178- // Get canvas data as base64 image
179- let canvasImageData : string | undefined
180- if ( canvasRef . current ) {
181- try {
182- canvasImageData = canvasRef . current . toDataURL ( 'image/png' )
183- } catch ( error ) {
184- console . warn ( 'Could not export canvas data:' , error )
179+ const handleExportSVG = useCallback ( async ( ) => {
180+ try {
181+ // Create SVG export props
182+ const svgProps = {
183+ width : canvasWidth ,
184+ height : canvasHeight ,
185+ margins,
186+ timeTicks,
187+ yTickSet : yAxisInfo ?. showTicks ? yTickSet : undefined ,
188+ gridlineOpts,
189+ currentTimePixels,
190+ currentTimeIntervalPixels
185191 }
186- }
187192
188- // Create SVG export props
189- const svgProps = {
190- width : canvasWidth ,
191- height : canvasHeight ,
192- margins,
193- timeTicks,
194- yTickSet : yAxisInfo ?. showTicks ? yTickSet : undefined ,
195- gridlineOpts,
196- currentTimePixels,
197- currentTimeIntervalPixels,
198- canvasImageData
199- }
193+ let svgContent : string
200194
201- // Generate and download SVG
202- const svgContent = exportToSVG ( svgProps )
203- downloadSVG ( svgContent )
204- } , [ canvasWidth , canvasHeight , margins , timeTicks , yAxisInfo ?. showTicks , yTickSet , gridlineOpts , currentTimePixels , currentTimeIntervalPixels ] )
195+ if ( svgExportCapability ?. canExportToSVG && svgExportCapability . exportToSVG ) {
196+ // Use parent's SVG export capability for proper vector export
197+ const additionalSVGElements = await svgExportCapability . exportToSVG ( svgProps )
198+ svgContent = exportToSVG ( { ...svgProps , additionalSVGElements} )
199+ } else {
200+ // Fallback to bitmap export (should not happen if button is properly hidden)
201+ let canvasImageData : string | undefined
202+ if ( canvasRef . current ) {
203+ try {
204+ canvasImageData = canvasRef . current . toDataURL ( 'image/png' )
205+ } catch ( error ) {
206+ console . warn ( 'Could not export canvas data:' , error )
207+ }
208+ }
209+ svgContent = exportToSVG ( { ...svgProps , canvasImageData} )
210+ }
211+
212+ downloadSVG ( svgContent )
213+ } catch ( error ) {
214+ console . error ( 'Failed to export SVG:' , error )
215+ alert ( 'Failed to export SVG. Please try again.' )
216+ }
217+ } , [ canvasWidth , canvasHeight , margins , timeTicks , yAxisInfo ?. showTicks , yTickSet , gridlineOpts , currentTimePixels , currentTimeIntervalPixels , svgExportCapability ] )
205218
206219 const handleCanvasElement = useCallback ( ( elmt : HTMLCanvasElement ) => {
207220 canvasRef . current = elmt
@@ -239,14 +252,14 @@ const TimeScrollView2: FunctionComponent<Props> = ({width, height, onCanvasEleme
239252
240253 const exportAction = useMemo ( ( ) => ( {
241254 type : 'button' as const ,
242- title : 'Export to SVG' ,
255+ title : 'Export plot to SVG' ,
243256 icon : < GetAppIcon /> ,
244257 callback : handleExportSVG ,
245258 selected : false
246259 } ) , [ handleExportSVG ] )
247260
248261 const timeControlActions = useActionToolbar ( {
249- belowDefault : [ exportAction ]
262+ belowDefault : svgExportCapability ?. canExportToSVG ? [ exportAction ] : [ ]
250263 } )
251264
252265 if ( hideToolbar ) {
0 commit comments