From 9faf99d9d751b95ff445d3c4a0a762697bb14793 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Fri, 12 Apr 2024 04:24:19 +0300 Subject: [PATCH] Remove magic numbers in table height calculations --- src/components/EditorCanvas/Table.jsx | 15 +++++++++++++-- src/components/SimpleCanvas.jsx | 20 +++++++++++++++----- src/data/constants.js | 1 + src/data/heroDiagram.js | 26 ++++++++++++++++++++++---- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/components/EditorCanvas/Table.jsx b/src/components/EditorCanvas/Table.jsx index 6f730567..bcb0c60f 100644 --- a/src/components/EditorCanvas/Table.jsx +++ b/src/components/EditorCanvas/Table.jsx @@ -4,6 +4,7 @@ import { ObjectType, tableFieldHeight, tableHeaderHeight, + tableColorStripHeight, } from "../../data/constants"; import { IconEdit, @@ -289,9 +290,19 @@ export default function Table(props) { startFieldId: index, startTableId: tableData.id, startX: tableData.x + 15, - startY: tableData.y + index * 36 + 50 + 19, + startY: + tableData.y + + index * tableFieldHeight + + tableHeaderHeight + + tableColorStripHeight + + 12, endX: tableData.x + 15, - endY: tableData.y + index * 36 + 50 + 19, + endY: + tableData.y + + index * tableFieldHeight + + tableHeaderHeight + + tableColorStripHeight + + 12, })); }} /> diff --git a/src/components/SimpleCanvas.jsx b/src/components/SimpleCanvas.jsx index 2d374acd..5adfa44e 100644 --- a/src/components/SimpleCanvas.jsx +++ b/src/components/SimpleCanvas.jsx @@ -1,17 +1,27 @@ import { useEffect, useState, useRef } from "react"; -import { Cardinality } from "../data/constants"; +import { + Cardinality, + tableColorStripHeight, + tableFieldHeight, + tableHeaderHeight, + tableWidth, +} from "../data/constants"; import { calcPath } from "../utils/calcPath"; function Table({ table, grab }) { const [isHovered, setIsHovered] = useState(false); const [hoveredField, setHoveredField] = useState(-1); - const height = table.fields.length * 36 + 50 + 7; + const height = + table.fields.length * tableFieldHeight + + tableHeaderHeight + + tableColorStripHeight; + return ( prev.map((table, i) => - i === dragging ? { ...table, x: dx, y: dy } : table - ) + i === dragging ? { ...table, x: dx, y: dy } : table, + ), ); } }; diff --git a/src/data/constants.js b/src/data/constants.js index 350c9086..8d8d1e1a 100644 --- a/src/data/constants.js +++ b/src/data/constants.js @@ -52,6 +52,7 @@ export const defaultNoteTheme = "#fcf7ac"; export const tableHeaderHeight = 50; export const tableWidth = 200; export const tableFieldHeight = 36; +export const tableColorStripHeight = 7; export const Cardinality = { ONE_TO_ONE: "One to one", diff --git a/src/data/heroDiagram.js b/src/data/heroDiagram.js index beba0944..1f65a248 100644 --- a/src/data/heroDiagram.js +++ b/src/data/heroDiagram.js @@ -1,10 +1,19 @@ +import { + tableColorStripHeight, + tableFieldHeight, + tableHeaderHeight, +} from "./constants"; + const xOffset = window.innerWidth * 0.65; export const diagram = { tables: [ { name: "galactic_users", x: xOffset + 75, - y: window.innerHeight * 0.23 - (50 + 4 * 36 + 7) * 0.5, + y: + window.innerHeight * 0.23 - + (tableHeaderHeight + 4 * tableFieldHeight + tableColorStripHeight) * + 0.5, fields: [ { name: "id", @@ -29,7 +38,10 @@ export const diagram = { id: 1, name: "celestial_data", x: xOffset + 27, - y: window.innerHeight * 0.72 - (50 + 5 * 36 + 7) * 0.5, + y: + window.innerHeight * 0.72 - + (tableHeaderHeight + 5 * tableFieldHeight + tableColorStripHeight) * + 0.5, fields: [ { name: "id", @@ -58,7 +70,10 @@ export const diagram = { id: 2, name: "astro_mine", x: xOffset + 336, - y: window.innerHeight * 0.72 - (50 + 3 * 36 + 7) * 0.5, + y: + window.innerHeight * 0.72 - + (tableHeaderHeight + 3 * tableFieldHeight + tableColorStripHeight) * + 0.5, fields: [ { name: "id", @@ -79,7 +94,10 @@ export const diagram = { id: 3, name: "asteroid", x: xOffset + 310, - y: window.innerHeight * 0.23 - (50 + 3 * 36 + 7) * 0.5, + y: + window.innerHeight * 0.23 - + (tableHeaderHeight + 3 * tableFieldHeight + tableColorStripHeight) * + 0.5, fields: [ { name: "id",