import SmallCloseButton from '@/pages/_components/small-close-button'; import ThumbImg from '@/pages/playground/components/thumb-img'; import useAddImage from '@/pages/playground/hooks/use-add-image'; import { DeleteOutlined } from '@ant-design/icons'; import { useIntl } from '@umijs/max'; import { Button, Input, Tooltip } from 'antd'; import classNames from 'classnames'; import React, { useState } from 'react'; import './styles/row-textarea.less'; interface SystemMessageProps { style?: React.CSSProperties; data: { content: string; imgs?: { uid: number | string; dataUrl: string }[]; uid: number | string; name: string; role?: string; }; placeholder?: string; label?: React.ReactNode; height?: number; onUploadImage?: (list: { uid: number | string; dataUrl: string }[]) => void; onDeleteImage?: ( updatedImgs: { uid: number | string; dataUrl: string }[] ) => void; onChange: (e: any) => void; onPaste?: (e: any) => void; onDelete?: () => void; onSelect?: (data: { start: number; end: number; beforeText: string; afterText: string; }) => void; } const RowTextarea: React.FC = (props) => { const { data, onDeleteImage, onUploadImage, onChange, onSelect, onDelete, style, label, placeholder, height = 46 } = props; const intl = useIntl(); const rowTextAreaRef = React.useRef(null); const [autoSize, setAutoSize] = useState<{ minRows: number; maxRows: number; focus: boolean; }>({ minRows: 1, maxRows: 1, focus: false }); const handleFocus = () => { setAutoSize({ minRows: 3, maxRows: 3, focus: true }); setTimeout(() => { rowTextAreaRef.current?.focus?.({ cursor: 'end' }); }, 50); }; const handleBlur = (e: any) => { setAutoSize({ minRows: 2, maxRows: 2, focus: false }); }; const handleOnChange = (e: any) => { onChange?.(e); }; const handleOnPaste = (e: any) => { props.onPaste?.(e); }; const handleOnSelect = (e: any) => { e.stopPropagation(); const start = e.target.selectionStart; const end = e.target.selectionEnd; const beforeText = data.content.substring(0, start); const afterText = data.content.substring(end, data.content.length); onSelect?.({ start, end, beforeText, afterText }); }; const handleUploadImage = ( list: { uid: number | string; dataUrl: string }[] ) => { onUploadImage?.(list); }; const { ImageURLInput, UploadImageButton, isFromUrl, dropDownOpen } = useAddImage({ size: 'small', inputProps: { variant: 'filled' }, handleUpdateImgList: handleUploadImage, updateUidCount: () => `img-${Date.now()}` }); const handleClear = () => { onChange?.({ target: { value: '' } }); onDeleteImage?.([]); }; const handleDeleteImage = (uid: number) => { const updatedImgs = (data.imgs || []).filter((img) => img.uid !== uid); onDeleteImage?.(updatedImgs); }; const renderTextContent = () => { if (data.imgs?.length || isFromUrl) { return null; } return ( <> {label && {label}} {data.content || ( {placeholder} )} ); }; const expanded = autoSize.focus && !isFromUrl && !data.imgs?.length; return (
{!data.imgs?.length && (
{label && {label}} { }
)} {!expanded && (
{renderTextContent()} {!!data.imgs?.length && (
{label && {label}}
)}
)}
{ImageURLInput}
{!expanded && (data.content || !!data.imgs?.length) && ( )} {UploadImageButton}
); }; export default RowTextarea;