Skip to content

Commit e4f1d2b

Browse files
authored
Merge pull request #356 from FalkorDB/add-capture-image-in-canvas
Fix #347 add capture image in canvas
2 parents 717755c + 719a968 commit e4f1d2b

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

app/components/code-graph.tsx

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GraphData, Link, Node } from "./model";
33
import { GraphContext } from "./provider";
44
import { Toolbar } from "./toolbar";
55
import { Labels } from "./labels";
6-
import { GitFork, Search, X } from "lucide-react";
6+
import { Download, GitFork, Search, X } from "lucide-react";
77
import ElementMenu from "./elementMenu";
88
import Combobox from "./combobox";
99
import { 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

package-lock.json

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
"@radix-ui/react-tooltip": "^1.1.4",
2121
"@types/react-gtm-module": "^2.0.4",
2222
"autoprefixer": "^10.4.20",
23+
"canvas2svg": "^1.0.16",
2324
"class-variance-authority": "^0.7.1",
2425
"clsx": "^2.1.0",
26+
"html2canvas": "^1.4.1",
2527
"lucide-react": "^0.441.0",
2628
"next": "^15.1.2",
2729
"playwright": "^1.49.1",

0 commit comments

Comments
 (0)