|
1 | 1 | import { useRef } from "react"; |
2 | | -import { Cardinality, ObjectType, Tab } from "../../data/constants"; |
| 2 | +import { |
| 3 | + Cardinality, |
| 4 | + darkBgTheme, |
| 5 | + ObjectType, |
| 6 | + Tab, |
| 7 | +} from "../../data/constants"; |
3 | 8 | import { calcPath } from "../../utils/calcPath"; |
4 | 9 | import { useDiagram, useSettings, useLayout, useSelect } from "../../hooks"; |
5 | 10 | import { useTranslation } from "react-i18next"; |
6 | 11 | import { SideSheet } from "@douyinfe/semi-ui"; |
7 | 12 | import RelationshipInfo from "../EditorSidePanel/RelationshipsTab/RelationshipInfo"; |
8 | 13 |
|
| 14 | +const labelFontSize = 16; |
| 15 | + |
9 | 16 | export default function Relationship({ data }) { |
10 | 17 | const { settings } = useSettings(); |
11 | 18 | const { tables } = useDiagram(); |
12 | 19 | const { layout } = useLayout(); |
13 | 20 | const { selectedElement, setSelectedElement } = useSelect(); |
14 | 21 | const { t } = useTranslation(); |
| 22 | + |
| 23 | + const theme = localStorage.getItem("theme"); |
| 24 | + |
15 | 25 | const pathRef = useRef(); |
| 26 | + const labelRef = useRef(); |
16 | 27 |
|
17 | 28 | let cardinalityStart = "1"; |
18 | 29 | let cardinalityEnd = "1"; |
@@ -42,11 +53,21 @@ export default function Relationship({ data }) { |
42 | 53 | let cardinalityEndX = 0; |
43 | 54 | let cardinalityStartY = 0; |
44 | 55 | let cardinalityEndY = 0; |
| 56 | + let labelX = 0; |
| 57 | + let labelY = 0; |
| 58 | + |
| 59 | + let labelWidth = labelRef.current?.getBBox().width ?? 0; |
| 60 | + let labelHeight = labelRef.current?.getBBox().height ?? 0; |
45 | 61 |
|
46 | 62 | const cardinalityOffset = 28; |
47 | 63 |
|
48 | 64 | if (pathRef.current) { |
49 | 65 | const pathLength = pathRef.current.getTotalLength(); |
| 66 | + |
| 67 | + const labelPoint = pathRef.current.getPointAtLength(pathLength / 2); |
| 68 | + labelX = labelPoint.x - (labelWidth ?? 0) / 2; |
| 69 | + labelY = labelPoint.y + (labelHeight ?? 0) / 2; |
| 70 | + |
50 | 71 | const point1 = pathRef.current.getPointAtLength(cardinalityOffset); |
51 | 72 | cardinalityStartX = point1.x; |
52 | 73 | cardinalityStartY = point1.y; |
@@ -105,6 +126,28 @@ export default function Relationship({ data }) { |
105 | 126 | strokeWidth={2} |
106 | 127 | cursor="pointer" |
107 | 128 | /> |
| 129 | + {settings.showRelationshipLabels && ( |
| 130 | + <> |
| 131 | + <rect |
| 132 | + x={labelX - 2} |
| 133 | + y={labelY - labelFontSize} |
| 134 | + fill={theme === "dark" ? darkBgTheme : "white"} |
| 135 | + width={labelWidth + 4} |
| 136 | + height={labelHeight} |
| 137 | + /> |
| 138 | + <text |
| 139 | + x={labelX} |
| 140 | + y={labelY} |
| 141 | + fill={theme === "dark" ? "lightgrey" : "#333"} |
| 142 | + fontSize={labelFontSize} |
| 143 | + fontWeight={500} |
| 144 | + ref={labelRef} |
| 145 | + className="group-hover:fill-sky-700" |
| 146 | + > |
| 147 | + {data.name} |
| 148 | + </text> |
| 149 | + </> |
| 150 | + )} |
108 | 151 | {pathRef.current && settings.showCardinality && ( |
109 | 152 | <> |
110 | 153 | <circle |
|
0 commit comments