import React, { useState, useRef, useEffect } from 'react'; import { NodeViewWrapper } from '@tiptap/react'; import { AlignLeft, AlignCenter, AlignRight, Maximize2, Link as LinkIcon, X, Check } from 'lucide-react'; export const ResizableImageComponent = (props: any) => { const { node, updateAttributes, selected, editor } = props; const [width, setWidth] = useState(node.attrs.width || '100%'); const [height, setHeight] = useState(node.attrs.height || 'auto'); const [align, setAlign] = useState(node.attrs.align || 'center'); const [isResizing, setIsResizing] = useState(false); const [isLightboxOpen, setIsLightboxOpen] = useState(false); const [isLinkMenuOpen, setIsLinkMenuOpen] = useState(false); const [tempLink, setTempLink] = useState(''); const imageRef = useRef(null); const resizeRef = useRef<{ startX: number; startWidth: number } | null>(null); useEffect(() => { setWidth(node.attrs.width || '100%'); setHeight(node.attrs.height || 'auto'); setAlign(node.attrs.align || 'center'); }, [node.attrs]); const handleMouseDown = (e: React.MouseEvent) => { e.preventDefault(); if (imageRef.current) { setIsResizing(true); resizeRef.current = { startX: e.clientX, startWidth: imageRef.current.offsetWidth, }; document.addEventListener('mousemove', handleMouseMove); document.addEventListener('mouseup', handleMouseUp); } }; const handleMouseMove = (e: MouseEvent) => { if (!resizeRef.current) return; const diff = e.clientX - resizeRef.current.startX; const newWidth = resizeRef.current.startWidth + diff; // Limit min width if (newWidth > 100) { setWidth(`${newWidth}px`); } }; const handleMouseUp = () => { setIsResizing(false); resizeRef.current = null; document.removeEventListener('mousemove', handleMouseMove); document.removeEventListener('mouseup', handleMouseUp); // Update attributes only on mouse up to avoid too many updates if (imageRef.current) { updateAttributes({ width: `${imageRef.current.offsetWidth}px` }); } }; const handleAlign = (newAlign: 'left' | 'center' | 'right') => { setAlign(newAlign); updateAttributes({ align: newAlign }); }; const handleLink = () => { setTempLink(node.attrs.href || ''); setIsLinkMenuOpen(true); }; const saveLink = () => { updateAttributes({ href: tempLink || null }); setIsLinkMenuOpen(false); }; const handleImageClick = (e: React.MouseEvent) => { // Prevent navigation in edit mode if (editor.isEditable) { e.preventDefault(); } // Triple click detection handled by TipTap or manual logic if needed. // For simplicity, we can use detail property if available or just rely on toolbar. if (e.detail === 3 && editor.isEditable) { handleLink(); } }; const handleDoubleClick = (e: React.MouseEvent) => { if (!editor.isEditable && !node.attrs.href) { setIsLightboxOpen(true); } }; let containerStyle: React.CSSProperties = { display: 'flex', justifyContent: align === 'left' ? 'flex-start' : align === 'right' ? 'flex-end' : 'center', margin: '1rem 0', clear: 'both', // Default to clear position: 'relative', }; // For "Word-like" wrapping, we might use float. // However, flexbox centering doesn't work well with float. // If align is left/right, we can use float. if (align === 'left') { containerStyle = { float: 'left', marginRight: '1rem', marginBottom: '0.5rem', display: 'inline-block', }; } else if (align === 'right') { containerStyle = { float: 'right', marginLeft: '1rem', marginBottom: '0.5rem', display: 'inline-block', }; } return (
{node.attrs.href ? ( {node.attrs.alt} ) : ( {node.attrs.alt} )} {/* Resize Handle - Only in Edit Mode */} {selected && editor.isEditable && (
)} {/* Toolbar (Visible on hover or selected) - Only in Edit Mode */} {editor.isEditable && (
)}
{/* Link Menu Modal */} {isLinkMenuOpen && (

Editar Enlace

setTempLink(e.target.value)} placeholder="https://ejemplo.com" className="w-full bg-black/20 border border-white/10 rounded-lg px-3 py-2 text-sm text-white placeholder-zinc-500 focus:outline-none focus:border-primary transition-colors" autoFocus onKeyDown={(e) => { if (e.key === 'Enter') saveLink(); if (e.key === 'Escape') setIsLinkMenuOpen(false); }} />
)} {/* Lightbox Overlay */} {isLightboxOpen && (
setIsLightboxOpen(false)} > {node.attrs.alt} e.stopPropagation()} />
)} ); };