Fix nodes re-render whene dragging

This commit is contained in:
KarimTamani
2025-05-16 01:23:28 +01:00
parent 84d086c585
commit 7f7779d579
20 changed files with 508 additions and 248 deletions
+57
View File
@@ -0,0 +1,57 @@
import { RelationshipType } from "@/lib/schemas/relationship-schema";
import { TableType } from "@/lib/schemas/table-schema";
import { Node } from "@xyflow/react";
import ELK from "elkjs/lib/elk.bundled.js";
const elk = new ELK();
const adjustTablesPositions = async (
nodes: Node[],
relationships: RelationshipType[]
): Promise<TableType[]> => {
// Extract tables (same as before)
const tables: TableType[] = nodes.map((node: Node) => node.data.table) as TableType[];
// Build the ELK graph structure
const graph = {
id: "root",
layoutOptions: {
'elk.algorithm': 'layered',
'elk.layered.spacing.nodeNodeBetweenLayers': '100',
'elk.spacing.nodeNode': '80',
},
children: nodes.map((node) => ({
id: node.id,
width: node.measured?.width ?? 224,
height: node.measured?.height ?? 150,
})),
edges: relationships.map((rel) => ({
id: `${rel.sourceTableId}->${rel.targetTableId}`,
sources: [rel.sourceTableId],
targets: [rel.targetTableId],
})),
};
// Run ELK layout (async)
const layoutedGraph = await elk.layout(graph);
// Map positions back to your tables
tables.forEach((table) => {
const node = layoutedGraph?.children?.find((n) => n.id === table.id);
if (node) {
// ELK positions are top-left, adjust to center like before
table.posX = node.x || 0 + (node.width / 2) + 112; // 112 = half your fixed width
table.posY = node.y || 0 + (node.height / 2) + 75; // 75 = half your fixed height
}
});
return tables;
};
export {
adjustTablesPositions,
}
+13
View File
@@ -0,0 +1,13 @@
const areArraysEqual = (a: string[], b: string[]): boolean => {
if (a.length !== b.length) return false;
const sortedA = [...a].sort();
const sortedB = [...b].sort();
return sortedA.every((val, index) => val === sortedB[index]);
};
export {
areArraysEqual
}