Files
paseo/packages/react-native-enriched-markdown/ios/segments/ENRMTableIOSGridView.m
T
Mohamed Boudra 80c44321cc Move the Markdown engine into the repository and fix two Android list layouts (#10499)
* chore(app): vendor react-native-enriched-markdown with the Paseo patch applied

Copy react-native-enriched-markdown@1.2.0-nightly-20261014-766fc0e67 (upstream
software-mansion/enriched-markdown 766fc0e67) into
packages/react-native-enriched-markdown, with
patches/react-native-enriched-markdown+1.2.0-nightly-20261014-766fc0e67.patch
applied. Nothing else changes in this commit; the package is not a workspace yet.

Reproduce:

  npm pack react-native-enriched-markdown@1.2.0-nightly-20261014-766fc0e67
  tar xzf react-native-enriched-markdown-*.tgz
  mkdir -p t/node_modules && mv package t/node_modules/react-native-enriched-markdown
  (cd t && git init -q && git apply ../patches/react-native-enriched-markdown+*.patch)
  diff -r t/node_modules/react-native-enriched-markdown packages/react-native-enriched-markdown

The diff is empty.

* chore(app): consume react-native-enriched-markdown from its workspace

Make packages/react-native-enriched-markdown a private workspace and point the
app at it, then remove the patch-package patch it replaces.

- package.json: private, version follows the root like every workspace, only the
  postinstall script that downloads tree-sitter grammars. Upstream's
  devDependencies would install a second react-native, so they are gone.
- The package keeps its name, so autolinking, the Gradle project
  :react-native-enriched-markdown, the CocoaPods spec and imports are unchanged.
- Lockfile: drop the nested registry copy under packages/app/node_modules; npm
  links node_modules/react-native-enriched-markdown to the workspace.
- Formatting, lint and knip skip the vendored code. CI routes changes to it
  through the app and browser suites.
- The codegen test checks the vendored package's generated props.
- docs/markdown-renderer.md describes the fork and how to edit it.

* fix(app): keep numbered sub-list markers off their text on Android

From #9964, moved from the package patch into the vendored package.

On Android, a numbered list nested in a bulleted item drew "1." over
the first letters of each item. A nested item's text indent summed the
root list's narrow bullet column with a fixed marginLeft, while its
marker was right-aligned to its own wider "99." column, so the number
landed on the text. iOS already indents from the item's own column.

Backport the Android hunks of upstream enriched-markdown #1015 (size
the number column to the widest marker) and #1023 (nested items start
their text from their own marker column).

test:android now also runs the backported ListRenderingTest and
TaskListInteractionTest suites. docs/markdown-renderer.md lists the
fork's backports, this one and #924, which the removed postinstall
comment used to track.

The vendored tree equals the #9964 patch applied to the pristine
package.

* fix(app): keep Android assistant replies inside their turns

From #10380, moved from the package patch into the vendored package.

On Android, an assistant reply with blockquotes nested inside a list
could overlap its "Worked for ..." footer and the next user message:
the displayed text and the layout measurement applied spans in
different orders, and the list-wide line height inflated the blockquote
spacers when it ran after them.

Use the same span order for Android Markdown measurement and display.
Apply list line height before blockquote spacers so nested quotes keep
their configured padding and the reply fits above the turn footer.

test:android now also runs ListBlockquoteMeasurementTest.

Applied on top of #9964; the two changes share only ListRenderer.kt,
where #10380's import and line-height call sit beside #9964's marker
width code. Every other file equals the #10380 patch applied to the
pristine package.

* chore(app): match the vendored Markdown web block renderer source to its build

The package patch changed lib/module/web/renderers/BlockRenderers.js (the
renderers.codeBlock override and the zoom-in cursor on images) but not
src/web/renderers/BlockRenderers.tsx. The app runs lib, so behavior does
not change. Port both changes into src so src stays the source of lib.
2026-11-23 10:15:43 +07:00

269 lines
9.0 KiB
Objective-C

