Skip to content

Commit ca875e7

Browse files
committed
Show relationship names on the diagrams(#314)
1 parent 097ac59 commit ca875e7

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

src/components/EditorCanvas/Canvas.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Action,
44
Cardinality,
55
Constraint,
6+
darkBgTheme,
67
ObjectType,
78
} from "../../data/constants";
89
import { Toast } from "@douyinfe/semi-ui";
@@ -498,7 +499,7 @@ export default function Canvas() {
498499
className="w-full h-full"
499500
style={{
500501
cursor: pointer.style,
501-
backgroundColor: theme === "dark" ? "rgba(22, 22, 26, 1)" : "white",
502+
backgroundColor: theme === "dark" ? darkBgTheme : "white",
502503
}}
503504
>
504505
{settings.showGrid && (

src/components/EditorCanvas/Relationship.jsx

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
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";
38
import { calcPath } from "../../utils/calcPath";
49
import { useDiagram, useSettings, useLayout, useSelect } from "../../hooks";
510
import { useTranslation } from "react-i18next";
611
import { SideSheet } from "@douyinfe/semi-ui";
712
import RelationshipInfo from "../EditorSidePanel/RelationshipsTab/RelationshipInfo";
813

14+
const labelFontSize = 16;
15+
916
export default function Relationship({ data }) {
1017
const { settings } = useSettings();
1118
const { tables } = useDiagram();
1219
const { layout } = useLayout();
1320
const { selectedElement, setSelectedElement } = useSelect();
1421
const { t } = useTranslation();
22+
23+
const theme = localStorage.getItem("theme");
24+
1525
const pathRef = useRef();
26+
const labelRef = useRef();
1627

1728
let cardinalityStart = "1";
1829
let cardinalityEnd = "1";
@@ -42,11 +53,21 @@ export default function Relationship({ data }) {
4253
let cardinalityEndX = 0;
4354
let cardinalityStartY = 0;
4455
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;
4561

4662
const cardinalityOffset = 28;
4763

4864
if (pathRef.current) {
4965
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+
5071
const point1 = pathRef.current.getPointAtLength(cardinalityOffset);
5172
cardinalityStartX = point1.x;
5273
cardinalityStartY = point1.y;
@@ -105,6 +126,28 @@ export default function Relationship({ data }) {
105126
strokeWidth={2}
106127
cursor="pointer"
107128
/>
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+
)}
108151
{pathRef.current && settings.showCardinality && (
109152
<>
110153
<circle

src/components/EditorHeader/ControlPanel.jsx

+12
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,18 @@ export default function ControlPanel({
12291229
showCardinality: !prev.showCardinality,
12301230
})),
12311231
},
1232+
show_relationship_labels: {
1233+
state: settings.showRelationshipLabels ? (
1234+
<i className="bi bi-toggle-on" />
1235+
) : (
1236+
<i className="bi bi-toggle-off" />
1237+
),
1238+
function: () =>
1239+
setSettings((prev) => ({
1240+
...prev,
1241+
showRelationshipLabels: !prev.showRelationshipLabels,
1242+
})),
1243+
},
12321244
show_debug_coordinates: {
12331245
state: settings.showDebugCoordinates ? (
12341246
<i className="bi bi-toggle-on" />

src/context/SettingsContext.jsx

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const defaultSettings = {
99
autosave: true,
1010
panning: true,
1111
showCardinality: true,
12+
showRelationshipLabels: true,
1213
tableWidth: tableWidth,
1314
showDebugCoordinates: false,
1415
};

src/data/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const noteThemes = [
2323

2424
export const defaultBlue = "#175e7a";
2525
export const defaultNoteTheme = "#fcf7ac";
26+
export const darkBgTheme = "#16161A";
2627
export const tableHeaderHeight = 50;
2728
export const tableWidth = 220;
2829
export const tableFieldHeight = 36;

src/i18n/locales/en.js

+1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ const en = {
243243
failed_to_load: "Failed to load. Make sure the link is correct.",
244244
share_info:
245245
"* Sharing this link will not create a live real-time collaboration session.",
246+
show_relationship_labels: "Show relationship labels",
246247
},
247248
};
248249

0 commit comments

Comments
 (0)