'use client' import { useEffect, useRef } from 'react' import { createPortal } from 'react-dom' import { useLanguage } from '@/lib/i18n' import { Link2, ImageIcon, Video, Code, FileText, Layout } from 'lucide-react' export type SmartPasteExtendedMenuProps = { type: 'url' | 'code' text: string anchor: { top: number; left: number } isImage?: boolean isVideo?: boolean onLink?: () => void onLinkPreview?: () => void onImage?: () => void onVideo?: () => void onCodeBlock?: () => void onPlain: () => void onClose: () => void } export function SmartPasteExtendedMenu({ type, text, anchor, isImage, isVideo, onLink, onLinkPreview, onImage, onVideo, onCodeBlock, onPlain, onClose, }: SmartPasteExtendedMenuProps) { const { t } = useLanguage() const menuRef = useRef(null) useEffect(() => { function handleClickOutside(e: MouseEvent) { if (menuRef.current && !menuRef.current.contains(e.target as Node)) { onClose() } } function handleKeyDown(e: KeyboardEvent) { if (e.key === 'Escape') onClose() } document.addEventListener('mousedown', handleClickOutside) document.addEventListener('keydown', handleKeyDown) return () => { document.removeEventListener('mousedown', handleClickOutside) document.removeEventListener('keydown', handleKeyDown) } }, [onClose]) const menuStyle: React.CSSProperties = { position: 'fixed', left: anchor.left, top: anchor.top + 8, zIndex: 9999, maxWidth: 340, width: '100%', } if (Number(menuStyle.left) + 340 > window.innerWidth) { menuStyle.left = Math.max(8, window.innerWidth - 348) } // Estimation de hauteur pour éviter les débordements const expectedHeight = type === 'url' ? (200) : (140) if (Number(menuStyle.top) + expectedHeight > window.innerHeight) { menuStyle.top = Math.max(8, anchor.top - expectedHeight - 8) } const shortenedText = text.length > 50 ? `${text.slice(0, 47)}...` : text return createPortal(

{type === 'url' ? t('richTextEditor.smartPasteUrlTitle') || 'Lien ou Média détecté' : t('richTextEditor.smartPasteCodeTitle') || 'Code source détecté'}

{shortenedText}

{type === 'url' ? t('richTextEditor.smartPasteUrlHint') || 'Que souhaitez-vous faire avec ce lien ?' : t('richTextEditor.smartPasteCodeHint') || 'Du code source a été détecté. Souhaitez-vous l\'insérer comme bloc de code ?'}

{type === 'url' && ( <> {onLinkPreview && ( )} {isImage && onImage && ( )} {isVideo && onVideo && ( )} )} {type === 'code' && onCodeBlock && ( )}
, document.body, ) }