diff --git a/src/components/relationship.jsx b/src/components/relationship.jsx index e734fac..5cc3caf 100644 --- a/src/components/relationship.jsx +++ b/src/components/relationship.jsx @@ -1,88 +1,8 @@ import { React, useState } from "react"; +import { calcPath } from "../utils"; export default function Relationship(props) { const [hovered, setHovered] = useState(false); - const calcPath = (x1, x2, y1, y2) => { - let r = 16; - const offsetX = 8; - const tableWidth = 200; - const midX = (x2 + x1 + tableWidth) / 2; - const endX = x2 + tableWidth < x1 ? x2 + tableWidth - offsetX * 2 : x2; - - if (Math.abs(y1 - y2) <= 36) { - r = Math.abs(y2 - y1) / 3; - if (r <= 2) { - if (x1 + tableWidth <= x2) - return `M ${x1 + tableWidth - 2 * offsetX} ${y1} L ${x2 + 0.1} ${ - y2 + 0.1 - }`; - else if (x2 + tableWidth < x1) - return `M ${x2 + tableWidth - 2 * offsetX} ${y2} L ${x1 + 0.1} ${ - y1 + 0.1 - }`; - } - } - - if (y1 <= y2) { - if (x1 + tableWidth <= x2) { - return `M ${x1 + tableWidth - offsetX * 2} ${y1} L ${ - midX - r - } ${y1} A ${r} ${r} 0 0 1 ${midX} ${y1 + r} L ${midX} ${ - y2 - r - } A ${r} ${r} 0 0 0 ${midX + r} ${y2} L ${endX} ${y2}`; - } else if (x2 <= x1 + tableWidth && x1 <= x2) { - return `M ${x1 + tableWidth - 2 * offsetX} ${y1} L ${ - x2 + tableWidth - } ${y1} A ${r} ${r} 0 0 1 ${x2 + tableWidth + r} ${y1 + r} L ${ - x2 + tableWidth + r - } ${y2 - r} A ${r} ${r} 0 0 1 ${x2 + tableWidth} ${y2} L ${ - x2 + tableWidth - 2 * offsetX - } ${y2}`; - } else if (x2 + tableWidth >= x1 && x2 + tableWidth <= x1 + tableWidth) { - return `M ${x1} ${y1} L ${x2 - r} ${y1} A ${r} ${r} 0 0 ${0} ${ - x2 - r - r - } ${y1 + r} L ${x2 - r - r} ${y2 - r} A ${r} ${r} 0 0 0 ${ - x2 - r - } ${y2} L ${x2} ${y2}`; - } else { - return `M ${x1} ${y1} L ${midX + r} ${y1} A ${r} ${r} 0 0 0 ${midX} ${ - y1 + r - } L ${midX} ${y2 - r} A ${r} ${r} 0 0 1 ${ - midX - r - } ${y2} L ${endX} ${y2}`; - } - } else { - if (x1 + tableWidth <= x2) { - return `M ${x1 + tableWidth - offsetX * 2} ${y1} L ${ - midX - r - } ${y1} A ${r} ${r} 0 0 0 ${midX} ${y1 - r} L ${midX} ${ - y2 + r - } A ${r} ${r} 0 0 1 ${midX + r} ${y2} L ${endX} ${y2}`; - } else if (x1 + tableWidth >= x2 && x1 + tableWidth <= x2 + tableWidth) { - return `M ${x1} ${y1} L ${x1 - r} ${y1} A ${r} ${r} 0 0 1 ${ - x1 - r - r - } ${y1 - r} L ${x1 - r - r} ${y2 + r} A ${r} ${r} 0 0 1 ${ - x1 - r - } ${y2} L ${endX} ${y2}`; - } else if (x1 >= x2 && x1 <= x2 + tableWidth) { - return `M ${x1 + tableWidth - 2 * offsetX} ${y1} L ${ - x1 + tableWidth - 2 * offsetX + r - } ${y1} A ${r} ${r} 0 0 0 ${x1 + tableWidth - 2 * offsetX + r + r} ${ - y1 - r - } L ${x1 + tableWidth - 2 * offsetX + r + r} ${ - y2 + r - } A ${r} ${r} 0 0 0 ${x1 + tableWidth - 2 * offsetX + r} ${y2} L ${ - x2 + tableWidth - 2 * offsetX - } ${y2}`; - } else { - return `M ${x1} ${y1} L ${midX + r} ${y1} A ${r} ${r} 0 0 1 ${midX} ${ - y1 - r - } L ${midX} ${y2 + r} A ${r} ${r} 0 0 0 ${ - midX - r - } ${y2} L ${endX} ${y2}`; - } - } - }; return ( diff --git a/src/data/seeds.js b/src/data/seeds.js index a6ffc33..d27ff4e 100644 --- a/src/data/seeds.js +++ b/src/data/seeds.js @@ -2,9 +2,9 @@ const t1 = { tables: [ { id: 0, - name: "template_1", - x: 57, - y: 63, + name: "table_0", + x: 73, + y: 69, fields: [ { name: "id", @@ -21,12 +21,72 @@ const t1 = { ], comment: "", indices: [], - color: "#f03c3c", + color: "#175e7a", + }, + { + id: 1, + name: "table_1", + x: 366, + y: 117, + fields: [ + { + name: "id", + type: "INT", + default: "", + check: "", + primary: true, + unique: true, + notNull: true, + increment: true, + comment: "", + id: 0, + }, + ], + comment: "", + indices: [], + color: "#175e7a", + }, + ], + relationships: [ + { + startTableId: 0, + startFieldId: 0, + endTableId: 1, + endFieldId: 0, + startX: 88, + startY: 138, + endX: 381, + endY: 186, + name: "table_0_id_fk", + cardinality: "One to one", + updateConstraint: "No action", + deleteConstraint: "No action", + mandatory: false, + id: 0, + }, + ], + notes: [ + { + id: 0, + x: 526, + y: 240, + title: "note_0", + content: "hi", + color: "#fcf7ac", + height: 65, + }, + ], + subjectAreas: [ + { + id: 0, + name: "area_0", + x: 43, + y: 28, + width: 558, + height: 206, + color: "#175e7a", }, ], - relationships: [], - notes: [], - subjectAreas: [], types: [], title: "Template 1", description: "Lorem ipsum", diff --git a/src/pages/Templates.jsx b/src/pages/Templates.jsx index fddf9fe..93c158d 100644 --- a/src/pages/Templates.jsx +++ b/src/pages/Templates.jsx @@ -4,10 +4,12 @@ import { Link } from "react-router-dom"; import { Tabs, TabPane } from "@douyinfe/semi-ui"; import { useLiveQuery } from "dexie-react-hooks"; import { db } from "../data/db"; +import { calcPath } from "../utils"; -function cardCanvas(diagram) { +function Thumbnail({ diagram }) { + const zoom = 0.3; return ( -
+
- {/* {diagram.areas.map((a) => ( - {}} - setResize={null} - initCoords={null} - setInitCoords={null} - zoom={null} - > - ))} */} - {diagram.tables.map((table, i) => { + {diagram.subjectAreas?.map((a) => ( + 0 ? a.width * zoom : 0} + height={a.height > 0 ? a.height * zoom : 0} + > +
+
+
+
+ {a.name} +
+ + ))} + {diagram.tables?.map((table, i) => { const height = table.fields.length * 36 + 50 + 7; return (
@@ -85,19 +97,58 @@ function cardCanvas(diagram) { ); })} - {/* - {diagram.relationships.map((e, i) => ( - - ))} - {diagram.notes.map((n) => ( - - {} - } - > - ))} */} + {diagram.relationships?.map((e, i) => ( + + ))} + {diagram.notes?.map((n) => { + const x = n.x * zoom; + const y = n.y * zoom; + const w = 180 * zoom; + const r = 3 * zoom; + const fold = 24 * zoom; + const h = n.height * zoom; + return ( + + + + +
+ +
{n.content}
+
+
+
+ ); + })}
@@ -149,7 +200,7 @@ export default function Templates() { key={i} className="p-4 bg-gray-200 hover:translate-y-[-6px] transition-all duration-300" > - {cardCanvas(t)} +
{t.title}
{t.description}
diff --git a/src/utils/index.js b/src/utils/index.js index 5f25378..3f5313f 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -605,6 +605,92 @@ function validateDiagram(diagram) { return issues; } +const calcPath = (x1, x2, y1, y2, zoom = 1) => { + x1 *= zoom; + x2 *= zoom; + y1 *= zoom; + y2 *= zoom; + let r = 16 * zoom; + const offsetX = 8 * zoom; + const tableWidth = 200 * zoom; + const midX = (x2 + x1 + tableWidth) / 2; + const endX = x2 + tableWidth < x1 ? x2 + tableWidth - offsetX * 2 : x2; + + if (Math.abs(y1 - y2) <= 36 * zoom) { + r = Math.abs(y2 - y1) / 3; + if (r <= 2) { + if (x1 + tableWidth <= x2) + return `M ${x1 + tableWidth - 2 * offsetX} ${y1} L ${x2 + 0.1} ${ + y2 + 0.1 + }`; + else if (x2 + tableWidth < x1) + return `M ${x2 + tableWidth - 2 * offsetX} ${y2} L ${x1 + 0.1} ${ + y1 + 0.1 + }`; + } + } + + if (y1 <= y2) { + if (x1 + tableWidth <= x2) { + return `M ${x1 + tableWidth - offsetX * 2} ${y1} L ${ + midX - r + } ${y1} A ${r} ${r} 0 0 1 ${midX} ${y1 + r} L ${midX} ${ + y2 - r + } A ${r} ${r} 0 0 0 ${midX + r} ${y2} L ${endX} ${y2}`; + } else if (x2 <= x1 + tableWidth && x1 <= x2) { + return `M ${x1 + tableWidth - 2 * offsetX} ${y1} L ${ + x2 + tableWidth + } ${y1} A ${r} ${r} 0 0 1 ${x2 + tableWidth + r} ${y1 + r} L ${ + x2 + tableWidth + r + } ${y2 - r} A ${r} ${r} 0 0 1 ${x2 + tableWidth} ${y2} L ${ + x2 + tableWidth - 2 * offsetX + } ${y2}`; + } else if (x2 + tableWidth >= x1 && x2 + tableWidth <= x1 + tableWidth) { + return `M ${x1} ${y1} L ${x2 - r} ${y1} A ${r} ${r} 0 0 ${0} ${ + x2 - r - r + } ${y1 + r} L ${x2 - r - r} ${y2 - r} A ${r} ${r} 0 0 0 ${ + x2 - r + } ${y2} L ${x2} ${y2}`; + } else { + return `M ${x1} ${y1} L ${midX + r} ${y1} A ${r} ${r} 0 0 0 ${midX} ${ + y1 + r + } L ${midX} ${y2 - r} A ${r} ${r} 0 0 1 ${ + midX - r + } ${y2} L ${endX} ${y2}`; + } + } else { + if (x1 + tableWidth <= x2) { + return `M ${x1 + tableWidth - offsetX * 2} ${y1} L ${ + midX - r + } ${y1} A ${r} ${r} 0 0 0 ${midX} ${y1 - r} L ${midX} ${ + y2 + r + } A ${r} ${r} 0 0 1 ${midX + r} ${y2} L ${endX} ${y2}`; + } else if (x1 + tableWidth >= x2 && x1 + tableWidth <= x2 + tableWidth) { + return `M ${x1} ${y1} L ${x1 - r} ${y1} A ${r} ${r} 0 0 1 ${x1 - r - r} ${ + y1 - r + } L ${x1 - r - r} ${y2 + r} A ${r} ${r} 0 0 1 ${ + x1 - r + } ${y2} L ${endX} ${y2}`; + } else if (x1 >= x2 && x1 <= x2 + tableWidth) { + return `M ${x1 + tableWidth - 2 * offsetX} ${y1} L ${ + x1 + tableWidth - 2 * offsetX + r + } ${y1} A ${r} ${r} 0 0 0 ${x1 + tableWidth - 2 * offsetX + r + r} ${ + y1 - r + } L ${x1 + tableWidth - 2 * offsetX + r + r} ${ + y2 + r + } A ${r} ${r} 0 0 0 ${x1 + tableWidth - 2 * offsetX + r} ${y2} L ${ + x2 + tableWidth - 2 * offsetX + } ${y2}`; + } else { + return `M ${x1} ${y1} L ${midX + r} ${y1} A ${r} ${r} 0 0 1 ${midX} ${ + y1 - r + } L ${midX} ${y2 + r} A ${r} ${r} 0 0 0 ${ + midX - r + } ${y2} L ${endX} ${y2}`; + } + } +}; + export { enterFullscreen, exitFullscreen, @@ -620,4 +706,5 @@ export { hasPrecision, validateDateStr, hasCheck, + calcPath, };