import { useCallback, useEffect, useRef, type KeyboardEventHandler, type PointerEventHandler } from "react"; import { createPortal } from "react-dom"; import type { Editor } from "@tiptap/core"; import { EditorContent } from "@tiptap/react"; import { cn } from "../../../app/renderer/lib/utils"; import type { TranslateFn } from "../../../i18n/index"; import type { AgentRichTextContextMenuState } from "./AgentRichTextEditor.types"; export function AgentRichTextEditorSurface({ className, contextMenu, copySelection, cutSelection, disabled, editor, handleKeyDownCapture, handlePointerDownCapture, pasteClipboardText, placeholder, testId, t }: { className?: string; contextMenu: AgentRichTextContextMenuState | null; copySelection: () => void | Promise; cutSelection: () => void | Promise; disabled: boolean; editor: Editor | null; handleKeyDownCapture: KeyboardEventHandler; handlePointerDownCapture?: PointerEventHandler; pasteClipboardText: () => void | Promise; placeholder: string; testId?: string; t: TranslateFn; }): React.JSX.Element { useEffect(() => { if (!editor) return; editor.setEditable(!disabled); editor.view.dom.setAttribute("aria-disabled", disabled ? "true" : "false"); editor.view.dom.setAttribute("aria-label", placeholder); if (testId) editor.view.dom.setAttribute("data-testid", testId); else editor.view.dom.removeAttribute("data-testid"); editor.view.dispatch( editor.state.tr.setMeta("agentRichTextPlaceholder", placeholder) ); }, [disabled, editor, placeholder, testId]); return (
{editor ? ( ) : (
)} {contextMenu ? createPortal(
event.preventDefault()} onPointerDown={(event) => event.stopPropagation()} >
, document.body ) : null}
); } function AgentRichTextContextMenuButton({ disabled, label, onSelect }: { disabled: boolean; label: string; onSelect: () => void | Promise; }): React.JSX.Element { const selectionStartedRef = useRef(false); const select = useCallback(() => { if (disabled || selectionStartedRef.current) return; selectionStartedRef.current = true; void Promise.resolve(onSelect()).finally(() => { selectionStartedRef.current = false; }); }, [disabled, onSelect]); return ( ); }