Skip to content

Commit

Permalink
Merge pull request #356 from FalkorDB/add-capture-image-in-canvas
Browse files Browse the repository at this point in the history
Fix #347 add capture image in canvas
  • Loading branch information
Anchel123 authored Feb 6, 2025
2 parents 717755c + 719a968 commit e4f1d2b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
41 changes: 37 additions & 4 deletions app/components/code-graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GraphData, Link, Node } from "./model";
import { GraphContext } from "./provider";
import { Toolbar } from "./toolbar";
import { Labels } from "./labels";
import { GitFork, Search, X } from "lucide-react";
import { Download, GitFork, Search, X } from "lucide-react";
import ElementMenu from "./elementMenu";
import Combobox from "./combobox";
import { toast } from '@/components/ui/use-toast';
Expand Down Expand Up @@ -217,12 +217,12 @@ export function CodeGraph({

const handleSearchSubmit = (node: any) => {
const chart = chartRef.current

if (chart) {
const n = { name: node.properties.name, id: node.id }

let chartNode = graph.Elements.nodes.find(n => n.id == node.id)

if (!chartNode?.visible) {
if (!chartNode) {
chartNode = graph.extend({ nodes: [node], edges: [] }).nodes[0]
Expand All @@ -234,7 +234,7 @@ export function CodeGraph({
graph.visibleLinks(true, [chartNode!.id])
setData({ ...graph.Elements })
}

setSearchNode(n)
setTimeout(() => {
chart.zoomToFit(1000, 150, (n: NodeObject<Node>) => n.id === chartNode!.id);
Expand All @@ -253,6 +253,33 @@ export function CodeGraph({
setData({ ...graph.Elements })
}

const handleDownloadImage = async () => {
try {
const canvas = document.querySelector('.force-graph-container canvas') as HTMLCanvasElement;
if (!canvas) {
toast({
title: "Error",
description: "Canvas not found",
variant: "destructive",
});
return;
}

const dataURL = canvas.toDataURL('image/webp');
const link = document.createElement('a');
link.href = dataURL;
link.download = `${graphName}.webp`;
link.click();
} catch (error) {
console.error('Error downloading graph image:', error);
toast({
title: "Error",
description: "Failed to download image. Please try again.",
variant: "destructive",
});
}
};

return (
<div className="h-full w-full flex flex-col gap-4 p-8 bg-gray-100">
<header className="flex flex-col gap-4">
Expand Down Expand Up @@ -346,6 +373,12 @@ export function CodeGraph({
className="pointer-events-auto"
chartRef={chartRef}
/>
<button
className="pointer-events-auto bg-white p-2 rounded-md"
onClick={handleDownloadImage}
>
<Download />
</button>
</div>
</div>
<ElementMenu
Expand Down
51 changes: 51 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
"@radix-ui/react-tooltip": "^1.1.4",
"@types/react-gtm-module": "^2.0.4",
"autoprefixer": "^10.4.20",
"canvas2svg": "^1.0.16",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.0",
"html2canvas": "^1.4.1",
"lucide-react": "^0.441.0",
"next": "^15.1.2",
"playwright": "^1.49.1",
Expand Down

0 comments on commit e4f1d2b

Please sign in to comment.