drawDB/src/components/Table.jsx

1367 lines
54 KiB
React
Raw Normal View History

2023-12-16 11:39:13 +08:00
import { useState, useContext } from "react";
2023-09-19 20:48:57 +08:00
import {
sqlDataTypes,
tableThemes,
defaultTableTheme,
Tab,
2023-09-19 20:50:16 +08:00
Action,
ObjectType,
2023-09-19 20:48:57 +08:00
} from "../data/data";
2023-09-19 20:48:53 +08:00
import {
IconEdit,
IconMore,
IconMinus,
IconDeleteStroked,
IconKeyStroked,
IconCheckboxTick,
} from "@douyinfe/semi-icons";
2023-09-19 20:47:18 +08:00
import {
2023-09-19 20:48:53 +08:00
Select,
2023-09-19 20:50:16 +08:00
Input,
TextArea,
2023-09-19 20:48:53 +08:00
Card,
Checkbox,
2023-09-19 20:51:11 +08:00
InputNumber,
2023-09-19 20:50:43 +08:00
TagInput,
2023-09-19 20:48:53 +08:00
Row,
Col,
2023-09-19 20:47:28 +08:00
Popover,
Tag,
2023-09-19 20:48:44 +08:00
Button,
2023-09-19 20:48:46 +08:00
SideSheet,
2023-09-19 20:48:53 +08:00
Toast,
2023-09-19 20:47:28 +08:00
} from "@douyinfe/semi-ui";
2023-09-19 20:49:16 +08:00
import {
LayoutContext,
2023-09-19 20:50:28 +08:00
SelectContext,
2023-09-19 20:49:16 +08:00
SettingsContext,
TabContext,
TableContext,
2023-09-19 20:51:34 +08:00
TypeContext,
2023-09-19 20:50:16 +08:00
UndoRedoContext,
} from "../pages/Editor";
2023-09-19 20:51:25 +08:00
import { getSize, hasCheck, hasPrecision, isSized } from "../utils";
2023-09-19 20:47:06 +08:00
2023-09-19 20:47:43 +08:00
export default function Table(props) {
2023-09-19 20:47:11 +08:00
const [isHovered, setIsHovered] = useState(false);
2023-09-19 20:47:18 +08:00
const [hoveredField, setHoveredField] = useState(-1);
2023-09-19 20:50:28 +08:00
const [editField, setEditField] = useState({});
2023-09-19 20:48:53 +08:00
const { layout } = useContext(LayoutContext);
2023-09-19 20:50:49 +08:00
const { deleteTable, updateTable, updateField, setRelationships } =
useContext(TableContext);
2023-09-19 20:48:57 +08:00
const { tab, setTab } = useContext(TabContext);
2023-09-19 20:49:16 +08:00
const { settings } = useContext(SettingsContext);
2023-09-19 20:51:34 +08:00
const { types } = useContext(TypeContext);
2023-09-19 20:50:28 +08:00
const { setUndoStack, setRedoStack } = useContext(UndoRedoContext);
const { selectedElement, setSelectedElement } = useContext(SelectContext);
2023-09-19 20:47:17 +08:00
2023-09-19 20:50:28 +08:00
const height = props.tableData.fields.length * 36 + 50 + 7;
2023-09-19 20:47:31 +08:00
2023-09-19 20:47:06 +08:00
return (
2023-09-19 20:50:16 +08:00
<>
2023-09-19 20:47:10 +08:00
<foreignObject
2023-09-19 20:48:48 +08:00
key={props.tableData.id}
x={props.tableData.x}
y={props.tableData.y}
2023-09-19 20:48:41 +08:00
width={200}
2023-09-19 20:47:17 +08:00
height={height}
2023-09-19 20:50:28 +08:00
className="drop-shadow-lg rounded-md cursor-move"
2023-09-19 20:47:06 +08:00
onMouseDown={props.onMouseDown}
2023-10-23 22:39:05 +08:00
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
2023-09-19 20:47:10 +08:00
>
2023-09-19 20:47:11 +08:00
<div
className={`border-2 ${
isHovered
2023-09-19 20:51:11 +08:00
? "border-dashed border-blue-500"
: selectedElement.element === ObjectType.TABLE &&
selectedElement.id === props.tableData.id
? "border-blue-500"
: "border-slate-400"
} select-none rounded-lg w-full ${
settings.mode === "light"
2023-09-19 20:51:08 +08:00
? "bg-zinc-100 text-zinc-800"
: "bg-zinc-800 text-zinc-200"
}`}
2023-09-19 20:47:11 +08:00
>
2023-09-19 20:47:54 +08:00
<div
2023-09-19 20:48:44 +08:00
className={`h-[10px] w-full rounded-t-md`}
style={{ backgroundColor: props.tableData.color }}
/>
2023-09-19 20:51:08 +08:00
<div
className={`font-bold h-[40px] flex justify-between items-center border-b border-gray-400 ${
settings.mode === "light" ? "bg-zinc-200" : "bg-zinc-900"
}`}
2023-09-19 20:51:08 +08:00
>
2023-09-19 20:50:49 +08:00
<div className="px-3">
{isHovered
2024-02-29 04:47:07 +08:00
? props.tableData.name.length <= 10
2023-09-19 20:50:49 +08:00
? props.tableData.name
: `${props.tableData.name.substring(0, 10)}...`
2024-02-29 04:47:07 +08:00
: props.tableData.name.length <= 18
? props.tableData.name
2024-02-29 04:47:07 +08:00
: `${props.tableData.name.substring(0, 19)}...`}
2023-09-19 20:50:49 +08:00
</div>
2023-09-19 20:47:18 +08:00
{isHovered && (
2023-09-19 20:48:44 +08:00
<div className="flex justify-end items-center mx-2">
<Button
icon={<IconEdit />}
size="small"
theme="solid"
style={{
backgroundColor: "#2f68ad",
opacity: "0.7",
marginRight: "6px",
}}
2023-09-19 20:48:53 +08:00
onClick={() => {
if (!layout.sidebar) {
2023-09-19 20:50:28 +08:00
setSelectedElement({
element: ObjectType.TABLE,
id: props.tableData.id,
openDialogue: true,
openCollapse: false,
});
2023-09-19 20:48:53 +08:00
} else {
2023-09-19 20:48:57 +08:00
setTab(Tab.tables);
2023-09-19 20:50:28 +08:00
setSelectedElement({
element: ObjectType.TABLE,
id: props.tableData.id,
openDialogue: false,
openCollapse: true,
});
2023-09-19 20:48:59 +08:00
if (tab !== Tab.tables) return;
2023-09-19 20:48:53 +08:00
document
.getElementById(`scroll_table_${props.tableData.id}`)
.scrollIntoView({ behavior: "smooth" });
}
}}
2023-09-19 20:48:44 +08:00
></Button>
<Popover
content={
2023-09-19 20:51:08 +08:00
<div className="popover-theme">
2023-09-19 20:48:55 +08:00
<div className="mb-2">
2023-09-19 20:48:44 +08:00
<strong>Comment :</strong>{" "}
2023-09-19 20:48:53 +08:00
{props.tableData.comment === "" ? (
"No comment"
) : (
<div>{props.tableData.comment}</div>
)}
2023-09-19 20:48:55 +08:00
</div>
2023-09-19 20:51:08 +08:00
<div>
2023-09-19 20:48:44 +08:00
<strong
className={`${
props.tableData.indices.length === 0 ? "" : "block"
}`}
2023-09-19 20:48:44 +08:00
>
Indices :
</strong>{" "}
{props.tableData.indices.length === 0 ? (
"No indices"
) : (
<div>
{props.tableData.indices.map((index, k) => (
2023-09-19 20:48:46 +08:00
<div
key={k}
className={`flex items-center my-1 px-2 py-1 rounded ${
settings.mode === "light"
2023-09-19 20:51:08 +08:00
? "bg-gray-100"
: "bg-zinc-800"
}`}
2023-09-19 20:48:46 +08:00
>
2023-09-19 20:48:44 +08:00
<i className="fa-solid fa-thumbtack me-2 mt-1 text-slate-500"></i>
<div>
{index.fields.map((f) => (
2023-09-19 20:48:46 +08:00
<Tag color="blue" key={f} className="me-1">
{f}
</Tag>
2023-09-19 20:48:44 +08:00
))}
</div>
</div>
))}
</div>
)}
2023-09-19 20:48:55 +08:00
</div>
2023-09-19 20:49:22 +08:00
<Button
icon={<IconDeleteStroked />}
type="danger"
block
style={{ marginTop: "8px" }}
onClick={() => {
Toast.success(`Table deleted!`);
2023-09-19 20:49:57 +08:00
deleteTable(props.tableData.id);
2023-09-19 20:49:22 +08:00
}}
>
Delete table
</Button>
2023-09-19 20:48:44 +08:00
</div>
}
position="rightTop"
showArrow
trigger="click"
2023-09-19 20:48:53 +08:00
style={{ width: "200px" }}
2023-09-19 20:47:26 +08:00
>
2023-09-19 20:48:44 +08:00
<Button
icon={<IconMore />}
type="tertiary"
size="small"
style={{
opacity: "0.7",
2023-12-18 07:21:33 +08:00
backgroundColor: "grey",
color: "white",
2023-09-19 20:48:44 +08:00
}}
></Button>
</Popover>
2023-09-19 20:47:18 +08:00
</div>
)}
2023-09-19 20:47:17 +08:00
</div>
{props.tableData.fields.map((e, i) => {
2023-09-19 20:49:16 +08:00
return settings.showFieldSummary ? (
2023-09-19 20:47:28 +08:00
<Popover
2023-09-19 20:47:24 +08:00
key={i}
2023-09-19 20:47:28 +08:00
content={
2023-09-19 20:51:08 +08:00
<div className="popover-theme">
2023-09-19 20:47:28 +08:00
<div className="flex justify-between items-center pb-2">
<p className="me-4 font-bold">{e.name}</p>
2023-09-19 20:51:08 +08:00
<p className="ms-4">{e.type}</p>
2023-09-19 20:47:18 +08:00
</div>
2023-09-19 20:47:28 +08:00
<hr />
{e.primary && (
<Tag color="blue" className="me-2 my-2">
Primary
</Tag>
)}
{e.unique && (
<Tag color="amber" className="me-2 my-2">
Unique
</Tag>
)}
{e.notNull && (
<Tag color="purple" className="me-2 my-2">
Not null
</Tag>
)}
{e.increment && (
<Tag color="green" className="me-2 my-2">
2023-09-19 20:47:29 +08:00
Increment
2023-09-19 20:47:28 +08:00
</Tag>
)}
2023-09-19 20:51:08 +08:00
<p>
2023-09-19 20:47:28 +08:00
<strong>Default :</strong>{" "}
{e.default === "" ? "Not set" : e.default}
</p>
2023-09-19 20:51:08 +08:00
</div>
2023-09-19 20:47:28 +08:00
}
position="right"
showArrow
>
2023-09-19 20:49:16 +08:00
{field(e, i)}
2023-09-19 20:47:28 +08:00
</Popover>
2023-09-19 20:49:16 +08:00
) : (
field(e, i)
2023-09-19 20:47:17 +08:00
);
})}
2023-09-19 20:47:10 +08:00
</div>
2023-09-19 20:47:08 +08:00
</foreignObject>
2023-09-19 20:48:46 +08:00
<SideSheet
2023-09-19 20:48:53 +08:00
title="Edit table"
2023-09-19 20:50:16 +08:00
size="small"
visible={
selectedElement.element === ObjectType.TABLE &&
selectedElement.id === props.tableData.id &&
selectedElement.openDialogue
}
2023-09-19 20:50:28 +08:00
onCancel={() =>
setSelectedElement((prev) => ({
...prev,
openDialogue: !prev.openDialogue,
}))
}
2023-09-19 20:48:53 +08:00
style={{ paddingBottom: "16px" }}
2023-09-19 20:48:46 +08:00
>
2023-09-19 20:51:08 +08:00
<div className="flex items-center sidesheet-theme">
2023-09-19 20:50:16 +08:00
<div className="text-md font-semibold">Name: </div>
<Input
value={props.tableData.name}
2023-09-19 20:51:17 +08:00
validateStatus={props.tableData.name === "" ? "error" : "default"}
2023-09-19 20:50:16 +08:00
placeholder="Name"
className="mx-2 mb-1"
onChange={(value) =>
updateTable(props.tableData.id, { name: value })
}
onFocus={(e) => setEditField({ name: e.target.value })}
onBlur={(e) => {
if (e.target.value === editField.name) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "self",
tid: props.tableData.id,
undo: editField,
redo: { name: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit table name to ${e.target.value}`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
}}
2023-09-19 20:48:53 +08:00
/>
2023-09-19 20:50:16 +08:00
</div>
2023-09-19 20:48:53 +08:00
<div>
{props.tableData.fields.map((f, j) => (
2023-09-19 20:51:08 +08:00
<Row gutter={6} key={j} className="hover-2 mt-2">
2023-09-19 20:50:16 +08:00
<Col span={7}>
<Input
value={f.name}
placeholder="Name"
onChange={(value) =>
updateField(props.tableData.id, j, { name: value })
}
onFocus={(e) => setEditField({ name: e.target.value })}
onBlur={(e) => {
if (e.target.value === editField.name) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: editField,
redo: { name: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit table field name to "${e.target.value}"`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
}}
/>
</Col>
<Col span={8}>
<Select
className="w-full"
2023-09-19 20:51:34 +08:00
optionList={[
...sqlDataTypes.map((value) => ({
2023-09-19 20:50:16 +08:00
label: value,
value: value,
2023-09-19 20:51:34 +08:00
})),
...types.map((type) => ({
label: type.name.toUpperCase(),
value: type.name.toUpperCase(),
})),
]}
2023-09-19 20:50:16 +08:00
filter
value={f.type}
2023-09-19 20:51:17 +08:00
validateStatus={f.type === "" ? "error" : "default"}
2023-09-19 20:50:16 +08:00
placeholder="Type"
onChange={(value) => {
if (value === f.type) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: { type: f.type },
redo: { type: value },
2023-09-19 20:50:52 +08:00
message: `Edit table field type to "${value}"`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
2023-09-19 20:50:43 +08:00
const incr =
f.increment &&
(value === "INT" ||
value === "BIGINT" ||
value === "SMALLINT");
if (value === "ENUM" || value === "SET") {
updateField(props.tableData.id, j, {
type: value,
default: "",
2023-09-19 20:51:26 +08:00
values: f.values ? [...f.values] : [],
increment: incr,
});
} else if (isSized(value) || hasPrecision(value)) {
updateField(props.tableData.id, j, {
type: value,
size: getSize(value),
increment: incr,
});
2023-09-19 20:51:21 +08:00
} else if (
value === "BLOB" ||
value === "JSON" ||
value === "UUID" ||
value === "TEXT" ||
2023-09-19 20:51:21 +08:00
incr
) {
updateField(props.tableData.id, j, {
type: value,
increment: incr,
default: "",
size: "",
2023-09-19 20:51:21 +08:00
values: [],
});
2023-09-19 20:51:25 +08:00
} else if (hasCheck(value)) {
updateField(props.tableData.id, j, {
type: value,
check: "",
increment: incr,
});
} else {
updateField(props.tableData.id, j, {
type: value,
increment: incr,
size: "",
values: [],
});
}
2023-09-19 20:50:16 +08:00
}}
></Select>
</Col>
<Col span={3}>
<Button
type={f.notNull ? "primary" : "tertiary"}
title="Nullable"
theme={f.notNull ? "solid" : "light"}
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: { notNull: f.notNull },
redo: { notNull: !f.notNull },
message: `Edit table field to${
f.notNull ? "" : " not"
} null`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateField(props.tableData.id, j, { notNull: !f.notNull });
}}
2023-09-19 20:48:53 +08:00
>
2023-09-19 20:50:16 +08:00
?
</Button>
</Col>
<Col span={3}>
<Button
type={f.primary ? "primary" : "tertiary"}
title="Primary"
theme={f.primary ? "solid" : "light"}
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: { primary: f.primary },
redo: { primary: !f.primary },
message: `Edit table field to${
f.primary ? " not" : ""
} primary`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateField(props.tableData.id, j, { primary: !f.primary });
}}
icon={<IconKeyStroked />}
></Button>
</Col>
<Col span={3}>
<Popover
content={
2023-09-19 20:51:08 +08:00
<div className="px-1 w-[240px] popover-theme">
2023-09-19 20:50:16 +08:00
<div className="font-semibold">Default value</div>
<Input
className="my-2"
placeholder="Set default"
value={f.default}
disabled={
f.type === "BLOB" ||
f.type === "JSON" ||
f.type === "TEXT" ||
f.type === "UUID" ||
f.increment
}
2023-09-19 20:50:16 +08:00
onChange={(value) =>
updateField(props.tableData.id, j, { default: value })
}
onFocus={(e) =>
setEditField({ default: e.target.value })
}
onBlur={(e) => {
if (e.target.value === editField.default) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: editField,
redo: { default: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit table field default to ${e.target.value}`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
}}
/>
2023-09-19 20:50:44 +08:00
{(f.type === "ENUM" || f.type === "SET") && (
2023-09-19 20:50:43 +08:00
<>
2023-09-19 20:50:44 +08:00
<div className="font-semibold mb-1">
{f.type} values
</div>
2023-09-19 20:50:43 +08:00
<TagInput
separator={[",", ", ", " ,"]}
2023-09-19 20:50:44 +08:00
value={f.values}
2023-09-19 20:51:17 +08:00
validateStatus={
!f.values || f.values.length === 0
? "error"
: "default"
}
2023-09-19 20:50:43 +08:00
className="my-2"
placeholder="Use ',' for batch input"
onChange={(v) =>
updateField(props.tableData.id, j, {
2023-09-19 20:50:44 +08:00
values: v,
2023-09-19 20:50:43 +08:00
})
}
2023-12-16 11:39:13 +08:00
onFocus={() => setEditField({ values: f.values })}
onBlur={() => {
2023-09-19 20:50:43 +08:00
if (
2023-09-19 20:50:44 +08:00
JSON.stringify(editField.values) ===
JSON.stringify(f.values)
2023-09-19 20:50:43 +08:00
)
return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: editField,
2023-09-19 20:50:44 +08:00
redo: { values: f.values },
2023-09-19 20:50:52 +08:00
message: `Edit table field values to "${JSON.stringify(
f.values
)}"`,
2023-09-19 20:50:43 +08:00
},
]);
setRedoStack([]);
}}
/>
</>
)}
{isSized(f.type) && (
2023-09-19 20:50:43 +08:00
<>
<div className="font-semibold">Size</div>
2023-09-19 20:51:11 +08:00
<InputNumber
className="my-2 w-full"
2023-09-19 20:50:43 +08:00
placeholder="Set length"
value={f.size}
onChange={(value) =>
updateField(props.tableData.id, j, {
size: value,
})
}
onFocus={(e) =>
setEditField({ size: e.target.value })
}
onBlur={(e) => {
if (e.target.value === editField.size) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: editField,
redo: { size: e.target.value },
message: `Edit table field size to ${e.target.value}`,
},
]);
setRedoStack([]);
}}
/>
</>
)}
{hasPrecision(f.type) && (
<>
<div className="font-semibold">Precision</div>
<Input
className="my-2 w-full"
placeholder="Set precision: (size, d)"
2023-09-19 20:51:17 +08:00
validateStatus={
/^\(\d+,\s*\d+\)$|^$/.test(f.size)
? "default"
: "error"
2023-09-19 20:51:17 +08:00
}
value={f.size}
2023-09-19 20:50:43 +08:00
onChange={(value) =>
updateField(props.tableData.id, j, {
size: value,
2023-09-19 20:50:43 +08:00
})
}
onFocus={(e) =>
setEditField({ size: e.target.value })
2023-09-19 20:50:43 +08:00
}
onBlur={(e) => {
if (e.target.value === editField.size) return;
2023-09-19 20:50:43 +08:00
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: editField,
redo: { size: e.target.value },
message: `Edit table field precision to ${e.target.value}`,
2023-09-19 20:50:43 +08:00
},
]);
setRedoStack([]);
}}
/>
</>
)}
2023-09-19 20:51:25 +08:00
{hasCheck(f.type) && (
<>
<div className="font-semibold">Check Expression</div>
<Input
className="my-2"
placeholder="Set constraint"
value={f.check}
disabled={f.increment}
onChange={(value) =>
updateField(props.tableData.id, j, {
check: value,
})
}
onFocus={(e) =>
setEditField({ check: e.target.value })
}
onBlur={(e) => {
if (e.target.value === editField.check) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: editField,
redo: { check: e.target.value },
message: `Edit table field check expression to ${e.target.value}`,
},
]);
setRedoStack([]);
}}
/>
<div className="text-xs mt-1">
*This will be in the script as is.
</div>
</>
)}
2023-09-19 20:50:16 +08:00
<div className="flex justify-between items-center my-3">
<div className="font-medium">Unique</div>
<Checkbox
value="unique"
2023-09-19 20:50:45 +08:00
checked={f.unique}
2023-09-19 20:50:16 +08:00
onChange={(checkedValues) => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: {
[checkedValues.target.value]:
!checkedValues.target.checked,
},
redo: {
2023-09-19 20:48:53 +08:00
[checkedValues.target.value]:
checkedValues.target.checked,
2023-09-19 20:50:16 +08:00
},
message: `Edit table field to${
f.unique ? " not" : ""
} unique`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateField(props.tableData.id, j, {
[checkedValues.target.value]:
checkedValues.target.checked,
});
}}
></Checkbox>
</div>
<div className="flex justify-between items-center my-3">
<div className="font-medium">Autoincrement</div>
<Checkbox
value="increment"
2023-09-19 20:50:45 +08:00
checked={f.increment}
disabled={
!(
f.type === "INT" ||
f.type === "BIGINT" ||
f.type === "SMALLINT"
)
}
2023-09-19 20:50:16 +08:00
onChange={(checkedValues) => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: {
[checkedValues.target.value]:
!checkedValues.target.checked,
},
redo: {
2023-09-19 20:48:53 +08:00
[checkedValues.target.value]:
checkedValues.target.checked,
2023-09-19 20:50:16 +08:00
},
message: `Edit table field to${
f.primary ? " not" : ""
} auto increment`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateField(props.tableData.id, j, {
2023-09-19 20:51:25 +08:00
increment: !f.increment,
check: f.increment ? f.check : "",
2023-09-19 20:48:53 +08:00
});
}}
2023-09-19 20:50:16 +08:00
></Checkbox>
2023-09-19 20:48:53 +08:00
</div>
2023-09-19 20:50:16 +08:00
<div className="font-semibold">Comment</div>
<TextArea
className="my-2"
label="Comment"
placeholder="Add comment"
value={f.comment}
autosize
rows={2}
onChange={(value) =>
updateField(props.tableData.id, j, { comment: value })
}
onFocus={(e) =>
setEditField({ comment: e.target.value })
}
onBlur={(e) => {
if (e.target.value === editField.comment) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: props.tableData.id,
fid: j,
undo: editField,
redo: { comment: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit table field comment to ${e.target.value}`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
}}
/>
<Button
icon={<IconDeleteStroked />}
type="danger"
block
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field_delete",
tid: props.tableData.id,
data: f,
2023-09-19 20:50:52 +08:00
message: `Delete field`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
2023-12-27 10:45:23 +08:00
setRelationships((prev) => {
return prev.map((e) => {
if (
e.startTableId === props.tableData.id &&
e.startFieldId > f.id
) {
return {
...e,
startFieldId: e.startFieldId - 1,
startX: props.tableData.x + 15,
startY:
props.tableData.y +
(e.startFieldId - 1) * 36 +
50 +
19,
};
}
if (
e.endTableId === props.tableData.id &&
e.endFieldId > f.id
) {
return {
...e,
endFieldId: e.endFieldId - 1,
endX: props.tableData.x + 15,
endY:
props.tableData.y +
(e.endFieldId - 1) * 36 +
50 +
19,
};
}
return e;
});
});
2023-09-19 20:50:16 +08:00
updateTable(props.tableData.id, {
fields: props.tableData.fields
.filter((field) => field.id !== j)
2023-12-27 10:45:23 +08:00
.map((e, k) => ({ ...e, id: k })),
2023-09-19 20:50:16 +08:00
});
}}
>
Delete field
</Button>
</div>
}
trigger="click"
position="right"
2023-09-19 20:50:16 +08:00
showArrow
>
<Button type="tertiary" icon={<IconMore />}></Button>
</Popover>
</Col>
</Row>
2023-09-19 20:48:53 +08:00
))}
{props.tableData.indices.length > 0 && (
<Card
bodyStyle={{ padding: "14px" }}
style={{ marginTop: "12px", marginBottom: "12px" }}
headerLine={false}
>
2023-09-19 20:50:16 +08:00
<div className="font-medium mb-2 ms-1">Indices</div>
2023-09-19 20:48:53 +08:00
{props.tableData.indices.map((idx, k) => (
<div className="flex justify-between items-center mb-2" key={k}>
<Select
placeholder="Select fields"
multiple
2023-09-19 20:51:17 +08:00
validateStatus={
idx.fields.length === 0 ? "error" : "default"
}
2023-09-19 20:48:53 +08:00
optionList={props.tableData.fields.map((e) => ({
value: e.name,
label: e.name,
}))}
className="w-full"
2023-09-19 20:50:16 +08:00
value={idx.fields}
2023-09-19 20:48:53 +08:00
onChange={(value) => {
2023-09-19 20:50:16 +08:00
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "index",
tid: props.tableData.id,
iid: k,
undo: {
fields: [...idx.fields],
name: `${idx.fields.join("_")}_index`,
},
redo: {
fields: [...value],
name: `${value.join("_")}_index`,
},
2023-09-19 20:50:52 +08:00
message: `Edit index fields to "${JSON.stringify(
value
)}"`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateTable(props.tableData.id, {
indices: props.tableData.indices.map((index) =>
index.id === k
? {
...index,
fields: [...value],
name: `${value.join("_")}_index`,
}
2023-09-19 20:50:16 +08:00
: index
),
});
2023-09-19 20:48:53 +08:00
}}
/>
<Popover
content={
2023-09-19 20:51:08 +08:00
<div className="px-1 popover-theme">
2023-09-19 20:50:45 +08:00
<div className="font-semibold mb-1">Index name: </div>
2023-09-19 20:50:16 +08:00
<Input
value={idx.name}
placeholder="Index name"
disabled
/>
2023-09-19 20:50:45 +08:00
<div className="flex justify-between items-center my-3">
<div className="font-medium">Unique</div>
<Checkbox
value="unique"
checked={idx.unique}
onChange={(checkedValues) => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "index",
tid: props.tableData.id,
iid: k,
undo: {
[checkedValues.target.value]:
!checkedValues.target.checked,
},
redo: {
[checkedValues.target.value]:
checkedValues.target.checked,
},
message: `Edit table field to${
idx.unique ? " not" : ""
} unique`,
2023-09-19 20:50:45 +08:00
},
]);
setRedoStack([]);
updateTable(props.tableData.id, {
indices: props.tableData.indices.map((index) =>
index.id === k
? {
...index,
[checkedValues.target.value]:
checkedValues.target.checked,
}
2023-09-19 20:50:45 +08:00
: index
),
});
}}
></Checkbox>
</div>
2023-09-19 20:48:53 +08:00
<Button
icon={<IconDeleteStroked />}
type="danger"
block
onClick={() => {
2023-09-19 20:50:16 +08:00
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "index_delete",
tid: props.tableData.id,
data: idx,
2023-09-19 20:50:52 +08:00
message: `Delete index`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateTable(props.tableData.id, {
indices: props.tableData.indices
.filter((e) => e.id !== k)
.map((e, j) => ({
...e,
id: j,
})),
});
2023-09-19 20:48:53 +08:00
}}
>
Delete
</Button>
</div>
}
trigger="click"
position="rightTop"
showArrow
>
<Button
icon={<IconMore />}
type="tertiary"
style={{ marginLeft: "12px" }}
></Button>
</Popover>
</div>
))}
</Card>
)}
<Card
bodyStyle={{ padding: "14px" }}
style={{ marginTop: "12px", marginBottom: "12px" }}
headerLine={false}
>
2023-09-19 20:50:16 +08:00
<div className="font-medium ms-1 mb-1">Comment</div>
<TextArea
value={props.tableData.comment}
autosize
placeholder="Add comment"
rows={1}
2023-09-19 20:48:53 +08:00
onChange={(value) =>
2023-09-19 20:50:16 +08:00
updateTable(props.tableData.id, { comment: value }, false)
2023-09-19 20:48:53 +08:00
}
2023-09-19 20:50:16 +08:00
onFocus={(e) => setEditField({ comment: e.target.value })}
onBlur={(e) => {
if (e.target.value === editField.comment) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
2023-09-19 20:50:52 +08:00
component: "self",
2023-09-19 20:50:16 +08:00
tid: props.tableData.id,
undo: editField,
redo: { comment: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit table comment to "${e.target.value}"`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
}}
/>
2023-09-19 20:48:53 +08:00
</Card>
<Row gutter={6} className="mt-2">
<Col span={8}>
<Popover
content={
<div>
<div className="flex justify-between items-center p-2 popover-theme">
2023-09-19 20:48:53 +08:00
<div className="font-medium">Theme</div>
<Button
type="tertiary"
size="small"
2023-09-19 20:50:16 +08:00
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "self",
tid: props.tableData.id,
undo: { color: props.tableData.color },
redo: { color: defaultTableTheme },
2023-09-19 20:50:52 +08:00
message: `Edit table color to default`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
2023-09-19 20:48:53 +08:00
updateTable(props.tableData.id, {
color: defaultTableTheme,
2023-09-19 20:50:16 +08:00
});
}}
2023-09-19 20:48:53 +08:00
>
Clear
</Button>
</div>
<hr />
<div className="py-3">
<div>
{tableThemes
.slice(0, Math.ceil(tableThemes.length / 2))
.map((c) => (
<button
key={c}
style={{ backgroundColor: c }}
className="p-3 rounded-full mx-1"
2023-09-19 20:50:16 +08:00
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "self",
tid: props.tableData.id,
undo: { color: props.tableData.color },
redo: { color: c },
2023-09-19 20:50:52 +08:00
message: `Edit table color to ${c}`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateTable(props.tableData.id, { color: c });
}}
2023-09-19 20:48:53 +08:00
>
{props.tableData.color === c ? (
<IconCheckboxTick style={{ color: "white" }} />
) : (
<IconCheckboxTick style={{ color: c }} />
)}
</button>
))}
</div>
<div className="mt-3">
{tableThemes
.slice(Math.ceil(tableThemes.length / 2))
.map((c) => (
<button
key={c}
style={{ backgroundColor: c }}
className="p-3 rounded-full mx-1"
2023-09-19 20:50:16 +08:00
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "self",
tid: props.tableData.id,
undo: { color: props.tableData.color },
redo: { color: c },
2023-09-19 20:50:52 +08:00
message: `Edit table color to ${c}`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateTable(props.tableData.id, { color: c });
}}
2023-09-19 20:48:53 +08:00
>
<IconCheckboxTick
style={{
color:
props.tableData.color === c ? "white" : c,
}}
/>
</button>
))}
</div>
</div>
</div>
}
trigger="click"
position="bottomLeft"
showArrow
>
<div
className="h-[32px] w-[32px] rounded"
style={{ backgroundColor: props.tableData.color }}
/>
2023-09-19 20:48:53 +08:00
</Popover>
</Col>
<Col span={7}>
<Button
block
onClick={() => {
2023-09-19 20:50:16 +08:00
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "index_add",
tid: props.tableData.id,
2023-09-19 20:50:52 +08:00
message: `Add index`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateTable(props.tableData.id, {
indices: [
...props.tableData.indices,
{
id: props.tableData.indices.length,
name: `index_${props.tableData.indices.length}`,
2023-09-19 20:50:45 +08:00
unique: false,
2023-09-19 20:50:16 +08:00
fields: [],
},
],
});
2023-09-19 20:48:53 +08:00
}}
>
Add index
</Button>
</Col>
<Col span={6}>
<Button
onClick={() => {
2023-09-19 20:50:16 +08:00
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field_add",
tid: props.tableData.id,
2023-09-19 20:50:52 +08:00
message: `Add field`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
updateTable(props.tableData.id, {
fields: [
...props.tableData.fields,
{
name: "",
type: "",
default: "",
check: "",
primary: false,
unique: false,
notNull: false,
increment: false,
comment: "",
id: props.tableData.fields.length,
},
],
});
2023-09-19 20:48:53 +08:00
}}
block
>
Add field
</Button>
</Col>
<Col span={3}>
<Button
icon={<IconDeleteStroked />}
type="danger"
onClick={() => {
Toast.success(`Table deleted!`);
2023-09-19 20:49:57 +08:00
deleteTable(props.tableData.id);
2023-09-19 20:48:53 +08:00
}}
></Button>
</Col>
</Row>
</div>
2023-09-19 20:48:46 +08:00
</SideSheet>
2023-09-19 20:50:16 +08:00
</>
2023-09-19 20:47:06 +08:00
);
2023-09-19 20:49:16 +08:00
function field(fieldData, index) {
return (
<div
className={`${
index === props.tableData.fields.length - 1
2023-09-19 20:49:16 +08:00
? ""
: "border-b border-gray-400"
} h-[36px] px-2 py-1 flex justify-between`}
2023-09-19 20:49:16 +08:00
onMouseEnter={() => {
setHoveredField(index);
props.setOnRect({
tableId: props.tableData.id,
field: index,
});
}}
onMouseLeave={() => {
setHoveredField(-1);
}}
>
2023-09-19 20:51:08 +08:00
<div className={`${hoveredField === index ? "text-zinc-400" : ""}`}>
2023-09-19 20:49:16 +08:00
<button
className={`w-[10px] h-[10px] bg-[#2f68ad] opacity-80 z-50 rounded-full me-2`}
2023-12-16 11:39:13 +08:00
onMouseDown={() => {
2023-09-19 20:49:16 +08:00
props.handleGripField(index);
props.setLine((prev) => ({
...prev,
startFieldId: index,
startTableId: props.tableData.id,
startX: props.tableData.x + 15,
startY: props.tableData.y + index * 36 + 50 + 19,
endX: props.tableData.x + 15,
endY: props.tableData.y + index * 36 + 50 + 19,
}));
}}
></button>
2024-02-29 04:47:07 +08:00
{fieldData.name.length <= 11
2024-02-28 23:56:32 +08:00
? fieldData.name
2024-02-29 04:47:07 +08:00
: fieldData.name.substring(0, 11)}
2023-09-19 20:49:16 +08:00
</div>
2023-09-19 20:51:08 +08:00
<div className="text-zinc-400">
2023-09-19 20:49:16 +08:00
{hoveredField === index ? (
<Button
theme="solid"
size="small"
style={{
opacity: "0.7",
backgroundColor: "#d42020",
}}
icon={<IconMinus />}
onClick={() => {
2023-09-19 20:50:16 +08:00
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field_delete",
tid: props.tableData.id,
data: fieldData,
2023-09-19 20:50:52 +08:00
message: `Delete field`,
2023-09-19 20:50:16 +08:00
},
]);
setRedoStack([]);
2023-09-19 20:50:49 +08:00
setRelationships((prev) =>
prev
.filter(
(e) =>
!(
(e.startTableId === props.tableData.id &&
e.startFieldId === index) ||
(e.endTableId === props.tableData.id &&
e.endFieldId === index)
)
)
.map((e, i) => ({ ...e, id: i }))
);
2023-12-27 10:45:23 +08:00
setRelationships((prev) => {
return prev.map((e) => {
if (
e.startTableId === props.tableData.id &&
e.startFieldId > fieldData.id
) {
return {
...e,
startFieldId: e.startFieldId - 1,
startX: props.tableData.x + 15,
startY:
props.tableData.y +
(e.startFieldId - 1) * 36 +
50 +
19,
};
}
if (
e.endTableId === props.tableData.id &&
e.endFieldId > fieldData.id
) {
return {
...e,
endFieldId: e.endFieldId - 1,
endX: props.tableData.x + 15,
endY:
props.tableData.y + (e.endFieldId - 1) * 36 + 50 + 19,
};
}
return e;
});
});
2023-09-19 20:50:16 +08:00
updateTable(props.tableData.id, {
fields: props.tableData.fields
.filter((e) => e.id !== fieldData.id)
2023-12-27 10:45:23 +08:00
.map((t, i) => {
return { ...t, id: i };
}),
2023-09-19 20:50:16 +08:00
});
}}
></Button>
2023-09-19 20:49:16 +08:00
) : (
fieldData.type
)}
</div>
</div>
);
}
2023-09-19 20:47:43 +08:00
}