Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 输入框内容超出时裁掉上方文字,附件保持可见

Dashea380259a8e9
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsx
index cc103a431f..5d06fca018 100644
--- a/apps/mobile/app/sessions/[sessionId].tsx
+++ b/apps/mobile/app/sessions/[sessionId].tsx
@@ -11972,6 +11972,7 @@ function SessionComposerInput({
const [composerFocused, setComposerFocused] = useState(false);
const composerPillOpen = useComposerPillOpen(composerDock.enabled && canUseComposer, () => composerInputRef.current?.focus());
const [composerInputContentHeight, setComposerInputContentHeight] = useState(COMPOSER_INPUT_SINGLE_LINE_CONTENT_HEIGHT);
+ const [composerAttachmentTrayHeight, setComposerAttachmentTrayHeight] = useState(0);
const [voiceDraftCaretFrame, setVoiceDraftCaretFrame] = useState({ left: 0, top: 0 });
const voiceDraftCaretRef = useRef<View>(null);
const voiceDraftMeasuredBlockRef = useRef<View>(null);
@@ -12097,8 +12098,9 @@ function SessionComposerInput({
const rowChrome = composerCardActive
? COMPOSER_CARD_ROW_CHROME_HEIGHT
: COMPOSER_INPUT_ROW_CHROME_HEIGHT;
- return COMPOSER_VERTICAL_PADDING_HEIGHT + statusReserve + rowChrome;
- }, [composerCardActive, voiceStatusVisible]);
+ // 附件托盘与文字同在卡片里:计入 chrome,文字超出时由输入框自己滚、裁掉上方文字。
+ return COMPOSER_VERTICAL_PADDING_HEIGHT + statusReserve + rowChrome + composerAttachmentTrayHeight;
+ }, [composerAttachmentTrayHeight, composerCardActive, voiceStatusVisible]);
const composerInputMaxContentHeight = useMemo(() => {
const availableHeight = nativeShellLayout.composerMaxHeight - composerChromeHeight;
return Math.min(
@@ -12361,6 +12363,7 @@ function SessionComposerInput({
accessibilityLabel={t('session.screen.composerPlaceholder')}
accessibilityHint={composerLayout.input.disabledReason ?? undefined}
accessoryAbove={controls.attachmentTray}
+ onAccessoryAboveHeightChange={setComposerAttachmentTrayHeight}
autoFocus={visualFocusComposer}
cardActive={composerCardActive}
caretHidden={voiceIsListening}
diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsx
index ff1496dd8f..65c040a3ce 100644
--- a/apps/mobile/app/sessions/new.tsx
+++ b/apps/mobile/app/sessions/new.tsx
@@ -1612,12 +1612,22 @@ function NewRemoteSessionContent({ creatingRef }: { creatingRef: { current: bool
setComposerVoiceHoldArmed(false);
firstMessageInputRef.current?.blur();
}, [collapseComposerImages]);
+ const [composerAttachmentTrayHeight, setComposerAttachmentTrayHeight] = useState(0);
+ const composerCardMaxHeight = Math.max(COMPOSER_CARD_CHROME_HEIGHT + MOBILE_COMPOSER_INPUT_SINGLE_LINE_HEIGHT, windowDimensions.height
+ - (keyboardState.visible ? keyboardState.height : safeAreaInsets.bottom)
+ - safeAreaInsets.top - navigationChrome.target - spacing.xl);
+ // 附件托盘与文字同在卡片里:计入 chrome,文字超出时由输入框自己滚、裁掉上方文字。
+ const composerChromeHeight = (composerCardActive ? COMPOSER_CARD_CHROME_HEIGHT : COMPOSER_RESIZE_CHROME_HEIGHT)
+ + composerAttachmentTrayHeight;
const composerResize = useComposerResize({
- autoMaxContentHeight: MOBILE_COMPOSER_INPUT_MAX_HEIGHT,
+ autoMaxContentHeight: Math.max(
+ MOBILE_COMPOSER_INPUT_SINGLE_LINE_HEIGHT,
+ Math.min(MOBILE_COMPOSER_INPUT_MAX_HEIGHT, composerCardMaxHeight - composerChromeHeight),
+ ),
// 简洁态一律收到单行(下拉收起和点别处收键盘的结果一致);
// auto / manual 记忆保留,重新聚焦后恢复。
collapsed: !composerCardActive,
- composerChromeHeight: composerCardActive ? COMPOSER_CARD_CHROME_HEIGHT : COMPOSER_RESIZE_CHROME_HEIGHT,
+ composerChromeHeight,
contentHeight: composerInputContentHeight,
keyboardHeight: keyboardState.visible ? keyboardState.height : 0,
onSnapToAuto: handleComposerSnapToAuto,
@@ -5465,9 +5475,7 @@ function NewRemoteSessionContent({ creatingRef }: { creatingRef: { current: bool
</ScrollView>
<View style={[styles.composerCard, {
- maxHeight: Math.max(COMPOSER_CARD_CHROME_HEIGHT + MOBILE_COMPOSER_INPUT_SINGLE_LINE_HEIGHT, windowDimensions.height
- - (keyboardState.visible ? keyboardState.height : safeAreaInsets.bottom)
- - safeAreaInsets.top - navigationChrome.target - spacing.xl),
+ maxHeight: composerCardMaxHeight,
paddingBottom: composerDock.enabled ? 0 : spacing.md,
}]} testID="newSession.composer">
<ComposerPluginPalette
@@ -5551,6 +5559,7 @@ function NewRemoteSessionContent({ creatingRef }: { creatingRef: { current: bool
bodyScrollGesture={composerResize.scrollGesture}
bodyScrollEnabled={!composerResize.dragging}
accessoryAbove={attachments.length > 0 || pendingUploads.length > 0 || pastePlaceholderCount > 0 ? renderComposerAttachmentTray() : null}
+ onAccessoryAboveHeightChange={setComposerAttachmentTrayHeight}
autoFocus={visualFocusComposer}
entryTransitionId={entryMorph?.id}
collapsedHeight={composerDock.enabled ? composerDock.pillHeight : undefined}
diff --git a/apps/mobile/src/__tests__/composerToolbarViewport.test.tsx b/apps/mobile/src/__tests__/composerToolbarViewport.test.tsx
index 67319345da..b5b35fda64 100644
--- a/apps/mobile/src/__tests__/composerToolbarViewport.test.tsx
+++ b/apps/mobile/src/__tests__/composerToolbarViewport.test.tsx
@@ -7,6 +7,7 @@ import type { SharedValue } from 'react-native-reanimated';
const nativeLayout = vi.hoisted(() => new WeakMap<Element, (height: number) => void>());
+const scrollViewProps = vi.hoisted(() => ({ current: {} as Record<string, unknown> }));
vi.mock('expo-constants', () => ({ default: { executionEnvironment: 'bare' }, ExecutionEnvironment: { StoreClient: 'storeClient' } }));
vi.mock('react-native', async () => {
const React = await import('react');
@@ -16,8 +17,10 @@ vi.mock('react-native', async () => {
React.createElement('div', { 'data-testid': testID, style: flatten(style), ref: (node: HTMLDivElement | null) => {
if (node && onLayout) nativeLayout.set(node, height => onLayout({ nativeEvent: { layout: { height } } }));
} }, children);
- const ScrollView = ({ children, testID, scrollEnabled }: { children?: ReactNode; testID?: string; scrollEnabled?: boolean }) =>
- React.createElement('div', { 'data-testid': testID, 'data-scroll': String(scrollEnabled) }, children);
+ const ScrollView = ({ children, testID, scrollEnabled, ...props }: { children?: ReactNode; testID?: string; scrollEnabled?: boolean }) => {
+ scrollViewProps.current = props;
+ return React.createElement('div', { 'data-testid': testID, 'data-scroll': String(scrollEnabled) }, children);
+ };
return { View, ScrollView, Pressable: View, Platform: { OS: 'ios' },
StyleSheet: { hairlineWidth: 1, absoluteFill: {} }, Animated: {}, Easing: {} };
});
@@ -131,3 +134,35 @@ it("releases attachment height after deletion and remeasures before the next dra
act(() => root.unmount());
}
});
+
+it('keeps typed text visible when the card body overflows and reports a hidden tray as zero height', () => {
+ const container = document.createElement('div');
+ const root = createRoot(container);
+ const scrollToEnd = vi.fn();
+ const outerRef = React.createRef<unknown>();
+ const onTrayHeight = vi.fn();
+ const gesture = {} as NonNullable<React.ComponentProps<typeof MobileComposerInputRow>['bodyScrollGesture']>;
+ const render = (cardActive: boolean) => act(() => root.render(<MobileComposerInputRow
+ accessibilityLabel="Message" inputTestID="input" placeholder="Message" placeholderTextColor="inherit" value="Long text"
+ onChangeText={() => {}} cardActive={cardActive} bodyScrollGesture={gesture} bodyScrollRef={outerRef as never}
+ accessoryAbove={<div>Image</div>} onAccessoryAboveHeightChange={onTrayHeight} testID="composer"
+ />));
+ try {
+ render(true);
+ const props = scrollViewProps.current as {
+ ref: (node: unknown) => void; onContentSizeChange: () => void; onLayout: () => void;
+ };
+ const node = { scrollToEnd };
+ act(() => props.ref(node));
+ expect(outerRef.current).toBe(node);
+ props.onContentSizeChange();
+ props.onLayout();
+ expect(scrollToEnd).toHaveBeenCalledTimes(2);
+ expect(scrollToEnd).toHaveBeenLastCalledWith({ animated: false });
+ expect(onTrayHeight).not.toHaveBeenCalled();
+ render(false);
+ (scrollViewProps.current as typeof props).onContentSizeChange();
+ expect(scrollToEnd).toHaveBeenCalledTimes(2);
+ expect(onTrayHeight).toHaveBeenLastCalledWith(0);
+ } finally { act(() => root.unmount()); }
+});
diff --git a/apps/mobile/src/session/MobileComposerInputRow.tsx b/apps/mobile/src/session/MobileComposerInputRow.tsx
index c38cfa3c62..a5c9243034 100644
--- a/apps/mobile/src/session/MobileComposerInputRow.tsx
+++ b/apps/mobile/src/session/MobileComposerInputRow.tsx
@@ -21,7 +21,7 @@ import type { NativeGesture, PanGesture } from 'react-native-gesture-handler';
import Constants, { ExecutionEnvironment } from 'expo-constants';
import { TextInputWrapper, type PasteEventPayload } from 'expo-paste-input';
import { Mic } from 'lucide-react-native';
-import { useCallback, useEffect, useRef, type ReactNode, type Ref } from 'react';
+import { useCallback, useEffect, useRef, type MutableRefObject, type ReactNode, type Ref } from 'react';
import { useTranslation } from 'react-i18next';
import { iconSize, iconStroke, useTheme, useThemedStyles, type ThemeColors } from '@/theme';
import { motionDuration, motionEasing, radius, spacing } from '@/theme/tokens';
@@ -103,6 +103,11 @@ export interface MobileComposerInputRowProps {
* 仅 cardActive 形态渲染;简洁态的对应物是 leading 插槽的迷你徽标。
*/
accessoryAbove?: ReactNode;
+ /**
+ * accessoryAbove 的自然高度(未显示时为 0)。页面把它计入输入区高度上限:
+ * 内容超出时由输入框自身滚动裁掉上方文字,附件保持可见。
+ */
+ onAccessoryAboveHeightChange?: (height: number) => void;
/**
* 聚焦卡片形态开关。true 时输入区独占整行、底部工具排展开;
* false 时保持单行简洁态,工具排折叠为 0 高(保持挂载,展开时按钮
@@ -217,6 +222,7 @@ export function MobileComposerInputRow({
accessibilityHint,
accessibilityLabel,
accessoryAbove,
+ onAccessoryAboveHeightChange,
autoFocus,
cardActive,
frameOutside,
@@ -299,6 +305,21 @@ export function MobileComposerInputRow({
onPasteImagesLoadFailed?.();
}
}, [onPasteImages, onPasteImagesLoading, onPasteImagesLoadFailed]);
+ const bodyScrollNodeRef = useRef<ScrollView | null>(null);
+ const setBodyScrollRef = useCallback((node: ScrollView | null) => {
+ bodyScrollNodeRef.current = node;
+ if (typeof bodyScrollRef === 'function') bodyScrollRef(node);
+ else if (bodyScrollRef) (bodyScrollRef as MutableRefObject<ScrollView | null>).current = node;
+ }, [bodyScrollRef]);
+ // 输入区上限已扣除附件高度,正常不会溢出;极端情况(附件多行、屏幕很矮)仍溢出时贴底,
+ // 保证正在输入的文字不被裁掉。
+ const pinBodyScrollToEnd = useCallback(() => {
+ if (cardLayout) bodyScrollNodeRef.current?.scrollToEnd({ animated: false });
+ }, [cardLayout]);
+ const showAccessoryAbove = cardLayout && !!accessoryAbove;
+ useEffect(() => {
+ if (!showAccessoryAbove) onAccessoryAboveHeightChange?.(0);
+ }, [showAccessoryAbove, onAccessoryAboveHeightChange]);
const textInputElement = (
<TextInput
ref={inputRef as never}
@@ -333,7 +354,11 @@ export function MobileComposerInputRow({
);
const inputBody = (
<>
- {cardLayout && accessoryAbove ? <ComposerFoldingSection progress={collapseProgress}>{accessoryAbove}</ComposerFoldingSection> : null}
+ {showAccessoryAbove ? (
+ <ComposerFoldingSection onNaturalHeight={onAccessoryAboveHeightChange} progress={collapseProgress}>
+ {accessoryAbove}
+ </ComposerFoldingSection>
+ ) : null}
<View
style={[
styles.mainRow,
@@ -398,7 +423,9 @@ export function MobileComposerInputRow({
{bodyScrollGesture ? (
<GestureDetector gesture={bodyScrollGesture}>
<ScrollView
- ref={bodyScrollRef}
+ ref={setBodyScrollRef}
+ onContentSizeChange={pinBodyScrollToEnd}
+ onLayout={pinBodyScrollToEnd}
style={[styles.bodyScroll, geometricSingleLine && styles.bodyScrollCollapsed]}
contentContainerStyle={[styles.bodyScrollContent, geometricSingleLine && styles.bodyScrollCollapsed]}
keyboardShouldPersistTaps="handled"
@@ -499,7 +526,11 @@ export interface ComposerResizeGrabberProps {
testID?: string;
}
-function ComposerFoldingSection({ progress, children }: { progress?: SharedValue<number>; children: ReactNode }) {
+function ComposerFoldingSection({ progress, onNaturalHeight, children }: {
+ progress?: SharedValue<number>;
+ onNaturalHeight?: (height: number) => void;
+ children: ReactNode;
+}) {
const measuredHeight = useSharedValue(0);
const style = useAnimatedStyle(() => {
const amount = progress?.value ?? 0;
@@ -516,6 +547,7 @@ function ComposerFoldingSection({ progress, children }: { progress?: SharedValue
// Keep the natural expanded size until the gesture has fully reversed.
if ((progress?.value ?? 0) === 0) {
measuredHeight.value = event.nativeEvent.layout.height;
+ onNaturalHeight?.(event.nativeEvent.layout.height);
}
}}>
{children}