|
1 |
| -import { useMemo, useRef } from "react"; |
2 |
| -import { |
3 |
| - Cardinality, |
4 |
| - darkBgTheme, |
5 |
| - ObjectType, |
6 |
| - Tab, |
7 |
| -} from "../../data/constants"; |
| 1 | +import { useMemo, useRef, useState, useEffect } from "react"; |
| 2 | +import { Cardinality, ObjectType, Tab } from "../../data/constants"; |
8 | 3 | import { calcPath } from "../../utils/calcPath";
|
9 | 4 | import { useDiagram, useSettings, useLayout, useSelect } from "../../hooks";
|
10 | 5 | import { useTranslation } from "react-i18next";
|
@@ -46,13 +41,13 @@ export default function Relationship({ data }) {
|
46 | 41 | // the translated values are to ensure backwards compatibility
|
47 | 42 | case t(Cardinality.MANY_TO_ONE):
|
48 | 43 | case Cardinality.MANY_TO_ONE:
|
49 |
| - cardinalityStart = "n"; |
| 44 | + cardinalityStart = data.manyLabel || "n"; |
50 | 45 | cardinalityEnd = "1";
|
51 | 46 | break;
|
52 | 47 | case t(Cardinality.ONE_TO_MANY):
|
53 | 48 | case Cardinality.ONE_TO_MANY:
|
54 | 49 | cardinalityStart = "1";
|
55 |
| - cardinalityEnd = "n"; |
| 50 | + cardinalityEnd = data.manyLabel || "n"; |
56 | 51 | break;
|
57 | 52 | case t(Cardinality.ONE_TO_ONE):
|
58 | 53 | case Cardinality.ONE_TO_ONE:
|
@@ -128,63 +123,30 @@ export default function Relationship({ data }) {
|
128 | 123 | cursor="pointer"
|
129 | 124 | />
|
130 | 125 | {settings.showRelationshipLabels && (
|
131 |
| - <> |
132 |
| - <rect |
133 |
| - x={labelX - 2} |
134 |
| - y={labelY - labelFontSize} |
135 |
| - fill={settings.mode === "dark" ? darkBgTheme : "white"} |
136 |
| - width={labelWidth + 4} |
137 |
| - height={labelHeight} |
138 |
| - /> |
139 |
| - <text |
140 |
| - x={labelX} |
141 |
| - y={labelY} |
142 |
| - fill={settings.mode === "dark" ? "lightgrey" : "#333"} |
143 |
| - fontSize={labelFontSize} |
144 |
| - fontWeight={500} |
145 |
| - ref={labelRef} |
146 |
| - className="group-hover:fill-sky-700" |
147 |
| - > |
148 |
| - {data.name} |
149 |
| - </text> |
150 |
| - </> |
| 126 | + <text |
| 127 | + x={labelX} |
| 128 | + y={labelY} |
| 129 | + fill={settings.mode === "dark" ? "lightgrey" : "#333"} |
| 130 | + fontSize={labelFontSize} |
| 131 | + fontWeight={500} |
| 132 | + ref={labelRef} |
| 133 | + className="group-hover:fill-sky-700" |
| 134 | + > |
| 135 | + {data.name} |
| 136 | + </text> |
151 | 137 | )}
|
152 | 138 | {pathRef.current && settings.showCardinality && (
|
153 | 139 | <>
|
154 |
| - <circle |
155 |
| - cx={cardinalityStartX} |
156 |
| - cy={cardinalityStartY} |
157 |
| - r="12" |
158 |
| - fill="grey" |
159 |
| - className="group-hover:fill-sky-700" |
160 |
| - /> |
161 |
| - <text |
| 140 | + <CardinalityLabel |
162 | 141 | x={cardinalityStartX}
|
163 | 142 | y={cardinalityStartY}
|
164 |
| - fill="white" |
165 |
| - strokeWidth="0.5" |
166 |
| - textAnchor="middle" |
167 |
| - alignmentBaseline="middle" |
168 |
| - > |
169 |
| - {cardinalityStart} |
170 |
| - </text> |
171 |
| - <circle |
172 |
| - cx={cardinalityEndX} |
173 |
| - cy={cardinalityEndY} |
174 |
| - r="12" |
175 |
| - fill="grey" |
176 |
| - className="group-hover:fill-sky-700" |
| 143 | + text={cardinalityStart} |
177 | 144 | />
|
178 |
| - <text |
| 145 | + <CardinalityLabel |
179 | 146 | x={cardinalityEndX}
|
180 | 147 | y={cardinalityEndY}
|
181 |
| - fill="white" |
182 |
| - strokeWidth="0.5" |
183 |
| - textAnchor="middle" |
184 |
| - alignmentBaseline="middle" |
185 |
| - > |
186 |
| - {cardinalityEnd} |
187 |
| - </text> |
| 148 | + text={cardinalityEnd} |
| 149 | + /> |
188 | 150 | </>
|
189 | 151 | )}
|
190 | 152 | </g>
|
@@ -212,3 +174,41 @@ export default function Relationship({ data }) {
|
212 | 174 | </>
|
213 | 175 | );
|
214 | 176 | }
|
| 177 | + |
| 178 | +function CardinalityLabel({ x, y, text, r = 12, padding = 14 }) { |
| 179 | + const [textWidth, setTextWidth] = useState(0); |
| 180 | + const textRef = useRef(null); |
| 181 | + |
| 182 | + useEffect(() => { |
| 183 | + if (textRef.current) { |
| 184 | + const bbox = textRef.current.getBBox(); |
| 185 | + setTextWidth(bbox.width); |
| 186 | + } |
| 187 | + }, [text]); |
| 188 | + |
| 189 | + return ( |
| 190 | + <g> |
| 191 | + <rect |
| 192 | + x={x - textWidth / 2 - padding / 2} |
| 193 | + y={y - r} |
| 194 | + rx={r} |
| 195 | + ry={r} |
| 196 | + width={textWidth + padding} |
| 197 | + height={r * 2} |
| 198 | + fill="grey" |
| 199 | + className="group-hover:fill-sky-700" |
| 200 | + /> |
| 201 | + <text |
| 202 | + ref={textRef} |
| 203 | + x={x} |
| 204 | + y={y} |
| 205 | + fill="white" |
| 206 | + strokeWidth="0.5" |
| 207 | + textAnchor="middle" |
| 208 | + alignmentBaseline="middle" |
| 209 | + > |
| 210 | + {text} |
| 211 | + </text> |
| 212 | + </g> |
| 213 | + ); |
| 214 | +} |
0 commit comments