import { useEffect } from 'react' import FormatBoldOutlinedIcon from '@mui/icons-material/FormatBoldOutlined' import FormatItalicOutlinedIcon from '@mui/icons-material/FormatItalicOutlined' import FormatListBulletedOutlinedIcon from '@mui/icons-material/FormatListBulletedOutlined' import Box from '@mui/material/Box' import IconButton from '@mui/material/IconButton' import Stack from '@mui/material/Stack' import Placeholder from '@tiptap/extension-placeholder' import { EditorContent, useEditor } from '@tiptap/react' import TiptapStarterKit from '@tiptap/starter-kit' type RichTextMessageEditorProps = { value: string onChange: (next: string) => void placeholder?: string disabled?: boolean } export function RichTextMessageEditor({ value, onChange, placeholder = 'Введите сообщение', disabled = false, }: RichTextMessageEditorProps) { const initialContent = value.trim() ? value : '
' const editor = useEditor({ extensions: [ TiptapStarterKit.configure({ heading: false, codeBlock: false, blockquote: false, horizontalRule: false }), Placeholder.configure({ placeholder }), ], content: initialContent, editable: !disabled, onUpdate: ({ editor: tiptap }) => { const plainText = tiptap.getText().trim() onChange(plainText ? tiptap.getHTML() : '') }, }) useEffect(() => { if (!editor) return editor.setEditable(!disabled) }, [disabled, editor]) useEffect(() => { if (!editor) return const normalizedValue = value.trim() ? value : '' if (editor.getHTML() === normalizedValue) return editor.commands.setContent(normalizedValue, { emitUpdate: false }) }, [editor, value]) return (