Skip to content

Commit e2b1c77

Browse files
mwolsonjuliusmarminge
authored andcommitted
fix(mobile): Keep the last line of iOS assistant replies visible
The first layout of a long selectable markdown row can be one line too short, so UITextView clips the ending until the thread is opened again. Measure every drawn line and grow the row when it is taller than that first layout.
1 parent 07e8f8d commit e2b1c77

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

‎apps/mobile/modules/t3-markdown-text/ios/T3MarkdownText.mm‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ - (void)drawRect:(CGRect)rect
364364
if (frameChanged) {
365365
_textView.frame = _view.frame;
366366
}
367+
const CGFloat layoutWidth = CGRectGetWidth(_view.bounds);
368+
if (layoutWidth > 0) {
369+
_textView.textContainer.size = CGSizeMake(layoutWidth, CGFLOAT_MAX);
370+
[_textView.layoutManager ensureLayoutForTextContainer:_textView.textContainer];
371+
}
367372

368373
__block std::vector<std::string> lines;
369374
const int maxLines = props.numberOfLines;

‎apps/mobile/modules/t3-markdown-text/src/MarkdownTextPrimitive.tsx‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ export type ContextMenuActionEvent = {
2828
nativeEvent: { target: number; actionIdentifier: string };
2929
};
3030

31-
/**
32-
* `onTextLayout` is not offered: the native view reports plain line strings
33-
* while the React Native Text fallback reports measured `TextLayoutLine`s.
34-
*/
3531
export type MarkdownTextPrimitiveProps = Omit<TextProps, "onTextLayout"> & {
3632
nativeTextRef?: Ref<RNText>;
3733
uiTextView?: boolean;
3834
contextMenuConfig?: string;
3935
onContextMenuAction?: (event: ContextMenuActionEvent) => void;
36+
/** Native iOS line strings; the React Native Text fallback does not emit this event. */
37+
onNativeTextLayout?: React.ComponentProps<typeof T3MarkdownTextNativeComponent>["onTextLayout"];
4038
/**
4139
* Fired when the native text selection changes. Only fires on iOS when
4240
* `uiTextView` is true. Note: fires on every selection-edge adjustment
@@ -50,6 +48,7 @@ function MarkdownTextPrimitiveChild({
5048
style,
5149
children,
5250
nativeTextRef: _nativeTextRef,
51+
onNativeTextLayout,
5352
...rest
5453
}: MarkdownTextPrimitiveProps) {
5554
const [isAncestor, rootStyle] = useTextAncestorContext();
@@ -92,6 +91,7 @@ function MarkdownTextPrimitiveChild({
9291
<T3MarkdownTextNativeComponent
9392
{...textDefaults}
9493
{...containerProps}
94+
onTextLayout={onNativeTextLayout}
9595
style={[flattenedStyle]}
9696
>
9797
{nativeChildren}
@@ -103,7 +103,11 @@ function MarkdownTextPrimitiveChild({
103103
return <>{nativeChildren}</>;
104104
}
105105

106-
function MarkdownTextPrimitiveInner({ nativeTextRef, ...props }: MarkdownTextPrimitiveProps) {
106+
function MarkdownTextPrimitiveInner({
107+
nativeTextRef,
108+
onNativeTextLayout,
109+
...props
110+
}: MarkdownTextPrimitiveProps) {
107111
const [isAncestor] = useTextAncestorContext();
108112

109113
// Even if the uiTextView prop is set, we can still default to using
@@ -112,12 +116,12 @@ function MarkdownTextPrimitiveInner({ nativeTextRef, ...props }: MarkdownTextPri
112116
if ((!props.selectable || !props.uiTextView) && !isAncestor) {
113117
return <RNText ref={nativeTextRef} {...props} />;
114118
}
115-
return <MarkdownTextPrimitiveChild {...props} />;
119+
return <MarkdownTextPrimitiveChild {...props} onNativeTextLayout={onNativeTextLayout} />;
116120
}
117121

118122
export function MarkdownTextPrimitive(props: MarkdownTextPrimitiveProps) {
119123
if (Platform.OS !== "ios") {
120-
const { nativeTextRef, ...textProps } = props;
124+
const { nativeTextRef, onNativeTextLayout: _onNativeTextLayout, ...textProps } = props;
121125
return <RNText ref={nativeTextRef} {...textProps} />;
122126
}
123127
return <MarkdownTextPrimitiveInner {...props} />;

‎apps/mobile/modules/t3-markdown-text/src/NativeMarkdownSelectableText.ios.tsx‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { createContext, useCallback, useContext } from "react";
1+
import { createContext, useCallback, useContext, useState } from "react";
22
import {
33
findNodeHandle,
44
Image,
55
Linking,
6+
PixelRatio,
67
Platform,
78
StyleSheet,
89
Text as RNText,
@@ -212,6 +213,7 @@ export function NativeMarkdownSelectableText(props: {
212213

213214
return { key: `${signature}:${occurrence}`, run, text };
214215
});
216+
const [minHeight, setMinHeight] = useState(0);
215217
// T3MarkdownText only rebuilds its attributed string during native layout. A
216218
// color-only child update can otherwise leave the previous appearance cached.
217219
const appearanceKey = [
@@ -239,9 +241,19 @@ export function NativeMarkdownSelectableText(props: {
239241
nativeTextRef={attachAndroidText}
240242
uiTextView
241243
selectable
244+
onNativeTextLayout={(event) => {
245+
const lines = event.nativeEvent.lines;
246+
if (!Array.isArray(lines) || lines.length === 0) {
247+
return;
248+
}
249+
setMinHeight(
250+
Math.ceil(lines.length * props.textStyle.lineHeight * PixelRatio.getFontScale()),
251+
);
252+
}}
242253
style={{
243254
flexShrink: 1,
244255
minWidth: 0,
256+
minHeight: minHeight > 0 ? minHeight : undefined,
245257
color: props.textStyle.color,
246258
fontFamily: props.textStyle.fontFamily,
247259
fontSize: props.textStyle.fontSize,

0 commit comments

Comments
 (0)