Arrange tables in import from source (#28)

This commit is contained in:
1ilit 2024-04-22 21:42:47 +03:00
parent 4a87935412
commit 11e83bb12b
2 changed files with 27 additions and 3 deletions

View File

@ -132,6 +132,7 @@ export default function Modal({
if (importSource.overwrite) {
setTables(d.tables);
setRelationships(d.relationships);
setTransform((prev) => ({ ...prev, pan: { x: 0, y: 0 } }));
setNotes([]);
setAreas([]);
setTypes([]);

View File

@ -1,4 +1,9 @@
import { Cardinality } from "../data/constants";
import {
Cardinality,
tableColorStripHeight,
tableFieldHeight,
tableHeaderHeight,
} from "../data/constants";
export function astToDiagram(ast) {
const tables = [];
@ -14,8 +19,6 @@ export function astToDiagram(ast) {
table.color = "#175e7a";
table.fields = [];
table.indices = [];
table.x = 0;
table.y = 0;
e.create_definitions.forEach((d) => {
if (d.resource === "column") {
const field = {};
@ -278,5 +281,25 @@ export function astToDiagram(ast) {
relationships.forEach((r, i) => (r.id = i));
let maxHeight = -1;
const tableWidth = 200;
const gapX = 54;
const gapY = 40;
tables.forEach((table, i) => {
if (i < tables.length / 2) {
table.x = i * tableWidth + (i + 1) * gapX;
table.y = gapY;
const height =
table.fields.length * tableFieldHeight +
tableHeaderHeight +
tableColorStripHeight;
maxHeight = Math.max(height, maxHeight);
} else {
const index = tables.length - i - 1;
table.x = index * tableWidth + (index + 1) * gapX;
table.y = maxHeight + 2 * gapY;
}
});
return { tables, relationships };
}