Skip to content

Commit

Permalink
Recover relationships after undoing 'field_delete'
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed May 31, 2024
1 parent 9c82066 commit 679f6b0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 63 deletions.
56 changes: 29 additions & 27 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 @@ -166,27 +167,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 @@ -334,28 +357,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
86 changes: 50 additions & 36 deletions src/context/TablesContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,49 +130,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 679f6b0

Please sign in to comment.