Skip to content

Commit 857d0ee

Browse files
committed
fix(mobile): keep an inline chip on its line without stretching the bubble
A chip is taller than the lowercase band it sits in, and both platforms grow the line to fit it: React Native raises the line's ascent around an inline view, and TextKit sizes the fragment to the attachment. The chip rode above the words and the message gained a gap on top that no amount of moving the chip could remove — a transform or margin shifts only the drawn pixels and leaves the line box where it was. Size the box from the paragraph font instead of the chip. Android reports an inline box no taller than the font's ascent and hangs the bitmap off it, which text does not clip; iOS centres the attachment on the run font's ascender and descender, the rule the composer's span already used. The line then measures exactly as tall as a line of plain text. Two smaller faults surfaced alongside. A run whose whole text is U+FFFC became a bare attachment carrying no font or paragraph style, so a chip opening a paragraph dropped its line height; the placeholder is now a zero-width space and the iOS attachment string keeps the run's attributes. And the attachment container rendered even when empty, so its parent gap padded every bubble without attachments. Measured on device: iOS bubble padding 18.0/9.7pt becomes 11.0/11.0pt, Android 14.9/12.6dp becomes 10.6/10.3dp against the 10dp the bubble asks for.
1 parent 34148a8 commit 857d0ee

8 files changed

Lines changed: 151 additions & 40 deletions

File tree

