Skip to content

Commit 0d0c634

Browse files
committed
Add editing relationship names(#314)
1 parent 319f3f5 commit 0d0c634

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/components/EditorHeader/ControlPanel.jsx

+3-6
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default function ControlPanel({
105105
setRelationships,
106106
addRelationship,
107107
deleteRelationship,
108+
updateRelationship,
108109
database,
109110
} = useDiagram();
110111
const { enums, setEnums, deleteEnum, addEnum, updateEnum } = useEnums();
@@ -275,9 +276,7 @@ export default function ControlPanel({
275276
updateTable(a.tid, a.undo);
276277
}
277278
} else if (a.element === ObjectType.RELATIONSHIP) {
278-
setRelationships((prev) =>
279-
prev.map((e, idx) => (idx === a.rid ? { ...e, ...a.undo } : e)),
280-
);
279+
updateRelationship(a.rid, a.undo);
281280
} else if (a.element === ObjectType.TYPE) {
282281
if (a.component === "field_add") {
283282
updateType(a.tid, {
@@ -455,9 +454,7 @@ export default function ControlPanel({
455454
updateTable(a.tid, a.redo, false);
456455
}
457456
} else if (a.element === ObjectType.RELATIONSHIP) {
458-
setRelationships((prev) =>
459-
prev.map((e, idx) => (idx === a.rid ? { ...e, ...a.redo } : e)),
460-
);
457+
updateRelationship(a.rid, a.redo);
461458
} else if (a.element === ObjectType.TYPE) {
462459
if (a.component === "field_add") {
463460
updateType(a.tid, {

src/components/EditorSidePanel/RelationshipsTab/RelationshipInfo.jsx

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Row, Col, Select, Button, Popover, Table } from "@douyinfe/semi-ui";
1+
import {
2+
Row,
3+
Col,
4+
Select,
5+
Button,
6+
Popover,
7+
Table,
8+
Input,
9+
} from "@douyinfe/semi-ui";
210
import {
311
IconDeleteStroked,
412
IconLoopTextStroked,
@@ -13,6 +21,7 @@ import {
1321
import { useDiagram, useUndoRedo } from "../../../hooks";
1422
import i18n from "../../../i18n/i18n";
1523
import { useTranslation } from "react-i18next";
24+
import { useState } from "react";
1625

1726
const columns = [
1827
{
@@ -27,8 +36,10 @@ const columns = [
2736

2837
export default function RelationshipInfo({ data }) {
2938
const { setUndoStack, setRedoStack } = useUndoRedo();
30-
const { tables, setRelationships, deleteRelationship } = useDiagram();
39+
const { tables, setRelationships, deleteRelationship, updateRelationship } =
40+
useDiagram();
3141
const { t } = useTranslation();
42+
const [editField, setEditField] = useState({});
3243

3344
const swapKeys = () => {
3445
setUndoStack((prev) => [
@@ -121,6 +132,36 @@ export default function RelationshipInfo({ data }) {
121132

122133
return (
123134
<>
135+
<div className="flex items-center mb-2.5">
136+
<div className="text-md font-semibold break-keep">{t("name")}: </div>
137+
<Input
138+
value={data.name}
139+
validateStatus={data.name.trim() === "" ? "error" : "default"}
140+
placeholder={t("name")}
141+
className="ms-2"
142+
onChange={(value) => updateRelationship(data.id, { name: value })}
143+
onFocus={(e) => setEditField({ name: e.target.value })}
144+
onBlur={(e) => {
145+
if (e.target.value === editField.name) return;
146+
setUndoStack((prev) => [
147+
...prev,
148+
{
149+
action: Action.EDIT,
150+
element: ObjectType.RELATIONSHIP,
151+
component: "self",
152+
rid: data.id,
153+
undo: editField,
154+
redo: { name: e.target.value },
155+
message: t("edit_relationship", {
156+
refName: e.target.value,
157+
extra: "[name]",
158+
}),
159+
},
160+
]);
161+
setRedoStack([]);
162+
}}
163+
/>
164+
</div>
124165
<div className="flex justify-between items-center mb-3">
125166
<div className="me-3">
126167
<span className="font-semibold">{t("primary")}: </span>

src/context/DiagramContext.jsx

+7
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ export default function DiagramContextProvider({ children }) {
246246
);
247247
};
248248

249+
const updateRelationship = (id, updatedValues) => {
250+
setRelationships((prev) =>
251+
prev.map((t) => (t.id === id ? { ...t, ...updatedValues } : t)),
252+
);
253+
};
254+
249255
return (
250256
<DiagramContext.Provider
251257
value={{
@@ -260,6 +266,7 @@ export default function DiagramContextProvider({ children }) {
260266
setRelationships,
261267
addRelationship,
262268
deleteRelationship,
269+
updateRelationship,
263270
database,
264271
setDatabase,
265272
tablesCount: tables.length,

0 commit comments

Comments
 (0)