fix: paste text can not overwrite the selection

This commit is contained in:
jialin
2025-01-15 16:11:57 +08:00
parent 5bfca0f86f
commit 53a4ebb66e
7 changed files with 94 additions and 56 deletions
+31 -1
View File
@@ -13,10 +13,24 @@ interface SystemMessageProps {
height?: number;
onChange: (e: any) => void;
onPaste?: (e: any) => void;
onSelect?: (data: {
start: number;
end: number;
beforeText: string;
afterText: string;
}) => void;
}
const RowTextarea: React.FC<SystemMessageProps> = (props) => {
const { value, onChange, style, label, placeholder, height = 46 } = props;
const {
value,
onChange,
onSelect,
style,
label,
placeholder,
height = 46
} = props;
const intl = useIntl();
const rowTextAreaRef = React.useRef<any>(null);
const [autoSize, setAutoSize] = useState<{
@@ -57,6 +71,21 @@ const RowTextarea: React.FC<SystemMessageProps> = (props) => {
props.onPaste?.(e);
};
const handleOnSelect = (e: any) => {
e.stopPropagation();
const start = e.target.selectionStart;
const end = e.target.selectionEnd;
const beforeText = value.substring(0, start);
const afterText = value.substring(end, value.length);
onSelect?.({
start,
end,
beforeText,
afterText
});
};
return (
<div
className={classNames('row-textarea', {
@@ -87,6 +116,7 @@ const RowTextarea: React.FC<SystemMessageProps> = (props) => {
onBlur={handleBlur}
allowClear={false}
onChange={handleOnChange}
onSelect={handleOnSelect}
onPaste={handleOnPaste}
></Input.TextArea>
</div>