'use client'; import React, { useState, useRef } from 'react'; import { Bold, Italic, List, Link, Eye, EyeOff, Type, Quote } from 'lucide-react'; interface TextEditorProps { value: string; onChange: (value: string) => void; placeholder?: string; rows?: number; className?: string; } export default function TextEditor({ value, onChange, placeholder = 'Введите текст...', rows = 12, className = '' }: TextEditorProps) { const [showPreview, setShowPreview] = useState(false); const textareaRef = useRef(null); const insertText = (before: string, after: string = '') => { const textarea = textareaRef.current; if (!textarea) return; const start = textarea.selectionStart; const end = textarea.selectionEnd; const selectedText = value.substring(start, end); const newText = value.substring(0, start) + before + selectedText + after + value.substring(end); onChange(newText); // Восстанавливаем фокус и позицию курсора setTimeout(() => { textarea.focus(); textarea.setSelectionRange(start + before.length, start + before.length + selectedText.length); }, 0); }; const insertAtCursor = (text: string) => { const textarea = textareaRef.current; if (!textarea) return; const start = textarea.selectionStart; const end = textarea.selectionEnd; const newText = value.substring(0, start) + text + value.substring(end); onChange(newText); // Восстанавливаем фокус и позицию курсора setTimeout(() => { textarea.focus(); textarea.setSelectionRange(start + text.length, start + text.length); }, 0); }; const formatContent = (content: string) => { return content .replace(/\n/g, '
') .replace(/\*\*(.*?)\*\*/g, '$1') .replace(/\*(.*?)\*/g, '$1') .replace(/^### (.*$)/gm, '

$1

') .replace(/^## (.*$)/gm, '

$1

') .replace(/^# (.*$)/gm, '

$1

') .replace(/^> (.*$)/gm, '
$1
') .replace(/^- (.*$)/gm, '
  • $1
  • ') .replace(/()/g, '') .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1'); }; const toolbarButtons = [ { icon: Bold, label: 'Жирный', action: () => insertText('**', '**'), shortcut: 'Ctrl+B' }, { icon: Italic, label: 'Курсив', action: () => insertText('*', '*'), shortcut: 'Ctrl+I' }, { icon: Type, label: 'Заголовок', action: () => insertAtCursor('## '), shortcut: 'Ctrl+H' }, { icon: Quote, label: 'Цитата', action: () => insertAtCursor('> '), shortcut: 'Ctrl+Q' }, { icon: List, label: 'Список', action: () => insertAtCursor('- '), shortcut: 'Ctrl+L' }, { icon: Link, label: 'Ссылка', action: () => insertText('[', '](url)'), shortcut: 'Ctrl+K' } ]; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.ctrlKey || e.metaKey) { switch (e.key) { case 'b': e.preventDefault(); insertText('**', '**'); break; case 'i': e.preventDefault(); insertText('*', '*'); break; case 'h': e.preventDefault(); insertAtCursor('## '); break; case 'q': e.preventDefault(); insertAtCursor('> '); break; case 'l': e.preventDefault(); insertAtCursor('- '); break; case 'k': e.preventDefault(); insertText('[', '](url)'); break; } } }; return (
    {/* Toolbar */}
    {toolbarButtons.map((button, index) => ( ))}
    {/* Content */}
    {showPreview ? ( /* Preview Mode */
    ) : ( /* Editor Mode */