Show relationship names on the diagrams(#314)

This commit is contained in:
1ilit 2025-01-20 21:48:21 +04:00
parent 0d0c6341c0
commit aaf83b6f28
6 changed files with 61 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import {
Action, Action,
Cardinality, Cardinality,
Constraint, Constraint,
darkBgTheme,
ObjectType, ObjectType,
} from "../../data/constants"; } from "../../data/constants";
import { Toast } from "@douyinfe/semi-ui"; import { Toast } from "@douyinfe/semi-ui";
@ -498,7 +499,7 @@ export default function Canvas() {
className="w-full h-full" className="w-full h-full"
style={{ style={{
cursor: pointer.style, cursor: pointer.style,
backgroundColor: theme === "dark" ? "rgba(22, 22, 26, 1)" : "white", backgroundColor: theme === "dark" ? darkBgTheme : "white",
}} }}
> >
{settings.showGrid && ( {settings.showGrid && (

View File

@ -1,18 +1,29 @@
import { useRef } from "react"; import { useRef } from "react";
import { Cardinality, ObjectType, Tab } from "../../data/constants"; import {
Cardinality,
darkBgTheme,
ObjectType,
Tab,
} from "../../data/constants";
import { calcPath } from "../../utils/calcPath"; import { calcPath } from "../../utils/calcPath";
import { useDiagram, useSettings, useLayout, useSelect } from "../../hooks"; import { useDiagram, useSettings, useLayout, useSelect } from "../../hooks";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { SideSheet } from "@douyinfe/semi-ui"; import { SideSheet } from "@douyinfe/semi-ui";
import RelationshipInfo from "../EditorSidePanel/RelationshipsTab/RelationshipInfo"; import RelationshipInfo from "../EditorSidePanel/RelationshipsTab/RelationshipInfo";
const labelFontSize = 16;
export default function Relationship({ data }) { export default function Relationship({ data }) {
const { settings } = useSettings(); const { settings } = useSettings();
const { tables } = useDiagram(); const { tables } = useDiagram();
const { layout } = useLayout(); const { layout } = useLayout();
const { selectedElement, setSelectedElement } = useSelect(); const { selectedElement, setSelectedElement } = useSelect();
const { t } = useTranslation(); const { t } = useTranslation();
const theme = localStorage.getItem("theme");
const pathRef = useRef(); const pathRef = useRef();
const labelRef = useRef();
let cardinalityStart = "1"; let cardinalityStart = "1";
let cardinalityEnd = "1"; let cardinalityEnd = "1";
@ -42,11 +53,21 @@ export default function Relationship({ data }) {
let cardinalityEndX = 0; let cardinalityEndX = 0;
let cardinalityStartY = 0; let cardinalityStartY = 0;
let cardinalityEndY = 0; let cardinalityEndY = 0;
let labelX = 0;
let labelY = 0;
let labelWidth = labelRef.current?.getBBox().width ?? 0;
let labelHeight = labelRef.current?.getBBox().height ?? 0;
const cardinalityOffset = 28; const cardinalityOffset = 28;
if (pathRef.current) { if (pathRef.current) {
const pathLength = pathRef.current.getTotalLength(); const pathLength = pathRef.current.getTotalLength();
const labelPoint = pathRef.current.getPointAtLength(pathLength / 2);
labelX = labelPoint.x - (labelWidth ?? 0) / 2;
labelY = labelPoint.y + (labelHeight ?? 0) / 2;
const point1 = pathRef.current.getPointAtLength(cardinalityOffset); const point1 = pathRef.current.getPointAtLength(cardinalityOffset);
cardinalityStartX = point1.x; cardinalityStartX = point1.x;
cardinalityStartY = point1.y; cardinalityStartY = point1.y;
@ -105,6 +126,28 @@ export default function Relationship({ data }) {
strokeWidth={2} strokeWidth={2}
cursor="pointer" cursor="pointer"
/> />
{settings.showRelationshipLabels && (
<>
<rect
x={labelX - 2}
y={labelY - labelFontSize}
fill={theme === "dark" ? darkBgTheme : "white"}
width={labelWidth + 4}
height={labelHeight}
/>
<text
x={labelX}
y={labelY}
fill={theme === "dark" ? "lightgrey" : "#333"}
fontSize={labelFontSize}
fontWeight={500}
ref={labelRef}
className="group-hover:fill-sky-700"
>
{data.name}
</text>
</>
)}
{pathRef.current && settings.showCardinality && ( {pathRef.current && settings.showCardinality && (
<> <>
<circle <circle

View File

@ -1229,6 +1229,18 @@ export default function ControlPanel({
showCardinality: !prev.showCardinality, showCardinality: !prev.showCardinality,
})), })),
}, },
show_relationship_labels: {
state: settings.showRelationshipLabels ? (
<i className="bi bi-toggle-on" />
) : (
<i className="bi bi-toggle-off" />
),
function: () =>
setSettings((prev) => ({
...prev,
showRelationshipLabels: !prev.showRelationshipLabels,
})),
},
show_debug_coordinates: { show_debug_coordinates: {
state: settings.showDebugCoordinates ? ( state: settings.showDebugCoordinates ? (
<i className="bi bi-toggle-on" /> <i className="bi bi-toggle-on" />

View File

@ -9,6 +9,7 @@ const defaultSettings = {
autosave: true, autosave: true,
panning: true, panning: true,
showCardinality: true, showCardinality: true,
showRelationshipLabels: true,
tableWidth: tableWidth, tableWidth: tableWidth,
showDebugCoordinates: false, showDebugCoordinates: false,
}; };

View File

@ -23,6 +23,7 @@ export const noteThemes = [
export const defaultBlue = "#175e7a"; export const defaultBlue = "#175e7a";
export const defaultNoteTheme = "#fcf7ac"; export const defaultNoteTheme = "#fcf7ac";
export const darkBgTheme = "#16161A";
export const tableHeaderHeight = 50; export const tableHeaderHeight = 50;
export const tableWidth = 220; export const tableWidth = 220;
export const tableFieldHeight = 36; export const tableFieldHeight = 36;

View File

@ -243,6 +243,7 @@ const en = {
failed_to_load: "Failed to load. Make sure the link is correct.", failed_to_load: "Failed to load. Make sure the link is correct.",
share_info: share_info:
"* Sharing this link will not create a live real-time collaboration session.", "* Sharing this link will not create a live real-time collaboration session.",
show_relationship_labels: "Show relationship labels",
}, },
}; };