import React, { useState, useEffect, useRef, forwardRef, useImperativeHandle } from 'react'; import { Bold, Italic, List, ListOrdered, Quote, Code, Type, Palette, Layout, Settings, X, Check, Copy, AlignLeft, AlignCenter, AlignRight, Maximize2, Minimize2 } from 'lucide-react'; import { DEFAULT_STYLE_CONFIG, subscribeToStyleConfig, saveStyleConfigToCloud, type StyleConfig, type CodeStyleConfig, type QuoteStyleConfig } from '../services/codeStyleService'; import { StyleSettingsPanel } from './StyleSettingsPanel'; interface TextFormatToolbarProps { textareaRef: React.RefObject; onApplyFormat: (start: number, end: number, formatted: string) => void; } export interface TextFormatToolbarRef { openSettings: (tab: 'code' | 'quote') => void; syncStylesFromSelection: () => void; } type FormatType = 'bold' | 'italic' | 'code' | 'quote' | 'uppercase' | 'lowercase' | 'plain'; type SettingsTab = 'code' | 'quote' | 'general' | 'link'; export const TextFormatToolbar = forwardRef(({ textareaRef, onApplyFormat }, ref) => { const [selectedText, setSelectedText] = useState(''); const [selectedRange, setSelectedRange] = useState<{ start: number; end: number } | null>(null); const [activeFormats, setActiveFormats] = useState>(new Set()); const [showSettings, setShowSettings] = useState(false); const [settingsTab, setSettingsTab] = useState('code'); const [localStyleConfig, setLocalStyleConfig] = useState(DEFAULT_STYLE_CONFIG); // Estado temporal para la configuración (borrador) const [tempStyleConfig, setTempStyleConfig] = useState(null); const toolbarRef = useRef(null); // Expose methods via ref useImperativeHandle(ref, () => ({ openSettings: (tab) => { setShowSettings(true); setSettingsTab(tab); // Inicializar el borrador con la configuración actual setTempStyleConfig(localStyleConfig); }, syncStylesFromSelection: () => { checkSelectionFormat(); } })); useEffect(() => { const unsubscribe = subscribeToStyleConfig((config) => { setLocalStyleConfig(config); }); return () => unsubscribe(); }, []); // Usar tempStyleConfig si existe (modo edición), sino localStyleConfig const styleConfig = tempStyleConfig || localStyleConfig; // Helper to update style config (Draft logic) const handleConfigUpdate = (newConfig: StyleConfig) => { // Solo actualizamos el estado temporal, no aplicamos cambios al texto ni a la nube todavía setTempStyleConfig(newConfig); }; const handleConfirmSettings = () => { if (!tempStyleConfig) return; // Aplicar cambios permanentemente setLocalStyleConfig(tempStyleConfig); // Guardar en la nube y aplicar al texto seleccionado const newConfig = tempStyleConfig; const textarea = textareaRef.current; if (!textarea) { saveStyleConfigToCloud(newConfig); setShowSettings(false); setTempStyleConfig(null); return; } const cursor = textarea.selectionStart; const text = textarea.value; let isInBlock = false; // Check for Blockquote const quoteRegex = /]*>([\s\S]*?)<\/blockquote>/g; let match; while ((match = quoteRegex.exec(text)) !== null) { if (cursor >= match.index && cursor <= match.index + match[0].length) { isInBlock = true; const newStyle = getQuoteStyleString(newConfig.quote); const openTagRegex = /]*>/; const openTagMatch = openTagRegex.exec(match[0]); if (openTagMatch) { const newOpenTag = `
`; const newFullBlock = match[0].replace(openTagMatch[0], newOpenTag); setTimeout(() => { onApplyFormat(match.index, match.index + match[0].length, newFullBlock); }, 0); } break; } } // Check for Code if not in quote if (!isInBlock) { const codeRegex = /]*>([\s\S]*?)<\/code>/g; while ((match = codeRegex.exec(text)) !== null) { if (cursor >= match.index && cursor <= match.index + match[0].length) { isInBlock = true; const newStyle = getCodeStyleString(newConfig.code); const openTagRegex = /]*>/; const openTagMatch = openTagRegex.exec(match[0]); if (openTagMatch) { const newOpenTag = ``; const newFullBlock = match[0].replace(openTagMatch[0], newOpenTag); setTimeout(() => { onApplyFormat(match.index, match.index + match[0].length, newFullBlock); }, 0); } break; } } } // If NOT in a block, update global config if (!isInBlock) { saveStyleConfigToCloud(newConfig); } setShowSettings(false); setTempStyleConfig(null); }; const handleCancelSettings = () => { // Descartar cambios setTempStyleConfig(null); setShowSettings(false); }; const checkSelectionFormat = () => { // Placeholder for now, can be implemented to read styles from selection // For now, double click just opens the settings }; const updateSelectionState = () => { const textarea = textareaRef.current; if (!textarea) return; const start = textarea.selectionStart; const end = textarea.selectionEnd; const text = textarea.value.substring(start, end); setSelectedText(text); setSelectedRange({ start, end }); }; useEffect(() => { const textarea = textareaRef.current; if (!textarea) return; textarea.addEventListener('mouseup', updateSelectionState); textarea.addEventListener('keyup', updateSelectionState); textarea.addEventListener('click', updateSelectionState); textarea.addEventListener('select', updateSelectionState); textarea.addEventListener('focus', updateSelectionState); return () => { textarea.removeEventListener('mouseup', updateSelectionState); textarea.removeEventListener('keyup', updateSelectionState); textarea.removeEventListener('click', updateSelectionState); textarea.removeEventListener('select', updateSelectionState); textarea.removeEventListener('focus', updateSelectionState); }; }, [textareaRef]); useEffect(() => { const style = document.createElement('style'); style.id = 'dynamic-style-config'; const codeStyle = styleConfig.code; const quoteStyle = styleConfig.quote; const rgbaMatch = codeStyle.bgColor.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*[\d.]+)?\)/); let bgColorWithOpacity = codeStyle.bgColor; if (rgbaMatch) { const [_, r, g, b] = rgbaMatch; bgColorWithOpacity = `rgba(${r}, ${g}, ${b}, ${codeStyle.opacity / 100})`; } const widthValue = codeStyle.width === 'auto' ? 0 : parseFloat(codeStyle.width) || 0; const heightValue = codeStyle.height === 'auto' ? 0 : parseFloat(codeStyle.height) || 0; style.innerHTML = ` :not(pre) > code { background-color: ${bgColorWithOpacity} !important; color: ${codeStyle.textColor} !important; font-size: ${codeStyle.fontSize} !important; border-radius: ${codeStyle.borderRadius} !important; padding: 0px 2px !important; ${codeStyle.width !== 'auto' && widthValue > 0 ? `min-width: ${codeStyle.width} !important;` : ''} ${codeStyle.height !== 'auto' && heightValue > 0 ? `min-height: ${codeStyle.height} !important;` : ''} display: inline-flex !important; align-items: center !important; justify-content: center !important; vertical-align: middle !important; border: 1px solid rgba(255,255,255,0.05) !important; font-family: 'Consolas', 'Monaco', monospace !important; line-height: 1 !important; margin: 0 1px !important; } blockquote { background-color: ${quoteStyle.bgColor} !important; color: ${quoteStyle.textColor} !important; border-left: ${quoteStyle.borderWidth} solid ${quoteStyle.borderColor} !important; padding: ${quoteStyle.padding} !important; font-size: ${quoteStyle.fontSize} !important; border-radius: ${quoteStyle.borderRadius} !important; font-style: ${quoteStyle.italic ? 'italic' : 'normal'} !important; ${quoteStyle.width !== 'auto' ? `width: ${quoteStyle.width} !important;` : ''} ${quoteStyle.height !== 'auto' ? `min-height: ${quoteStyle.height} !important;` : ''} overflow: visible !important; position: relative !important; margin-bottom: 1rem !important; display: flex !important; flex-direction: column !important; justify-content: center !important; word-break: break-word !important; } button code, a code, .btn code { background: transparent !important; padding: 0 !important; color: inherit !important; border: none !important; margin: 0 !important; } `; const existingStyle = document.getElementById('dynamic-style-config'); if (existingStyle) { existingStyle.remove(); } document.head.appendChild(style); return () => { style.remove(); }; }, [styleConfig]); const applyFormat = (format: FormatType) => { let currentStart = selectedRange?.start || 0; let currentEnd = selectedRange?.end || 0; let currentText = selectedText; if (!selectedRange && textareaRef.current) { currentStart = textareaRef.current.selectionStart; currentEnd = textareaRef.current.selectionEnd; currentText = textareaRef.current.value.substring(currentStart, currentEnd); } if (textareaRef.current) { currentStart = textareaRef.current.selectionStart; currentEnd = textareaRef.current.selectionEnd; currentText = textareaRef.current.value.substring(currentStart, currentEnd); } let formatted = currentText; switch (format) { case 'bold': formatted = `**${currentText}**`; break; case 'italic': formatted = `*${currentText}*`; break; case 'code': formatted = `\`${currentText}\``; break; case 'quote': const lines = currentText.split('\n'); const quotedLines = lines.map(line => `> ${line}`); formatted = quotedLines.join('\n') + '\n\n'; break; case 'uppercase': formatted = currentText.toUpperCase(); break; case 'lowercase': formatted = currentText.toLowerCase(); break; case 'plain': formatted = currentText .replace(/\*\*/g, '') .replace(/\*/g, '') .replace(/`/g, '') .replace(/<[^>]+>/g, ''); // Remove HTML tags break; } onApplyFormat(currentStart, currentEnd, formatted); setSelectedText(formatted); const newActiveFormats = new Set(activeFormats); // Code format is transient/stateless for this toolbar if (format === 'code') { // Do nothing to activeFormats, or remove if it was there (though it shouldn't be) if (newActiveFormats.has('code')) { newActiveFormats.delete('code'); } } else { if (newActiveFormats.has(format)) { newActiveFormats.delete(format); } else { if (['uppercase', 'lowercase', 'plain'].includes(format)) { newActiveFormats.clear(); } newActiveFormats.add(format); } } setActiveFormats(newActiveFormats); }; const getCodeStyleString = (config: CodeStyleConfig) => { const rgbaMatch = config.bgColor.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*[\d.]+)?\)/); let bgColor = config.bgColor; if (rgbaMatch) { const [_, r, g, b] = rgbaMatch; bgColor = `rgba(${r}, ${g}, ${b}, ${config.opacity / 100})`; } return [ `background-color: ${bgColor}`, `color: ${config.textColor}`, `font-size: ${config.fontSize}`, `border-radius: ${config.borderRadius}`, `padding: 0px 2px`, config.width !== 'auto' ? `min-width: ${config.width}` : '', config.height !== 'auto' ? `min-height: ${config.height}` : '', 'display: inline-flex', 'align-items: center', 'justify-content: center', 'vertical-align: middle', 'border: 1px solid rgba(255,255,255,0.05)', "font-family: 'Consolas', 'Monaco', monospace", 'line-height: 1', 'margin: 0 1px' ].filter(Boolean).join('; '); }; const getQuoteStyleString = (config: QuoteStyleConfig) => { return [ `background-color: ${config.bgColor}`, `color: ${config.textColor}`, `border-left: ${config.borderWidth} solid ${config.borderColor}`, `padding: ${config.padding}`, `font-size: ${config.fontSize}`, `border-radius: ${config.borderRadius}`, config.italic ? 'font-style: italic' : 'font-style: normal', config.width !== 'auto' ? `width: ${config.width}` : '', config.height !== 'auto' ? `height: ${config.height}` : '', 'overflow: visible', 'position: relative', 'margin-bottom: 1rem', 'display: flex', 'flex-direction: column', 'justify-content: center' ].filter(Boolean).join('; '); }; return ( <>
{showSettings && (
)} ); });