From 20bd33fada00ef9326fbae8cd1edfc7b6f52f3d4 Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 26 Jan 2024 15:13:06 +0800 Subject: [PATCH] feat: prompt IDE support change height (#2232) --- .../config-prompt/advanced-prompt-input.tsx | 27 ++++-- .../prompt-editor-height-resize-wrap.tsx | 91 +++++++++++++++++++ .../config-prompt/simple-prompt-input.tsx | 14 ++- 3 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 web/app/components/app/configuration/config-prompt/prompt-editor-height-resize-wrap.tsx diff --git a/web/app/components/app/configuration/config-prompt/advanced-prompt-input.tsx b/web/app/components/app/configuration/config-prompt/advanced-prompt-input.tsx index 18830f98ff..b91831de96 100644 --- a/web/app/components/app/configuration/config-prompt/advanced-prompt-input.tsx +++ b/web/app/components/app/configuration/config-prompt/advanced-prompt-input.tsx @@ -10,6 +10,7 @@ import produce from 'immer' import s from './style.module.css' import MessageTypeSelector from './message-type-selector' import ConfirmAddVar from './confirm-add-var' +import PromptEditorHeightResizeWrap from './prompt-editor-height-resize-wrap' import type { PromptRole, PromptVariable } from '@/models/debug' import { HelpCircle, Trash03 } from '@/app/components/base/icons/src/vender/line/general' import { Clipboard, ClipboardCheck } from '@/app/components/base/icons/src/vender/line/files' @@ -25,7 +26,6 @@ import { useToastContext } from '@/app/components/base/toast' import { useEventEmitterContextContext } from '@/context/event-emitter' import { ADD_EXTERNAL_DATA_TOOL } from '@/app/components/app/configuration/config-var' import { INSERT_VARIABLE_VALUE_BLOCK_COMMAND } from '@/app/components/base/prompt-editor/plugins/variable-block' - type Props = { type: PromptRole isChatMode: boolean @@ -130,7 +130,8 @@ const AdvancedPromptInput: FC = ({ } } - const editorHeight = isChatMode ? 'h-[200px]' : 'h-[508px]' + const minHeight = 102 + const [editorHeight, setEditorHeight] = React.useState(isChatMode ? 200 : 508) const contextMissing = (
= ({ }} >
- +
{t('appDebug.promptMode.contextMissing')}
= ({
)} -
+ +
{value.length}
+
+ )} + > = ({ onChange={handlePromptChange} onBlur={handleBlur} /> -
-
-
{value.length}
-
+ + {isShowConfirmAddVar && ( diff --git a/web/app/components/app/configuration/config-prompt/prompt-editor-height-resize-wrap.tsx b/web/app/components/app/configuration/config-prompt/prompt-editor-height-resize-wrap.tsx new file mode 100644 index 0000000000..cffc728593 --- /dev/null +++ b/web/app/components/app/configuration/config-prompt/prompt-editor-height-resize-wrap.tsx @@ -0,0 +1,91 @@ +'use client' +import React, { useCallback, useEffect, useState } from 'react' +import type { FC } from 'react' +import { useDebounceFn } from 'ahooks' +import cn from 'classnames' + +type Props = { + className?: string + height: number + minHeight: number + onHeightChange: (height: number) => void + children: JSX.Element + footer?: JSX.Element +} + +const PromptEditorHeightResizeWrap: FC = ({ + className, + height, + minHeight, + onHeightChange, + children, + footer, +}) => { + const [clientY, setClientY] = useState(0) + const [isResizing, setIsResizing] = useState(false) + const [prevUserSelectStyle, setPrevUserSelectStyle] = useState(getComputedStyle(document.body).userSelect) + + const handleStartResize = useCallback((e: React.MouseEvent) => { + setClientY(e.clientY) + setIsResizing(true) + setPrevUserSelectStyle(getComputedStyle(document.body).userSelect) + document.body.style.userSelect = 'none' + }, []) + + const handleStopResize = useCallback(() => { + setIsResizing(false) + document.body.style.userSelect = prevUserSelectStyle + }, [prevUserSelectStyle]) + + const { run: didHandleResize } = useDebounceFn((e) => { + if (!isResizing) + return + + const offset = e.clientY - clientY + let newHeight = height + offset + setClientY(e.clientY) + if (newHeight < minHeight) + newHeight = minHeight + onHeightChange(newHeight) + }, { + wait: 0, + }) + + const handleResize = useCallback(didHandleResize, [isResizing, height, minHeight, clientY]) + + useEffect(() => { + document.addEventListener('mousemove', handleResize) + return () => { + document.removeEventListener('mousemove', handleResize) + } + }, [handleResize]) + + useEffect(() => { + document.addEventListener('mouseup', handleStopResize) + return () => { + document.removeEventListener('mouseup', handleStopResize) + } + }, [handleStopResize]) + + return ( +
+
+ {children} +
+ {/* resize handler */} + {footer} +
+
+
+
+ ) +} +export default React.memo(PromptEditorHeightResizeWrap) diff --git a/web/app/components/app/configuration/config-prompt/simple-prompt-input.tsx b/web/app/components/app/configuration/config-prompt/simple-prompt-input.tsx index a5e453c15d..bbbb4948c4 100644 --- a/web/app/components/app/configuration/config-prompt/simple-prompt-input.tsx +++ b/web/app/components/app/configuration/config-prompt/simple-prompt-input.tsx @@ -1,6 +1,6 @@ 'use client' import type { FC } from 'react' -import React from 'react' +import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import { useBoolean } from 'ahooks' import cn from 'classnames' @@ -8,6 +8,7 @@ import produce from 'immer' import { useContext } from 'use-context-selector' import ConfirmAddVar from './confirm-add-var' import s from './style.module.css' +import PromptEditorHeightResizeWrap from './prompt-editor-height-resize-wrap' import { PromptMode, type PromptVariable } from '@/models/debug' import Tooltip from '@/app/components/base/tooltip' import { AppType } from '@/types/app' @@ -125,6 +126,8 @@ const Prompt: FC = ({ setIntroduction(res.opening_statement) showAutomaticFalse() } + const minHeight = 228 + const [editorHeight, setEditorHeight] = useState(minHeight) return (
@@ -161,7 +164,12 @@ const Prompt: FC = ({ )}
-
+ = ({ handleChange(promptTemplate, getVars(promptTemplate)) }} /> -
+ {isShowConfirmAddVar && (