Files
FastGPT/packages/web/components/common/Textarea/PromptEditor/index.tsx
Archer 3f9b0fa1d4 V4.12.3 features (#5595)
* refactor: remove ModelProviderIdType and update related types (#5549)

* perf: model provider

* fix eval create split (#5570)

* git rebase --continuedoc

* add more variable types (#5540)

* variable types

* password

* time picker

* internal var

* file

* fix-test

* time select default value & range

* password & type render

* fix

* fix build

* fix

* move method

* split date select

* icon

* perf: variable code

* prompt editor add markdown plugin (#5556)

* editor markdown

* fix build

* pnpm lock

* add props

* update code

* fix list

* editor ui

* fix variable reset (#5586)

* perf: variables type code

* customize lexical indent (#5588)

* perf: multiple selector

* perf: tab plugin

* doc

* refactor: update workflow constants to use ToolTypeEnum (#5491)

* refactor: replace FlowNodeTemplateTypeEnum with string literals in workflow templates

* perf: tool type

---------

Co-authored-by: archer <545436317@qq.com>

* update doc

* fix: make table's row more natural while dragging it (#5596)

* feat: add APIGetTemplate function and refactor template fetching logic (#5498)

* feat: add APIGetTemplate function and refactor template fetching logic

* chore: adjust the code

* chore: update sdk

---------

Co-authored-by: FinleyGe <m13203533462@163.com>

* perf init system

* doc

* remove log

* remove i18n

* perf: variables render

---------

Co-authored-by: Ctrlz <143257420+ctrlz526@users.noreply.github.com>
Co-authored-by: heheer <heheer@sealos.io>
Co-authored-by: 伍闲犬 <whoeverimf5@gmail.com>
Co-authored-by: FinleyGe <m13203533462@163.com>
2025-09-07 14:41:48 +08:00

116 lines
2.7 KiB
TypeScript

import { Box, Button, ModalBody, ModalFooter, useDisclosure } from '@chakra-ui/react';
import React, { useMemo, useCallback } from 'react';
import { editorStateToText } from './utils';
import type { EditorProps } from './Editor';
import Editor from './Editor';
import MyModal from '../../MyModal';
import { useTranslation } from 'next-i18next';
import type { EditorState, LexicalEditor } from 'lexical';
import type { FormPropsType } from './type';
const PromptEditor = ({
showOpenModal = true,
value,
onChange,
onBlur,
title,
isDisabled,
...props
}: FormPropsType &
EditorProps & {
title?: string;
isDisabled?: boolean;
onChange?: (text: string) => void;
onBlur?: (text: string) => void;
}) => {
const { isOpen, onOpen, onClose } = useDisclosure();
const { t } = useTranslation();
const onChangeInput = useCallback(
(editorState: EditorState, editor: LexicalEditor) => {
const text = editorStateToText(editor);
onChange?.(text);
},
[onChange]
);
const onBlurInput = useCallback(
(editor: LexicalEditor) => {
if (onBlur) {
const text = editorStateToText(editor);
onBlur(text);
}
},
[onBlur]
);
const formattedValue = useMemo(() => {
if (typeof value === 'object') {
return JSON.stringify(value);
}
if (value === undefined || value === null) {
return '';
}
return String(value || '');
}, [value]);
return (
<>
<Box position="relative">
<Editor
{...props}
showOpenModal={showOpenModal}
onOpenModal={onOpen}
value={formattedValue}
onChange={onChangeInput}
onChangeText={onChange}
onBlur={onBlurInput}
/>
{isDisabled && (
<Box
position="absolute"
top={0}
left={0}
right={0}
bottom={0}
bg="rgba(255, 255, 255, 0.4)"
borderRadius="md"
zIndex={1}
cursor="not-allowed"
/>
)}
</Box>
<MyModal
isOpen={isOpen}
onClose={onClose}
iconSrc="modal/edit"
title={title || t('common:Edit')}
w={'full'}
>
<ModalBody>
<Editor
{...props}
minH={400}
maxH={400}
showOpenModal={false}
value={value}
onChange={onChangeInput}
onChangeText={onChange}
onBlur={onBlurInput}
/>
</ModalBody>
<ModalFooter>
<Button mr={2} onClick={onClose} px={6}>
{t('common:Confirm')}
</Button>
</ModalFooter>
</MyModal>
</>
);
};
export default React.memo(PromptEditor);