Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(chat-input): preserve fullscreen editor state and send behavior (#13481)

Inneid8534c2966d1
diff --git a/src/features/ChatInput/Desktop/index.tsx b/src/features/ChatInput/Desktop/index.tsx
index 42b372d8f75..ba24a8bd303 100644
--- a/src/features/ChatInput/Desktop/index.tsx
+++ b/src/features/ChatInput/Desktop/index.tsx
@@ -4,11 +4,13 @@ import { type ChatInputProps } from '@lobehub/editor/react';
import { ChatInput, ChatInputActionBar } from '@lobehub/editor/react';
import { Center, Flexbox, Text } from '@lobehub/ui';
import { createStaticStyles, cx } from 'antd-style';
-import { type ReactNode } from 'react';
+import { type ReactNode, use } from 'react';
import { memo, useEffect } from 'react';
+import { createPortal } from 'react-dom';
import { useTranslation } from 'react-i18next';
import { useChatInputStore } from '@/features/ChatInput/store';
+import { LayoutContainerContext } from '@/routes/(main)/_layout/DesktopLayoutContainer/LayoutContainerContext';
import { useChatStore } from '@/store/chat';
import { chatSelectors } from '@/store/chat/selectors';
import { fileChatSelectors, useFileStore } from '@/store/file';
@@ -23,7 +25,7 @@ import SendArea from '../SendArea';
import TypoBar from '../TypoBar';
import ContextContainer from './ContextContainer';
-const styles = createStaticStyles(({ css }) => ({
+const styles = createStaticStyles(({ css, cssVar }) => ({
container: css`
.show-on-hover {
opacity: 0;
@@ -46,6 +48,8 @@ const styles = createStaticStyles(({ css }) => ({
width: 100%;
height: 100%;
margin-block-start: 0;
+
+ background: ${cssVar.colorBgContainer};
`,
inputFullscreen: css`
border: none;
@@ -77,6 +81,7 @@ const DesktopChatInput = memo<DesktopChatInputProps>(
sendAreaPrefix,
}) => {
const { t } = useTranslation('chat');
+ const layoutContainerRef = use(LayoutContainerContext);
const [chatInputHeight, updateSystemStatus] = useGlobalStore((s) => [
systemStatusSelectors.chatInputHeight(s),
s.updateSystemStatus,
@@ -93,15 +98,18 @@ const DesktopChatInput = memo<DesktopChatInputProps>(
const chatKey = useChatStore(chatSelectors.currentChatKey);
+ const setExpand = useChatInputStore((s) => s.setExpand);
+
useEffect(() => {
if (editor) editor.focus();
- }, [chatKey, editor]);
+ setExpand(false);
+ }, [chatKey, editor, setExpand]);
const shouldShowContextContainer =
leftActions.flat().includes('fileUpload') || hasContextSelections || hasFiles;
const contextContainerNode = shouldShowContextContainer && <ContextContainer />;
- return (
+ const content = (
<Flexbox
className={cx(styles.container, expand && styles.fullscreen)}
gap={8}
@@ -164,6 +172,11 @@ const DesktopChatInput = memo<DesktopChatInputProps>(
)}
</Flexbox>
);
+
+ if (expand && layoutContainerRef.current)
+ return createPortal(content, layoutContainerRef.current);
+
+ return content;
},
);
diff --git a/src/features/ChatInput/InputEditor/index.tsx b/src/features/ChatInput/InputEditor/index.tsx
index 9a482528093..e55378f8775 100644
--- a/src/features/ChatInput/InputEditor/index.tsx
+++ b/src/features/ChatInput/InputEditor/index.tsx
@@ -198,7 +198,6 @@ const InputEditor = memo<{ defaultRows?: number }>(({ defaultRows = 2 }) => {
}}
onCompositionEnd={({ event }) => compositionProps.onCompositionEnd(event)}
onCompositionStart={({ event }) => compositionProps.onCompositionStart(event)}
- onInit={(editor) => storeApi.setState({ editor })}
onBlur={() => {
disableScope(HotkeyEnum.AddUserMessage);
}}
@@ -220,11 +219,28 @@ const InputEditor = memo<{ defaultRows?: number }>(({ defaultRows = 2 }) => {
onFocus={() => {
enableScope(HotkeyEnum.AddUserMessage);
}}
+ onInit={(editor) => {
+ const saved = storeApi.getState()._savedEditorState;
+ storeApi.setState({ _savedEditorState: undefined, editor });
+ if (saved) {
+ requestAnimationFrame(() => {
+ editor.setDocument('json', saved);
+ });
+ }
+ }}
onPressEnter={({ event: e }) => {
if (e.shiftKey || isComposingRef.current) return;
// when user like alt + enter to add ai message
if (e.altKey && hotkey === combineKeys([KeyEnum.Alt, KeyEnum.Enter])) return true;
const commandKey = isCommandPressed(e);
+ // In fullscreen mode, Enter inserts newline; only Cmd/Ctrl+Enter sends
+ if (expand) {
+ if (commandKey) {
+ send();
+ return true;
+ }
+ return;
+ }
// when user like cmd + enter to send message
if (useCmdEnterToSend) {
if (commandKey) {
diff --git a/src/features/ChatInput/store/action.ts b/src/features/ChatInput/store/action.ts
index 01a0b602c69..3a731d2a692 100644
--- a/src/features/ChatInput/store/action.ts
+++ b/src/features/ChatInput/store/action.ts
@@ -41,6 +41,9 @@ export const store: CreateStore = (publicState) => (set, get) => ({
getEditorData: get().getJSONState,
getMarkdownContent: get().getMarkdownContent,
});
+ if (get().expand) {
+ set({ _savedEditorState: undefined, expand: false });
+ }
requestAnimationFrame(() => {
requestAnimationFrame(() => {
editor.focus();
@@ -59,7 +62,9 @@ export const store: CreateStore = (publicState) => (set, get) => ({
},
setExpand: (expand) => {
- set({ expand });
+ const editor = get().editor;
+ const _savedEditorState = editor?.getDocument('json') as Record<string, any> | undefined;
+ set({ _savedEditorState, expand });
},
setJSONState: (content) => {
diff --git a/src/features/ChatInput/store/initialState.ts b/src/features/ChatInput/store/initialState.ts
index d37dfc0dce6..9ad7d9e25b9 100644
--- a/src/features/ChatInput/store/initialState.ts
+++ b/src/features/ChatInput/store/initialState.ts
@@ -47,6 +47,7 @@ export interface PublicState {
}
export interface State extends PublicState {
+ _savedEditorState?: Record<string, any>;
editor?: IEditor;
isContentEmpty: boolean;
markdownContent: string;
diff --git a/src/routes/(main)/_layout/DesktopLayoutContainer.tsx b/src/routes/(main)/_layout/DesktopLayoutContainer.tsx
index 2a5dcd7b004..3a64e4c43b8 100644
--- a/src/routes/(main)/_layout/DesktopLayoutContainer.tsx
+++ b/src/routes/(main)/_layout/DesktopLayoutContainer.tsx
@@ -1,7 +1,7 @@
import { Flexbox } from '@lobehub/ui';
import { cssVar } from 'antd-style';
import { type FC, type PropsWithChildren } from 'react';
-import { useMemo } from 'react';
+import { useMemo, useRef } from 'react';
import { isDesktop } from '@/const/version';
import { useIsDark } from '@/hooks/useIsDark';
@@ -9,9 +9,11 @@ import { useGlobalStore } from '@/store/global';
import { systemStatusSelectors } from '@/store/global/selectors';
import { getDarwinMajorVersion, isMacOSWithLargeWindowBorders } from '@/utils/platform';
+import { LayoutContainerContext } from './DesktopLayoutContainer/LayoutContainerContext';
import { styles } from './DesktopLayoutContainer/style';
const DesktopLayoutContainer: FC<PropsWithChildren> = ({ children }) => {
+ const innerContainerRef = useRef<HTMLDivElement>(null);
const isDarkMode = useIsDark();
const [expand] = useGlobalStore((s) => [systemStatusSelectors.showLeftPanel(s)]);
@@ -49,10 +51,11 @@ const DesktopLayoutContainer: FC<PropsWithChildren> = ({ children }) => {
<Flexbox
className={styles.innerContainer}
height={'100%'}
+ ref={innerContainerRef}
style={innerCssVariables}
width={'100%'}
>
- {children}
+ <LayoutContainerContext value={innerContainerRef}>{children}</LayoutContainerContext>
</Flexbox>
</Flexbox>
);
diff --git a/src/routes/(main)/_layout/DesktopLayoutContainer/LayoutContainerContext.ts b/src/routes/(main)/_layout/DesktopLayoutContainer/LayoutContainerContext.ts
new file mode 100644
index 00000000000..19cde62e30c
--- /dev/null
+++ b/src/routes/(main)/_layout/DesktopLayoutContainer/LayoutContainerContext.ts
@@ -0,0 +1,5 @@
+import { createContext, type RefObject } from 'react';
+
+export const LayoutContainerContext = createContext<RefObject<HTMLDivElement | null>>({
+ current: null,
+});