Skip to content

Commit 2f1adf3

Browse files
committed
Add color picker for relationships
Issue : #651
1 parent 6aa8eb1 commit 2f1adf3

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/components/EditorCanvas/Canvas.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
gridSize,
99
gridCircleRadius,
1010
minAreaSize,
11+
defaultBlue,
1112
} from "../../data/constants";
1213
import { Toast } from "@douyinfe/semi-ui";
1314
import Table from "./Table";
@@ -624,6 +625,7 @@ export default function Canvas() {
624625
deleteConstraint: Constraint.NONE,
625626
name: `fk_${startTableName}_${startField.name}_${endTableName}`,
626627
id: nanoid(),
628+
color: defaultBlue
627629
};
628630
delete newRelationship.startX;
629631
delete newRelationship.startY;

src/components/EditorCanvas/Relationship.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export default function Relationship({ data }) {
127127
className="relationship-path"
128128
fill="none"
129129
cursor="pointer"
130+
style={{ stroke: data.color }}
130131
/>
131132
{settings.showRelationshipLabels && (
132133
<text

src/components/EditorSidePanel/RelationshipsTab/RelationshipInfo.jsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {
2121
import { useDiagram, useLayout, useUndoRedo } from "../../../hooks";
2222
import i18n from "../../../i18n/i18n";
2323
import { useTranslation } from "react-i18next";
24-
import { useMemo, useState } from "react";
24+
import { useMemo, useRef, useState } from "react";
25+
import ColorPicker from "../ColorPicker.jsx";
2526

2627
const columns = [
2728
{
@@ -40,6 +41,8 @@ export default function RelationshipInfo({ data }) {
4041
const { t } = useTranslation();
4142
const { layout } = useLayout();
4243
const [editField, setEditField] = useState({});
44+
const initialColorRef = useRef(data.color);
45+
4346

4447
const relValues = useMemo(() => {
4548
const { fields: startTableFields, name: startTableName } = tables.find(
@@ -141,6 +144,41 @@ export default function RelationshipInfo({ data }) {
141144
updateRelationship(data.id, { [undoKey]: value });
142145
};
143146

147+
const handleColorPick = (color) => {
148+
setUndoStack((prev) => {
149+
let undoColor = initialColorRef.current;
150+
const lastColorChange = prev.findLast(
151+
(e) =>
152+
e.element === ObjectType.RELATIONSHIP &&
153+
e.rid === data.id &&
154+
e.action === Action.EDIT &&
155+
e.redo?.color,
156+
);
157+
if (lastColorChange) {
158+
undoColor = lastColorChange.redo.color;
159+
}
160+
161+
if (color === undoColor) return prev;
162+
163+
const newStack = [
164+
...prev,
165+
{
166+
action: Action.EDIT,
167+
element: ObjectType.RELATIONSHIP,
168+
rid: data.id,
169+
undo: { color: undoColor },
170+
redo: { color: color },
171+
message: t("edit_relationship", {
172+
refName: data.name,
173+
extra: "[color]",
174+
}),
175+
},
176+
];
177+
return newStack;
178+
});
179+
setRedoStack([]);
180+
};
181+
144182
return (
145183
<>
146184
<div className="flex items-center mb-2.5">
@@ -173,6 +211,15 @@ export default function RelationshipInfo({ data }) {
173211
setRedoStack([]);
174212
}}
175213
/>
214+
<div className="ms-2">
215+
<ColorPicker
216+
usePopover={true}
217+
readOnly={layout.readOnly}
218+
value={data.color}
219+
onChange={(color) => updateRelationship(data.id, { color })}
220+
onColorPick={(color) => handleColorPick(color)}
221+
/>
222+
</div>
176223
</div>
177224
<div className="flex justify-between items-center mb-3">
178225
<div className="me-3">

0 commit comments

Comments
 (0)