Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

fix(img-export): add ChartDB watermark to exported image (#588)

Jonathan Fishnerb935b7f25111
diff --git a/src/context/export-image-context/export-image-provider.tsx b/src/context/export-image-context/export-image-provider.tsx
index 2625f5ac..df416ac6 100644
--- a/src/context/export-image-context/export-image-provider.tsx
+++ b/src/context/export-image-context/export-image-provider.tsx
@@ -1,4 +1,4 @@
-import React, { useCallback, useMemo } from 'react';
+import React, { useCallback, useMemo, useEffect, useState } from 'react';
import type { ExportImageContext, ImageType } from './export-image-context';
import { exportImageContext } from './export-image-context';
import { toJpeg, toPng, toSvg } from 'html-to-image';
@@ -6,6 +6,8 @@ import { useReactFlow } from '@xyflow/react';
import { useChartDB } from '@/hooks/use-chartdb';
import { useFullScreenLoader } from '@/hooks/use-full-screen-spinner';
import { useTheme } from '@/hooks/use-theme';
+import logoDark from '@/assets/logo-dark.png';
+import logoLight from '@/assets/logo-light.png';
export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({
children,
@@ -14,6 +16,24 @@ export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({
const { setNodes, getViewport } = useReactFlow();
const { effectiveTheme } = useTheme();
const { diagramName } = useChartDB();
+ const [logoBase64, setLogoBase64] = useState<string>('');
+
+ useEffect(() => {
+ // Convert logo to base64 on component mount
+ const img = new Image();
+ img.src = effectiveTheme === 'light' ? logoLight : logoDark;
+ img.onload = () => {
+ const canvas = document.createElement('canvas');
+ canvas.width = img.width;
+ canvas.height = img.height;
+ const ctx = canvas.getContext('2d');
+ if (ctx) {
+ ctx.drawImage(img, 0, 0);
+ const base64 = canvas.toDataURL('image/png');
+ setLogoBase64(base64);
+ }
+ };
+ }, [effectiveTheme]);
const downloadImage = useCallback(
(dataUrl: string, type: ImageType) => {
@@ -128,16 +148,22 @@ export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({
'http://www.w3.org/2000/svg',
'rect'
);
- const padding = 2000;
- backgroundRect.setAttribute('x', String(-viewport.x - padding));
- backgroundRect.setAttribute('y', String(-viewport.y - padding));
+ const bgPadding = 2000;
+ backgroundRect.setAttribute(
+ 'x',
+ String(-viewport.x - bgPadding)
+ );
+ backgroundRect.setAttribute(
+ 'y',
+ String(-viewport.y - bgPadding)
+ );
backgroundRect.setAttribute(
'width',
- String(reactFlowBounds.width + 2 * padding)
+ String(reactFlowBounds.width + 2 * bgPadding)
);
backgroundRect.setAttribute(
'height',
- String(reactFlowBounds.height + 2 * padding)
+ String(reactFlowBounds.height + 2 * bgPadding)
);
backgroundRect.setAttribute('fill', 'url(#background-pattern)');
tempSvg.appendChild(backgroundRect);
@@ -148,28 +174,110 @@ export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({
);
try {
- const dataUrl = await imageCreateFn(viewportElement, {
- ...(type === 'jpeg' || type === 'png'
- ? {
- backgroundColor:
- effectiveTheme === 'light'
- ? '#ffffff'
- : '#141414',
- }
- : {}),
- width: reactFlowBounds.width,
- height: reactFlowBounds.height,
- style: {
- width: `${reactFlowBounds.width}px`,
- height: `${reactFlowBounds.height}px`,
- transform: `translate(${viewport.x}px, ${viewport.y}px) scale(${viewport.zoom})`,
- },
- quality: 1,
- pixelRatio: scale,
- skipFonts: true,
- });
+ // Handle SVG export differently
+ if (type === 'svg') {
+ const dataUrl = await imageCreateFn(viewportElement, {
+ width: reactFlowBounds.width,
+ height: reactFlowBounds.height,
+ style: {
+ width: `${reactFlowBounds.width}px`,
+ height: `${reactFlowBounds.height}px`,
+ transform: `translate(${viewport.x}px, ${viewport.y}px) scale(${viewport.zoom})`,
+ },
+ quality: 1,
+ pixelRatio: scale,
+ skipFonts: true,
+ });
+ downloadImage(dataUrl, type);
+ return;
+ }
+
+ // For PNG and JPEG, continue with the watermark process
+ const initialDataUrl = await imageCreateFn(
+ viewportElement,
+ {
+ backgroundColor:
+ effectiveTheme === 'light'
+ ? '#ffffff'
+ : '#141414',
+ width: reactFlowBounds.width,
+ height: reactFlowBounds.height,
+ style: {
+ width: `${reactFlowBounds.width}px`,
+ height: `${reactFlowBounds.height}px`,
+ transform: `translate(${viewport.x}px, ${viewport.y}px) scale(${viewport.zoom})`,
+ },
+ quality: 1,
+ pixelRatio: scale,
+ skipFonts: true,
+ }
+ );
+
+ // Create a canvas to combine the diagram and watermark
+ const canvas = document.createElement('canvas');
+ const ctx = canvas.getContext('2d');
- downloadImage(dataUrl, type);
+ if (!ctx) {
+ downloadImage(initialDataUrl, type);
+ return;
+ }
+
+ // Set canvas size to match the export size
+ canvas.width = reactFlowBounds.width * scale;
+ canvas.height = reactFlowBounds.height * scale;
+
+ // Load the exported diagram
+ const diagramImage = new Image();
+ diagramImage.src = initialDataUrl;
+
+ await new Promise((resolve) => {
+ diagramImage.onload = async () => {
+ // Draw the diagram
+ ctx.drawImage(diagramImage, 0, 0);
+
+ // Calculate logo size
+ const logoHeight = Math.max(
+ 24,
+ Math.floor(canvas.width * 0.024)
+ );
+ const padding = Math.max(
+ 12,
+ Math.floor(logoHeight * 0.5)
+ );
+
+ // Load and draw the logo
+ const logoImage = new Image();
+ logoImage.src = logoBase64;
+
+ await new Promise((resolve) => {
+ logoImage.onload = () => {
+ // Calculate logo width while maintaining aspect ratio
+ const logoWidth =
+ (logoImage.width / logoImage.height) *
+ logoHeight;
+
+ // Draw logo in bottom-left corner
+ ctx.globalAlpha = 0.9;
+ ctx.drawImage(
+ logoImage,
+ padding,
+ canvas.height - logoHeight - padding,
+ logoWidth,
+ logoHeight
+ );
+ ctx.globalAlpha = 1;
+ resolve(null);
+ };
+ });
+
+ // Convert canvas to data URL
+ const finalDataUrl = canvas.toDataURL(
+ type === 'png' ? 'image/png' : 'image/jpeg'
+ );
+ downloadImage(finalDataUrl, type);
+ resolve(null);
+ };
+ });
} finally {
viewportElement.removeChild(tempSvg);
hideLoader();
@@ -184,6 +292,7 @@ export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({
setNodes,
showLoader,
effectiveTheme,
+ logoBase64,
]
);