Files
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

212 lines
8.1 KiB
Objective-C

#import "ENRMMarkdownTextView.h"
#import "ENRMLinkPillAttachment.h"
#import "ENRMLinkPillText.h"
#import "ENRMTextHitTest.h"
#import "LinkTapUtils.h"
#import "PasteboardUtils.h"
#if !TARGET_OS_OSX
@interface ENRMMarkdownTextView () <UIContextMenuInteractionDelegate>
@end
@implementation ENRMMarkdownTextView {
ENRMLinkMenuLift *_pillLift;
// The pill whose menu is being presented. It draws nothing for as long as its lifted image
// stands in for it: it would show through that image.
ENRMLinkPillAttachment *_liftedPill;
NSUInteger _pillMenuIndex;
}
- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer
{
if (self = [super initWithFrame:frame textContainer:textContainer]) {
_pillLift = [[ENRMLinkMenuLift alloc] init];
__weak ENRMMarkdownTextView *weakSelf = self;
_pillLift.onEnd = ^{ [weakSelf restoreLiftedPill]; };
[self addInteraction:[[UIContextMenuInteraction alloc] initWithDelegate:self]];
}
return self;
}
/// New text moves or replaces the pill under its lifted image.
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[_pillLift end];
[super setAttributedText:attributedText];
}
/// The selection with pills expanded, or nil when `range` is not the selection or holds no pill.
/// Only the selection is rewritten: UIKit also reads arbitrary ranges to find word boundaries,
/// and those must keep matching storage offsets.
- (nullable NSAttributedString *)expandedSelectionForRange:(UITextRange *)range
{
UITextRange *selection = self.selectedTextRange;
if (!range || !selection || selection.isEmpty || ![range isEqual:selection])
return nil;
NSRange selected = self.selectedRange;
NSTextStorage *storage = self.textStorage;
if (selected.location == NSNotFound || NSMaxRange(selected) > storage.length)
return nil;
NSAttributedString *text = [storage attributedSubstringFromRange:selected];
NSAttributedString *expanded = ENRMAttributedStringByExpandingLinkPills(text, NULL);
return expanded == text ? nil : expanded;
}
- (NSString *)textInRange:(UITextRange *)range
{
return [self expandedSelectionForRange:range].string ?: [super textInRange:range];
}
- (NSAttributedString *)attributedTextInRange:(UITextRange *)range
{
return [self expandedSelectionForRange:range] ?: [super attributedTextInRange:range];
}
/// The menu's Copy is already replaced by the library's own action; this covers the key command.
- (void)copy:(id)sender
{
NSRange selected = self.selectedRange;
if (self.copySelectionHandler && selected.location != NSNotFound && selected.length > 0) {
self.copySelectionHandler(selected);
return;
}
NSAttributedString *expanded = [self expandedSelectionForRange:self.selectedTextRange];
if (!expanded) {
[super copy:sender];
return;
}
copyAttributedStringToPasteboard(expanded, nil, nil);
}
#pragma mark - Pill menus
- (nullable ENRMLinkContextMenus *)linkContextMenus
{
id delegate = self.delegate;
return [delegate conformsToProtocol:@protocol(ENRMLinkContextMenuSource)]
? [(id<ENRMLinkContextMenuSource>)delegate linkContextMenusForTextView:self]
: nil;
}
/// Where the pill at `index` is drawn, in the text view's coordinates.
- (CGRect)pillFrameAtIndex:(NSUInteger)index
{
NSLayoutManager *layoutManager = self.layoutManager;
NSUInteger glyph = [layoutManager glyphIndexForCharacterAtIndex:index];
CGSize size = [layoutManager attachmentSizeForGlyphAtIndex:glyph];
if (size.width <= 0 || size.height <= 0)
return CGRectNull;
CGRect line = [layoutManager lineFragmentRectForGlyphAtIndex:glyph effectiveRange:NULL];
// TextKit places an attachment glyph at the bottom edge of its bounds.
CGPoint location = [layoutManager locationForGlyphAtIndex:glyph];
UIEdgeInsets inset = self.textContainerInset;
return CGRectMake(inset.left + line.origin.x + location.x, inset.top + line.origin.y + location.y - size.height,
size.width, size.height);
}
/// Whether the character at `index` is a pill whose link has a menu.
- (BOOL)presentsLinkMenuAtIndex:(NSUInteger)index
{
ENRMLinkContextMenus *menus = [self linkContextMenus];
NSTextStorage *storage = self.textStorage;
if (menus.entries.count == 0 || index >= storage.length)
return NO;
id attachment = [storage attribute:NSAttachmentAttributeName atIndex:index effectiveRange:NULL];
return [attachment isKindOfClass:ENRMLinkPillAttachment.class] &&
[menus hasMenuForURL:linkURLAtRange(self, NSMakeRange(index, 1))];
}
/// The character index of the pill under `point` when its link has a menu, else NSNotFound.
- (NSUInteger)menuPillIndexAtPoint:(CGPoint)point
{
if ([self linkContextMenus].entries.count == 0)
return NSNotFound;
NSUInteger index = ENRMCharacterIndexAtPoint(self, point);
if (index == NSNotFound || ![self presentsLinkMenuAtIndex:index] ||
!CGRectContainsPoint([self pillFrameAtIndex:index], point))
return NSNotFound;
return index;
}
- (void)setPill:(ENRMLinkPillAttachment *)pill lifted:(BOOL)lifted
{
pill.lifted = lifted;
NSTextStorage *storage = self.textStorage;
[storage enumerateAttribute:NSAttachmentAttributeName
inRange:NSMakeRange(0, storage.length)
options:0
usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value != pill)
return;
[self.layoutManager invalidateDisplayForCharacterRange:range];
*stop = YES;
}];
}
- (void)restoreLiftedPill
{
if (_liftedPill)
[self setPill:_liftedPill lifted:NO];
_liftedPill = nil;
}
- (UIContextMenuConfiguration *)contextMenuInteraction:(UIContextMenuInteraction *)interaction
configurationForMenuAtLocation:(CGPoint)location
{
[_pillLift end];
_pillMenuIndex = [self menuPillIndexAtPoint:location];
if (_pillMenuIndex == NSNotFound)
return nil;
UIMenu *menu = [[self linkContextMenus] menuForURL:linkURLAtRange(self, NSMakeRange(_pillMenuIndex, 1))
title:linkTitleAtIndex(self.textStorage, _pillMenuIndex)];
if (!menu)
return nil;
return [UIContextMenuConfiguration
configurationWithIdentifier:nil
previewProvider:nil
actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) { return menu; }];
}
/// The pill is lifted as an image of itself, see `ENRMLinkMenuLift`.
- (UITargetedPreview *)pillMenuPreview
{
if (![_pillLift isOverView:self]) {
NSTextStorage *storage = self.textStorage;
if (self.window == nil || _pillMenuIndex >= storage.length)
return nil;
id attachment = [storage attribute:NSAttachmentAttributeName atIndex:_pillMenuIndex effectiveRange:NULL];
CGRect frame = [self pillFrameAtIndex:_pillMenuIndex];
if (![attachment isKindOfClass:ENRMLinkPillAttachment.class] || CGRectIsNull(frame))
return nil;
UIImage *image = [(ENRMLinkPillAttachment *)attachment imageForBounds:(CGRect){CGPointZero, frame.size}
textContainer:self.textContainer
characterIndex:_pillMenuIndex];
[_pillLift layImage:image atFrame:frame overView:self];
_liftedPill = attachment;
[self setPill:_liftedPill lifted:YES];
}
return [_pillLift previewWithCornerRadius:_liftedPill.cornerRadius backgroundColor:UIColor.clearColor];
}
- (UITargetedPreview *)contextMenuInteraction:(UIContextMenuInteraction *)interaction
previewForHighlightingMenuWithConfiguration:(UIContextMenuConfiguration *)configuration
{
return [self pillMenuPreview];
}
- (UITargetedPreview *)contextMenuInteraction:(UIContextMenuInteraction *)interaction
previewForDismissingMenuWithConfiguration:(UIContextMenuConfiguration *)configuration
{
return [self pillMenuPreview];
}
- (void)contextMenuInteraction:(UIContextMenuInteraction *)interaction
willEndForConfiguration:(UIContextMenuConfiguration *)configuration
animator:(id<UIContextMenuInteractionAnimating>)animator
{
[_pillLift endWithAnimator:animator];
}
@end
#endif