|
1 | 1 | #import "EnrichedMarkdown.h" |
2 | | -#import "AccessibilityInfo.h" |
3 | | -#import "AttributedRenderer.h" |
4 | 2 | #import "ContextMenuUtils.h" |
5 | 3 | #import "ENRMImageAttachment.h" |
6 | 4 | #import "ENRMMarkdownParser.h" |
| 5 | +#import "ENRMTextRenderer.h" |
7 | 6 | #import "ENRMUIKit.h" |
8 | 7 | #import "EditMenuUtils.h" |
9 | 8 |
|
|
25 | 24 | #import "MarkdownAccessibilityElementBuilder.h" |
26 | 25 | #import "MarkdownExtractor.h" |
27 | 26 | #import "ParagraphStyleUtils.h" |
28 | | -#import "RenderContext.h" |
29 | 27 | #import "RuntimeKeys.h" |
30 | 28 | #import "StyleConfig.h" |
31 | 29 | #import "StylePropsUtils.h" |
@@ -91,32 +89,6 @@ + (instancetype)segmentWithLatex:(NSString *)latex |
91 | 89 | @end |
92 | 90 | #endif |
93 | 91 |
|
94 | | -@interface EMRenderedTextSegment : NSObject |
95 | | -@property (nonatomic, strong) NSMutableAttributedString *attributedText; |
96 | | -@property (nonatomic, strong) RenderContext *context; |
97 | | -@property (nonatomic, strong) AccessibilityInfo *accessibilityInfo; |
98 | | -@property (nonatomic, assign) CGFloat lastElementMarginBottom; |
99 | | -+ (instancetype)withAttributedText:(NSMutableAttributedString *)text |
100 | | - context:(RenderContext *)context |
101 | | - accessibilityInfo:(AccessibilityInfo *)info |
102 | | - lastElementMarginBottom:(CGFloat)marginBottom; |
103 | | -@end |
104 | | - |
105 | | -@implementation EMRenderedTextSegment |
106 | | -+ (instancetype)withAttributedText:(NSMutableAttributedString *)text |
107 | | - context:(RenderContext *)context |
108 | | - accessibilityInfo:(AccessibilityInfo *)info |
109 | | - lastElementMarginBottom:(CGFloat)marginBottom |
110 | | -{ |
111 | | - EMRenderedTextSegment *segment = [[EMRenderedTextSegment alloc] init]; |
112 | | - segment.attributedText = text; |
113 | | - segment.context = context; |
114 | | - segment.accessibilityInfo = info; |
115 | | - segment.lastElementMarginBottom = marginBottom; |
116 | | - return segment; |
117 | | -} |
118 | | -@end |
119 | | - |
120 | 92 | @interface EnrichedMarkdown () <RCTEnrichedMarkdownViewProtocol, UITextViewDelegate> |
121 | 93 | @end |
122 | 94 |
|
@@ -363,11 +335,11 @@ - (void)renderMarkdownContent:(NSString *)markdownString |
363 | 335 |
|
364 | 336 | for (id segment in segments) { |
365 | 337 | if ([segment isKindOfClass:[EMTextSegment class]]) { |
366 | | - EMRenderedTextSegment *rendered = [self renderTextSegment:(EMTextSegment *)segment |
367 | | - config:config |
368 | | - allowTrailingMargin:allowTrailingMargin |
369 | | - allowFontScaling:allowFontScaling |
370 | | - maxFontSizeMultiplier:maxFontSizeMultiplier]; |
| 338 | + ENRMRenderResult *rendered = [self renderTextSegment:(EMTextSegment *)segment |
| 339 | + config:config |
| 340 | + allowTrailingMargin:allowTrailingMargin |
| 341 | + allowFontScaling:allowFontScaling |
| 342 | + maxFontSizeMultiplier:maxFontSizeMultiplier]; |
371 | 343 | [renderedSegments addObject:rendered]; |
372 | 344 | } else if ([segment isKindOfClass:[EMTableSegment class]]) { |
373 | 345 | [renderedSegments addObject:segment]; |
@@ -401,11 +373,11 @@ - (NSArray *)parseAndRenderSegments:(NSString *)markdownString |
401 | 373 |
|
402 | 374 | for (id segment in segments) { |
403 | 375 | if ([segment isKindOfClass:[EMTextSegment class]]) { |
404 | | - EMRenderedTextSegment *rendered = [self renderTextSegment:(EMTextSegment *)segment |
405 | | - config:_config |
406 | | - allowTrailingMargin:_allowTrailingMargin |
407 | | - allowFontScaling:_fontScaleObserver.allowFontScaling |
408 | | - maxFontSizeMultiplier:_maxFontSizeMultiplier]; |
| 376 | + ENRMRenderResult *rendered = [self renderTextSegment:(EMTextSegment *)segment |
| 377 | + config:_config |
| 378 | + allowTrailingMargin:_allowTrailingMargin |
| 379 | + allowFontScaling:_fontScaleObserver.allowFontScaling |
| 380 | + maxFontSizeMultiplier:_maxFontSizeMultiplier]; |
409 | 381 | [renderedSegments addObject:rendered]; |
410 | 382 | } else if ([segment isKindOfClass:[EMTableSegment class]]) { |
411 | 383 | [renderedSegments addObject:segment]; |
@@ -442,8 +414,8 @@ - (void)renderMarkdownSynchronously:(NSString *)markdownString |
442 | 414 | } |
443 | 415 |
|
444 | 416 | for (id segment in renderedSegments) { |
445 | | - if ([segment isKindOfClass:[EMRenderedTextSegment class]]) { |
446 | | - EnrichedMarkdownInternalText *view = [self createTextViewForRenderedSegment:(EMRenderedTextSegment *)segment]; |
| 417 | + if ([segment isKindOfClass:[ENRMRenderResult class]]) { |
| 418 | + EnrichedMarkdownInternalText *view = [self createTextViewForRenderedSegment:(ENRMRenderResult *)segment]; |
447 | 419 | [_segmentViews addObject:view]; |
448 | 420 | [self addSubview:view]; |
449 | 421 | } else if ([segment isKindOfClass:[EMTableSegment class]]) { |
@@ -471,8 +443,8 @@ - (void)applyRenderedSegments:(NSArray *)renderedSegments |
471 | 443 | [_segmentViews removeAllObjects]; |
472 | 444 |
|
473 | 445 | for (id segment in renderedSegments) { |
474 | | - if ([segment isKindOfClass:[EMRenderedTextSegment class]]) { |
475 | | - EnrichedMarkdownInternalText *view = [self createTextViewForRenderedSegment:(EMRenderedTextSegment *)segment]; |
| 446 | + if ([segment isKindOfClass:[ENRMRenderResult class]]) { |
| 447 | + EnrichedMarkdownInternalText *view = [self createTextViewForRenderedSegment:(ENRMRenderResult *)segment]; |
476 | 448 | [_segmentViews addObject:view]; |
477 | 449 | [self addSubview:view]; |
478 | 450 | } else if ([segment isKindOfClass:[EMTableSegment class]]) { |
@@ -501,34 +473,17 @@ - (void)applyRenderedSegments:(NSArray *)renderedSegments |
501 | 473 | } |
502 | 474 | } |
503 | 475 |
|
504 | | -- (EMRenderedTextSegment *)renderTextSegment:(EMTextSegment *)textSegment |
505 | | - config:(StyleConfig *)config |
506 | | - allowTrailingMargin:(BOOL)allowTrailingMargin |
507 | | - allowFontScaling:(BOOL)allowFontScaling |
508 | | - maxFontSizeMultiplier:(CGFloat)maxFontSizeMultiplier |
| 476 | +- (ENRMRenderResult *)renderTextSegment:(EMTextSegment *)textSegment |
| 477 | + config:(StyleConfig *)config |
| 478 | + allowTrailingMargin:(BOOL)allowTrailingMargin |
| 479 | + allowFontScaling:(BOOL)allowFontScaling |
| 480 | + maxFontSizeMultiplier:(CGFloat)maxFontSizeMultiplier |
509 | 481 | { |
510 | | - MarkdownASTNode *temporaryRoot = [[MarkdownASTNode alloc] initWithType:MarkdownNodeTypeDocument]; |
511 | | - for (MarkdownASTNode *node in textSegment.nodes) { |
512 | | - [temporaryRoot addChild:node]; |
513 | | - } |
514 | | - |
515 | | - AttributedRenderer *renderer = [[AttributedRenderer alloc] initWithConfig:config]; |
516 | | - [renderer setAllowTrailingMargin:allowTrailingMargin]; |
517 | | - RenderContext *context = [RenderContext new]; |
518 | | - context.allowFontScaling = allowFontScaling; |
519 | | - context.maxFontSizeMultiplier = maxFontSizeMultiplier; |
520 | | - NSMutableAttributedString *attributedText = [renderer renderRoot:temporaryRoot context:context]; |
521 | | - |
522 | | - CGFloat lastMarginBottom = [renderer getLastElementMarginBottom]; |
523 | | - AccessibilityInfo *accessibilityInfo = [AccessibilityInfo infoFromContext:context]; |
524 | | - |
525 | | - return [EMRenderedTextSegment withAttributedText:attributedText |
526 | | - context:context |
527 | | - accessibilityInfo:accessibilityInfo |
528 | | - lastElementMarginBottom:lastMarginBottom]; |
| 482 | + return ENRMRenderASTNodes(textSegment.nodes, config, allowTrailingMargin, allowFontScaling, maxFontSizeMultiplier, |
| 483 | + currentWritingDirection()); |
529 | 484 | } |
530 | 485 |
|
531 | | -- (EnrichedMarkdownInternalText *)createTextViewForRenderedSegment:(EMRenderedTextSegment *)segment |
| 486 | +- (EnrichedMarkdownInternalText *)createTextViewForRenderedSegment:(ENRMRenderResult *)segment |
532 | 487 | { |
533 | 488 | EnrichedMarkdownInternalText *view = [[EnrichedMarkdownInternalText alloc] initWithConfig:_config]; |
534 | 489 | view.spoilerMode = _spoilerMode; |
|
0 commit comments