import React from 'react';
import { useRichTextEditor } from './use-rich-text-editor';
import { cn } from '../../shared/utils';
import { Button } from '../button';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '../dropdown-menu';
import { Popover, PopoverContent, PopoverTrigger } from '../popover';
import {
Bold,
Italic,
Underline,
AlignLeft,
AlignCenter,
AlignRight,
List,
ListOrdered,
Type,
Heading1,
Heading2,
Heading3,
ChevronDown,
Undo,
Redo,
Search,
ChevronUp,
X,
Link as LinkIcon,
} from 'lucide-react';
import { Input } from '../input';
export interface RichTextEditorProps {
value: string;
onChange?: (value: string) => void;
placeholder?: string;
className?: string;
actionButton?: React.ReactNode;
allowSearch?: boolean;
allowLinks?: boolean;
allowUndoRedo?: boolean;
allowHeadings?: boolean;
allowFormatting?: boolean;
allowAlignment?: boolean;
allowLists?: boolean;
showWordCount?: boolean;
showCharacterCount?: boolean;
/** Disables all editing and applies opacity-50. Hides the toolbar. */
disabled?: boolean;
/** Makes the editor non-editable while keeping full visual fidelity. Hides the toolbar. */
readOnly?: boolean;
/** Called when the editor area receives focus. */
onFocus?: () => void;
/** Called when the editor area loses focus. */
onBlur?: () => void;
/** Minimum height of the editable area (CSS value, e.g. "200px"). */
minHeight?: string;
/** Maximum height of the editable area (CSS value, e.g. "600px"). */
maxHeight?: string;
}
// ─────────────────────────────────────────────────────────────────────────────
// Component
// ─────────────────────────────────────────────────────────────────────────────
export function RichTextEditor({
value,
onChange,
placeholder,
className,
actionButton,
allowSearch = true,
allowLinks = true,
allowUndoRedo = true,
allowHeadings = true,
allowFormatting = true,
allowAlignment = true,
allowLists = true,
showWordCount = true,
showCharacterCount = true,
disabled = false,
readOnly = false,
onFocus,
onBlur,
minHeight,
maxHeight,
}: RichTextEditorProps) {
const isEditable = !disabled && !readOnly;
const showToolbar = isEditable;
const {
editorRef,
searchInputRef,
linkInputRef,
activeFormats,
isSearchOpen,
setIsSearchOpen,
searchQuery,
setSearchQuery,
linkUrl,
setLinkUrl,
isLinkOpen,
hasSavedSelection,
wordCount,
characterCount,
updateActiveFormats,
execCommand,
handleInput,
performSearch,
handleCreateLink,
handleUnlink,
onLinkPopoverOpenChange,
} = useRichTextEditor({ value, onChange });
const getCurrentBlockLabel = () => {
if (activeFormats.h1)
return (
<>
Selecione um texto para criar um link.
) : (