Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
fix(export): SVG export captures the whole diagram instead of the visible viewport (#1174)
Jonathan Fishnere84a3f0a0cb4
diff --git a/src/context/export-image-context/__tests__/dedupe-svg-styles.test.ts b/src/context/export-image-context/__tests__/dedupe-svg-styles.test.tsnew file mode 100644index 00000000..726d237b--- /dev/null+++ b/src/context/export-image-context/__tests__/dedupe-svg-styles.test.ts@@ -0,0 +1,74 @@+import { describe, it, expect } from 'vitest';+import {+ dedupeSvgDataUrlStyles,+ dedupeSvgInlineStyles,+} from '../dedupe-svg-styles';++describe('dedupeSvgInlineStyles', () => {+ it('hoists duplicate inline styles into one rule each', () => {+ const svg =+ '<svg xmlns="http://www.w3.org/2000/svg" width="10"><foreignObject>' ++ '<div class="a" style="color: red; font-family: "Inter";">x</div>' ++ '<div style="color: red; font-family: "Inter";">y</div>' ++ '<span style="color: blue;">z</span>' ++ '</foreignObject></svg>';++ const out = dedupeSvgInlineStyles(svg);++ expect(out).not.toContain(' style="');+ expect(out).toContain('<div class="a" data-s="0">x</div>');+ expect(out).toContain('<div data-s="0">y</div>');+ expect(out).toContain('<span data-s="1">z</span>');+ expect(out).toContain(+ '<svg xmlns="http://www.w3.org/2000/svg" width="10"><style>' ++ '[data-s="0"]{color: red; font-family: "Inter";}' ++ '[data-s="1"]{color: blue;}</style>'+ );+ });++ it('returns input unchanged when there are no inline styles', () => {+ const svg = '<svg><rect/></svg>';+ expect(dedupeSvgInlineStyles(svg)).toBe(svg);+ });++ it('XML-escapes markup characters inside style values', () => {+ const out = dedupeSvgInlineStyles(+ '<svg><a style="content: "</style>";"/></svg>'+ );+ expect(out).toContain(+ '<style>[data-s="0"]{content: "</style>";}</style>'+ );+ });++ it('produces well-formed XML', () => {+ const out = dedupeSvgInlineStyles(+ '<svg xmlns="http://www.w3.org/2000/svg"><g style="fill: rgb(1, 2, 3);"/></svg>'+ );+ const doc = new DOMParser().parseFromString(out, 'image/svg+xml');+ expect(doc.getElementsByTagName('parsererror')).toHaveLength(0);+ expect(doc.querySelector('style')?.textContent).toBe(+ '[data-s="0"]{fill: rgb(1, 2, 3);}'+ );+ });+});++describe('dedupeSvgDataUrlStyles', () => {+ it('round-trips a utf-8 SVG data URL', () => {+ const svg = '<svg><g style="fill: red;"/><g style="fill: red;"/></svg>';+ const url = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;+ const out = dedupeSvgDataUrlStyles(url);+ expect(out.startsWith('data:image/svg+xml;charset=utf-8,')).toBe(true);+ expect(decodeURIComponent(out.split(',')[1])).toBe(+ dedupeSvgInlineStyles(svg)+ );+ });++ it('leaves non-SVG or base64 data URLs untouched', () => {+ expect(dedupeSvgDataUrlStyles('data:image/png;base64,AAAA')).toBe(+ 'data:image/png;base64,AAAA'+ );+ expect(dedupeSvgDataUrlStyles('data:image/svg+xml;base64,AAAA')).toBe(+ 'data:image/svg+xml;base64,AAAA'+ );+ });+});diff --git a/src/context/export-image-context/dedupe-svg-styles.ts b/src/context/export-image-context/dedupe-svg-styles.tsnew file mode 100644index 00000000..c8ff82bb--- /dev/null+++ b/src/context/export-image-context/dedupe-svg-styles.ts@@ -0,0 +1,62 @@+/**+ * html-to-image inlines the full computed style on every element, which makes+ * large SVG exports huge (150 tables ~ 72 MB, #1083). Almost all of those+ * style strings are duplicates, so each unique one is hoisted into a single+ * CSS rule keyed by a `data-s` attribute.+ *+ * Inside the exported SVG no page stylesheets apply (html-to-image only+ * relies on inline styles plus its own pseudo-element rules), so moving a+ * style from inline to an attribute selector does not change the cascade:+ * each element matches exactly one of our rules.+ */+export const dedupeSvgInlineStyles = (svg: string): string => {+ const keyByStyle = new Map<string, string>();++ const body = svg.replace(/ style="([^"]*)"/g, (_match, style: string) => {+ let key = keyByStyle.get(style);+ if (!key) {+ key = keyByStyle.size.toString(36);+ keyByStyle.set(style, key);+ }+ return ` data-s="${key}"`;+ });++ if (keyByStyle.size === 0) return svg;++ const rules: string[] = [];+ keyByStyle.forEach((key, style) => {+ rules.push(`[data-s="${key}"]{${unescapeXmlAttr(style)}}`);+ });+ // XML-escape the CSS (instead of CDATA, which some parsers mishandle)+ const css = rules+ .join('')+ .replace(/&/g, '&')+ .replace(/</g, '<')+ .replace(/>/g, '>');+ const styleTag = `<style>${css}</style>`;++ return body.replace(/<svg\b[^>]*>/, (open) => open + styleTag);+};++/** Same as {@link dedupeSvgInlineStyles} but for an SVG data URL. */+export const dedupeSvgDataUrlStyles = (dataUrl: string): string => {+ const comma = dataUrl.indexOf(',');+ const header = dataUrl.slice(0, comma);+ if (+ comma < 0 ||+ !header.startsWith('data:image/svg+xml') ||+ header.includes(';base64')+ ) {+ return dataUrl;+ }+ const svg = decodeURIComponent(dataUrl.slice(comma + 1));+ return `${header},${encodeURIComponent(dedupeSvgInlineStyles(svg))}`;+};++const unescapeXmlAttr = (value: string): string =>+ value+ .replace(/"/g, '"')+ .replace(/'/g, "'")+ .replace(/</g, '<')+ .replace(/>/g, '>')+ .replace(/&/g, '&');diff --git a/src/context/export-image-context/export-image-provider.tsx b/src/context/export-image-context/export-image-provider.tsxindex 4c76ac56..9a5b207e 100644--- a/src/context/export-image-context/export-image-provider.tsx+++ b/src/context/export-image-context/export-image-provider.tsx@@ -2,20 +2,32 @@ 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';-import { useReactFlow } from '@xyflow/react';+import {+ getNodesBounds,+ getViewportForBounds,+ 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';import type { EffectiveTheme } from '../theme-context/theme-context';+import { useTranslation } from 'react-i18next';+import { useToast } from '@/components/toast/use-toast';+import { dedupeSvgDataUrlStyles } from './dedupe-svg-styles';++// Above this many tables a full-diagram SVG export can take several seconds+const LARGE_SVG_TABLE_COUNT = 100;export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({children,}) => {const { hideLoader, showLoader } = useFullScreenLoader();- const { setNodes, getViewport } = useReactFlow();+ const { setNodes, getViewport, getNodes, setViewport } = useReactFlow();const { effectiveTheme } = useTheme();+ const { t } = useTranslation();+ const { toast } = useToast();const { diagramName } = useChartDB();const [logoBase64, setLogoBase64] = useState<string>('');@@ -87,6 +99,42 @@ export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({return;}+ // The canvas only renders visible nodes, so for SVG fit the+ // live viewport to all nodes first so every table is in the DOM+ // (#1083). The original viewport is restored afterwards.+ // Empty diagrams fall back to exporting the current viewport.+ const visibleNodes = getNodes().filter((node) => !node.hidden);+ const fitWholeDiagram = type === 'svg' && visibleNodes.length > 0;+ if (+ fitWholeDiagram &&+ visibleNodes.filter((node) => node.type === 'table').length >+ LARGE_SVG_TABLE_COUNT+ ) {+ toast({+ description: t('export_image_dialog.large_svg_warning'),+ });+ }+ if (fitWholeDiagram) {+ // Let the loader and warning paint before the capture work+ // blocks the main thread+ await new Promise((resolve) =>+ requestAnimationFrame(() => setTimeout(resolve, 50))+ );+ }+ const nodesBounds = getNodesBounds(visibleNodes);+ if (fitWholeDiagram) {+ await setViewport(+ getViewportForBounds(+ nodesBounds,+ reactFlowBounds.width,+ reactFlowBounds.height,+ 0.01,+ 2,+ 0.05+ )+ );+ }+const imageCreateFn = imageCreatorMap[type];setTimeout(async () => {@@ -250,19 +298,38 @@ export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({try {// Handle SVG export differentlyif (type === 'svg') {+ // Capture the whole diagram, not just the visible+ // viewport (#1083)+ const padding = 40;+ const svgWidth = fitWholeDiagram+ ? Math.ceil(nodesBounds.width + padding * 2)+ : reactFlowBounds.width;+ const svgHeight = fitWholeDiagram+ ? Math.ceil(nodesBounds.height + padding * 2)+ : reactFlowBounds.height;+ const fitted = fitWholeDiagram+ ? getViewportForBounds(+ nodesBounds,+ svgWidth,+ svgHeight,+ 1,+ 1,+ 0+ )+ : viewport;const dataUrl = await imageCreateFn(viewportElement, {- width: reactFlowBounds.width,- height: reactFlowBounds.height,+ width: svgWidth,+ height: svgHeight,style: {- width: `${reactFlowBounds.width}px`,- height: `${reactFlowBounds.height}px`,- transform: `translate(${viewport.x}px, ${viewport.y}px) scale(${viewport.zoom})`,+ width: `${svgWidth}px`,+ height: `${svgHeight}px`,+ transform: `translate(${fitted.x}px, ${fitted.y}px) scale(${fitted.zoom})`,},quality: 1,pixelRatio: scale,skipFonts: true,});- downloadImage(dataUrl, type);+ downloadImage(dedupeSvgDataUrlStyles(dataUrl), type);return;}@@ -361,6 +428,9 @@ export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({});viewportElement.removeChild(tempSvg);+ if (fitWholeDiagram) {+ setViewport(viewport);+ }hideLoader();}}, 0);@@ -369,12 +439,16 @@ export const ExportImageProvider: React.FC<React.PropsWithChildren> = ({getBackgroundColor,downloadImage,getViewport,+ getNodes,+ setViewport,hideLoader,imageCreatorMap,setNodes,showLoader,effectiveTheme,logoBase64,+ t,+ toast,]);diff --git a/src/i18n/locales/ar.ts b/src/i18n/locales/ar.tsindex 1d2b75ad..b6ed6eb7 100644--- a/src/i18n/locales/ar.ts+++ b/src/i18n/locales/ar.ts@@ -434,6 +434,7 @@ export const ar: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/bn.ts b/src/i18n/locales/bn.tsindex d07b1527..94bdf10d 100644--- a/src/i18n/locales/bn.ts+++ b/src/i18n/locales/bn.ts@@ -442,6 +442,7 @@ export const bn: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/de.ts b/src/i18n/locales/de.tsindex 800d49e5..f8579cd8 100644--- a/src/i18n/locales/de.ts+++ b/src/i18n/locales/de.ts@@ -443,6 +443,7 @@ export const de: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.tsindex 132940ab..9f88a44a 100644--- a/src/i18n/locales/en.ts+++ b/src/i18n/locales/en.ts@@ -430,6 +430,7 @@ export const en = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/es.ts b/src/i18n/locales/es.tsindex 7ca28f69..be388437 100644--- a/src/i18n/locales/es.ts+++ b/src/i18n/locales/es.ts@@ -442,6 +442,7 @@ export const es: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/fr.ts b/src/i18n/locales/fr.tsindex 4b9ababb..69813c31 100644--- a/src/i18n/locales/fr.ts+++ b/src/i18n/locales/fr.ts@@ -404,6 +404,7 @@ export const fr: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/gu.ts b/src/i18n/locales/gu.tsindex beea7b10..caa279fd 100644--- a/src/i18n/locales/gu.ts+++ b/src/i18n/locales/gu.ts@@ -439,6 +439,7 @@ export const gu: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/hi.ts b/src/i18n/locales/hi.tsindex cf46d764..47509d4b 100644--- a/src/i18n/locales/hi.ts+++ b/src/i18n/locales/hi.ts@@ -443,6 +443,7 @@ export const hi: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/hr.ts b/src/i18n/locales/hr.tsindex da951e58..c63bf97f 100644--- a/src/i18n/locales/hr.ts+++ b/src/i18n/locales/hr.ts@@ -434,6 +434,7 @@ export const hr: LanguageTranslation = {pattern_description: 'Dodaj suptilni mrežni uzorak u pozadinu.',transparent: 'Prozirna pozadina',transparent_description: 'Ukloni boju pozadine iz slike.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/id_ID.ts b/src/i18n/locales/id_ID.tsindex 401a3169..45da49da 100644--- a/src/i18n/locales/id_ID.ts+++ b/src/i18n/locales/id_ID.ts@@ -438,6 +438,7 @@ export const id_ID: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/ja.ts b/src/i18n/locales/ja.tsindex adc4f962..fa981e49 100644--- a/src/i18n/locales/ja.ts+++ b/src/i18n/locales/ja.ts@@ -443,6 +443,7 @@ export const ja: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/ko_KR.ts b/src/i18n/locales/ko_KR.tsindex e44b66f6..c3bb3547 100644--- a/src/i18n/locales/ko_KR.ts+++ b/src/i18n/locales/ko_KR.ts@@ -438,6 +438,7 @@ export const ko_KR: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/mr.ts b/src/i18n/locales/mr.tsindex e154bcfc..023e318f 100644--- a/src/i18n/locales/mr.ts+++ b/src/i18n/locales/mr.ts@@ -446,6 +446,7 @@ export const mr: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/ne.ts b/src/i18n/locales/ne.tsindex d7c3112a..169362f1 100644--- a/src/i18n/locales/ne.ts+++ b/src/i18n/locales/ne.ts@@ -442,6 +442,7 @@ export const ne: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/pt_BR.ts b/src/i18n/locales/pt_BR.tsindex d97e6bc5..31d49dcf 100644--- a/src/i18n/locales/pt_BR.ts+++ b/src/i18n/locales/pt_BR.ts@@ -442,6 +442,7 @@ export const pt_BR: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/ru.ts b/src/i18n/locales/ru.tsindex 6e7ce445..fa4cfad7 100644--- a/src/i18n/locales/ru.ts+++ b/src/i18n/locales/ru.ts@@ -439,6 +439,7 @@ export const ru: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/te.ts b/src/i18n/locales/te.tsindex 6dbb0cac..7c1f869a 100644--- a/src/i18n/locales/te.ts+++ b/src/i18n/locales/te.ts@@ -441,6 +441,7 @@ export const te: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/tr.ts b/src/i18n/locales/tr.tsindex 704baa39..e90dd83e 100644--- a/src/i18n/locales/tr.ts+++ b/src/i18n/locales/tr.ts@@ -432,6 +432,7 @@ export const tr: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {title: 'Şema Seç',diff --git a/src/i18n/locales/uk.ts b/src/i18n/locales/uk.tsindex 40dfcdbb..d8e226fa 100644--- a/src/i18n/locales/uk.ts+++ b/src/i18n/locales/uk.ts@@ -440,6 +440,7 @@ export const uk: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/vi.ts b/src/i18n/locales/vi.tsindex dc9d01a0..c4bedc90 100644--- a/src/i18n/locales/vi.ts+++ b/src/i18n/locales/vi.ts@@ -438,6 +438,7 @@ export const vi: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/zh_CN.ts b/src/i18n/locales/zh_CN.tsindex 6eb04867..8c4a6eea 100644--- a/src/i18n/locales/zh_CN.ts+++ b/src/i18n/locales/zh_CN.ts@@ -432,6 +432,7 @@ export const zh_CN: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {diff --git a/src/i18n/locales/zh_TW.ts b/src/i18n/locales/zh_TW.tsindex 098d9bdb..875f70a1 100644--- a/src/i18n/locales/zh_TW.ts+++ b/src/i18n/locales/zh_TW.ts@@ -431,6 +431,7 @@ export const zh_TW: LanguageTranslation = {pattern_description: 'Add subtle grid pattern to background.',transparent: 'Transparent background',transparent_description: 'Remove background color from image.',+ large_svg_warning: 'Large diagram: SVG export may take a while.',},new_table_schema_dialog: {