#import "ENRMTableIOSGridView.h"
#import "LinkTapUtils.h"
#if !TARGET_OS_OSX
@interface ENRMTableIOSGridView () <UIGestureRecognizerDelegate>
@end
@implementation ENRMTableIOSItemHit
@end
@implementation ENRMTableIOSRowData
@end
@implementation ENRMTableIOSGridView {
NSArray<ENRMTableIOSRowData *> *_tableRows;
NSArray<NSNumber *> *_columnWidths;
NSArray<NSNumber *> *_rowHeights;
UIColor *_borderColor;
CGFloat _borderWidth;
CGFloat _horizontalCellPadding;
CGFloat _verticalCellPadding;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.contentMode = UIViewContentModeRedraw;
self.opaque = NO;
self.accessibilityElementsHidden = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:tap];
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.delegate = self;
[self addGestureRecognizer:longPress];
}
return self;
}
#pragma mark - Data
- (void)updateWithRows:(NSArray<ENRMTableIOSRowData *> *)rows
columnWidths:(NSArray<NSNumber *> *)columnWidths
rowHeights:(NSArray<NSNumber *> *)rowHeights
borderColor:(UIColor *)borderColor
borderWidth:(CGFloat)borderWidth
horizontalCellPadding:(CGFloat)horizontalCellPadding
verticalCellPadding:(CGFloat)verticalCellPadding
cornerRadius:(CGFloat)cornerRadius
{
_tableRows = [rows copy];
_columnWidths = [columnWidths copy];
_rowHeights = [rowHeights copy];
_borderColor = borderColor;
_borderWidth = borderWidth;
_horizontalCellPadding = horizontalCellPadding;
_verticalCellPadding = verticalCellPadding;
[self setNeedsDisplay];
}
- (void)fadeInRowsFrom:(NSUInteger)startRow duration:(NSTimeInterval)duration
{
if (startRow >= _tableRows.count)
return;
[UIView transitionWithView:self
duration:duration
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{}
completion:nil];
}
#pragma mark - Drawing
- (void)drawRect:(CGRect)dirtyRect
{
if (!_tableRows.count || !_columnWidths.count || !_rowHeights.count)
return;
CGFloat yOffset = 0;
for (NSUInteger r = 0; r < _tableRows.count; r++) {
ENRMTableIOSRowData *rowData = _tableRows[r];
CGFloat rowHeight = [_rowHeights[r] doubleValue];
CGFloat xOffset = 0;
NSUInteger colCount = MIN(rowData.cellTexts.count, _columnWidths.count);
for (NSUInteger c = 0; c < colCount; c++) {
CGFloat columnWidth = [_columnWidths[c] doubleValue];
CGRect cellRect = CGRectMake(xOffset, yOffset, columnWidth + _borderWidth, rowHeight + _borderWidth);
[rowData.backgroundColor setFill];
UIRectFill(cellRect);
[_borderColor setStroke];
UIBezierPath *border = [UIBezierPath bezierPathWithRect:cellRect];
border.lineWidth = _borderWidth;
[border stroke];
NSAttributedString *text = rowData.cellTexts[c];
if (text.length > 0) {
CGRect textRect = CGRectMake(xOffset + _horizontalCellPadding, yOffset + _verticalCellPadding,
columnWidth - _horizontalCellPadding * 2, rowHeight - _verticalCellPadding * 2);
[text drawWithRect:textRect
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
context:nil];
}
xOffset += columnWidth;
}
yOffset += rowHeight;
}
}
#pragma mark - Link hit-testing
- (BOOL)cellAtPoint:(CGPoint)point
rowOrigin:(out CGFloat *)outRowY
rowHeight:(out CGFloat *)outRowH
colOrigin:(out CGFloat *)outColX
colWidth:(out CGFloat *)outColW
cellText:(out NSAttributedString *__autoreleasing *)outText
{
CGFloat rowY = 0;
for (NSUInteger r = 0; r < _tableRows.count; r++) {
CGFloat rh = [_rowHeights[r] doubleValue];
if (point.y >= rowY && point.y < rowY + rh) {
CGFloat colX = 0;
for (NSUInteger c = 0; c < _columnWidths.count; c++) {
CGFloat cw = [_columnWidths[c] doubleValue];
if (point.x >= colX && point.x < colX + cw) {
ENRMTableIOSRowData *rowData = _tableRows[r];
if (c >= rowData.cellTexts.count)
return NO;
*outRowY = rowY;
*outRowH = rh;
*outColX = colX;
*outColW = cw;
*outText = rowData.cellTexts[c];
return YES;
}
colX += cw;
}
return NO;
}
rowY += rh;
}
return NO;
}
static ENRMTableIOSItemHit *itemInAttributedString(NSAttributedString *text, CGRect textRect, CGPoint point)
{
if (text.length == 0)
return nil;
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:text];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:textRect.size];
textContainer.lineFragmentPadding = 0;
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
[layoutManager ensureLayoutForTextContainer:textContainer];
CGPoint local = CGPointMake(point.x - textRect.origin.x, point.y - textRect.origin.y);
CGFloat fraction = 0;
NSUInteger glyphIndex = [layoutManager glyphIndexForPoint:local
inTextContainer:textContainer
fractionOfDistanceThroughGlyph:&fraction];
if (glyphIndex >= layoutManager.numberOfGlyphs)
return nil;
NSUInteger charIndex = [layoutManager characterIndexForGlyphAtIndex:glyphIndex];
if (charIndex >= text.length)
return nil;
NSRange itemRange;
NSRange whole = NSMakeRange(0, text.length);
NSString *linkURL = [text attribute:@"linkURL" atIndex:charIndex longestEffectiveRange:&itemRange inRange:whole];
NSString *imageURL =
linkURL ? nil : [text attribute:@"imageURL" atIndex:charIndex longestEffectiveRange:&itemRange inRange:whole];
if (!linkURL && !imageURL)
return nil;
NSRange glyphRange = [layoutManager glyphRangeForCharacterRange:itemRange actualCharacterRange:NULL];
CGRect frame = [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
// Links keep nearest-glyph hits; an image needs the tap inside its bounds.
if (imageURL && !CGRectContainsPoint(frame, local))
return nil;
ENRMTableIOSItemHit *hit = [[ENRMTableIOSItemHit alloc] init];
hit.kind = linkURL ? ENRMTableIOSItemKindLink : ENRMTableIOSItemKindImage;
hit.url = linkURL ?: imageURL;
hit.sourceType = [text attribute:@"linkSourceType" atIndex:charIndex effectiveRange:NULL] ?: @"markdown";
hit.title = linkURL ? linkTitleAtIndex(text, charIndex)
: [text attribute:@"imageAltText" atIndex:charIndex effectiveRange:NULL];
hit.frame = CGRectOffset(frame, textRect.origin.x, textRect.origin.y);
return hit;
}
- (ENRMTableIOSItemHit *)itemAtPoint:(CGPoint)point
{
CGFloat rowY, rowH, colX, colW;
NSAttributedString *text;
if (![self cellAtPoint:point rowOrigin:&rowY rowHeight:&rowH colOrigin:&colX colWidth:&colW cellText:&text])
return nil;
CGRect textRect = CGRectMake(colX + _horizontalCellPadding, rowY + _verticalCellPadding,
colW - _horizontalCellPadding * 2, rowH - _verticalCellPadding * 2);
if (!CGRectContainsPoint(textRect, point))
return nil;
return itemInAttributedString(text, textRect, point);
}
- (ENRMTableIOSItemHit *)linkAtPoint:(CGPoint)point
{
ENRMTableIOSItemHit *hit = [self itemAtPoint:point];
return hit.kind == ENRMTableIOSItemKindLink ? hit : nil;
}
#pragma mark - Gesture handlers
- (void)handleLinkGesture:(UIGestureRecognizer *)recognizer block:(ENRMTableIOSLinkBlock)block
{
CGPoint point = [recognizer locationInView:self];
NSString *url = [self linkAtPoint:point].url;
if (url && block) {
block(url);
}
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
if (recognizer.state != UIGestureRecognizerStateEnded)
return;
ENRMTableIOSItemHit *hit = [self itemAtPoint:[recognizer locationInView:self]];
if (!hit)
return;
if (hit.kind == ENRMTableIOSItemKindLink) {
if (self.onLinkTap)
self.onLinkTap(hit.url, hit.sourceType);
} else if (self.onImageTap) {
self.onImageTap(hit.url, hit.title ?: @"");
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
return YES;
// Reject before recognition so the context-menu interaction can own this touch.
NSString *url = [self linkAtPoint:[touch locationInView:self]].url;
return !(url && self.hasLinkContextMenu && self.hasLinkContextMenu(url));
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan) {
[self handleLinkGesture:recognizer block:self.onLinkLongTap];
}
}
@end
#endif // !TARGET_OS_OSX