@@ -3,7 +3,7 @@ import { GraphData, Link, Node } from "./model";
33import { GraphContext } from "./provider" ;
44import { Toolbar } from "./toolbar" ;
55import { Labels } from "./labels" ;
6- import { GitFork , Search , X } from "lucide-react" ;
6+ import { Download , GitFork , Search , X } from "lucide-react" ;
77import ElementMenu from "./elementMenu" ;
88import Combobox from "./combobox" ;
99import { toast } from '@/components/ui/use-toast' ;
@@ -217,12 +217,12 @@ export function CodeGraph({
217217
218218 const handleSearchSubmit = ( node : any ) => {
219219 const chart = chartRef . current
220-
220+
221221 if ( chart ) {
222222 const n = { name : node . properties . name , id : node . id }
223223
224224 let chartNode = graph . Elements . nodes . find ( n => n . id == node . id )
225-
225+
226226 if ( ! chartNode ?. visible ) {
227227 if ( ! chartNode ) {
228228 chartNode = graph . extend ( { nodes : [ node ] , edges : [ ] } ) . nodes [ 0 ]
@@ -234,7 +234,7 @@ export function CodeGraph({
234234 graph . visibleLinks ( true , [ chartNode ! . id ] )
235235 setData ( { ...graph . Elements } )
236236 }
237-
237+
238238 setSearchNode ( n )
239239 setTimeout ( ( ) => {
240240 chart . zoomToFit ( 1000 , 150 , ( n : NodeObject < Node > ) => n . id === chartNode ! . id ) ;
@@ -253,6 +253,33 @@ export function CodeGraph({
253253 setData ( { ...graph . Elements } )
254254 }
255255
256+ const handleDownloadImage = async ( ) => {
257+ try {
258+ const canvas = document . querySelector ( '.force-graph-container canvas' ) as HTMLCanvasElement ;
259+ if ( ! canvas ) {
260+ toast ( {
261+ title : "Error" ,
262+ description : "Canvas not found" ,
263+ variant : "destructive" ,
264+ } ) ;
265+ return ;
266+ }
267+
268+ const dataURL = canvas . toDataURL ( 'image/webp' ) ;
269+ const link = document . createElement ( 'a' ) ;
270+ link . href = dataURL ;
271+ link . download = `${ graphName } .webp` ;
272+ link . click ( ) ;
273+ } catch ( error ) {
274+ console . error ( 'Error downloading graph image:' , error ) ;
275+ toast ( {
276+ title : "Error" ,
277+ description : "Failed to download image. Please try again." ,
278+ variant : "destructive" ,
279+ } ) ;
280+ }
281+ } ;
282+
256283 return (
257284 < div className = "h-full w-full flex flex-col gap-4 p-8 bg-gray-100" >
258285 < header className = "flex flex-col gap-4" >
@@ -346,6 +373,12 @@ export function CodeGraph({
346373 className = "pointer-events-auto"
347374 chartRef = { chartRef }
348375 />
376+ < button
377+ className = "pointer-events-auto bg-white p-2 rounded-md"
378+ onClick = { handleDownloadImage }
379+ >
380+ < Download />
381+ </ button >
349382 </ div >
350383 </ div >
351384 < ElementMenu
0 commit comments