From df89b1047ddbf2ceb3c3b8a71f6484de092b9bac Mon Sep 17 00:00:00 2001 From: 1ilit Date: Tue, 19 Sep 2023 15:48:04 +0300 Subject: [PATCH] areas --- src/components/area.jsx | 47 ++++++++++++++++++++ src/components/canvas.jsx | 78 ++++++++++++++++++++++++++------- src/components/editor_panel.jsx | 20 +++++++++ src/pages/editor.jsx | 5 +++ 4 files changed, 134 insertions(+), 16 deletions(-) create mode 100644 src/components/area.jsx diff --git a/src/components/area.jsx b/src/components/area.jsx new file mode 100644 index 0000000..37fd154 --- /dev/null +++ b/src/components/area.jsx @@ -0,0 +1,47 @@ +import { React, useState } from "react"; + +export default function Area(props) { + const [size, setSize] = useState({ + width: props.areaData.width, + height: props.areaData.height, + }); + + return ( + + +
+ {props.areaData.name} +
+
+ + + + {}} + /> +
+ ); +} diff --git a/src/components/canvas.jsx b/src/components/canvas.jsx index 711e4c6..a0aba34 100644 --- a/src/components/canvas.jsx +++ b/src/components/canvas.jsx @@ -2,9 +2,15 @@ import React, { useRef, useState } from "react"; import { useDrop } from "react-dnd"; import Table from "./table"; import { defaultTableTheme, Cardinality, Constraint } from "../data/data"; +import Area from "./area"; export default function Canvas(props) { - const [dragging, setDragging] = useState(-1); + const ObjectType = { + NONE: 0, + TABLE: 1, + AREA: 2, + }; + const [dragging, setDragging] = useState([ObjectType.NONE, -1]); const [linking, setLinking] = useState(false); const [line, setLine] = useState({ startTableId: -1, @@ -32,14 +38,23 @@ export default function Canvas(props) { const canvas = useRef(null); - const handleMouseDownRect = (e, id) => { + const handleMouseDownRect = (e, id, type) => { const { clientX, clientY } = e; - const table = props.tables.find((t) => t.id === id); - setOffset({ - x: clientX - table.x, - y: clientY - table.y, - }); - setDragging(id); + if (type === ObjectType.TABLE) { + const table = props.tables.find((t) => t.id === id); + setOffset({ + x: clientX - table.x, + y: clientY - table.y, + }); + setDragging([ObjectType.TABLE, id]); + } else if (type === ObjectType.AREA) { + const area = props.areas.find((t) => t.id === id); + setOffset({ + x: clientX - area.x, + y: clientY - area.y, + }); + setDragging([ObjectType.AREA, id]); + } }; const handleMouseMove = (e) => { @@ -77,9 +92,17 @@ export default function Canvas(props) { endY: r.endY + dy, })) ); - } else if (dragging >= 0) { + + props.setAreas( + props.areas.map((t) => ({ + ...t, + x: t.x + dx, + y: t.y + dy, + })) + ); + } else if (dragging[0] === ObjectType.TABLE && dragging[1] >= 0) { const updatedTables = props.tables.map((t) => { - if (t.id === dragging) { + if (t.id === dragging[1]) { const updatedTable = { ...t, x: e.clientX - offset.x, @@ -91,14 +114,14 @@ export default function Canvas(props) { }); props.setTables(updatedTables); const updatedRelationShips = props.relationships.map((r) => { - if (r.startTableId === dragging) { + if (r.startTableId === dragging[1]) { return { ...r, startX: props.tables[r.startTableId].x + 15, startY: props.tables[r.startTableId].y + r.startFieldId * 36 + 40 + 19, }; - } else if (r.endTableId === dragging) { + } else if (r.endTableId === dragging[1]) { return { ...r, endX: props.tables[r.endTableId].x + 15, @@ -108,11 +131,25 @@ export default function Canvas(props) { return r; }); props.setRelationships(updatedRelationShips); + } else if (dragging[0] === ObjectType.AREA && dragging[1] >= 0) { + console.log("hi"); + const updatedAreas = props.areas.map((t) => { + if (t.id === dragging[1]) { + const updatedArea = { + ...t, + x: e.clientX - offset.x, + y: e.clientY - offset.y, + }; + return updatedArea; + } + return t; + }); + props.setAreas(updatedAreas); } }; const handleMouseDown = (e) => { - if (dragging < 0) { + if (dragging[0] === ObjectType.TABLE && dragging[1] < 0) { if (onRect.tableId < 0) { setPanning(true); setPanOffset({ x: e.clientX, y: e.clientY }); @@ -122,7 +159,7 @@ export default function Canvas(props) { }; const handleMouseUp = () => { - setDragging(-1); + setDragging([ObjectType.NONE, -1]); setPanning(false); setCursor("default"); if (linking) handleLinking(); @@ -137,7 +174,7 @@ export default function Canvas(props) { const handleGripField = (id) => { setPanning(false); - setDragging(-1); + setDragging([ObjectType.NONE, -1]); setLinking(true); }; @@ -248,6 +285,13 @@ export default function Canvas(props) { + {props.areas.map((a) => ( + handleMouseDownRect(e, a.id, ObjectType.AREA)} + > + ))} {props.tables.map((table, i) => ( handleMouseDownRect(e, table.id)} + onMouseDown={(e) => + handleMouseDownRect(e, table.id, ObjectType.TABLE) + } onDelete={deleteTable} /> ))} diff --git a/src/components/editor_panel.jsx b/src/components/editor_panel.jsx index aae419d..dfe987f 100644 --- a/src/components/editor_panel.jsx +++ b/src/components/editor_panel.jsx @@ -85,6 +85,26 @@ export default function EditorPanel(props) { {contentList[parseInt(tab) - 1]} + +