Skip to content

Commit 7436b60

Browse files
authored
Merge pull request #54 from HaecheonLee/fea/make-table-fields-draggable-and-order
Make table fields draggable and order
2 parents f57ff74 + 0347dbf commit 7436b60

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/components/EditorSidePanel/TablesTab/TableInfo.jsx

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react";
1+
import { useRef, useState } from "react";
22
import {
33
Collapse,
44
Row,
@@ -19,9 +19,11 @@ import IndexDetails from "./IndexDetails";
1919

2020
export default function TableInfo({ data }) {
2121
const [indexActiveKey, setIndexActiveKey] = useState("");
22-
const { deleteTable, updateTable } = useTables();
22+
const { deleteTable, updateTable, updateField } = useTables();
2323
const { setUndoStack, setRedoStack } = useUndoRedo();
2424
const [editField, setEditField] = useState({});
25+
const draggingElementIndex = useRef();
26+
const isDragging = useRef();
2527

2628
return (
2729
<div>
@@ -53,7 +55,38 @@ export default function TableInfo({ data }) {
5355
/>
5456
</div>
5557
{data.fields.map((f, j) => (
56-
<TableField key={"field_" + j} data={f} tid={data.id} index={j} />
58+
<div
59+
key={"field_" + j}
60+
className="cursor-pointer"
61+
draggable
62+
onDragOver={(e) => {
63+
e.preventDefault();
64+
if (isDragging.current) return;
65+
isDragging.current = true;
66+
draggingElementIndex.current = j;
67+
}}
68+
onDrop={(e) => {
69+
e.preventDefault();
70+
isDragging.current = false;
71+
const index = draggingElementIndex.current;
72+
73+
if (index == null || index === j) {
74+
return;
75+
}
76+
77+
const a = data.fields[index];
78+
const b = data.fields[j];
79+
80+
updateField(data.id, index, { ...b, id: index });
81+
updateField(data.id, j, { ...a, id: j });
82+
}}
83+
onDragEnd={(e) => {
84+
e.preventDefault();
85+
isDragging.current = false;
86+
}}
87+
>
88+
<TableField data={f} tid={data.id} index={j} />
89+
</div>
5790
))}
5891
{data.indices.length > 0 && (
5992
<Card

0 commit comments

Comments
 (0)