From 25926cb9b8d3fbb2729898aa2d48ebe8c84132dc Mon Sep 17 00:00:00 2001 From: 1ilit Date: Tue, 19 Sep 2023 15:48:49 +0300 Subject: [PATCH] table and relationship context --- src/components/canvas.jsx | 34 +++++++------ src/components/editor_panel.jsx | 15 +++--- src/components/reference_overview.jsx | 18 +++---- src/pages/editor.jsx | 72 ++++++++++++--------------- 4 files changed, 67 insertions(+), 72 deletions(-) diff --git a/src/components/canvas.jsx b/src/components/canvas.jsx index 207b6d7..e0d859f 100644 --- a/src/components/canvas.jsx +++ b/src/components/canvas.jsx @@ -4,10 +4,12 @@ import Table from "./table"; import { defaultTableTheme, Cardinality, Constraint } from "../data/data"; import Area from "./area"; import Relationship from "./relationship"; -import { TableContext } from "../pages/editor"; +import { AreaContext, TableContext } from "../pages/editor"; export default function Canvas(props) { - const { tables, setTables } = useContext(TableContext); + const { tables, setTables, relationships, setRelationships } = + useContext(TableContext); + const { areas, setAreas } = useContext(AreaContext); const ObjectType = { NONE: 0, TABLE: 1, @@ -60,7 +62,7 @@ export default function Canvas(props) { }); setDragging([ObjectType.TABLE, id]); } else if (type === ObjectType.AREA) { - const area = props.areas.find((t) => t.id === id); + const area = areas.find((t) => t.id === id); setOffset({ x: clientX - area.x, y: clientY - area.y, @@ -97,8 +99,8 @@ export default function Canvas(props) { })) ); - props.setRelationships( - props.relationships.map((r) => ({ + setRelationships( + relationships.map((r) => ({ ...r, startX: r.startX + dx, startY: r.startY + dy, @@ -107,8 +109,8 @@ export default function Canvas(props) { })) ); - props.setAreas( - props.areas.map((t) => ({ + setAreas( + areas.map((t) => ({ ...t, x: t.x + dx, y: t.y + dy, @@ -127,7 +129,7 @@ export default function Canvas(props) { return t; }); setTables(updatedTables); - const updatedRelationShips = props.relationships.map((r) => { + const updatedRelationShips = relationships.map((r) => { if (r.startTableId === dragging[1]) { return { ...r, @@ -143,13 +145,13 @@ export default function Canvas(props) { } return r; }); - props.setRelationships(updatedRelationShips); + setRelationships(updatedRelationShips); } else if ( dragging[0] === ObjectType.AREA && dragging[1] >= 0 && areaResize.id === -1 ) { - const updatedAreas = props.areas.map((t) => { + const updatedAreas = areas.map((t) => { if (t.id === dragging[1]) { const updatedArea = { ...t, @@ -160,7 +162,7 @@ export default function Canvas(props) { } return t; }); - props.setAreas(updatedAreas); + setAreas(updatedAreas); } else if (areaResize.id !== -1) { if (areaResize.dir === "none") return; @@ -187,7 +189,7 @@ export default function Canvas(props) { newHeight = initCoords.height + (e.clientY - initCoords.mouseY); } - props.setAreas((prev) => + setAreas((prev) => prev.map((a) => { if (a.id === areaResize.id) { return { @@ -241,7 +243,7 @@ export default function Canvas(props) { line.startFieldId === onRect.field ) return; - props.setRelationships((prev) => [ + setRelationships((prev) => [ ...prev, { ...line, @@ -341,7 +343,7 @@ export default function Canvas(props) { height="100%" fill="url(#pattern-circles)" > - {props.areas.map((a) => ( + {areas.map((a) => ( ))} {tables.map((table, i) => ( @@ -372,7 +374,7 @@ export default function Canvas(props) { strokeDasharray="8,8" /> )} - {props.relationships.map((e, i) => ( + {relationships.map((e, i) => ( ))} diff --git a/src/components/editor_panel.jsx b/src/components/editor_panel.jsx index cc8a90d..b3d3d9a 100644 --- a/src/components/editor_panel.jsx +++ b/src/components/editor_panel.jsx @@ -1,4 +1,4 @@ -import { React, useState } from "react"; +import { React, useContext, useState } from "react"; import CodeMirror from "@uiw/react-codemirror"; import { createTheme } from "@uiw/codemirror-themes"; import { sql } from "@codemirror/lang-sql"; @@ -9,6 +9,7 @@ import { Tabs } from "@douyinfe/semi-ui"; import TableOverview from "./table_overview"; import ReferenceOverview from "./reference_overview"; import { defaultTableTheme } from "../data/data"; +import { AreaContext } from "../pages/editor"; // import { TableContext } from "../pages/editor"; const myTheme = createTheme({ @@ -29,6 +30,7 @@ const EditorPanel = (props) => { const [tab, setTab] = useState("1"); // const map = useRef(new Map()); // const {tables, setTables} = useContext(TableContext); + const {areas, setAreas} = useContext(AreaContext); const tabList = [ { tab: "Tables", itemKey: "1" }, @@ -38,10 +40,7 @@ const EditorPanel = (props) => { ]; const contentList = [ , - , + , , {