import { useRef, useState } from "react";
import { Button, Popover, Input } from "@douyinfe/semi-ui";
import { IconEdit, IconDeleteStroked } from "@douyinfe/semi-icons";
import {
Tab,
Action,
ObjectType,
defaultBlue,
State,
} from "../../data/constants";
import {
useCanvas,
useLayout,
useSettings,
useUndoRedo,
useSelect,
useAreas,
useSaveState,
} from "../../hooks";
import ColorPalette from "../ColorPicker";
import { useTranslation } from "react-i18next";
import { useHover } from "usehooks-ts";
export default function Area({
data,
onPointerDown,
setResize,
setInitCoords,
}) {
const ref = useRef(null);
const isHovered = useHover(ref);
const {
pointer: {
spaces: { diagram: pointer },
},
} = useCanvas();
const { layout } = useLayout();
const { settings } = useSettings();
const { setSaveState } = useSaveState();
const { selectedElement, setSelectedElement } = useSelect();
const handleResize = (e, dir) => {
setResize({ id: data.id, dir: dir });
setInitCoords({
x: data.x,
y: data.y,
width: data.width,
height: data.height,
pointerX: pointer.x,
pointerY: pointer.y,
});
};
const edit = () => {
if (layout.sidebar) {
setSelectedElement((prev) => ({
...prev,
element: ObjectType.AREA,
id: data.id,
currentTab: Tab.AREAS,
open: true,
}));
if (selectedElement.currentTab !== Tab.AREAS) return;
document
.getElementById(`scroll_area_${data.id}`)
.scrollIntoView({ behavior: "smooth" });
} else {
setSelectedElement((prev) => ({
...prev,
element: ObjectType.AREA,
id: data.id,
open: true,
}));
}
};
const onClickOutSide = () => {
if (selectedElement.editFromToolbar) {
setSelectedElement((prev) => ({
...prev,
editFromToolbar: false,
}));
return;
}
setSelectedElement((prev) => ({
...prev,
open: false,
}));
setSaveState(State.SAVING);
};
const areaIsSelected = () =>
selectedElement.element === ObjectType.AREA &&
selectedElement.id === data.id &&
selectedElement.open;
return (