2025-10-14 13:31:48 +08:00
|
|
|
import { cn } from '@/lib/utils';
|
2025-04-25 11:15:44 +08:00
|
|
|
import { forwardRef } from 'react';
|
|
|
|
|
import { useFormContext } from 'react-hook-form';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import {
|
|
|
|
|
FormControl,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
|
|
|
|
} from './ui/form';
|
|
|
|
|
import { Input, InputProps } from './ui/input';
|
|
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
|
value?: string | undefined;
|
|
|
|
|
onChange?: (val: string | undefined) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const DelimiterInput = forwardRef<HTMLInputElement, InputProps & IProps>(
|
2025-10-09 12:36:19 +08:00
|
|
|
({ value, onChange, maxLength, defaultValue, ...props }, ref) => {
|
|
|
|
|
const nextValue = value
|
|
|
|
|
?.replaceAll('\n', '\\n')
|
|
|
|
|
.replaceAll('\t', '\\t')
|
|
|
|
|
.replaceAll('\r', '\\r');
|
2025-04-25 11:15:44 +08:00
|
|
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
const val = e.target.value;
|
2025-10-09 12:36:19 +08:00
|
|
|
const nextValue = val
|
|
|
|
|
.replaceAll('\\n', '\n')
|
|
|
|
|
.replaceAll('\\t', '\t')
|
|
|
|
|
.replaceAll('\\r', '\r');
|
2025-04-25 11:15:44 +08:00
|
|
|
onChange?.(nextValue);
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<Input
|
|
|
|
|
value={nextValue}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
maxLength={maxLength}
|
|
|
|
|
defaultValue={defaultValue}
|
|
|
|
|
ref={ref}
|
2025-10-14 13:31:48 +08:00
|
|
|
className={cn('bg-bg-base', props.className)}
|
2025-10-09 12:36:19 +08:00
|
|
|
{...props}
|
2025-04-25 11:15:44 +08:00
|
|
|
></Input>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export function DelimiterFormField() {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const form = useFormContext();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name={'parser_config.delimiter'}
|
2025-06-19 16:40:30 +08:00
|
|
|
render={({ field }) => {
|
|
|
|
|
if (typeof field.value === 'undefined') {
|
|
|
|
|
// default value set
|
|
|
|
|
form.setValue('parser_config.delimiter', '\n');
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<FormItem className=" items-center space-y-0 ">
|
2025-08-19 17:35:32 +08:00
|
|
|
<div className="flex items-center gap-1">
|
2025-06-19 16:40:30 +08:00
|
|
|
<FormLabel
|
2025-09-05 09:57:15 +08:00
|
|
|
required
|
2025-06-19 16:40:30 +08:00
|
|
|
tooltip={t('knowledgeDetails.delimiterTip')}
|
2025-10-11 18:45:55 +08:00
|
|
|
className="text-sm text-text-secondary whitespace-break-spaces w-1/4"
|
2025-06-19 16:40:30 +08:00
|
|
|
>
|
|
|
|
|
{t('knowledgeDetails.delimiter')}
|
|
|
|
|
</FormLabel>
|
|
|
|
|
<div className="w-3/4">
|
|
|
|
|
<FormControl>
|
|
|
|
|
<DelimiterInput {...field}></DelimiterInput>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex pt-1">
|
|
|
|
|
<div className="w-1/4"></div>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
|
|
|
|
</FormItem>
|
|
|
|
|
);
|
|
|
|
|
}}
|
2025-04-25 11:15:44 +08:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|