Skip to content

Free, simple, and intuitive online database diagram editor and SQL generator.

Add color to relationships (#1131)

1ilit5efc5fd10a27
diff --git a/src/components/EditorCanvas/Canvas.jsx b/src/components/EditorCanvas/Canvas.jsx
index 976dcd33..8a6a9ad4 100644
--- a/src/components/EditorCanvas/Canvas.jsx
+++ b/src/components/EditorCanvas/Canvas.jsx
@@ -9,6 +9,7 @@ import {
gridSize,
gridCircleRadius,
minAreaSize,
+ defaultRelationshipColor,
} from "../../data/constants";
import { Toast } from "@douyinfe/semi-ui";
import Table from "./Table";
@@ -666,6 +667,7 @@ export default function Canvas() {
updateConstraint: Constraint.NONE,
deleteConstraint: Constraint.NONE,
name: `fk_${startTableName}_${startField.name}_${endTableName}`,
+ color: defaultRelationshipColor,
id: nanoid(),
};
delete newRelationship.startX;
diff --git a/src/components/EditorCanvas/Relationship.jsx b/src/components/EditorCanvas/Relationship.jsx
index ddc227d9..a5531699 100644
--- a/src/components/EditorCanvas/Relationship.jsx
+++ b/src/components/EditorCanvas/Relationship.jsx
@@ -83,6 +83,7 @@ export default function Relationship({ data }) {
const pathRef = useRef();
const labelRef = useRef();
+ const [hovered, setHovered] = useState(false);
let cardinalityStart = "1";
let cardinalityEnd = "1";
@@ -171,7 +172,12 @@ export default function Relationship({ data }) {
return (
<>
- <g className="select-none group" onDoubleClick={edit}>
+ <g
+ className="select-none group"
+ onDoubleClick={edit}
+ onPointerEnter={() => setHovered(true)}
+ onPointerLeave={() => setHovered(false)}
+ >
{/* invisible wider path for better hover ux */}
<path
d={
@@ -202,6 +208,7 @@ export default function Relationship({ data }) {
)
}
className="relationship-path"
+ style={{ stroke: hovered ? undefined : data.color }}
fill="none"
cursor="pointer"
/>
@@ -209,7 +216,7 @@ export default function Relationship({ data }) {
<text
x={labelX}
y={labelY}
- fill={settings.mode === "dark" ? "lightgrey" : "#333"}
+ fill={data.color ?? (settings.mode === "dark" ? "lightgrey" : "#333")}
fontSize={labelFontSize}
fontWeight={500}
ref={labelRef}
@@ -224,11 +231,13 @@ export default function Relationship({ data }) {
x={cardinalityStartX}
y={cardinalityStartY}
text={cardinalityStart}
+ color={data.color}
/>
<CardinalityLabel
x={cardinalityEndX}
y={cardinalityEndY}
text={cardinalityEnd}
+ color={data.color}
/>
</>
)}
@@ -258,7 +267,7 @@ export default function Relationship({ data }) {
);
}
-function CardinalityLabel({ x, y, text, r = 12, padding = 14 }) {
+function CardinalityLabel({ x, y, text, color, r = 12, padding = 14 }) {
const [textWidth, setTextWidth] = useState(0);
const textRef = useRef(null);
@@ -278,7 +287,7 @@ function CardinalityLabel({ x, y, text, r = 12, padding = 14 }) {
ry={r}
width={textWidth + padding}
height={r * 2}
- fill="grey"
+ fill={color ?? "grey"}
className="group-hover:fill-sky-600"
/>
<text
diff --git a/src/components/EditorSidePanel/RelationshipsTab/RelationshipInfo.jsx b/src/components/EditorSidePanel/RelationshipsTab/RelationshipInfo.jsx
index 095c92bf..9d679261 100644
--- a/src/components/EditorSidePanel/RelationshipsTab/RelationshipInfo.jsx
+++ b/src/components/EditorSidePanel/RelationshipsTab/RelationshipInfo.jsx
@@ -17,11 +17,13 @@ import {
Constraint,
Action,
ObjectType,
+ defaultRelationshipColor,
} from "../../../data/constants";
import { useDiagram, useLayout, useUndoRedo } from "../../../hooks";
import { getRelationshipFields } from "../../../utils/utils";
+import ColorPicker from "../ColorPicker";
import { useTranslation } from "react-i18next";
-import { useMemo, useState } from "react";
+import { useMemo, useRef, useState } from "react";
export default function RelationshipInfo({ data }) {
const { setUndoStack, setRedoStack } = useUndoRedo();
@@ -29,6 +31,43 @@ export default function RelationshipInfo({ data }) {
const { t } = useTranslation();
const { layout } = useLayout();
const [editField, setEditField] = useState({});
+ const initialColorRef = useRef(data.color ?? defaultRelationshipColor);
+
+ const handleColorPick = (color) => {
+ if (layout.readOnly) return;
+ setUndoStack((prev) => {
+ let undoColor = initialColorRef.current;
+ const lastColorChange = prev.findLast(
+ (e) =>
+ e.element === ObjectType.RELATIONSHIP &&
+ e.rid === data.id &&
+ e.action === Action.EDIT &&
+ e.redo?.color,
+ );
+ if (lastColorChange) {
+ undoColor = lastColorChange.redo.color;
+ }
+
+ if (color === undoColor) return prev;
+
+ return [
+ ...prev,
+ {
+ action: Action.EDIT,
+ element: ObjectType.RELATIONSHIP,
+ component: "self",
+ rid: data.id,
+ undo: { color: undoColor },
+ redo: { color },
+ message: t("edit_relationship", {
+ refName: data.name,
+ extra: "[color]",
+ }),
+ },
+ ];
+ });
+ setRedoStack([]);
+ };
const startTable = useMemo(
() => tables.find((tb) => tb.id === data.startTableId),
@@ -240,14 +279,6 @@ export default function RelationshipInfo({ data }) {
<span className="font-semibold">{t("foreign")}: </span>
{startTableName}
</div>
- <Button
- icon={<i className="bi bi-arrow-left-right" />}
- type="tertiary"
- size="small"
- onClick={swapKeys}
- disabled={layout.readOnly}
- title={t("swap")}
- />
</div>
<div className="font-semibold my-1">{t("cardinality")}:</div>
@@ -387,15 +418,31 @@ export default function RelationshipInfo({ data }) {
</Collapse>
</Card>
- <Button
- block
- type="danger"
- disabled={layout.readOnly}
- icon={<IconDeleteStroked />}
- onClick={() => deleteRelationship(data.id)}
- >
- {t("delete")}
- </Button>
+ <div className="flex justify-between items-center gap-1 mt-2 mb-2">
+ <ColorPicker
+ usePopover={true}
+ readOnly={layout.readOnly}
+ value={data.color ?? defaultRelationshipColor}
+ onChange={(color) => updateRelationship(data.id, { color })}
+ onColorPick={(color) => handleColorPick(color)}
+ />
+ <div className="flex gap-1">
+ <Button
+ icon={<i className="bi bi-arrow-left-right" />}
+ type="tertiary"
+ onClick={swapKeys}
+ disabled={layout.readOnly}
+ title={t("swap")}
+ />
+ <Button
+ type="danger"
+ disabled={layout.readOnly}
+ icon={<IconDeleteStroked />}
+ onClick={() => deleteRelationship(data.id)}
+ title={t("delete")}
+ />
+ </div>
+ </div>
</>
);
}
diff --git a/src/data/constants.js b/src/data/constants.js
index 67e679a1..81b51a91 100644
--- a/src/data/constants.js
+++ b/src/data/constants.js
@@ -1,5 +1,6 @@
export const defaultBlue = "#175e7a";
export const defaultNoteTheme = "#fcf7ac";
+export const defaultRelationshipColor = "#808080";
export const noteWidth = 180;
export const noteRadius = 3;
export const noteFold = 24;