import ActiveObjectContext from "@/components/Context/activeObject/ObjectContext"; import CanvasContext from "@/components/Context/canvasContext/CanvasContext"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { useCallback, useContext, useEffect, useRef, useState } from "react"; import { AlignLeft, AlignCenter, AlignRight, Bold, Italic, Underline, Strikethrough, Minus, Plus, } from "lucide-react"; import { RiLineHeight } from "react-icons/ri"; import { Slider } from "@/components/ui/slider"; import { Tooltip } from "react-tooltip"; const fonts = [ "Roboto", "Open Sans", "Lato", "Montserrat", "Raleway", "Poppins", "Merriweather", "Playfair Display", "Nunito", "Oswald", "Source Sans Pro", "Ubuntu", "Noto Sans", "Work Sans", "Bebas Neue", "Arimo", "PT Sans", "PT Serif", "Titillium Web", "Fira Sans", "Karla", "Josefin Sans", "Cairo", "Rubik", "Mulish", "IBM Plex Sans", "Quicksand", "Cabin", "Heebo", "Exo 2", "Manrope", "Jost", "Anton", "Asap", "Baloo 2", "Barlow", "Cantarell", "Chivo", "Inter", "Dosis", "Crimson Text", "Amatic SC", "ABeeZee", "Raleway Dots", "Pacifico", "Orbitron", "Varela Round", "Acme", "Teko", ]; const TextCustomization = () => { const { activeObject } = useContext(ActiveObjectContext); const { canvas, setSelectedPanel, textColor } = useContext(CanvasContext); const activeObjectType = activeObject?.type; const hasClipPath = !!activeObject?.clipPath; const customClipPath = activeObject?.isClipPath; const prevTextRef = useRef(""); const [text, setText] = useState(""); const [fontFamily, setFontFamily] = useState("Arial"); const [fontSize, setFontSize] = useState(20); const [fontStyle, setFontStyle] = useState("normal"); const [fontWeight, setFontWeight] = useState("normal"); const [lineHeight, setLineHeight] = useState(1.16); const [charSpacing, setCharSpacing] = useState(0); const [underline, setUnderline] = useState(false); const [linethrough, setLinethrough] = useState(false); const [textAlign, setTextAlign] = useState("left"); const [fillColor, setFillColor] = useState("black"); useEffect(() => { if (activeObject?.type === "i-text") { if ( activeObject?.text !== undefined && activeObject.text !== prevTextRef.current ) { setText(activeObject.text); prevTextRef.current = activeObject.text; } setText(activeObject?.text || ""); setFontFamily(activeObject?.fontFamily || "Arial"); setFontSize(activeObject?.fontSize || 20); setFontStyle(activeObject?.fontStyle || "normal"); setFontWeight(activeObject?.fontWeight || "normal"); setLineHeight(activeObject?.lineHeight || 1.16); setCharSpacing(activeObject?.charSpacing || 0); setUnderline(activeObject?.underline || false); setLinethrough(activeObject?.linethrough || false); setTextAlign(activeObject?.textAlign || "left"); setFillColor(textColor?.fill || "black"); } }, [activeObject, textColor]); const updateActiveObject = useCallback( (properties) => { if (activeObject?.type === "i-text") { // Preserve the text value when updating other properties const updatedProperties = { ...properties, text: text || prevTextRef.current || properties.text, }; activeObject.set(updatedProperties); canvas?.renderAll(); } }, [activeObject, canvas, text] ); // Add dependencies const applyChanges = useCallback(() => { updateActiveObject({ text, fontFamily, fontSize, fontStyle, fontWeight, lineHeight, charSpacing, underline, linethrough, textAlign, }); }, [ text, fontFamily, fontSize, fontStyle, fontWeight, lineHeight, charSpacing, underline, linethrough, textAlign, updateActiveObject, // Add this dependency ]); const handleColorPanelClick = () => { // Store current text value before switching panels prevTextRef.current = text; setSelectedPanel("color"); }; // Automatically apply changes when state updates useEffect(() => { if (activeObject?.type === "i-text") { applyChanges(); } }, [ applyChanges, // Now included in dependencies activeObject?.type, // Track active object type ]); const handleFontFamilyChange = (newFontFamily) => { setFontFamily(newFontFamily); }; const handleFontSizeChange = (newFontSize) => { setFontSize(newFontSize); }; const handleTextAlignChange = (newTextAlign) => { setTextAlign(newTextAlign); }; const handleFontStyleChange = () => { setFontStyle(fontStyle === "normal" ? "italic" : "normal"); }; const handleFontWeightChange = () => { setFontWeight(fontWeight === "normal" ? "bold" : "normal"); }; const handleLineHeightChange = (newLineHeight) => { setLineHeight(parseFloat(newLineHeight)); }; const handleCharSpacingChange = (newCharSpacing) => { setCharSpacing(parseInt(newCharSpacing)); }; const handleUnderlineChange = () => { setUnderline(!underline); }; const handleLinethroughChange = () => { setLinethrough(!linethrough); }; const content = () => { if (!(activeObject?.type === "i-text")) { return
; } return (