Skip to content

Commit f32c999

Browse files
committed
Add type field ids
1 parent 57c8b65 commit f32c999

File tree

5 files changed

+79
-29
lines changed

5 files changed

+79
-29
lines changed

src/components/EditorHeader/ControlPanel.jsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,12 @@ export default function ControlPanel({
258258
updateRelationship(a.rid, a.undo);
259259
} else if (a.element === ObjectType.TYPE) {
260260
if (a.component === "field_add") {
261+
const type = types.find((t, i) =>
262+
typeof a.tid === "number" ? i === a.tid : t.id === a.tid,
263+
);
261264
updateType(a.tid, {
262-
fields: types[a.tid].fields.filter(
263-
(_, i) => i !== types[a.tid].fields.length - 1,
265+
fields: type.fields.filter((f, i) =>
266+
f.id ? f.id !== a.data.field.id : i !== type.fields.length - 1,
264267
),
265268
});
266269
}
@@ -436,14 +439,11 @@ export default function ControlPanel({
436439
updateRelationship(a.rid, a.redo);
437440
} else if (a.element === ObjectType.TYPE) {
438441
if (a.component === "field_add") {
442+
const type = types.find((t, i) =>
443+
typeof a.tid === "number" ? i === a.tid : t.id === a.tid,
444+
);
439445
updateType(a.tid, {
440-
fields: [
441-
...types[a.tid].fields,
442-
{
443-
name: "",
444-
type: "",
445-
},
446-
],
446+
fields: [...type.fields, a.data.field],
447447
});
448448
} else if (a.component === "field") {
449449
updateType(a.tid, {

src/components/EditorSidePanel/TypesTab/TypeInfo.jsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { IconDeleteStroked, IconPlus } from "@douyinfe/semi-icons";
1313
import { useUndoRedo, useTypes, useDiagram, useLayout } from "../../../hooks";
1414
import TypeField from "./TypeField";
1515
import { useTranslation } from "react-i18next";
16+
import { nanoid } from "nanoid";
1617

1718
export default function TypeInfo({ index, data }) {
1819
const { layout } = useLayout();
@@ -22,8 +23,11 @@ export default function TypeInfo({ index, data }) {
2223
const [editField, setEditField] = useState({});
2324
const { t } = useTranslation();
2425

26+
// TODO: remove indexes, not a valid case after adding id to types
27+
const typeId = data.id ?? index;
28+
2529
return (
26-
<div id={`scroll_type_${index}`}>
30+
<div id={`scroll_type_${typeId}`}>
2731
<Collapse.Panel
2832
header={
2933
<div className="overflow-hidden text-ellipsis whitespace-nowrap">
@@ -41,7 +45,7 @@ export default function TypeInfo({ index, data }) {
4145
placeholder={t("name")}
4246
className="ms-2"
4347
onChange={(value) => {
44-
updateType(index, { name: value });
48+
updateType(typeId, { name: value });
4549
tables.forEach((table) => {
4650
table.fields.forEach((field) => {
4751
if (field.type.toLowerCase() === data.name.toLowerCase()) {
@@ -71,7 +75,7 @@ export default function TypeInfo({ index, data }) {
7175
action: Action.EDIT,
7276
element: ObjectType.TYPE,
7377
component: "self",
74-
tid: index,
78+
tid: typeId,
7579
undo: editField,
7680
redo: { name: e.target.value },
7781
updatedFields,
@@ -103,7 +107,7 @@ export default function TypeInfo({ index, data }) {
103107
placeholder={t("comment")}
104108
rows={1}
105109
onChange={(value) =>
106-
updateType(index, { comment: value }, false)
110+
updateType(typeId, { comment: value }, false)
107111
}
108112
onFocus={(e) => setEditField({ comment: e.target.value })}
109113
onBlur={(e) => {
@@ -114,7 +118,7 @@ export default function TypeInfo({ index, data }) {
114118
action: Action.EDIT,
115119
element: ObjectType.TYPE,
116120
component: "self",
117-
tid: index,
121+
tid: typeId,
118122
undo: editField,
119123
redo: { comment: e.target.value },
120124
message: t("edit_type", {
@@ -135,28 +139,31 @@ export default function TypeInfo({ index, data }) {
135139
icon={<IconPlus />}
136140
disabled={layout.readOnly}
137141
onClick={() => {
142+
const newField = {
143+
name: "",
144+
type: "",
145+
id: nanoid(),
146+
};
138147
setUndoStack((prev) => [
139148
...prev,
140149
{
141150
action: Action.EDIT,
142151
element: ObjectType.TYPE,
143152
component: "field_add",
144-
tid: index,
153+
data: {
154+
field: newField,
155+
index: data.fields.length,
156+
},
157+
tid: typeId,
145158
message: t("edit_type", {
146159
typeName: data.name,
147160
extra: "[add field]",
148161
}),
149162
},
150163
]);
151164
setRedoStack([]);
152-
updateType(index, {
153-
fields: [
154-
...data.fields,
155-
{
156-
name: "",
157-
type: "",
158-
},
159-
],
165+
updateType(typeId, {
166+
fields: [...data.fields, newField],
160167
});
161168
}}
162169
block
@@ -170,7 +177,7 @@ export default function TypeInfo({ index, data }) {
170177
type="danger"
171178
disabled={layout.readOnly}
172179
icon={<IconDeleteStroked />}
173-
onClick={() => deleteType(index)}
180+
onClick={() => deleteType(typeId)}
174181
>
175182
{t("delete")}
176183
</Button>

src/components/Workspace.jsx

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,17 @@ export default function WorkSpace() {
199199
if (databases[database].hasTypes) {
200200
if (d.types) {
201201
setTypes(
202-
d.types.map((t) => (t.id ? t : { ...t, id: nanoid() })),
202+
d.types.map((t) =>
203+
t.id
204+
? t
205+
: {
206+
...t,
207+
id: nanoid(),
208+
fields: t.fields.map((f) =>
209+
f.id ? f : { ...f, id: nanoid() },
210+
),
211+
},
212+
),
203213
);
204214
} else {
205215
setTypes([]);
@@ -247,7 +257,17 @@ export default function WorkSpace() {
247257
if (databases[database].hasTypes) {
248258
if (diagram.types) {
249259
setTypes(
250-
diagram.types.map((t) => (t.id ? t : { ...t, id: nanoid() })),
260+
diagram.types.map((t) =>
261+
t.id
262+
? t
263+
: {
264+
...t,
265+
id: nanoid(),
266+
fields: t.fields.map((f) =>
267+
f.id ? f : { ...f, id: nanoid() },
268+
),
269+
},
270+
),
251271
);
252272
} else {
253273
setTypes([]);
@@ -292,7 +312,17 @@ export default function WorkSpace() {
292312
if (databases[database].hasTypes) {
293313
if (diagram.types) {
294314
setTypes(
295-
diagram.types.map((t) => (t.id ? t : { ...t, id: nanoid() })),
315+
diagram.types.map((t) =>
316+
t.id
317+
? t
318+
: {
319+
...t,
320+
id: nanoid(),
321+
fields: t.fields.map((f) =>
322+
f.id ? f : { ...f, id: nanoid() },
323+
),
324+
},
325+
),
296326
);
297327
} else {
298328
setTypes([]);
@@ -330,7 +360,15 @@ export default function WorkSpace() {
330360
if (parsedDiagram.types) {
331361
setTypes(
332362
parsedDiagram.types.map((t) =>
333-
t.id ? t : { ...t, id: nanoid() },
363+
t.id
364+
? t
365+
: {
366+
...t,
367+
id: nanoid(),
368+
fields: t.fields.map((f) =>
369+
f.id ? f : { ...f, id: nanoid() },
370+
),
371+
},
334372
),
335373
);
336374
} else {

src/context/TypesContext.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ export default function TypesContextProvider({ children }) {
7979

8080
const updateType = (id, values) => {
8181
setTypes((prev) =>
82-
prev.map((e, i) => (i === id ? { ...e, ...values } : e)),
82+
prev.map((item, index) => {
83+
const isMatch = typeof id === "number" ? index === id : item.id === id;
84+
85+
return isMatch ? { ...item, ...values } : item;
86+
}),
8387
);
8488
};
8589

src/data/schemas.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export const noteSchema = {
9797
export const typeSchema = {
9898
type: "object",
9999
properties: {
100+
id: { type: ["string"] },
100101
name: { type: "string" },
101102
fields: {
102103
type: "array",

0 commit comments

Comments
 (0)