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

422 lines
12 KiB
Objective-C

#import "ENRMFeatureFlags.h"
#if ENRICHED_MARKDOWN_VIDEO
#import "ENRMVideoContainerView.h"
#import "MarkdownASTNode.h"
#import "PasteboardUtils.h"
#import <AVKit/AVKit.h>
#if TARGET_OS_OSX
#import "ENRMMenuAction.h"
#endif
static const CGFloat kDefaultVideoAspectRatio = 16.0 / 9.0;
static inline CGFloat ENRMVideoAspectRatio(StyleConfig *config)
{
CGFloat aspectRatio = [config videoAspectRatio];
return aspectRatio > 0 ? aspectRatio : kDefaultVideoAspectRatio;
}
#if !TARGET_OS_OSX
// A thin wrapper VC that hosts AVPlayerViewController as a child with correct
// containment. RNSScreen (react-native-screens) overrides
// shouldAutomaticallyForwardAppearanceMethods, so using our own intermediate VC
// lets us trigger appearance transitions explicitly and guarantees the player
// controls render.
@interface ENRMVideoHostController : UIViewController
@property (nonatomic, strong, readonly) AVPlayerViewController *playerViewController;
- (void)applyCornerRadius:(CGFloat)radius backgroundColor:(UIColor *)color;
@end
@implementation ENRMVideoHostController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.showsPlaybackControls = YES;
_playerViewController.entersFullScreenWhenPlaybackBegins = NO;
_playerViewController.exitsFullScreenWhenPlaybackEnds = YES;
_playerViewController.view.layer.masksToBounds = YES;
[self addChildViewController:_playerViewController];
[self.view addSubview:_playerViewController.view];
[_playerViewController didMoveToParentViewController:self];
}
- (void)applyCornerRadius:(CGFloat)radius backgroundColor:(UIColor *)color
{
_playerViewController.view.layer.cornerRadius = radius;
_playerViewController.view.backgroundColor = color ?: [UIColor blackColor];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
_playerViewController.view.frame = self.view.bounds;
}
@end
#endif // !TARGET_OS_OSX
// ===== iOS implementation =====
#if !TARGET_OS_OSX
static RCTUIView *ENRMCreatePlayIconOverlay(void)
{
CGFloat size = 52;
RCTUIView *circle = [[RCTUIView alloc] initWithFrame:CGRectMake(0, 0, size, size)];
circle.userInteractionEnabled = NO;
circle.layer.backgroundColor = [RCTUIColor colorWithWhite:0 alpha:0.5].CGColor;
circle.layer.cornerRadius = size / 2.0;
CGFloat inset = size * 0.3;
CGFloat triLeft = inset + size * 0.04;
CGFloat triTop = inset - size * 0.04;
CGFloat triRight = size - inset + size * 0.04;
CGFloat triMid = size / 2.0;
UIBezierPath *triangle = [UIBezierPath bezierPath];
[triangle moveToPoint:CGPointMake(triLeft, triTop)];
BezierPathAddLine(triangle, CGPointMake(triRight, triMid));
BezierPathAddLine(triangle, CGPointMake(triLeft, size - triTop));
[triangle closePath];
CAShapeLayer *triLayer = [CAShapeLayer layer];
triLayer.path = triangle.CGPath;
triLayer.fillColor = [RCTUIColor whiteColor].CGColor;
[circle.layer addSublayer:triLayer];
return circle;
}
@implementation ENRMVideoContainerView {
ENRMVideoHostController *_hostController;
RCTUIView *_playIconOverlay;
NSString *_currentURL;
BOOL _hostInstalled;
BOOL _hasBeenTapped;
}
- (instancetype)initWithConfig:(StyleConfig *)config
{
if (self = [super init]) {
_config = config;
_dynamicProps = [[ENRMDynamicBlockProps alloc] init];
self.userInteractionEnabled = YES;
_hostController = [[ENRMVideoHostController alloc] init];
[_hostController loadViewIfNeeded];
[_hostController applyCornerRadius:[config videoBorderRadius] backgroundColor:[config videoBackgroundColor]];
[self addSubview:_hostController.view];
_playIconOverlay = ENRMCreatePlayIconOverlay();
_playIconOverlay.userInteractionEnabled = NO;
[self addSubview:_playIconOverlay];
UIContextMenuInteraction *contextMenu = [[UIContextMenuInteraction alloc] initWithDelegate:self];
[_hostController.view addInteraction:contextMenu];
}
return self;
}
#pragma mark - Touch Forwarding
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.userInteractionEnabled || self.hidden || self.alpha < 0.01) {
return nil;
}
if ([self pointInside:point withEvent:event]) {
if (!_hasBeenTapped) {
_hasBeenTapped = YES;
_playIconOverlay.hidden = YES;
}
return [_hostController.view hitTest:[self convertPoint:point toView:_hostController.view] withEvent:event];
}
return nil;
}
#pragma mark - View Controller Containment
- (void)didMoveToWindow
{
[super didMoveToWindow];
if (self.window && !_hostInstalled) {
UIViewController *parentVC = [self enrm_closestViewController];
if (parentVC) {
[parentVC addChildViewController:_hostController];
[_hostController beginAppearanceTransition:YES animated:NO];
[_hostController didMoveToParentViewController:parentVC];
[_hostController endAppearanceTransition];
_hostInstalled = YES;
}
} else if (!self.window && _hostInstalled) {
[_hostController beginAppearanceTransition:NO animated:NO];
[_hostController endAppearanceTransition];
[_hostController willMoveToParentViewController:nil];
[_hostController removeFromParentViewController];
_hostInstalled = NO;
}
}
- (UIViewController *)enrm_closestViewController
{
UIResponder *responder = self.nextResponder;
while (responder) {
if ([responder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)responder;
}
responder = responder.nextResponder;
}
return nil;
}
#pragma mark - Style Updates
- (void)reapplyStyle
{
[_hostController applyCornerRadius:[_config videoBorderRadius] backgroundColor:[_config videoBackgroundColor]];
}
#pragma mark - Video Loading
- (void)applyVideoNode:(MarkdownASTNode *)node
{
NSString *url = [node.attributes objectForKey:@"url"];
if (!url || [url isEqualToString:_currentURL]) {
return;
}
_currentURL = [url copy];
_hasBeenTapped = NO;
_playIconOverlay.hidden = NO;
AVPlayerViewController *playerVC = _hostController.playerViewController;
[playerVC.player pause];
NSURL *videoURL = [NSURL URLWithString:url];
if (!videoURL) {
playerVC.player = nil;
return;
}
playerVC.player = [AVPlayer playerWithURL:videoURL];
}
#pragma mark - Layout & Measurement
- (void)layoutSubviews
{
[super layoutSubviews];
_hostController.view.frame = self.bounds;
_playIconOverlay.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
}
- (CGFloat)measureHeight:(CGFloat)maxWidth
{
return maxWidth / ENRMVideoAspectRatio(_config);
}
+ (CGFloat)measureHeightForVideoNode:(__unused MarkdownASTNode *)node
config:(StyleConfig *)config
maxWidth:(CGFloat)maxWidth
{
return maxWidth / ENRMVideoAspectRatio(config);
}
#pragma mark - Context Menu
- (void)copyURLToPasteboard
{
if (_currentURL.length > 0) {
copyStringToPasteboard(_currentURL);
}
}
- (void)copyMarkdownToPasteboard
{
if (_currentURL.length > 0) {
NSString *markdown;
if ([_currentURL containsString:@"\""]) {
markdown = [NSString stringWithFormat:@"<video src='%@' />", _currentURL];
} else {
markdown = [NSString stringWithFormat:@"<video src=\"%@\" />", _currentURL];
}
copyStringToPasteboard(markdown);
}
}
- (UIContextMenuConfiguration *)contextMenuInteraction:(UIContextMenuInteraction *)interaction
configurationForMenuAtLocation:(CGPoint)location
{
if (!self.dynamicProps.enableBlockContextMenu || _currentURL.length == 0) {
return nil;
}
return [UIContextMenuConfiguration
configurationWithIdentifier:nil
previewProvider:nil
actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
UIAction *copyURL =
[UIAction actionWithTitle:self.dynamicProps.menuCopyLabel
image:[RCTUIImage systemImageNamed:@"doc.on.doc"]
identifier:nil
handler:^(__kindof UIAction *action) { [self copyURLToPasteboard]; }];
UIAction *copyMarkdown =
[UIAction actionWithTitle:self.dynamicProps.menuCopyAsMarkdownLabel
image:[RCTUIImage systemImageNamed:@"doc.text"]
identifier:nil
handler:^(__kindof UIAction *action) { [self copyMarkdownToPasteboard]; }];
return [UIMenu menuWithTitle:@"" children:@[ copyURL, copyMarkdown ]];
}];
}
#pragma mark - Cleanup
- (void)dealloc
{
[_hostController.playerViewController.player pause];
_hostController.playerViewController.player = nil;
[_hostController beginAppearanceTransition:NO animated:NO];
[_hostController endAppearanceTransition];
[_hostController willMoveToParentViewController:nil];
[_hostController removeFromParentViewController];
}
@end
#else // TARGET_OS_OSX
// ===== macOS implementation =====
@implementation ENRMVideoContainerView {
AVPlayerView *_playerView;
NSString *_currentURL;
}
- (instancetype)initWithConfig:(StyleConfig *)config
{
if (self = [super init]) {
_config = config;
_dynamicProps = [[ENRMDynamicBlockProps alloc] init];
self.wantsLayer = YES;
_playerView = [[AVPlayerView alloc] init];
_playerView.controlsStyle = AVPlayerViewControlsStyleInline;
_playerView.wantsLayer = YES;
_playerView.layer.masksToBounds = YES;
_playerView.layer.cornerRadius = [config videoBorderRadius];
_playerView.layer.backgroundColor = ([config videoBackgroundColor] ?: [NSColor blackColor]).CGColor;
[self addSubview:_playerView];
}
return self;
}
- (BOOL)isFlipped
{
return YES;
}
#pragma mark - Style Updates
- (void)reapplyStyle
{
_playerView.layer.cornerRadius = [_config videoBorderRadius];
_playerView.layer.backgroundColor = ([_config videoBackgroundColor] ?: [NSColor blackColor]).CGColor;
}
#pragma mark - Video Loading
- (void)applyVideoNode:(MarkdownASTNode *)node
{
NSString *url = [node.attributes objectForKey:@"url"];
if (!url || [url isEqualToString:_currentURL]) {
return;
}
_currentURL = [url copy];
[_playerView.player pause];
NSURL *videoURL = [NSURL URLWithString:url];
if (!videoURL) {
_playerView.player = nil;
return;
}
_playerView.player = [AVPlayer playerWithURL:videoURL];
}
#pragma mark - Layout & Measurement
- (void)layout
{
[super layout];
_playerView.frame = self.bounds;
}
- (CGFloat)measureHeight:(CGFloat)maxWidth
{
return maxWidth / ENRMVideoAspectRatio(_config);
}
+ (CGFloat)measureHeightForVideoNode:(__unused MarkdownASTNode *)node
config:(StyleConfig *)config
maxWidth:(CGFloat)maxWidth
{
return maxWidth / ENRMVideoAspectRatio(config);
}
#pragma mark - Context Menu
- (void)copyURLToPasteboard
{
if (_currentURL.length > 0) {
copyStringToPasteboard(_currentURL);
}
}
- (void)copyMarkdownToPasteboard
{
if (_currentURL.length > 0) {
NSString *markdown;
if ([_currentURL containsString:@"\""]) {
markdown = [NSString stringWithFormat:@"<video src='%@' />", _currentURL];
} else {
markdown = [NSString stringWithFormat:@"<video src=\"%@\" />", _currentURL];
}
copyStringToPasteboard(markdown);
}
}
- (NSMenu *)menuForEvent:(NSEvent *)event
{
if (!self.dynamicProps.enableBlockContextMenu || _currentURL.length == 0) {
return [super menuForEvent:event];
}
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
[menu addItem:ENRMCreateMenuItem(self.dynamicProps.menuCopyLabel, ^{ [self copyURLToPasteboard]; })];
[menu addItem:ENRMCreateMenuItem(self.dynamicProps.menuCopyAsMarkdownLabel, ^{ [self copyMarkdownToPasteboard]; })];
return menu;
}
#pragma mark - Cleanup
- (void)dealloc
{
[_playerView.player pause];
_playerView.player = nil;
}
@end
#endif // TARGET_OS_OSX
#endif // ENRICHED_MARKDOWN_VIDEO