‎apps/mobile/modules/t3-markdown-text/android/src/main/java/expo/modules/t3markdowntext/T3MarkdownTextSelectionModule.kt‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import android.content.Context
66
import android.graphics.Bitmap
77
import android.graphics.Canvas
88
import android.graphics.Color
9+
import android.graphics.Paint
10+
import android.graphics.Typeface
911
import android.text.Spannable
1012
import android.text.SpannableStringBuilder
1113
import android.text.Spanned
14+
import android.text.TextPaint
1215
import android.text.style.ReplacementSpan
1316
import android.view.ActionMode
1417
import android.view.Menu
@@ -17,6 +20,7 @@ import android.widget.TextView
1720
import android.util.Base64
1821
import android.util.LruCache
1922
import com.facebook.react.bridge.ReactContext
23+
import com.facebook.react.common.assets.ReactFontManager
2024
import com.facebook.react.uimanager.UIManagerHelper
2125
import expo.modules.kotlin.modules.Module
2226
import expo.modules.kotlin.modules.ModuleDefinition
@@ -159,12 +163,30 @@ private class SanitizingSelectionActionModeCallback(
159163
class T3MarkdownTextSelectionModule : Module() {
160164
private val chipImages = LruCache<String, Map<String, Any>>(128)
161165

166+
/**
167+
* Metrics of the paragraph font a chip sits in, so the inline box it reports can be sized
168+
* from the same ascent and descent the surrounding text lays out with.
169+
*/
170+
private fun paragraphFontMetrics(text: JSONObject?, scale: Float): Paint.FontMetricsInt =
171+
TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
172+
textSize = (text?.optDouble("fontSize", 15.0)?.toFloat() ?: 15f).coerceIn(6f, 80f) * scale
173+
val fontFamily = text?.optString("fontFamily").orEmpty()
174+
typeface = if (fontFamily.isEmpty()) {
175+
Typeface.DEFAULT
176+
} else {
177+
ReactFontManager.getInstance()
178+
.getTypeface(fontFamily, Typeface.NORMAL, appContext.reactContext?.assets)
179+
}
180+
}.fontMetricsInt
181+
162182
override fun definition() = ModuleDefinition {
163183
Name("T3MarkdownTextSelection")
164184

165185
Function("renderContextChip") { payloadJson: String ->
166-
val metrics = appContext.reactContext?.resources?.displayMetrics ?: return@Function null
167-
val key = "${metrics.density}:${metrics.widthPixels}:$payloadJson"
186+
val resources = appContext.reactContext?.resources ?: return@Function null
187+
val metrics = resources.displayMetrics
188+
val fontScale = resources.configuration.fontScale
189+
val key = "${metrics.density}:$fontScale:${metrics.widthPixels}:$payloadJson"
168190
chipImages.get(key)?.let { return@Function it }
169191
val payload = JSONObject(payloadJson)
170192
val chip = T3ContextChip(
@@ -196,14 +218,30 @@ class T3MarkdownTextSelectionModule : Module() {
196218
val bytes = ByteArrayOutputStream()
197219
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes)
198220
bitmap.recycle()
221+
// React Native sits an inline view's box on the text baseline and grows the line's
222+
// ascent to fit it, so a box as tall as the chip lifts the chip above the words and
223+
// pushes the baseline down. Report a box no taller than the paragraph font's ascent,
224+
// which leaves the line exactly as tall as a line of plain text, plus where inside
225+
// that box the bitmap must sit so the chip centres on the font's ascent/descent box:
226+
// the same rule the composer's ReplacementSpan uses to draw its chips.
227+
val lineMetrics = paragraphFontMetrics(
228+
payload.optJSONObject("text"),
229+
fontScale * metrics.density,
230+
)
231+
val ascent = -lineMetrics.ascent
232+
val descent = lineMetrics.descent
233+
val bitmapWidth = ceil(chip.width) + bleed * 2
234+
val bitmapHeight = ceil(chip.height) + bleed * 2
199235
val result = mapOf<String, Any>(
200236
"uri" to
201237
"data:image/png;base64,${Base64.encodeToString(bytes.toByteArray(), Base64.NO_WRAP)}",
202238
// Layout rounds dp back to whole pixels. Reporting a hair less than the bitmap lets
203239
// that rounding land inside the image and crop its right-hand border, so round the
204240
// box up: an extra fraction of a pixel is invisible, a missing border is not.
205-
"width" to (ceil(chip.width) + bleed * 2) / metrics.density,
206-
"height" to (ceil(chip.height) + bleed * 2) / metrics.density,
241+
"width" to bitmapWidth / metrics.density,
242+
"height" to bitmapHeight / metrics.density,
243+
"boxHeight" to ascent / metrics.density,
244+
"offsetY" to (ascent + descent - bitmapHeight) / 2f / metrics.density,
207245
)
208246
chipImages.put(key, result)
209247
result

‎apps/mobile/modules/t3-markdown-text/ios/T3ContextChip.h‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,36 @@ static UIImage *T3ContextChipVectorIcon(NSString *symbol, CGFloat size, UIColor
6565
}];
6666
}
6767

68+
// Centres the chip on the run font's ascent/descent box, the rule the composer span and
69+
// Android use, so the chip lands in the same place beside the words on every surface.
70+
static inline CGRect T3ContextChipBounds(UIFont *font, CGSize size)
71+
{
72+
CGFloat y = font != nil ? (font.ascender + font.descender - size.height) / 2 : -3;
73+
return CGRectMake(0, y, size.width, size.height);
74+
}
75+
76+
// A bare attachment string carries none of the run's attributes. Losing the paragraph
77+
// style at a paragraph's first character drops its line height, and losing the font lets
78+
// a chip-only line shrink to the bitmap, so the placeholder keeps both, plus the run
79+
// colour so a later re-apply (after an image loads) still tints with it.
80+
static inline NSAttributedString *T3MarkdownTextAttachmentString(
81+
NSTextAttachment *attachment, NSDictionary<NSAttributedStringKey, id> *runAttributes)
82+
{
83+
NSMutableAttributedString *string =
84+
[[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
85+
for (NSAttributedStringKey key in
86+
@[
87+
NSFontAttributeName, NSParagraphStyleAttributeName, NSForegroundColorAttributeName,
88+
NSBaselineOffsetAttributeName
89+
]) {
90+
id value = runAttributes[key];
91+
if (value != nil) {
92+
[string addAttribute:key value:value range:NSMakeRange(0, string.length)];
93+
}
94+
}
95+
return string;
96+
}
97+
6898
static UIFont *T3ContextChipFont(NSDictionary *payload)
6999
{
70100
CGFloat size = MAX(10, MIN(40, [payload[@"fontSize"] doubleValue]));

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ static void T3MarkdownTextApplyAttachments(
133133
if (isSymbol) {
134134
image = [UIImage systemImageNamed:[imageUri substringFromIndex:3]];
135135
}
136-
UIColor *foregroundColor = [attributedString attribute:NSForegroundColorAttributeName
137-
atIndex:attachmentRange.location
138-
effectiveRange:nil];
136+
NSDictionary *runAttributes =
137+
[attributedString attributesAtIndex:attachmentRange.location effectiveRange:nil];
138+
UIColor *foregroundColor = runAttributes[NSForegroundColorAttributeName];
139139
if (image != nil && (isSymbol || attachmentRange.tintWithForeground)) {
140140
image = [image imageWithTintColor:foregroundColor ?: UIColor.labelColor
141141
renderingMode:UIImageRenderingModeAlwaysOriginal];
@@ -150,23 +150,15 @@ static void T3MarkdownTextApplyAttachments(
150150
NSDictionary *chip = T3ContextChipPayload(imageUri);
151151
if (chip != nil) {
152152
CGSize size = CGSizeMake(attachmentRange.chipWidth, attachmentRange.chipHeight);
153-
attachment.bounds = CGRectMake(0, -3, size.width, size.height);
153+
attachment.bounds = T3ContextChipBounds(runAttributes[NSFontAttributeName], size);
154154
NSString *iconUri = [chip[@"iconUri"] isKindOfClass:NSString.class] ? chip[@"iconUri"] : nil;
155155
attachment.image = T3ContextChipImage(chip, size, iconUri ? images[iconUri] : nil);
156156
}
157157
const NSRange range = NSMakeRange(
158158
attachmentRange.location,
159159
MIN(attachmentRange.length, attributedString.length - attachmentRange.location));
160-
NSMutableAttributedString *attachmentString =
161-
[[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
162-
// Keep the run color on the attachment so a later re-apply (after the image
163-
// loads asynchronously) still tints with the link color, not labelColor.
164-
if (foregroundColor != nil) {
165-
[attachmentString addAttribute:NSForegroundColorAttributeName
166-
value:foregroundColor
167-
range:NSMakeRange(0, attachmentString.length)];
168-
}
169-
[attributedString replaceCharactersInRange:range withAttributedString:attachmentString];
160+
[attributedString replaceCharactersInRange:range
161+
withAttributedString:T3MarkdownTextAttachmentString(attachment, runAttributes)];
170162
}
171163
}
172164

@@ -409,6 +401,8 @@ - (void)drawRect:(CGRect)rect
409401
convertedAttrString,
410402
_state->getData().attachmentRanges,
411403
_attachmentImages);
404+
// Matches the shadow node so drawn lines sit where measurement put them.
405+
RCTApplyBaselineOffset(convertedAttrString);
412406
NSUInteger runLocation = 0;
413407
for (UIView *child in self.subviews) {
414408
if (![child isKindOfClass:[T3MarkdownTextRun class]]) {

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,18 @@ static void applyAttachments(
6565
T3MarkdownTextAttachmentBaselineOffset(attachmentRange),
6666
attachmentSize,
6767
attachmentSize);
68+
NSDictionary *runAttributes =
69+
[attributedString attributesAtIndex:attachmentRange.location effectiveRange:nil];
6870
if (attachmentRange.chipWidth > 0) {
69-
attachment.bounds = CGRectMake(0, -3, attachmentRange.chipWidth, attachmentRange.chipHeight);
71+
attachment.bounds = T3ContextChipBounds(
72+
runAttributes[NSFontAttributeName],
73+
CGSizeMake(attachmentRange.chipWidth, attachmentRange.chipHeight));
7074
}
7175
const NSRange range = NSMakeRange(
7276
attachmentRange.location,
7377
MIN(attachmentRange.length, attributedString.length - attachmentRange.location));
74-
NSAttributedString *attachmentString =
75-
[NSAttributedString attributedStringWithAttachment:attachment];
76-
[attributedString replaceCharactersInRange:range withAttributedString:attachmentString];
78+
[attributedString replaceCharactersInRange:range
79+
withAttributedString:T3MarkdownTextAttachmentString(attachment, runAttributes)];
7780
}
7881
}
7982

@@ -240,6 +243,10 @@ static void applyAttachments(
240243
[RCTNSAttributedStringFromAttributedString(baseAttributedString) mutableCopy];
241244
applyParagraphStyles(convertedAttributedString, paragraphStyleRanges);
242245
applyAttachments(convertedAttributedString, attachmentRanges);
246+
// TextKit stacks a paragraph's extra line height above the glyphs. React Native's own
247+
// layout manager centres them with a baseline offset; do the same, after attachments
248+
// so chips shift with the words.
249+
RCTApplyBaselineOffset(convertedAttributedString);
243250

244251
const CGFloat maximumWidth = std::isfinite(layoutConstraints.maximumSize.width)
245252
? layoutConstraints.maximumSize.width

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Text as RNText,
1010
type TextStyle,
1111
useColorScheme,
12+
View,
1213
} from "react-native";
1314

1415
import { MarkdownTextPrimitive } from "./MarkdownTextPrimitive";
@@ -43,6 +44,11 @@ export const MarkdownFileContextMenuContext = createContext<MarkdownFileContextM
4344
const EXTERNAL_LINK_PREFIX = "◉ ";
4445
const INLINE_ATTACHMENT_PREFIX = "\uFFFC\u00A0";
4546
const SKILL_ICON_PLACEHOLDER = "\uFFFC";
47+
// React Native turns a run whose whole text is U+FFFC into a bare inline-view attachment
48+
// with no font or paragraph style, so a chip opening a paragraph would drop its line
49+
// height. Any other single character keeps the run's attributes; the native side swaps it
50+
// for the chip attachment either way.
51+
const IOS_CHIP_PLACEHOLDER = "\u200B";
4652
const PARAGRAPH_STYLE_ENCODING_OFFSET = 1000;
4753
const MONO_FONT_FAMILY = Platform.select({
4854
ios: "ui-monospace",
@@ -241,12 +247,24 @@ export function NativeMarkdownSelectableText(props: {
241247
border: props.textStyle.contextChipBorderColor ?? props.textStyle.dividerColor,
242248
}
243249
: null;
250+
// Android sizes the chip's inline box from the paragraph font so the line box stays
251+
// the height of a plain text line; see renderContextChip.
244252
const androidChip =
245-
Platform.OS === "android" && chip ? renderAndroidContextChip(JSON.stringify(chip)) : null;
253+
Platform.OS === "android" && chip
254+
? renderAndroidContextChip(
255+
JSON.stringify({
256+
...chip,
257+
text: {
258+
fontFamily: props.textStyle.fontFamily,
259+
fontSize: props.textStyle.fontSize,
260+
},
261+
}),
262+
)
263+
: null;
246264
if (androidChip) {
247265
text = "";
248266
} else if (chip && Platform.OS === "ios") {
249-
text = "\uFFFC";
267+
text = IOS_CHIP_PLACEHOLDER;
250268
} else if (run.fileIcon && Platform.OS === "ios") {
251269
text = `${INLINE_ATTACHMENT_PREFIX}${text}`;
252270
} else if (run.skillName && run.skillLabel) {
@@ -368,7 +386,10 @@ export function NativeMarkdownSelectableText(props: {
368386
}
369387
>
370388
{androidChip ? (
371-
<Image
389+
// The inline box sits on the baseline and is only as tall as the font's
390+
// ascent, so it never changes the line's height. The bitmap hangs off that
391+
// box (views in text are not clipped) to centre the chip on the text.
392+
<View
372393
accessible
373394
accessibilityLabel={chip?.label}
374395
accessibilityRole={onPress ? "button" : "image"}
@@ -380,13 +401,23 @@ export function NativeMarkdownSelectableText(props: {
380401
}
381402
: undefined
382403
}
383-
// The bitmap is measured in whole pixels but laid out in dp, so the box can
384-
// round a hair narrower than the image. `cover` would crop that difference
385-
// off the right-hand border; `contain` fits the whole chip instead.
386-
resizeMode="contain"
387-
source={{ uri: androidChip.uri }}
388-
style={{ width: androidChip.width, height: androidChip.height }}
389-
/>
404+
style={{ width: androidChip.width, height: androidChip.boxHeight }}
405+
>
406+
<Image
407+
// The bitmap is measured in whole pixels but laid out in dp, so the box can
408+
// round a hair narrower than the image. `cover` would crop that difference
409+
// off the right-hand border; `contain` fits the whole chip instead.
410+
resizeMode="contain"
411+
source={{ uri: androidChip.uri }}
412+
style={{
413+
position: "absolute",
414+
left: 0,
415+
top: androidChip.offsetY,
416+
width: androidChip.width,
417+
height: androidChip.height,
418+
}}
419+
/>
420+
</View>
390421
) : Platform.OS === "android" && run.fileIcon ? (
391422
<Image source={markdownFileIconSource(run.fileIcon)} style={styles.inlineIcon} />
392423
) : Platform.OS === "android" && linkIcon ? (

‎apps/mobile/modules/t3-markdown-text/src/T3MarkdownTextSelectionModule.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ interface T3MarkdownTextSelectionNativeModule {
66
readonly uri: string;
77
readonly width: number;
88
readonly height: number;
9+
/** Inline box height: the paragraph font's ascent, so the line box never grows. */
10+
readonly boxHeight: number;
11+
/** Bitmap top relative to the box top; negative when the chip overhangs the box. */
12+
readonly offsetY: number;
913
} | null;
1014
}
1115

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,9 @@ function renderFeedEntry(
14741474
: [],
14751475
),
14761476
);
1477+
const visibleAttachments = attachments.filter(
1478+
(attachment) => isImageAttachment(attachment) || !inlineAttachmentIds.has(attachment.id),
1479+
);
14771480
return (
14781481
<View className="mb-5 items-end">
14791482
<View
@@ -1510,13 +1513,10 @@ function renderFeedEntry(
15101513
<MessageAttachmentUnknown key={attachment.id} name={attachment.name} />
15111514
),
15121515
)}
1513-
<View className={inlineAttachmentIds.size ? "flex-row flex-wrap gap-2" : "gap-2"}>
1514-
{attachments
1515-
.filter(
1516-
(attachment) =>
1517-
isImageAttachment(attachment) || !inlineAttachmentIds.has(attachment.id),
1518-
)
1519-
.map((attachment) => {
1516+
{/* An empty container still takes a gap, which pads every attachment-free bubble. */}
1517+
{visibleAttachments.length > 0 ? (
1518+
<View className={inlineAttachmentIds.size ? "flex-row flex-wrap gap-2" : "gap-2"}>
1519+
{visibleAttachments.map((attachment) => {
15201520
return isImageAttachment(attachment) ? (
15211521
<MessageAttachmentImage
15221522
key={attachment.id}
@@ -1543,7 +1543,8 @@ function renderFeedEntry(
15431543
<MessageAttachmentUnknown key={attachment.id} name={attachment.name} />
15441544
);
15451545
})}
1546-
</View>
1546+
</View>
1547+
) : null}
15471548
{message.text.trim().length > 0 ? (
15481549
<MarkdownImageAvailableWidthContext
15491550
value={props.userBubbleMaxWidth - USER_BUBBLE_HORIZONTAL_PADDING * 2}

‎docs/internals/mobile-development.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ only when the generated stylesheet and theme list are unchanged. Skipping compil
2323
would lose new classes; invalidating every consumer for unchanged output makes an
2424
ordinary component edit refresh the whole app. The fingerprint is recorded only
2525
after initialization succeeds.
26+
27+
The native modules under `apps/mobile/modules/` are `file:` dependencies, and pnpm
28+
copies those into its virtual store instead of linking them. Metro bundles the copy,
29+
so an edit to a module's TypeScript is invisible to a running dev client until
30+
`vp i` re-syncs it, while Gradle and CocoaPods compile the worktree directory
31+
directly. A JavaScript change that "has no effect" on device is usually this.

0 commit comments

Comments
 (0)