Files
FastGPT/packages/web/components/common/Textarea/PromptEditor/Editor.tsx
Archer 2ed1545eb5 V4.12.4 features (#5626)
* fix: push again, user select option button and form input radio content overflow (#5601)

* fix: push again, user select option button and form input radio content overflow

* fix: use useCallback instead of useMemo, fix unnecessary delete

* fix: Move the variable inside the component

* fix: do not pass valueLabel to MySelect

* ui

* del collection api adapt

* refactor: inherit permission (#5529)

* refactor: permission update conflict check function

* refactor(permission): app collaborator update api

* refactor(permission): support app update collaborator

* feat: support fe permission conflict check

* refactor(permission): app permission

* refactor(permission): dataset permission

* refactor(permission): team permission

* chore: fe adjust

* fix: type error

* fix: audit pagiation

* fix: tc

* chore: initv4130

* fix: app/dataset auth logic

* chore: move code

* refactor(permission): remove selfPermission

* fix: mock

* fix: test

* fix: app & dataset auth

* fix: inherit

* test(inheritPermission): test syncChildrenPermission

* prompt editor add list plugin (#5620)

* perf: search result (#5608)

* fix: table size (#5598)

* temp: list value

* backspace

* optimize code

---------

Co-authored-by: Archer <545436317@qq.com>
Co-authored-by: 伍闲犬 <whoeverimf5@gmail.com>

* fix: fe & member list (#5619)

* chore: initv4130

* fix: MemberItemCard

* fix: MemberItemCard

* chore: fe adjust & init script

* perf: test code

* doc

* fix debug variables (#5617)

* perf: search result (#5608)

* fix: table size (#5598)

* fix debug variables

* fix

---------

Co-authored-by: Archer <545436317@qq.com>
Co-authored-by: 伍闲犬 <whoeverimf5@gmail.com>

* perf: member ui

* fix: inherit bug (#5624)

* refactor(permission): remove getClbsWithInfo, which is useless

* fix: app list privateApp

* fix: get infos

* perf(fe): remove delete icon when it is disable in MemberItemCard

* fix: dataset private dataset

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Archer <545436317@qq.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* perf: auto coupon

* chore: upgrade script & get infos avatar  (#5625)

* fix: get infos

* chore: initv4130

* feat: support WecomRobot publish, and fix AesKey can not save bug (#5526)

* feat: resolve conflicts

* fix: add param 'show_publish_wecom'

* feat: abstract out WecomCrypto type

* doc: wecom robot document

* fix: solve instability in AI output

* doc: update some pictures

* feat: remove functions from request.ts to chat.ts and toolCall.ts

* doc: wecom robot doc update

* fix

* delete unused code

* doc: update version and prompt

* feat: remove wecom crypto, delete wecom code in workflow

* feat: delete unused codes

---------

Co-authored-by: heheer <zhiyu44@qq.com>

* remove test

* rename init shell

* feat: collection page store

* reload sandbox

* pysandbox

* remove log

* chore: remove useless code (#5629)

* chore: remove useless code

* fix: checkConflict

* perf: support hidden type for RoleList

* fix: copy node

* update doc

* fix(permission): some bug (#5632)

* fix: app/dataset list

* fix: inherit bug

* perf: del app;i18n;save chat

* fix: test

* i18n

* fix: sumper overflow return OwnerRoleVal (#5633)

* remove invalid code

* fix: scroll

* fix: objectId

* update next

* update package

* object id

* mock redis

* feat: add redis append to resolve wecom stream response  (#5643)

* feat: resolve conflicts

* fix: add param 'show_publish_wecom'

* feat: abstract out WecomCrypto type

* doc: wecom robot document

* fix: solve instability in AI output

* doc: update some pictures

* feat: remove functions from request.ts to chat.ts and toolCall.ts

* doc: wecom robot doc update

* fix

* delete unused code

* doc: update version and prompt

* feat: remove wecom crypto, delete wecom code in workflow

* feat: delete unused codes

* feat: add redis append method

---------

Co-authored-by: heheer <zhiyu44@qq.com>

* cache per

* fix(test): init team sub when creating mocked user (#5646)

* fix: button is not vertically centered (#5647)

* doc

* fix: gridFs objectId (#5649)

---------

Co-authored-by: Zeng Qingwen <143274079+fishwww-ww@users.noreply.github.com>
Co-authored-by: Finley Ge <32237950+FinleyGe@users.noreply.github.com>
Co-authored-by: heheer <heheer@sealos.io>
Co-authored-by: 伍闲犬 <whoeverimf5@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: heheer <zhiyu44@qq.com>
2025-09-15 20:02:54 +08:00

258 lines
8.0 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import { useMemo, useState, useTransition } from 'react';
import { LexicalComposer } from '@lexical/react/LexicalComposer';
import { PlainTextPlugin } from '@lexical/react/LexicalPlainTextPlugin';
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
import { ListPlugin } from '@lexical/react/LexicalListPlugin';
import { CheckListPlugin } from '@lexical/react/LexicalCheckListPlugin';
import { TabIndentationPlugin } from '@lexical/react/LexicalTabIndentationPlugin';
import LexicalErrorBoundary from '@lexical/react/LexicalErrorBoundary';
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
import { ListItemNode, ListNode } from '@lexical/list';
import { CodeHighlightNode, CodeNode } from '@lexical/code';
import VariableLabelPickerPlugin from './plugins/VariableLabelPickerPlugin';
import ListDisplayFixPlugin from './plugins/ListDisplayFixPlugin';
import { Box, Flex } from '@chakra-ui/react';
import styles from './index.module.scss';
import VariablePlugin from './plugins/VariablePlugin';
import { VariableNode } from './plugins/VariablePlugin/node';
import type { EditorState, LexicalEditor } from 'lexical';
import OnBlurPlugin from './plugins/OnBlurPlugin';
import type { FormPropsType } from './type';
import { type EditorVariableLabelPickerType, type EditorVariablePickerType } from './type';
import { getNanoid } from '@fastgpt/global/common/string/tools';
import FocusPlugin from './plugins/FocusPlugin';
import { textToEditorState } from './utils';
import { MaxLengthPlugin } from './plugins/MaxLengthPlugin';
import { VariableLabelNode } from './plugins/VariableLabelPlugin/node';
import VariableLabelPlugin from './plugins/VariableLabelPlugin';
import { useDeepCompareEffect } from 'ahooks';
import VariablePickerPlugin from './plugins/VariablePickerPlugin';
import MarkdownPlugin from './plugins/MarkdownPlugin';
import MyIcon from '../../Icon';
import TabToSpacesPlugin from './plugins/TabToSpacesPlugin';
import ListExitPlugin from './plugins/ListExitPlugin';
const Placeholder = ({ children }: { children: React.ReactNode }) => (
<Box
position={'absolute'}
top={0}
left={0}
right={0}
bottom={0}
py={3}
px={3.5}
pointerEvents={'none'}
overflow={'hidden'}
>
<Box
color={'myGray.400'}
fontSize={'mini'}
userSelect={'none'}
whiteSpace={'pre-wrap'}
wordBreak={'break-all'}
h={'100%'}
>
{children}
</Box>
</Box>
);
export type EditorProps = {
isRichText?: boolean;
variables?: EditorVariablePickerType[];
variableLabels?: EditorVariableLabelPickerType[];
value?: string;
showOpenModal?: boolean;
minH?: number;
maxH?: number;
maxLength?: number;
placeholder?: string;
isInvalid?: boolean;
ExtensionPopover?: ((e: {
onChangeText: (text: string) => void;
iconButtonStyle: Record<string, any>;
}) => React.ReactNode)[];
};
export default function Editor({
isRichText = false,
minH = 200,
maxH = 400,
maxLength,
showOpenModal = true,
onOpenModal,
variables = [],
variableLabels = [],
onChange,
onChangeText,
onBlur,
value,
placeholder = '',
bg = 'white',
isInvalid,
ExtensionPopover
}: EditorProps &
FormPropsType & {
onOpenModal?: () => void;
onChange: (editorState: EditorState, editor: LexicalEditor) => void;
onChangeText?: ((text: string) => void) | undefined;
onBlur?: (editor: LexicalEditor) => void;
}) {
const [key, setKey] = useState(getNanoid(6));
const [_, startSts] = useTransition();
const [focus, setFocus] = useState(false);
const [scrollHeight, setScrollHeight] = useState(0);
const initialConfig = {
namespace: isRichText ? 'richPromptEditor' : 'promptEditor',
nodes: [
VariableNode,
VariableLabelNode,
HeadingNode,
ListNode,
ListItemNode,
QuoteNode,
CodeNode,
CodeHighlightNode
],
editorState: textToEditorState(value),
onError: (error: Error) => {
throw error;
}
};
useDeepCompareEffect(() => {
if (focus) return;
setKey(getNanoid(6));
}, [value, variables, variableLabels]);
const showFullScreenIcon = useMemo(() => {
return showOpenModal && scrollHeight > maxH;
}, [showOpenModal, scrollHeight, maxH]);
const iconButtonStyle = useMemo(
() => ({
position: 'absolute' as const,
bottom: 1,
right: showFullScreenIcon ? '34px' : 2,
zIndex: 10,
cursor: 'pointer',
borderRadius: '6px',
background: 'rgba(255, 255, 255, 0.01)',
backdropFilter: 'blur(6.6666669845581055px)',
alignItems: 'center',
justifyContent: 'center',
w: 6,
h: 6
}),
[showFullScreenIcon]
);
return (
<Box
className="nowheel"
position={'relative'}
width={'full'}
cursor={'text'}
color={'myGray.700'}
bg={focus ? 'white' : bg}
borderRadius={'md'}
>
<LexicalComposer initialConfig={initialConfig} key={key}>
{/* Text type */}
{isRichText ? (
<RichTextPlugin
contentEditable={
<ContentEditable
className={`${isInvalid ? styles.contentEditable_invalid : styles.contentEditable} ${styles.richText}`}
style={{
minHeight: `${minH}px`,
maxHeight: `${maxH}px`
}}
onFocus={() => setFocus(true)}
onBlur={() => setFocus(false)}
/>
}
placeholder={<Placeholder>{placeholder}</Placeholder>}
ErrorBoundary={LexicalErrorBoundary}
/>
) : (
<PlainTextPlugin
contentEditable={
<ContentEditable
className={isInvalid ? styles.contentEditable_invalid : styles.contentEditable}
style={{
minHeight: `${minH}px`,
maxHeight: `${maxH}px`
}}
/>
}
placeholder={<Placeholder>{placeholder}</Placeholder>}
ErrorBoundary={LexicalErrorBoundary}
/>
)}
{/* Basic Plugin */}
<>
<HistoryPlugin />
<MaxLengthPlugin maxLength={maxLength || 999999} />
<FocusPlugin focus={focus} setFocus={setFocus} />
<VariablePlugin variables={variables} />
{variableLabels.length > 0 && (
<>
<VariableLabelPlugin variables={variableLabels} />
<VariableLabelPickerPlugin variables={variableLabels} isFocus={focus} />
</>
)}
{variableLabels.length > 0 && <VariablePickerPlugin variables={variables} />}
<OnBlurPlugin onBlur={onBlur} />
<OnChangePlugin
onChange={(editorState, editor) => {
const rootElement = editor.getRootElement();
setScrollHeight(rootElement?.scrollHeight || 0);
startSts(() => {
onChange?.(editorState, editor);
});
}}
/>
{isRichText && (
<>
<ListDisplayFixPlugin />
<TabIndentationPlugin />
<ListPlugin />
<CheckListPlugin />
<ListExitPlugin />
<MarkdownPlugin />
</>
)}
</>
</LexicalComposer>
{onChangeText &&
ExtensionPopover?.map((Item, index) => (
<Item key={index} iconButtonStyle={iconButtonStyle} onChangeText={onChangeText} />
))}
{showFullScreenIcon && (
<Flex onClick={onOpenModal} {...iconButtonStyle} right={2}>
<MyIcon name={'common/fullScreenLight'} w={'1rem'} color={'myGray.500'} />
</Flex>
)}
</Box>
);
}