Skip to content

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

when opening diagram - fit canvas to nodes

Guy Ben-Aharon9f59f003874f
diff --git a/package-lock.json b/package-lock.json
index 77ca1faf..c75367b1 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -39,6 +39,7 @@
"cmdk": "^1.0.0",
"dexie": "^4.0.8",
"eslint-config-airbnb-typescript": "^18.0.0",
+ "fast-deep-equal": "^3.1.3",
"html-to-image": "^1.11.11",
"i18next": "^23.14.0",
"lucide-react": "^0.424.0",
diff --git a/package.json b/package.json
index fa444227..09c98c49 100644
--- a/package.json
+++ b/package.json
@@ -43,6 +43,7 @@
"cmdk": "^1.0.0",
"dexie": "^4.0.8",
"eslint-config-airbnb-typescript": "^18.0.0",
+ "fast-deep-equal": "^3.1.3",
"html-to-image": "^1.11.11",
"i18next": "^23.14.0",
"lucide-react": "^0.424.0",
diff --git a/src/pages/editor-page/canvas/canvas.tsx b/src/pages/editor-page/canvas/canvas.tsx
index 8a7db208..842985a2 100644
--- a/src/pages/editor-page/canvas/canvas.tsx
+++ b/src/pages/editor-page/canvas/canvas.tsx
@@ -1,4 +1,4 @@
-import React, { useCallback, useEffect, useMemo } from 'react';
+import React, { useCallback, useEffect, useMemo, useState } from 'react';
import {
ReactFlow,
useEdgesState,
@@ -16,7 +16,8 @@ import {
OnNodesChange,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
-import { TableNode, TableNodeType } from './table-node';
+import equal from 'fast-deep-equal';
+import { MIN_TABLE_SIZE, TableNode, TableNodeType } from './table-node';
import { TableEdge, TableEdgeType } from './table-edge';
import { useChartDB } from '@/hooks/use-chartdb';
import { LEFT_HANDLE_ID_PREFIX, TARGET_ID_PREFIX } from './table-node-field';
@@ -29,16 +30,28 @@ import { useBreakpoint } from '@/hooks/use-breakpoint';
import { Badge } from '@/components/badge/badge';
import { useTheme } from '@/hooks/use-theme';
import { useTranslation } from 'react-i18next';
+import { DBTable } from '@/lib/domain/db-table';
type AddEdgeParams = Parameters<typeof addEdge<TableEdgeType>>[0];
-const initialNodes: TableNodeType[] = [];
const initialEdges: TableEdgeType[] = [];
-export interface CanvasProps {}
+const tableToTableNode = (table: DBTable): TableNodeType => ({
+ id: table.id,
+ type: 'table',
+ position: { x: table.x, y: table.y },
+ data: {
+ table,
+ },
+ width: table.width ?? MIN_TABLE_SIZE,
+});
-export const Canvas: React.FC<CanvasProps> = () => {
- const { getEdge, getInternalNode } = useReactFlow();
+export interface CanvasProps {
+ initialTables: DBTable[];
+}
+
+export const Canvas: React.FC<CanvasProps> = ({ initialTables }) => {
+ const { getEdge, getInternalNode, fitView } = useReactFlow();
const { toast } = useToast();
const { t } = useTranslation();
const {
@@ -54,12 +67,31 @@ export const Canvas: React.FC<CanvasProps> = () => {
const { isMd: isDesktop } = useBreakpoint('md');
const nodeTypes = useMemo(() => ({ table: TableNode }), []);
const edgeTypes = useMemo(() => ({ 'table-edge': TableEdge }), []);
+ const [isInitialLoadingNodes, setIsInitialLoadingNodes] = useState(true);
- const [nodes, setNodes, onNodesChange] =
- useNodesState<TableNodeType>(initialNodes);
+ const [nodes, setNodes, onNodesChange] = useNodesState<TableNodeType>(
+ initialTables.map(tableToTableNode)
+ );
const [edges, setEdges, onEdgesChange] =
useEdgesState<TableEdgeType>(initialEdges);
+ useEffect(() => {
+ setIsInitialLoadingNodes(true);
+ }, [initialTables]);
+
+ useEffect(() => {
+ const initialNodes = initialTables.map(tableToTableNode);
+ if (equal(initialNodes, nodes)) {
+ setIsInitialLoadingNodes(false);
+ }
+ }, [initialTables, nodes]);
+
+ useEffect(() => {
+ if (!isInitialLoadingNodes) {
+ setTimeout(() => fitView({ maxZoom: 1, duration: 0 }), 0);
+ }
+ }, [isInitialLoadingNodes, fitView]);
+
useEffect(() => {
const targetIndexes: Record<string, number> = relationships.reduce(
(acc, relationship) => {
@@ -84,17 +116,7 @@ export const Canvas: React.FC<CanvasProps> = () => {
}, [relationships, setEdges]);
useEffect(() => {
- setNodes(
- tables.map((table) => ({
- id: table.id,
- type: 'table',
- position: { x: table.x, y: table.y },
- data: {
- table,
- },
- width: table.width ?? 224,
- }))
- );
+ setNodes(tables.map(tableToTableNode));
}, [tables, setNodes]);
const onConnectHandler = useCallback(
diff --git a/src/pages/editor-page/canvas/table-node.tsx b/src/pages/editor-page/canvas/table-node.tsx
index b47ff0f0..a808dc8e 100644
--- a/src/pages/editor-page/canvas/table-node.tsx
+++ b/src/pages/editor-page/canvas/table-node.tsx
@@ -21,9 +21,9 @@ export type TableNodeType = Node<
'table'
>;
-const MAX_TABLE_SIZE = 450;
-const MID_TABLE_SIZE = 337;
-const MIN_TABLE_SIZE = 224;
+export const MAX_TABLE_SIZE = 450;
+export const MID_TABLE_SIZE = 337;
+export const MIN_TABLE_SIZE = 224;
export const TableNode: React.FC<NodeProps<TableNodeType>> = ({
selected,
@@ -49,7 +49,7 @@ export const TableNode: React.FC<NodeProps<TableNodeType>> = ({
const expandTable = useCallback(() => {
updateTable(table.id, {
width:
- (table.width ?? 224) < MID_TABLE_SIZE
+ (table.width ?? MIN_TABLE_SIZE) < MID_TABLE_SIZE
? MID_TABLE_SIZE
: MAX_TABLE_SIZE,
});
diff --git a/src/pages/editor-page/editor-page.tsx b/src/pages/editor-page/editor-page.tsx
index 685d07af..eebb29b8 100644
--- a/src/pages/editor-page/editor-page.tsx
+++ b/src/pages/editor-page/editor-page.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect } from 'react';
+import React, { useEffect, useState } from 'react';
import { TopNavbar } from './top-navbar/top-navbar';
import {
ResizableHandle,
@@ -24,6 +24,7 @@ import {
DrawerTitle,
} from '@/components/drawer/drawer';
import { Separator } from '@/components/separator/separator';
+import { Diagram } from '@/lib/domain/diagram';
export const EditorPage: React.FC = () => {
const { loadDiagram, currentDiagram } = useChartDB();
@@ -37,6 +38,7 @@ export const EditorPage: React.FC = () => {
const { isLg } = useBreakpoint('lg');
const { isXl } = useBreakpoint('xl');
const { isMd: isDesktop } = useBreakpoint('md');
+ const [initialDiagram, setInitialDiagram] = useState<Diagram | undefined>();
useEffect(() => {
if (!config) {
@@ -49,6 +51,7 @@ export const EditorPage: React.FC = () => {
const loadDefaultDiagram = async () => {
if (diagramId) {
+ setInitialDiagram(undefined);
showLoader();
resetRedoStack();
resetUndoStack();
@@ -56,6 +59,7 @@ export const EditorPage: React.FC = () => {
if (!diagram) {
navigate('/');
}
+ setInitialDiagram(diagram);
hideLoader();
} else if (!diagramId && config.defaultDiagramId) {
navigate(`/diagrams/${config.defaultDiagramId}`);
@@ -98,7 +102,9 @@ export const EditorPage: React.FC = () => {
<ResizablePanel
defaultSize={isXl ? 75 : isLg ? 65 : 50}
>
- <Canvas />
+ <Canvas
+ initialTables={initialDiagram?.tables ?? []}
+ />
</ResizablePanel>
</ResizablePanelGroup>
) : (
@@ -118,7 +124,7 @@ export const EditorPage: React.FC = () => {
<SidePanel data-vaul-no-drag />
</DrawerContent>
</Drawer>
- <Canvas />
+ <Canvas initialTables={initialDiagram?.tables ?? []} />
</>
)}
</section>