import { NodeViewWrapper, NodeViewContent } from '@tiptap/react' import type { NodeViewProps } from '@tiptap/react' import { useState, useCallback, useRef, useEffect } from 'react' import { getLanguageIconUrl, getLanguageDisplayName, SUPPORTED_LANGUAGES } from './icons' import { Icons } from '../../../shared/Icons' export function CodeBlockView({ node, updateAttributes }: NodeViewProps) { const { language, title } = node.attrs const [showLanguageMenu, setShowLanguageMenu] = useState(false) const [copied, setCopied] = useState(false) const [isEditingTitle, setIsEditingTitle] = useState(false) const [titleValue, setTitleValue] = useState(title || '') const titleInputRef = useRef(null) const menuRef = useRef(null) const iconUrl = language ? getLanguageIconUrl(language) : null const displayName = language ? getLanguageDisplayName(language) : 'Plain text' const handleCopy = useCallback(async () => { const code = node.textContent await navigator.clipboard.writeText(code) setCopied(true) setTimeout(() => setCopied(false), 2000) }, [node.textContent]) const handleLanguageSelect = useCallback((lang: string) => { updateAttributes({ language: lang }) setShowLanguageMenu(false) }, [updateAttributes]) const handleTitleSubmit = useCallback(() => { updateAttributes({ title: titleValue || null }) setIsEditingTitle(false) }, [titleValue, updateAttributes]) const handleTitleKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.key === 'Enter') { e.preventDefault() handleTitleSubmit() } if (e.key === 'Escape') { setTitleValue(title || '') setIsEditingTitle(false) } }, [handleTitleSubmit, title]) useEffect(() => { if (isEditingTitle && titleInputRef.current) { titleInputRef.current.focus() titleInputRef.current.select() } }, [isEditingTitle]) useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (menuRef.current && !menuRef.current.contains(e.target as Node)) { setShowLanguageMenu(false) } } if (showLanguageMenu) { document.addEventListener('mousedown', handleClickOutside) } return () => document.removeEventListener('mousedown', handleClickOutside) }, [showLanguageMenu]) return ( {/* Header bar */}
{/* Language selector */}
{showLanguageMenu && (
{SUPPORTED_LANGUAGES.map((lang) => { const langIcon = getLanguageIconUrl(lang) return ( ) })}
)}
{/* Divider */}
{/* Title (editable) */}
{isEditingTitle ? ( setTitleValue(e.target.value)} onBlur={handleTitleSubmit} onKeyDown={handleTitleKeyDown} placeholder="filename.ext" className="w-full px-1 py-0.5 text-xs bg-white border border-zinc-300 rounded focus:outline-none focus:border-zinc-400" contentEditable={false} /> ) : ( )}
{/* Copy button */}
{/* Code content */}
        
          
        
      
) }