drawDB/src/components/area.jsx

155 lines
4.6 KiB
React
Raw Normal View History

2023-09-19 20:48:04 +08:00
import { React, useState } from "react";
export default function Area(props) {
2023-09-19 20:48:06 +08:00
const [resize, setResize] = useState("none");
2023-09-19 20:48:07 +08:00
const [initCoords, setInitCoords] = useState({
x: 0,
y: 0,
width: 0,
height: 0,
mouseX: 0,
mouseY: 0,
});
2023-09-19 20:48:08 +08:00
const [hovered, setHovered] = useState(false);
2023-09-19 20:48:06 +08:00
const handleMouseDown = (e, dir) => {
setResize(dir);
props.setPanning(false);
2023-09-19 20:48:07 +08:00
setInitCoords({
x: props.areaData.x,
y: props.areaData.y,
width: props.areaData.width,
height: props.areaData.height,
mouseX: e.clientX,
mouseY: e.clientY,
});
2023-09-19 20:48:06 +08:00
};
const handleMouseMove = (e) => {
2023-09-19 20:48:07 +08:00
if (resize === "none") return;
let newX = initCoords.x;
let newY = initCoords.y;
let newWidth = initCoords.width;
let newHeight = initCoords.height;
2023-09-19 20:48:06 +08:00
props.setPanning(false);
if (resize === "br") {
2023-09-19 20:48:07 +08:00
newWidth = initCoords.width + (e.clientX - initCoords.mouseX);
newHeight = initCoords.height + (e.clientY - initCoords.mouseY);
2023-09-19 20:48:06 +08:00
} else if (resize === "tl") {
2023-09-19 20:48:07 +08:00
newX = initCoords.x + (e.clientX - initCoords.mouseX);
newY = initCoords.y + (e.clientY - initCoords.mouseY);
newWidth = initCoords.width - (e.clientX - initCoords.mouseX);
newHeight = initCoords.height - (e.clientY - initCoords.mouseY);
2023-09-19 20:48:06 +08:00
} else if (resize === "tr") {
2023-09-19 20:48:07 +08:00
newY = initCoords.y + (e.clientY - initCoords.mouseY);
newWidth = initCoords.width + (e.clientX - initCoords.mouseX);
newHeight = initCoords.height - (e.clientY - initCoords.mouseY);
2023-09-19 20:48:06 +08:00
} else if (resize === "bl") {
2023-09-19 20:48:07 +08:00
newX = initCoords.x + (e.clientX - initCoords.mouseX);
newWidth = initCoords.width - (e.clientX - initCoords.mouseX);
newHeight = initCoords.height + (e.clientY - initCoords.mouseY);
2023-09-19 20:48:06 +08:00
}
2023-09-19 20:48:07 +08:00
props.setAreas((prev) => {
return prev.map((a) => {
if (a.id === props.areaData.id) {
return {
...a,
x: newX,
y: newY,
width: newWidth,
height: newHeight,
};
}
return a;
});
});
2023-09-19 20:48:06 +08:00
};
2023-09-19 20:48:08 +08:00
const unableResize = () => {
2023-09-19 20:48:06 +08:00
setResize("none");
};
2023-09-19 20:48:04 +08:00
return (
2023-09-19 20:48:08 +08:00
<g
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
>
2023-09-19 20:48:04 +08:00
<foreignObject
key={props.areaData.id}
x={props.areaData.x}
y={props.areaData.y}
2023-09-19 20:48:08 +08:00
width={props.areaData.width > 0 ? props.areaData.width : 0}
height={props.areaData.height > 0 ? props.areaData.height : 0}
2023-09-19 20:48:04 +08:00
style={{ cursor: "move" }}
onMouseDown={props.onMouseDown}
>
2023-09-19 20:48:06 +08:00
<div className="border-2 border-dashed border-blue-600 opacity-70 bg-slate-400 w-fill h-full select-none">
2023-09-19 20:48:04 +08:00
{props.areaData.name}
</div>
</foreignObject>
2023-09-19 20:48:08 +08:00
{hovered && (
<>
<rect
x={props.areaData.x - 8}
y={props.areaData.y - 8}
width={16}
height={16}
fill="lightblue"
stroke="blue"
strokeWidth={1}
cursor="nwse-resize"
onMouseDown={(e) => handleMouseDown(e, "tl")}
onMouseUp={unableResize}
onMouseMove={handleMouseMove}
onMouseLeave={unableResize}
/>
<rect
x={props.areaData.x + props.areaData.width - 8}
y={props.areaData.y - 8}
width={16}
height={16}
fill="lightblue"
stroke="blue"
strokeWidth={1}
cursor="nesw-resize"
onMouseDown={(e) => handleMouseDown(e, "tr")}
onMouseUp={unableResize}
onMouseMove={handleMouseMove}
onMouseLeave={unableResize}
/>
<rect
x={props.areaData.x - 8}
y={props.areaData.y + props.areaData.height - 8}
width={16}
height={16}
fill="lightblue"
stroke="blue"
strokeWidth={1}
cursor="nesw-resize"
onMouseDown={(e) => handleMouseDown(e, "bl")}
onMouseUp={unableResize}
onMouseMove={handleMouseMove}
onMouseLeave={unableResize}
/>
<rect
x={props.areaData.x + props.areaData.width - 8}
y={props.areaData.y + props.areaData.height - 8}
width={16}
height={16}
fill="lightblue"
stroke="blue"
strokeWidth={1}
cursor="nwse-resize"
onMouseDown={(e) => handleMouseDown(e, "br")}
onMouseUp={unableResize}
onMouseMove={handleMouseMove}
onMouseLeave={unableResize}
/>
</>
)}
2023-09-19 20:48:04 +08:00
</g>
);
}