Skip to content

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

some progress with edges

Guy Ben-Aharonbf3b9b2db63d
diff --git a/src/pages/editor-page/canvas/canvas.tsx b/src/pages/editor-page/canvas/canvas.tsx
index bca24b3c..b0b2f904 100644
--- a/src/pages/editor-page/canvas/canvas.tsx
+++ b/src/pages/editor-page/canvas/canvas.tsx
@@ -8,33 +8,49 @@ import {
BackgroundVariant,
MiniMap,
Controls,
+ Edge,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
import { TableNode } from './table-node';
+import { TableEdge } from './table-edge';
export interface CanvasProps {}
const initialNodes = [
{
- id: '1111',
+ id: '3',
+ type: 'table',
+ position: { x: 200, y: 200 },
+ data: { label: '3' },
+ },
+ {
+ id: '1',
type: 'table',
position: { x: 400, y: 400 },
data: { label: '1' },
},
- { id: '1', position: { x: 0, y: 0 }, data: { label: '1' } },
- { id: '2', position: { x: 0, y: 100 }, data: { label: '2' } },
+ {
+ id: '2',
+ type: 'table',
+ position: { x: 0, y: 0 },
+ data: { label: '2' },
+ },
+ // { id: '1', position: { x: 0, y: 0 }, data: { label: '1' } },
+ // { id: '2', position: { x: 0, y: 100 }, data: { label: '2' } },
];
-const initialEdges = [{ id: 'e1-2', source: '1', target: '2' }];
+// const initialEdges = [{ id: 'e1-2', source: '1', target: '2' }];
+const initialEdges: Edge[] = [];
export const Canvas: React.FC<CanvasProps> = () => {
const nodeTypes = useMemo(() => ({ table: TableNode }), []);
+ const edgeTypes = useMemo(() => ({ 'table-edge': TableEdge }), []);
const [nodes, , onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback(
(params: Parameters<typeof addEdge>[0]) =>
- setEdges((eds) => addEdge(params, eds)),
+ setEdges((eds) => addEdge({ ...params, type: 'table-edge' }, eds)),
[setEdges]
);
@@ -51,6 +67,7 @@ export const Canvas: React.FC<CanvasProps> = () => {
}}
fitView={false} // todo think about it
nodeTypes={nodeTypes}
+ edgeTypes={edgeTypes}
>
<Controls />
<MiniMap
diff --git a/src/pages/editor-page/canvas/table-edge.tsx b/src/pages/editor-page/canvas/table-edge.tsx
new file mode 100644
index 00000000..2eba1189
--- /dev/null
+++ b/src/pages/editor-page/canvas/table-edge.tsx
@@ -0,0 +1,97 @@
+import React, { useMemo } from 'react';
+import {
+ BaseEdge,
+ EdgeProps,
+ getSmoothStepPath,
+ Position,
+ useReactFlow,
+} from '@xyflow/react';
+import { RIGHT_HANDLE_ID } from './table-node';
+
+export const TableEdge: React.FC<EdgeProps> = ({
+ id,
+ sourceX,
+ sourceY,
+ targetX,
+ targetY,
+ source,
+ target,
+}) => {
+ const { getInternalNode, getEdge } = useReactFlow();
+
+ const sourceNode = getInternalNode(source);
+ const targetNode = getInternalNode(target);
+ const edge = getEdge(id);
+
+ const sourceHandle: 'left' | 'right' =
+ edge?.sourceHandle === RIGHT_HANDLE_ID ? 'right' : 'left';
+
+ const sourceWidth = sourceNode?.measured.width ?? 0;
+ const sourceLeftX =
+ sourceHandle === 'left' ? sourceX : sourceX - sourceWidth + 10;
+ const sourceRightX =
+ sourceHandle === 'left' ? sourceX + sourceWidth + 10 : sourceX;
+
+ const targetWidth = targetNode?.measured.width ?? 0;
+ const targetLeftX = targetX;
+ const targetRightX = targetX + targetWidth + 10;
+
+ const { sourceSide, targetSide } = useMemo(() => {
+ const distances = {
+ leftToLeft: Math.abs(sourceLeftX - targetLeftX),
+ leftToRight: Math.abs(sourceLeftX - targetRightX),
+ rightToLeft: Math.abs(sourceRightX - targetLeftX),
+ rightToRight: Math.abs(sourceRightX - targetRightX),
+ };
+
+ const minDistance = Math.min(
+ distances.leftToLeft,
+ distances.leftToRight,
+ distances.rightToLeft,
+ distances.rightToRight
+ );
+
+ const minDistanceKey = Object.keys(distances).find(
+ (key) => distances[key as keyof typeof distances] === minDistance
+ ) as keyof typeof distances;
+
+ switch (minDistanceKey) {
+ case 'leftToRight':
+ return { sourceSide: 'left', targetSide: 'right' };
+ case 'rightToLeft':
+ return { sourceSide: 'right', targetSide: 'left' };
+ case 'rightToRight':
+ return { sourceSide: 'right', targetSide: 'right' };
+ default:
+ return { sourceSide: 'left', targetSide: 'left' };
+ }
+ }, [sourceLeftX, sourceRightX, targetLeftX, targetRightX]);
+
+ const [edgePath] = useMemo(
+ () =>
+ getSmoothStepPath({
+ sourceX: sourceSide === 'left' ? sourceLeftX : sourceRightX,
+ sourceY,
+ targetX: targetSide === 'left' ? targetLeftX : targetRightX,
+ targetY,
+ borderRadius: 14,
+ sourcePosition:
+ sourceSide === 'left' ? Position.Left : Position.Right,
+ targetPosition:
+ targetSide === 'left' ? Position.Left : Position.Right,
+ offset: 20,
+ }),
+ [
+ sourceSide,
+ targetSide,
+ sourceLeftX,
+ sourceRightX,
+ targetLeftX,
+ targetRightX,
+ sourceY,
+ targetY,
+ ]
+ );
+
+ return <BaseEdge id={id} path={edgePath} className="!stroke-2" />;
+};
diff --git a/src/pages/editor-page/canvas/table-node.tsx b/src/pages/editor-page/canvas/table-node.tsx
index 9fd44237..ab64d81d 100644
--- a/src/pages/editor-page/canvas/table-node.tsx
+++ b/src/pages/editor-page/canvas/table-node.tsx
@@ -1,28 +1,96 @@
import React from 'react';
import { randomHSLA } from '@/lib/utils';
-import { Handle, Position, NodeProps, Node } from '@xyflow/react';
+import {
+ Handle,
+ Position,
+ NodeProps,
+ Node,
+ useConnection,
+} from '@xyflow/react';
import { Button } from '@/components/button/button';
import { Ellipsis, Trash2 } from 'lucide-react';
+export const LEFT_HANDLE_ID = 'left-handle';
+export const RIGHT_HANDLE_ID = 'right-handle';
+
export type TableNodeProps = Node<Record<string, never>, 'table'>;
export const TableNode: React.FC<NodeProps<TableNodeProps>> = ({
selected,
dragging,
+ id,
}) => {
+ // const { getEdges } = useReactFlow();
+ const connection = useConnection();
const tableColor = randomHSLA();
const focused = selected && !dragging;
+ const isTarget = connection.inProgress && connection.fromNode.id !== id;
+ // const edges = getEdges();
+ // const numberOfEdges = edges.filter((edge) => edge.target === id).length;
+ // console.log(numberOfEdges)
const renderColumn = () => (
<div className="flex relative items-center h-8 text-sm px-3 border-t justify-between hover:bg-primary-foreground group last:rounded-b-lg">
- {focused ? (
+ {/* If handles are conditionally rendered and not present initially, you need to update the node internals https://reactflow.dev/docs/api/hooks/use-update-node-internals/ */}
+ {/* In this case we don't need to use useUpdateNodeInternals, since !isConnecting is true at the beginning and all handles are rendered initially. */}
+ {!connection.inProgress && (
+ <>
+ <Handle
+ // className="customHandle"
+ id={RIGHT_HANDLE_ID}
+ className={`!h-3 !w-3 !bg-slate-400 ${!focused ? '!invisible' : ''}`}
+ position={Position.Right}
+ type="source"
+ />
+ <Handle
+ // className="customHandle"
+ id={LEFT_HANDLE_ID}
+ className={`!h-3 !w-3 !bg-slate-400 ${!focused ? '!invisible' : ''}`}
+ position={Position.Left}
+ type="source"
+ />
+ </>
+ )}
+ {/* We want to disable the target handle, if the connection was started from this node */}
+ {(!connection.inProgress || isTarget) && (
<Handle
- className="!h-3 !w-3 !bg-slate-400"
- type="target"
+ className={
+ isTarget
+ ? '!w-full !h-full !absolute !top-0 !left-0 !rounded-none !border-none !transform-none !opacity-0'
+ : `!h-3 !w-3 !bg-slate-400 ${!focused ? '!invisible' : ''}`
+ }
+ // style={
+ // isTarget
+ // ? {
+ // width: '100%',
+ // height: '100%',
+ // // background: 'blue',
+ // position: 'absolute',
+ // top: 0,
+ // left: 0,
+ // borderRadius: 0,
+ // transform: 'none',
+ // border: 'none',
+ // // opacity: 0,
+ // }
+ // : {}
+ // }
+ // className="customHandle"
position={Position.Left}
- isConnectable={true}
+ type="target"
+ isConnectableStart={false}
/>
- ) : null}
+ )}
+
+ {/* <Handle
+ id="1"
+ className={`!h-3 !w-3 !bg-slate-400 ${!focused ? '!invisible' : ''}`}
+ type="target"
+ isConnectableEnd={true}
+ isConnectableStart={true}
+ position={Position.Left}
+ isConnectable={true}
+ /> */}
<div className="flex">id</div>
<div className="flex">
<div className="text-muted-foreground flex group-hover:hidden">
@@ -37,15 +105,15 @@ export const TableNode: React.FC<NodeProps<TableNodeProps>> = ({
</Button>
</div>
</div>
-
- {focused ? (
- <Handle
- className="!h-3 !w-3 !bg-slate-400"
- type="target"
- position={Position.Right}
- isConnectable={true}
- />
- ) : null}
+ {/* <Handle
+ id="2"
+ className={`!h-3 !w-3 !bg-slate-400 ${!focused ? '!invisible' : ''}`}
+ type="source"
+ position={Position.Right}
+ isConnectable={true}
+ isConnectableEnd={true}
+ isConnectableStart={true}
+ /> */}
</div>
);
@@ -69,8 +137,6 @@ export const TableNode: React.FC<NodeProps<TableNodeProps>> = ({
</div>
</div>
{renderColumn()}
- {renderColumn()}
- {renderColumn()}
</div>
);
};
diff --git a/src/pages/editor-page/top-navbar/top-navbar.tsx b/src/pages/editor-page/top-navbar/top-navbar.tsx
index 5cccbf4c..532777c4 100644
--- a/src/pages/editor-page/top-navbar/top-navbar.tsx
+++ b/src/pages/editor-page/top-navbar/top-navbar.tsx
@@ -111,7 +111,9 @@ export const TopNavbar: React.FC<TopNavbarProps> = () => {
</div>
<div className="flex flex-1 justify-center">
<Label>Diagrams/</Label>
- <Label contentEditable>aaa</Label>
+ <Label contentEditable suppressContentEditableWarning>
+ aaa
+ </Label>
</div>
<div className="flex flex-1 justify-end"></div>
</nav>