From 1b89ea0af092e977cd4c61ddf0155f1e8023e0dc Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 20 Oct 2025 19:58:48 +0800 Subject: [PATCH] refactor(ai-chat): simplify isEmpty state checks in ChatInput and LexicalRichEditor Updated the ChatInput and LexicalRichEditor components to streamline the logic for determining if the editor is empty. The isEmpty state now directly utilizes the $getRoot().isEmpty() method, enhancing code clarity and maintainability. Removed unnecessary text content checks in favor of a more efficient approach. Signed-off-by: Innei --- .../ai-chat/components/layouts/ChatInput.tsx | 6 ++---- .../components/src/ui/input/TextAreaWrapper.tsx | 14 -------------- .../ui/lexical-rich-editor/LexicalRichEditor.tsx | 7 +------ 3 files changed, 3 insertions(+), 24 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx index 1ba91a0f6..6d6c5f4f3 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx @@ -74,11 +74,9 @@ export const ChatInput = memo(({ onSend, variant, ref: forwardedRef }: ChatInput const handleEditorChange = useCallback((editorState: EditorState, editor: LexicalEditor) => { setCurrentEditor(editor) - // Update isEmpty state based on editor content + editorState.read(() => { - const root = $getRoot() - const textContent = root.getTextContent().trim() - setIsEmpty(textContent === "") + setIsEmpty($getRoot().isEmpty()) }) }, []) diff --git a/packages/internal/components/src/ui/input/TextAreaWrapper.tsx b/packages/internal/components/src/ui/input/TextAreaWrapper.tsx index 5c78a6e17..a3989dc3d 100644 --- a/packages/internal/components/src/ui/input/TextAreaWrapper.tsx +++ b/packages/internal/components/src/ui/input/TextAreaWrapper.tsx @@ -1,6 +1,5 @@ import { cn } from "@follow/utils" import clsx from "clsx" -import { useMotionValue } from "motion/react" import type { PropsWithChildren } from "react" import { useCallback, useState } from "react" @@ -72,18 +71,6 @@ export const TextAreaWrapper = ({ const [internalIsFocused, setInternalIsFocused] = useState(false) const isFocused = externalIsFocused ?? internalIsFocused - const mouseX = useMotionValue(0) - const mouseY = useMotionValue(0) - - const handleMouseMove = useCallback( - ({ clientX, clientY, currentTarget }: React.MouseEvent) => { - const bounds = currentTarget.getBoundingClientRect() - mouseX.set(clientX - bounds.left) - mouseY.set(clientY - bounds.top) - }, - [mouseX, mouseY], - ) - const handleFocus = useCallback(() => { if (externalIsFocused === undefined) { setInternalIsFocused(true) @@ -115,7 +102,6 @@ export const TextAreaWrapper = ({ paddingClassName, wrapperClassName, )} - onMouseMove={handleMouseMove} onFocus={handleFocus} onBlur={handleBlur} onPointerDown={onPointerDown} diff --git a/packages/internal/components/src/ui/lexical-rich-editor/LexicalRichEditor.tsx b/packages/internal/components/src/ui/lexical-rich-editor/LexicalRichEditor.tsx index f779f6557..28142e430 100644 --- a/packages/internal/components/src/ui/lexical-rich-editor/LexicalRichEditor.tsx +++ b/packages/internal/components/src/ui/lexical-rich-editor/LexicalRichEditor.tsx @@ -75,12 +75,7 @@ export const LexicalRichEditor = function LexicalRichEditor({ root.clear() }) }, - isEmpty: () => - editorRef?.getEditorState().read(() => { - const root = $getRoot() - const textContent = root.getTextContent().trim() - return textContent === "" - }) || false, + isEmpty: () => editorRef?.getEditorState().read(() => $getRoot().isEmpty()) || false, })) return (