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 <tukon479@gmail.com>
This commit is contained in:
Innei
2025-10-20 19:58:48 +08:00
parent 97d77c4868
commit 1b89ea0af0
3 changed files with 3 additions and 24 deletions
@@ -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())
})
}, [])
@@ -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}
@@ -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 (