areas
This commit is contained in:
parent
8929639d6d
commit
df89b1047d
47
src/components/area.jsx
Normal file
47
src/components/area.jsx
Normal file
@ -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 (
|
||||||
|
<g>
|
||||||
|
<foreignObject
|
||||||
|
key={props.areaData.id}
|
||||||
|
x={props.areaData.x}
|
||||||
|
y={props.areaData.y}
|
||||||
|
width={size.width}
|
||||||
|
height={size.height}
|
||||||
|
style={{ cursor: "move" }}
|
||||||
|
onMouseDown={props.onMouseDown}
|
||||||
|
>
|
||||||
|
<div className="border-2 border-dashed border-blue-600 opacity-70 bg-slate-400 w-fill h-full">
|
||||||
|
{props.areaData.name}
|
||||||
|
</div>
|
||||||
|
</foreignObject>
|
||||||
|
<circle cx={props.areaData.x} cy={props.areaData.y} r={5} fill="blue" />
|
||||||
|
<circle
|
||||||
|
cx={props.areaData.x + props.areaData.width}
|
||||||
|
cy={props.areaData.y}
|
||||||
|
r={5}
|
||||||
|
fill="blue"
|
||||||
|
/>
|
||||||
|
<circle
|
||||||
|
cx={props.areaData.x}
|
||||||
|
cy={props.areaData.y + props.areaData.height}
|
||||||
|
r={5}
|
||||||
|
fill="blue"
|
||||||
|
/>
|
||||||
|
<circle
|
||||||
|
cx={props.areaData.x + size.width}
|
||||||
|
cy={props.areaData.y + size.height}
|
||||||
|
r={5}
|
||||||
|
fill="blue"
|
||||||
|
cursor="pointer"
|
||||||
|
onMouseDown={(e) => {}}
|
||||||
|
/>
|
||||||
|
</g>
|
||||||
|
);
|
||||||
|
}
|
@ -2,9 +2,15 @@ import React, { useRef, useState } from "react";
|
|||||||
import { useDrop } from "react-dnd";
|
import { useDrop } from "react-dnd";
|
||||||
import Table from "./table";
|
import Table from "./table";
|
||||||
import { defaultTableTheme, Cardinality, Constraint } from "../data/data";
|
import { defaultTableTheme, Cardinality, Constraint } from "../data/data";
|
||||||
|
import Area from "./area";
|
||||||
|
|
||||||
export default function Canvas(props) {
|
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 [linking, setLinking] = useState(false);
|
||||||
const [line, setLine] = useState({
|
const [line, setLine] = useState({
|
||||||
startTableId: -1,
|
startTableId: -1,
|
||||||
@ -32,14 +38,23 @@ export default function Canvas(props) {
|
|||||||
|
|
||||||
const canvas = useRef(null);
|
const canvas = useRef(null);
|
||||||
|
|
||||||
const handleMouseDownRect = (e, id) => {
|
const handleMouseDownRect = (e, id, type) => {
|
||||||
const { clientX, clientY } = e;
|
const { clientX, clientY } = e;
|
||||||
const table = props.tables.find((t) => t.id === id);
|
if (type === ObjectType.TABLE) {
|
||||||
setOffset({
|
const table = props.tables.find((t) => t.id === id);
|
||||||
x: clientX - table.x,
|
setOffset({
|
||||||
y: clientY - table.y,
|
x: clientX - table.x,
|
||||||
});
|
y: clientY - table.y,
|
||||||
setDragging(id);
|
});
|
||||||
|
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) => {
|
const handleMouseMove = (e) => {
|
||||||
@ -77,9 +92,17 @@ export default function Canvas(props) {
|
|||||||
endY: r.endY + dy,
|
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) => {
|
const updatedTables = props.tables.map((t) => {
|
||||||
if (t.id === dragging) {
|
if (t.id === dragging[1]) {
|
||||||
const updatedTable = {
|
const updatedTable = {
|
||||||
...t,
|
...t,
|
||||||
x: e.clientX - offset.x,
|
x: e.clientX - offset.x,
|
||||||
@ -91,14 +114,14 @@ export default function Canvas(props) {
|
|||||||
});
|
});
|
||||||
props.setTables(updatedTables);
|
props.setTables(updatedTables);
|
||||||
const updatedRelationShips = props.relationships.map((r) => {
|
const updatedRelationShips = props.relationships.map((r) => {
|
||||||
if (r.startTableId === dragging) {
|
if (r.startTableId === dragging[1]) {
|
||||||
return {
|
return {
|
||||||
...r,
|
...r,
|
||||||
startX: props.tables[r.startTableId].x + 15,
|
startX: props.tables[r.startTableId].x + 15,
|
||||||
startY:
|
startY:
|
||||||
props.tables[r.startTableId].y + r.startFieldId * 36 + 40 + 19,
|
props.tables[r.startTableId].y + r.startFieldId * 36 + 40 + 19,
|
||||||
};
|
};
|
||||||
} else if (r.endTableId === dragging) {
|
} else if (r.endTableId === dragging[1]) {
|
||||||
return {
|
return {
|
||||||
...r,
|
...r,
|
||||||
endX: props.tables[r.endTableId].x + 15,
|
endX: props.tables[r.endTableId].x + 15,
|
||||||
@ -108,11 +131,25 @@ export default function Canvas(props) {
|
|||||||
return r;
|
return r;
|
||||||
});
|
});
|
||||||
props.setRelationships(updatedRelationShips);
|
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) => {
|
const handleMouseDown = (e) => {
|
||||||
if (dragging < 0) {
|
if (dragging[0] === ObjectType.TABLE && dragging[1] < 0) {
|
||||||
if (onRect.tableId < 0) {
|
if (onRect.tableId < 0) {
|
||||||
setPanning(true);
|
setPanning(true);
|
||||||
setPanOffset({ x: e.clientX, y: e.clientY });
|
setPanOffset({ x: e.clientX, y: e.clientY });
|
||||||
@ -122,7 +159,7 @@ export default function Canvas(props) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleMouseUp = () => {
|
const handleMouseUp = () => {
|
||||||
setDragging(-1);
|
setDragging([ObjectType.NONE, -1]);
|
||||||
setPanning(false);
|
setPanning(false);
|
||||||
setCursor("default");
|
setCursor("default");
|
||||||
if (linking) handleLinking();
|
if (linking) handleLinking();
|
||||||
@ -137,7 +174,7 @@ export default function Canvas(props) {
|
|||||||
|
|
||||||
const handleGripField = (id) => {
|
const handleGripField = (id) => {
|
||||||
setPanning(false);
|
setPanning(false);
|
||||||
setDragging(-1);
|
setDragging([ObjectType.NONE, -1]);
|
||||||
setLinking(true);
|
setLinking(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -248,6 +285,13 @@ export default function Canvas(props) {
|
|||||||
</defs>
|
</defs>
|
||||||
|
|
||||||
<rect width="100%" height="100%" fill="url(#grid)" />
|
<rect width="100%" height="100%" fill="url(#grid)" />
|
||||||
|
{props.areas.map((a) => (
|
||||||
|
<Area
|
||||||
|
key={a.id}
|
||||||
|
areaData={a}
|
||||||
|
onMouseDown={(e) => handleMouseDownRect(e, a.id, ObjectType.AREA)}
|
||||||
|
></Area>
|
||||||
|
))}
|
||||||
{props.tables.map((table, i) => (
|
{props.tables.map((table, i) => (
|
||||||
<Table
|
<Table
|
||||||
key={table.id}
|
key={table.id}
|
||||||
@ -258,7 +302,9 @@ export default function Canvas(props) {
|
|||||||
setOnRect={setOnRect}
|
setOnRect={setOnRect}
|
||||||
handleGripField={handleGripField}
|
handleGripField={handleGripField}
|
||||||
setLine={setLine}
|
setLine={setLine}
|
||||||
onMouseDown={(e) => handleMouseDownRect(e, table.id)}
|
onMouseDown={(e) =>
|
||||||
|
handleMouseDownRect(e, table.id, ObjectType.TABLE)
|
||||||
|
}
|
||||||
onDelete={deleteTable}
|
onDelete={deleteTable}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
@ -85,6 +85,26 @@ export default function EditorPanel(props) {
|
|||||||
{contentList[parseInt(tab) - 1]}
|
{contentList[parseInt(tab) - 1]}
|
||||||
</Tabs>
|
</Tabs>
|
||||||
|
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
const newArea = {
|
||||||
|
id: props.areas.length,
|
||||||
|
name: `area_${props.areas.length}`,
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: 200,
|
||||||
|
height: 200,
|
||||||
|
color: defaultTableTheme,
|
||||||
|
};
|
||||||
|
props.setAreas((prev) => {
|
||||||
|
const updatedTables = [...prev, newArea];
|
||||||
|
return updatedTables;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
add area
|
||||||
|
</button>
|
||||||
|
<br />
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
const newTable = {
|
const newTable = {
|
||||||
|
@ -11,6 +11,7 @@ export default function Editor(props) {
|
|||||||
const [code, setCode] = useState("");
|
const [code, setCode] = useState("");
|
||||||
const [tables, setTables] = useState([]);
|
const [tables, setTables] = useState([]);
|
||||||
const [relationships, setRelationships] = useState([]);
|
const [relationships, setRelationships] = useState([]);
|
||||||
|
const [areas, setAreas] = useState([]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -25,6 +26,8 @@ export default function Editor(props) {
|
|||||||
setCode={setCode}
|
setCode={setCode}
|
||||||
relationships={relationships}
|
relationships={relationships}
|
||||||
setRelationships={setRelationships}
|
setRelationships={setRelationships}
|
||||||
|
areas={areas}
|
||||||
|
setAreas={setAreas}
|
||||||
/>
|
/>
|
||||||
<Canvas
|
<Canvas
|
||||||
tables={tables}
|
tables={tables}
|
||||||
@ -33,6 +36,8 @@ export default function Editor(props) {
|
|||||||
setCode={setCode}
|
setCode={setCode}
|
||||||
relationships={relationships}
|
relationships={relationships}
|
||||||
setRelationships={setRelationships}
|
setRelationships={setRelationships}
|
||||||
|
areas={areas}
|
||||||
|
setAreas={setAreas}
|
||||||
/>
|
/>
|
||||||
</DndProvider>
|
</DndProvider>
|
||||||
<Sidebar />
|
<Sidebar />
|
||||||
|
Loading…
Reference in New Issue
Block a user