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 ( 0 ? data.width : 0} height={data.height > 0 ? data.height : 0} onPointerDown={onPointerDown} >
{data.name}
{(isHovered || (areaIsSelected() && !layout.sidebar)) && ( } trigger="custom" position="rightTop" showArrow >
{isHovered && ( <> e.isPrimary && handleResize(e, "tl")} /> e.isPrimary && handleResize(e, "tr")} /> e.isPrimary && handleResize(e, "bl")} /> e.isPrimary && handleResize(e, "br")} /> )}
); } function EditPopoverContent({ data }) { const [editField, setEditField] = useState({}); const { setSaveState } = useSaveState(); const { updateArea, deleteArea } = useAreas(); const { setUndoStack, setRedoStack } = useUndoRedo(); const { t } = useTranslation(); return (
{t("edit")}
updateArea(data.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.AREA, aid: data.id, undo: editField, redo: { name: e.target.value }, message: t("edit_area", { areaName: e.target.value, extra: "[name]", }), }, ]); setRedoStack([]); }} /> { setUndoStack((prev) => [ ...prev, { action: Action.EDIT, element: ObjectType.AREA, aid: data.id, undo: { color: data.color }, redo: { color: c }, message: t("edit_area", { areaName: data.name, extra: "[color]", }), }, ]); setRedoStack([]); updateArea(data.id, { color: c, }); }} onClearColor={() => { updateArea(data.id, { color: defaultBlue, }); setSaveState(State.SAVING); }} />
} position="rightTop" showArrow >
); }