Skip to content

Commit

Permalink
Fix recovering relationships after delete operations (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed May 31, 2024
2 parents 1768c39 + 3988b8d commit 44d5f7b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/components/EditorCanvas/Canvas.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export default function Canvas() {
tables[linkingLine.startTableId].fields[linkingLine.startFieldId].type !==
tables[hoveredTable.tableId].fields[hoveredTable.field].type
) {
Toast.info(t("connot_connect"));
Toast.info(t("cannot_connect"));
return;
}
if (
Expand Down
61 changes: 32 additions & 29 deletions src/components/EditorHeader/ControlPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export default function ControlPanel({
setTables,
addTable,
updateTable,
deleteField,
deleteTable,
updateField,
setRelationships,
Expand Down Expand Up @@ -145,7 +146,8 @@ export default function ControlPanel({
}
} else if (a.action === Action.DELETE) {
if (a.element === ObjectType.TABLE) {
addTable(a.data, false);
a.data.relationship.forEach((x) => addRelationship(x, false));
addTable(a.data.table, false);
} else if (a.element === ObjectType.RELATIONSHIP) {
addRelationship(a.data, false);
} else if (a.element === ObjectType.NOTE) {
Expand All @@ -166,27 +168,49 @@ export default function ControlPanel({
updateField(a.tid, a.fid, a.undo);
} else if (a.component === "field_delete") {
setRelationships((prev) => {
return prev.map((e) => {
if (e.startTableId === a.tid && e.startFieldId >= a.data.id) {
let temp = [...prev];
a.data.relationship.forEach((r) => {
temp.splice(r.id, 0, r);
});
temp = temp.map((e, i) => {
const recoveredRel = a.data.relationship.find(
(x) =>
(x.startTableId === e.startTableId &&
x.startFieldId === e.startFieldId) ||
(x.endTableId === e.endTableId &&
x.endFieldId === a.endFieldId),
);
if (
e.startTableId === a.tid &&
e.startFieldId >= a.data.field.id &&
!recoveredRel
) {
return {
...e,
id: i,
startFieldId: e.startFieldId + 1,
};
}
if (e.endTableId === a.tid && e.endFieldId >= a.data.id) {
if (
e.endTableId === a.tid &&
e.endFieldId >= a.data.field.id &&
!recoveredRel
) {
return {
...e,
id: i,
endFieldId: e.endFieldId + 1,
};
}
return e;
return { ...e, id: i };
});
return temp;
});
setTables((prev) =>
prev.map((t) => {
if (t.id === a.tid) {
const temp = t.fields.slice();
temp.splice(a.data.id, 0, a.data);
temp.splice(a.data.field.id, 0, a.data.field);
return { ...t, fields: temp.map((t, i) => ({ ...t, id: i })) };
}
return t;
Expand Down Expand Up @@ -314,7 +338,7 @@ export default function ControlPanel({
}
} else if (a.action === Action.DELETE) {
if (a.element === ObjectType.TABLE) {
deleteTable(a.data.id, false);
deleteTable(a.data.table.id, false);
} else if (a.element === ObjectType.RELATIONSHIP) {
deleteRelationship(a.data.id, false);
} else if (a.element === ObjectType.NOTE) {
Expand All @@ -334,28 +358,7 @@ export default function ControlPanel({
if (a.component === "field") {
updateField(a.tid, a.fid, a.redo);
} else if (a.component === "field_delete") {
setRelationships((prev) => {
return prev.map((e) => {
if (e.startTableId === a.tid && e.startFieldId > a.data.id) {
return {
...e,
startFieldId: e.startFieldId - 1,
};
}
if (e.endTableId === a.tid && e.endFieldId > a.data.id) {
return {
...e,
endFieldId: e.endFieldId - 1,
};
}
return e;
});
});
updateTable(a.tid, {
fields: tables[a.tid].fields
.filter((field) => field.id !== a.data.id)
.map((e, i) => ({ ...e, id: i })),
});
deleteField(a.data.field, a.tid, false);
} else if (a.component === "field_add") {
updateTable(a.tid, {
fields: [
Expand Down
94 changes: 57 additions & 37 deletions src/context/TablesContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ export default function TablesContextProvider({ children }) {
const deleteTable = (id, addToHistory = true) => {
if (addToHistory) {
Toast.success(t("table_deleted"));
const rels = relationships.reduce((acc, r) => {
if (r.startTableId === id || r.endTableId === id) {
acc.push(r);
}
return acc;
}, []);
setUndoStack((prev) => [
...prev,
{
action: Action.DELETE,
element: ObjectType.TABLE,
data: tables[id],
data: { table: tables[id], relationship: rels },
message: t("delete_table", { tableName: tables[id] }),
},
]);
Expand Down Expand Up @@ -130,49 +136,63 @@ export default function TablesContextProvider({ children }) {
);
};

const deleteField = (field, tid) => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field_delete",
tid: tid,
data: field,
message: t("edit_table", {
tableName: tables[tid].name,
extra: "[delete field]",
}),
},
]);
setRedoStack([]);
setRelationships((prev) =>
prev
const deleteField = (field, tid, addToHistory = true) => {
if (addToHistory) {
const rels = relationships.reduce((acc, r) => {
if (
(r.startTableId === tid && r.startFieldId === field.id) ||
(r.endTableId === tid && r.endFieldId === field.id)
) {
acc.push(r);
}
return acc;
}, []);
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field_delete",
tid: tid,
data: {
field: field,
relationship: rels,
},
message: t("edit_table", {
tableName: tables[tid].name,
extra: "[delete field]",
}),
},
]);
setRedoStack([]);
}
setRelationships((prev) => {
const temp = prev
.filter(
(e) =>
!(
(e.startTableId === tid && e.startFieldId === field.id) ||
(e.endTableId === tid && e.endFieldId === field.id)
),
)
.map((e, i) => ({ ...e, id: i })),
);
setRelationships((prev) => {
return prev.map((e) => {
if (e.startTableId === tid && e.startFieldId > field.id) {
return {
...e,
startFieldId: e.startFieldId - 1,
};
}
if (e.endTableId === tid && e.endFieldId > field.id) {
return {
...e,
endFieldId: e.endFieldId - 1,
};
}
return e;
});
.map((e, i) => {
if (e.startTableId === tid && e.startFieldId > field.id) {
return {
...e,
startFieldId: e.startFieldId - 1,
id: i,
};
}
if (e.endTableId === tid && e.endFieldId > field.id) {
return {
...e,
endFieldId: e.endFieldId - 1,
id: i,
};
}
return { ...e, id: i };
});
return temp;
});
updateTable(tid, {
fields: tables[tid].fields
Expand Down

0 comments on commit 44d5f7b

Please sign in to comment.