import { Button } from '@/components/ui/button'; import { Sheet, SheetContent, SheetHeader, SheetTitle } from '@/components/ui/sheet'; import { Textarea } from '@/components/ui/textarea'; import { useMitt } from '@/plugins/MittContext'; import useVideoEditorStore from '@/stores/VideoEditorStore'; import { Bold, Minus, Plus, Type } from 'lucide-react'; import { useEffect, useState } from 'react'; export default function TextSidebar({ isOpen, onClose }) { const { selectedTextElement } = useVideoEditorStore(); const emitter = useMitt(); const [textValue, setTextValue] = useState(''); const [fontSize, setFontSize] = useState(24); // Default font size const [isBold, setIsBold] = useState(true); // Default to bold // Font size constraints const MIN_FONT_SIZE = 8; const MAX_FONT_SIZE = 120; const FONT_SIZE_STEP = 2; // Update textarea, fontSize, and bold when selected element changes useEffect(() => { if (selectedTextElement) { setTextValue(selectedTextElement.text || ''); setFontSize(selectedTextElement.fontSize || 24); setIsBold(selectedTextElement.fontWeight === 'bold' || selectedTextElement.fontWeight === 700 || true); // Default to bold if not set } }, [selectedTextElement]); // Handle text changes const handleTextChange = (e) => { const newText = e.target.value; setTextValue(newText); if (selectedTextElement) { emitter.emit('text-update', { elementId: selectedTextElement.id, updates: { text: newText }, }); } }; // Handle font size changes const handleFontSizeChange = (newSize) => { const clampedSize = Math.max(MIN_FONT_SIZE, Math.min(MAX_FONT_SIZE, newSize)); setFontSize(clampedSize); if (selectedTextElement) { emitter.emit('text-update', { elementId: selectedTextElement.id, updates: { fontSize: clampedSize }, }); } }; // Handle bold toggle const handleBoldToggle = () => { const newBoldState = !isBold; setIsBold(newBoldState); if (selectedTextElement) { emitter.emit('text-update', { elementId: selectedTextElement.id, updates: { fontWeight: newBoldState ? 'bold' : 'normal' }, }); } }; // Increase font size const increaseFontSize = () => { handleFontSizeChange(fontSize + FONT_SIZE_STEP); }; // Decrease font size const decreaseFontSize = () => { handleFontSizeChange(fontSize - FONT_SIZE_STEP); }; return ( !open && onClose()}> Edit Text
{selectedTextElement ? ( <> {/* Text Content */}