import { Text, Transformer } from "react-konva"; import { useRef, useEffect, useState } from "react"; import useEditorStore from "../useEditorStore"; import { Html } from "react-konva-utils"; export default function TextElement({ element }) { const updateElement = useEditorStore((state) => state.updateElement); const setSelectedId = useEditorStore((state) => state.setSelectedId); const selectedId = useEditorStore((state) => state.selectedId); const shapeRef = useRef(); const trRef = useRef(); const [isEditing, setIsEditing] = useState(false); const [editValue, setEditValue] = useState(element.text); const isSelected = selectedId === element.id; useEffect(() => { if (isSelected && trRef.current && shapeRef.current) { trRef.current.nodes([shapeRef.current]); trRef.current.getLayer().batchDraw(); } }, [isSelected]); useEffect(() => { setEditValue(element.text); }, [element.text]); const handleDoubleClick = () => { setIsEditing(true); setEditValue(element.text); }; const handleBlur = () => { setIsEditing(false); updateElement(element.id, { text: editValue }); }; const handleKeyDown = (e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); handleBlur(); } if (e.key === "Escape") { setIsEditing(false); setEditValue(element.text); } }; return ( <> {isEditing ? (