import React, { useState, useRef } from 'react'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import { Icons } from './IconSet'; interface MarkdownEditorProps { value: string; onChange: (value: string) => void; className?: string; } const MarkdownEditor: React.FC = ({ value, onChange, className = '' }) => { const textareaRef = useRef(null); const insertText = (before: string, after: string = '', placeholder: string = 'text') => { const textarea = textareaRef.current; if (!textarea) return; const start = textarea.selectionStart; const end = textarea.selectionEnd; const selectedText = value.substring(start, end); const textToInsert = selectedText || placeholder; const newText = value.substring(0, start) + before + textToInsert + after + value.substring(end); onChange(newText); // Set cursor position after the inserted text setTimeout(() => { const newCursorPos = start + before.length + textToInsert.length + after.length; textarea.setSelectionRange(newCursorPos, newCursorPos); textarea.focus(); }, 0); }; const formatButtons = [ { icon: 'H', label: 'Überschrift', action: () => insertText('# ', ''), className: 'text-lg font-bold' }, { icon: , label: 'Fett', action: () => insertText('**', '**', 'fetter Text'), }, { icon: , label: 'Kursiv', action: () => insertText('*', '*', 'kursiver Text'), }, { icon: , label: 'Liste', action: () => insertText('- ', ''), }, { icon: , label: 'Nummerierte Liste', action: () => insertText('1. ', ''), }, { icon: , label: 'Zitat', action: () => insertText('> ', ''), }, { icon: '🔗', label: 'Link', action: () => insertText('[', '](url)', 'Link-Text'), }, { icon: '📷', label: 'Bild', action: () => insertText('![', '](url)', 'Alt-Text'), }, ]; return (
{/* Toolbar */}
{formatButtons.map((button, index) => ( ))}
{/* Editor and Preview */}
{/* Textarea */}