Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Chore/support downloading current view as png in schema visualizer (#33527)

Joshen Lim9c0b77500f75
diff --git a/apps/studio/components/interfaces/Database/Schemas/SchemaGraph.tsx b/apps/studio/components/interfaces/Database/Schemas/SchemaGraph.tsx
index d404c52701a..b50ce66d334 100644
--- a/apps/studio/components/interfaces/Database/Schemas/SchemaGraph.tsx
+++ b/apps/studio/components/interfaces/Database/Schemas/SchemaGraph.tsx
@@ -1,7 +1,8 @@
import type { PostgresSchema } from '@supabase/postgres-meta'
-import { Loader2 } from 'lucide-react'
+import { toPng } from 'html-to-image'
+import { Download, Loader2 } from 'lucide-react'
import { useTheme } from 'next-themes'
-import { useEffect, useMemo } from 'react'
+import { useEffect, useMemo, useState } from 'react'
import ReactFlow, { Background, BackgroundVariant, MiniMap, useReactFlow } from 'reactflow'
import 'reactflow/dist/style.css'
@@ -28,6 +29,8 @@ export const SchemaGraph = () => {
const { project } = useProjectContext()
const { selectedSchema, setSelectedSchema } = useQuerySchemaState()
+ const [isDownloading, setIsDownloading] = useState(false)
+
const miniMapNodeColor = '#111318'
const miniMapMaskColor = resolvedTheme?.includes('dark')
? 'rgb(17, 19, 24, .8)'
@@ -94,6 +97,36 @@ export const SchemaGraph = () => {
}
}
+ const downloadImage = () => {
+ const reactflowViewport = document.querySelector('.react-flow__viewport') as HTMLElement
+ if (!reactflowViewport) return
+
+ setIsDownloading(true)
+ const width = reactflowViewport.clientWidth
+ const height = reactflowViewport.clientHeight
+ const { x, y, zoom } = reactFlowInstance.getViewport()
+
+ toPng(reactflowViewport, {
+ backgroundColor: 'white',
+ width,
+ height,
+ style: {
+ width: width.toString(),
+ height: height.toString(),
+ transform: `translate(${x}px, ${y}px) scale(${zoom})`,
+ },
+ })
+ .then((data) => {
+ const a = document.createElement('a')
+ a.setAttribute('download', `supabase-schema-${ref}.png`)
+ a.setAttribute('href', data)
+ a.click()
+ })
+ .finally(() => {
+ setIsDownloading(false)
+ })
+ }
+
useEffect(() => {
if (isSuccessTables && isSuccessSchemas && tables.length > 0) {
const schema = schemas.find((s) => s.name === selectedSchema) as PostgresSchema
@@ -125,15 +158,28 @@ export const SchemaGraph = () => {
selectedSchemaName={selectedSchema}
onSelectSchema={setSelectedSchema}
/>
- <ButtonTooltip
- type="default"
- onClick={resetLayout}
- tooltip={{
- content: { side: 'bottom', text: 'Automatically arrange the layout of all nodes' },
- }}
- >
- Auto layout
- </ButtonTooltip>
+ <div className="flex items-center gap-x-2">
+ <ButtonTooltip
+ type="default"
+ loading={isDownloading}
+ className="px-1.5"
+ icon={<Download />}
+ onClick={downloadImage}
+ tooltip={{ content: { side: 'bottom', text: 'Download current view as PNG' } }}
+ />
+ <ButtonTooltip
+ type="default"
+ onClick={resetLayout}
+ tooltip={{
+ content: {
+ side: 'bottom',
+ text: 'Automatically arrange the layout of all nodes',
+ },
+ }}
+ >
+ Auto layout
+ </ButtonTooltip>
+ </div>
</>
)}
</div>