add simple table design

This commit is contained in:
1ilit 2023-09-19 15:47:17 +03:00
parent 73ae5a87a7
commit ec1712afa0
4 changed files with 64 additions and 23 deletions

View File

@ -35,9 +35,11 @@ export default function ControlPanel() {
break; break;
case Tool.ZOOM: case Tool.ZOOM:
setOpenZoom((prev) => !prev); setOpenZoom((prev) => !prev);
setOpenAdd(false);
break; break;
case Tool.ADD: case Tool.ADD:
setOpenAdd((prev) => !prev); setOpenAdd((prev) => !prev);
setOpenZoom(false);
break; break;
default: default:
break; break;
@ -143,7 +145,7 @@ export default function ControlPanel() {
<button className="py-1 px-2 hover:bg-gray-300" title="Zoom out"> <button className="py-1 px-2 hover:bg-gray-300" title="Zoom out">
<i className="fa-solid fa-magnifying-glass-minus"></i> <i className="fa-solid fa-magnifying-glass-minus"></i>
</button> </button>
<button className="py-1 px-2 hover:bg-gray-300" title="Undi"> <button className="py-1 px-2 hover:bg-gray-300" title="Undo">
<i className="fa-solid fa-rotate-left "></i> <i className="fa-solid fa-rotate-left "></i>
</button> </button>
<button className="py-1 px-2 hover:bg-gray-300" title="Redo"> <button className="py-1 px-2 hover:bg-gray-300" title="Redo">

View File

@ -124,19 +124,19 @@ export default function Canvas(props) {
const canvasRect = canvas.current.getBoundingClientRect(); const canvasRect = canvas.current.getBoundingClientRect();
const x = offset.x - canvasRect.left - 100 * 0.5; const x = offset.x - canvasRect.left - 100 * 0.5;
const y = offset.y - canvasRect.top - 100 * 0.5; const y = offset.y - canvasRect.top - 100 * 0.5;
const newRectangle = { const d = {
id: props.rectangles.length + 1, id: props.rectangles.length + 1,
x, x,
y, y,
width: 100, width: 240,
height: 100, height: 100,
label: `rect ${props.rectangles.length + 1}`, label: `rect ${props.rectangles.length + 1}`,
}; };
props.setRectangles([...props.rectangles, newRectangle]); props.setRectangles([...props.rectangles, d]);
props.setCode((prev) => props.setCode((prev) =>
prev === "" prev === ""
? `CREATE TABLE \`${newRectangle.label}\`;` ? `CREATE TABLE \`${d.label}\`;`
: `${prev}\n\nCREATE TABLE \`${newRectangle.label}\`;` : `${prev}\n\nCREATE TABLE \`${d.label}\`;`
); );
}, },
collect: (monitor) => ({ collect: (monitor) => ({

View File

@ -52,7 +52,7 @@ export default function EditorPanel(props) {
id: props.rectangles.length + 1, id: props.rectangles.length + 1,
x: 0, x: 0,
y: 0, y: 0,
width: 100, width: 240,
height: 100, height: 100,
label: `rect ${props.rectangles.length + 1}`, label: `rect ${props.rectangles.length + 1}`,
}; };
@ -95,7 +95,7 @@ export default function EditorPanel(props) {
id: props.rectangles.length + 1, id: props.rectangles.length + 1,
x: 0, x: 0,
y: 0, y: 0,
width: 100, width: 240,
height: 100, height: 100,
label: `rect ${props.rectangles.length + 1}`, label: `rect ${props.rectangles.length + 1}`,
}; };

View File

@ -6,6 +6,33 @@ const Rect = (props) => {
const [isHovered, setIsHovered] = useState(false); const [isHovered, setIsHovered] = useState(false);
const [node, setNode] = useState(Node.NONE); const [node, setNode] = useState(Node.NONE);
const table = {
name: "Students",
fields: [
{
name: "id",
type: "uuid",
default: "",
primary: true,
unique: true,
notNull: true,
increment: false,
},
{
name: "name",
type: "varchar(20)",
default: "n/a",
primary: false,
unique: false,
notNull: true,
increment: false,
},
],
};
const height =
table.fields.length * 36 + (table.fields.length - 1) * 2 + 40 + 4;
return ( return (
<g> <g>
<foreignObject <foreignObject
@ -13,7 +40,7 @@ const Rect = (props) => {
x={props.x} x={props.x}
y={props.y} y={props.y}
width={props.width} width={props.width}
height={props.height} height={height}
style={{ cursor: "move" }} style={{ cursor: "move" }}
onMouseDown={props.onMouseDown} onMouseDown={props.onMouseDown}
onMouseEnter={() => { onMouseEnter={() => {
@ -26,15 +53,27 @@ const Rect = (props) => {
}} }}
> >
<div <div
className={`${isHovered ? "ring-2 ring-blue-400 ring-inset" : ""} bg-gray-600 p-3 select-none rounded-md`} className={`border-2 ${
isHovered ? "border-sky-500" : "border-gray-500"
} bg-gray-300 select-none rounded-md`}
> >
<div className="text-white">{props.label}</div> <div className="p-3 font-bold text-slate-800 h-[40px] bg-gray-400 rounded-t-md">
<form onSubmit={(e) => e.preventDefault()}> {table.name}
<input type="text" className="w-full" /> </div>
</form> {table.fields.map((e, i) => {
<Button type="secondary" onClick={(e) => console.log("sup")}> return (
sup <div
</Button> className={`${
i === table.fields.length - 1
? ""
: "border-b-2 border-gray-400"
} h-[36px] p-2 flex justify-between`}
>
<div>{e.name}</div>
<div className="text-slate-600">{e.type}</div>
</div>
);
})}
</div> </div>
</foreignObject> </foreignObject>
<circle <circle
@ -59,7 +98,7 @@ const Rect = (props) => {
<circle <circle
id="LEFT" id="LEFT"
cx={props.x} cx={props.x}
cy={props.y + props.height / 2} cy={props.y + height / 2}
r={5} r={5}
onClick={(e) => { onClick={(e) => {
setNode(Node.LEFT); setNode(Node.LEFT);
@ -69,7 +108,7 @@ const Rect = (props) => {
rect: props.id, rect: props.id,
node: Node.LEFT, node: Node.LEFT,
x: props.x, x: props.x,
y: props.y + props.height / 2, y: props.y + height / 2,
}, },
]); ]);
}} }}
@ -78,7 +117,7 @@ const Rect = (props) => {
<circle <circle
id="RIGHT" id="RIGHT"
cx={props.x + props.width} cx={props.x + props.width}
cy={props.y + props.height / 2} cy={props.y + height / 2}
r={5} r={5}
onClick={(e) => { onClick={(e) => {
setNode(Node.RIGHT); setNode(Node.RIGHT);
@ -88,7 +127,7 @@ const Rect = (props) => {
rect: props.id, rect: props.id,
node: Node.RIGHT, node: Node.RIGHT,
x: props.x + props.width, x: props.x + props.width,
y: props.y + props.height / 2, y: props.y + height / 2,
}, },
]); ]);
}} }}
@ -97,7 +136,7 @@ const Rect = (props) => {
<circle <circle
id="BOTTOM" id="BOTTOM"
cx={props.x + props.width / 2} cx={props.x + props.width / 2}
cy={props.y + props.height} cy={props.y + height}
r={5} r={5}
onClick={(e) => { onClick={(e) => {
setNode(Node.BOTTOM); setNode(Node.BOTTOM);
@ -107,7 +146,7 @@ const Rect = (props) => {
rect: props.id, rect: props.id,
node: Node.BOTTOM, node: Node.BOTTOM,
x: props.x + props.width / 2, x: props.x + props.width / 2,
y: props.y + props.height, y: props.y + height,
}, },
]); ]);
}} }}