drawDB/src/components/area.jsx

122 lines
3.7 KiB
React
Raw Normal View History

2023-09-19 20:48:59 +08:00
import { React, useContext, useState } from "react";
import { Button } from "@douyinfe/semi-ui";
import { IconEdit } from "@douyinfe/semi-icons";
import { Tab } from "../data/data";
import { LayoutContext, TabContext } from "../pages/editor";
2023-09-19 20:48:04 +08:00
export default function Area(props) {
2023-09-19 20:48:08 +08:00
const [hovered, setHovered] = useState(false);
2023-09-19 20:48:59 +08:00
const { layout } = useContext(LayoutContext);
const { tab, setTab } = useContext(TabContext);
2023-09-19 20:48:06 +08:00
const handleMouseDown = (e, dir) => {
2023-09-19 20:48:55 +08:00
props.setResize({ id: props.areaData.id, dir: dir });
2023-09-19 20:48:42 +08:00
props.setInitCoords({
2023-09-19 20:48:07 +08:00
x: props.areaData.x,
y: props.areaData.y,
width: props.areaData.width,
height: props.areaData.height,
2023-09-19 20:49:57 +08:00
mouseX: e.clientX / props.zoom,
mouseY: e.clientY / props.zoom,
2023-09-19 20:48:07 +08:00
});
2023-09-19 20:48:06 +08:00
};
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
onMouseDown={props.onMouseDown}
>
2023-09-19 20:48:55 +08:00
<div
className={`${
hovered
? "border-4 border-dashed border-[#5891db]"
: "border-2 border-slate-400"
} w-full h-full cursor-move rounded relative`}
>
<div
className="opacity-40 w-fill p-2 h-full"
style={{ backgroundColor: props.areaData.color }}
/>
</div>
<div className="text-gray-900 absolute top-2 left-3 select-none">
2023-09-19 20:48:04 +08:00
{props.areaData.name}
</div>
2023-09-19 20:48:59 +08:00
{hovered && (
<div className="absolute top-2 right-3">
<Button
icon={<IconEdit />}
size="small"
theme="solid"
style={{
backgroundColor: "#2f68ad",
opacity: "0.7",
}}
onClick={() => {
if (layout.sidebar) {
setTab(Tab.subject_areas);
if (tab !== Tab.subject_areas) return;
document
.getElementById(`scroll_area_${props.areaData.id}`)
.scrollIntoView({ behavior: "smooth" });
}
}}
></Button>
</div>
)}
2023-09-19 20:48:04 +08:00
</foreignObject>
2023-09-19 20:48:08 +08:00
{hovered && (
<>
2023-09-19 20:48:55 +08:00
<circle
cx={props.areaData.x}
cy={props.areaData.y}
r={6}
fill="white"
stroke="#5891db"
strokeWidth={3}
2023-09-19 20:48:08 +08:00
cursor="nwse-resize"
onMouseDown={(e) => handleMouseDown(e, "tl")}
/>
2023-09-19 20:48:55 +08:00
<circle
cx={props.areaData.x + props.areaData.width}
cy={props.areaData.y}
r={6}
fill="white"
stroke="#5891db"
strokeWidth={3}
2023-09-19 20:48:08 +08:00
cursor="nesw-resize"
onMouseDown={(e) => handleMouseDown(e, "tr")}
/>
2023-09-19 20:48:55 +08:00
<circle
cx={props.areaData.x}
cy={props.areaData.y + props.areaData.height}
r={6}
fill="white"
stroke="#5891db"
strokeWidth={3}
2023-09-19 20:48:08 +08:00
cursor="nesw-resize"
onMouseDown={(e) => handleMouseDown(e, "bl")}
/>
2023-09-19 20:48:55 +08:00
<circle
cx={props.areaData.x + props.areaData.width}
cy={props.areaData.y + props.areaData.height}
r={6}
fill="white"
stroke="#5891db"
strokeWidth={3}
2023-09-19 20:48:08 +08:00
cursor="nwse-resize"
onMouseDown={(e) => handleMouseDown(e, "br")}
/>
</>
)}
2023-09-19 20:48:04 +08:00
</g>
);
}