-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathEnrichedMarkdownText.mm
More file actions
665 lines (546 loc) · 20.7 KB
/
EnrichedMarkdownText.mm
File metadata and controls
665 lines (546 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#import "EnrichedMarkdownText.h"
#import "CodeBlockBackground.h"
#import "ContextMenuUtils.h"
#import "ENRMContextMenuTextView+macOS.h"
#import "ENRMImageAttachment.h"
#import "ENRMMarkdownParser.h"
#import "ENRMSpoilerOverlayManager.h"
#import "ENRMSpoilerTapUtils.h"
#import "ENRMTailFadeInAnimator.h"
#import "ENRMTextRenderer.h"
#import "ENRMTextViewSetup.h"
#import "EditMenuUtils.h"
#import "FontScaleObserver.h"
#import "FontUtils.h"
#import "HeightUpdateUtils.h"
#import "LinkTapUtils.h"
#import "MarkdownASTNode.h"
#import "MarkdownAccessibilityElementBuilder.h"
#import "MarkdownExtractor.h"
#import "ParagraphStyleUtils.h"
#import "RuntimeKeys.h"
#import "StylePropsUtils.h"
#import "TaskListTapUtils.h"
#import <ReactNativeEnrichedMarkdown/EnrichedMarkdownTextComponentDescriptor.h>
#import <ReactNativeEnrichedMarkdown/EventEmitters.h>
#import <ReactNativeEnrichedMarkdown/Props.h>
#import <ReactNativeEnrichedMarkdown/RCTComponentViewHelpers.h>
#import "RCTFabricComponentsPlugins.h"
#import <React/RCTConversions.h>
#import <React/RCTFont.h>
#import <react/utils/ManagedObjectWrapper.h>
using namespace facebook::react;
@interface EnrichedMarkdownText () <RCTEnrichedMarkdownTextViewProtocol, UITextViewDelegate>
- (void)setupTextView;
- (void)renderMarkdownContent:(NSString *)markdownString;
- (void)applyRenderedText:(NSMutableAttributedString *)attributedText;
- (void)textTapped:(ENRMTapRecognizer *)recognizer;
- (void)setupLayoutManager;
@end
@implementation EnrichedMarkdownText {
ENRMPlatformTextView *_textView;
ENRMMarkdownParser *_parser;
NSString *_cachedMarkdown;
NSString *_renderedMarkdown;
StyleConfig *_config;
ENRMMd4cFlags *_md4cFlags;
dispatch_queue_t _renderQueue;
NSUInteger _currentRenderId;
BOOL _blockAsyncRender;
EnrichedMarkdownTextShadowNode::ConcreteState::Shared _state;
int _heightUpdateCounter;
FontScaleObserver *_fontScaleObserver;
CGFloat _maxFontSizeMultiplier;
CGFloat _lastElementMarginBottom;
BOOL _allowTrailingMargin;
BOOL _enableLinkPreview;
BOOL _streamingAnimation;
NSUInteger _previousTextLength;
ENRMTailFadeInAnimator *_fadeAnimator;
AccessibilityInfo *_accessibilityInfo;
#if !TARGET_OS_OSX
NSMutableArray<UIAccessibilityElement *> *_accessibilityElements;
#else
NSMutableArray *_accessibilityElements;
#endif
BOOL _accessibilityNeedsRebuild;
NSArray<NSString *> *_contextMenuItemTexts;
NSArray<NSString *> *_contextMenuItemIcons;
ENRMSpoilerOverlayManager *_spoilerManager;
}
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<EnrichedMarkdownTextComponentDescriptor>();
}
#pragma mark - Measuring and State
- (CGSize)measureSize:(CGFloat)maxWidth
{
CGSize size = ENRMMeasureMarkdownText(_textView, maxWidth, _config, _allowTrailingMargin, _lastElementMarginBottom);
if (CGSizeEqualToSize(size, CGSizeZero)) {
CGFloat defaultHeight = UIFontLineHeight([UIFont systemFontOfSize:16.0]);
return CGSizeMake(maxWidth, defaultHeight);
}
return size;
}
- (BOOL)hasRenderedMarkdown:(NSString *)markdown
{
return _renderedMarkdown != nil && [_renderedMarkdown isEqualToString:markdown];
}
- (void)updateState:(const facebook::react::State::Shared &)state
oldState:(const facebook::react::State::Shared &)oldState
{
_state = std::static_pointer_cast<const EnrichedMarkdownTextShadowNode::ConcreteState>(state);
if (oldState == nullptr) {
[self requestHeightUpdate];
}
}
- (void)requestHeightUpdate
{
if (_state == nullptr) {
return;
}
_heightUpdateCounter++;
auto selfRef = wrapManagedObjectWeakly(self);
_state->updateState(EnrichedMarkdownTextState(_heightUpdateCounter, selfRef));
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const EnrichedMarkdownTextProps>();
_props = defaultProps;
self.backgroundColor = [RCTUIColor clearColor];
_parser = [[ENRMMarkdownParser alloc] init];
_md4cFlags = [ENRMMd4cFlags defaultFlags];
_renderQueue = dispatch_queue_create("com.swmansion.enriched.markdown.render", DISPATCH_QUEUE_SERIAL);
_currentRenderId = 0;
_maxFontSizeMultiplier = 0;
_allowTrailingMargin = NO;
_enableLinkPreview = YES;
_fontScaleObserver = [[FontScaleObserver alloc] init];
__weak EnrichedMarkdownText *weakSelf = self;
_fontScaleObserver.onChange = ^{
EnrichedMarkdownText *strongSelf = weakSelf;
if (!strongSelf)
return;
if (strongSelf->_config != nil) {
[strongSelf->_config setFontScaleMultiplier:strongSelf->_fontScaleObserver.effectiveFontScale];
}
if (strongSelf->_cachedMarkdown != nil && strongSelf->_cachedMarkdown.length > 0) {
[strongSelf renderMarkdownContent:strongSelf->_cachedMarkdown];
}
};
[self setupTextView];
}
return self;
}
- (void)setupTextView
{
#if !TARGET_OS_OSX
_textView = [[ENRMPlatformTextView alloc] init];
_textView.text = @"";
#else
_textView = [[ENRMContextMenuTextView alloc] init];
_textView.string = @"";
#endif
ENRMConfigureMarkdownTextView(_textView);
_textView.delegate = self;
_textView.hidden = YES;
#if TARGET_OS_OSX
__weak EnrichedMarkdownText *weakSelf = self;
((ENRMContextMenuTextView *)_textView).contextMenuProvider =
^NSMenu *_Nullable(NSMenu *baseMenu, NSTextView *textView)
{
EnrichedMarkdownText *strongSelf = weakSelf;
if (!strongSelf) {
return baseMenu;
}
NSArray<NSMenuItem *> *customItems = ENRMBuildContextMenuItems(
strongSelf->_contextMenuItemTexts, strongSelf->_contextMenuItemIcons, textView,
^(NSString *itemText, NSString *selectedText, NSUInteger selectionStart, NSUInteger selectionEnd) {
auto eventEmitter =
std::static_pointer_cast<EnrichedMarkdownTextEventEmitter const>(strongSelf->_eventEmitter);
if (eventEmitter) {
eventEmitter->onContextMenuItemPress({
.itemText = std::string(itemText.UTF8String),
.selectedText = std::string(selectedText.UTF8String),
.selectionStart = (int)selectionStart,
.selectionEnd = (int)selectionEnd,
});
}
});
return buildEditMenuForSelection(textView.textStorage, textView.selectedRange, strongSelf->_cachedMarkdown,
strongSelf->_config, @[ baseMenu ], customItems);
};
#endif
ENRMTapRecognizer *tapRecognizer = [[ENRMTapRecognizer alloc] initWithTarget:self action:@selector(textTapped:)];
[_textView addGestureRecognizer:tapRecognizer];
self.contentView = _textView;
}
- (void)didAddSubview:(RCTUIView *)subview
{
[super didAddSubview:subview];
if (subview == _textView) {
[self setupLayoutManager];
}
}
- (void)willRemoveSubview:(RCTUIView *)subview
{
if (subview == _textView) {
ENRMDetachLayoutManager(_textView);
}
[super willRemoveSubview:subview];
}
- (void)setupLayoutManager
{
ENRMAttachLayoutManager(_textView, _config);
}
- (void)renderMarkdownContent:(NSString *)markdownString
{
if (_blockAsyncRender) {
return;
}
_cachedMarkdown = [markdownString copy];
NSUInteger renderId = ++_currentRenderId;
StyleConfig *config = [_config copy];
ENRMMarkdownParser *parser = _parser;
ENRMMd4cFlags *md4cFlags = [_md4cFlags copy];
BOOL allowFontScaling = _fontScaleObserver.allowFontScaling;
CGFloat maxFontSizeMultiplier = _maxFontSizeMultiplier;
BOOL allowTrailingMargin = _allowTrailingMargin;
NSWritingDirection writingDirection = currentWritingDirection();
dispatch_async(_renderQueue, ^{
MarkdownASTNode *ast = [parser parseMarkdown:markdownString flags:md4cFlags];
if (!ast) {
return;
}
ENRMRenderResult *result = ENRMRenderASTNodes(ast.children, config, allowTrailingMargin, allowFontScaling,
maxFontSizeMultiplier, writingDirection);
dispatch_async(dispatch_get_main_queue(), ^{
if (renderId != self->_currentRenderId) {
return;
}
self->_lastElementMarginBottom = result.lastElementMarginBottom;
self->_accessibilityInfo = result.accessibilityInfo;
[self applyRenderedText:result.attributedText];
});
});
}
- (NSMutableAttributedString *)parseAndRenderMarkdown:(NSString *)markdownString
{
MarkdownASTNode *ast = [_parser parseMarkdown:markdownString flags:_md4cFlags];
if (!ast) {
return nil;
}
ENRMRenderResult *result =
ENRMRenderASTNodes(ast.children, _config, _allowTrailingMargin, _fontScaleObserver.allowFontScaling,
_maxFontSizeMultiplier, currentWritingDirection());
_lastElementMarginBottom = result.lastElementMarginBottom;
_accessibilityInfo = result.accessibilityInfo;
return result.attributedText;
}
/// Synchronous rendering for mock view measurement (no UI updates needed).
- (void)renderMarkdownSynchronously:(NSString *)markdownString
{
if (!markdownString || markdownString.length == 0) {
return;
}
_blockAsyncRender = YES;
_cachedMarkdown = [markdownString copy];
NSMutableAttributedString *attributedText = [self parseAndRenderMarkdown:markdownString];
if (!attributedText) {
return;
}
_textView.attributedText = attributedText;
_renderedMarkdown = [_cachedMarkdown copy];
}
- (void)applyRenderedText:(NSMutableAttributedString *)attributedText
{
NSUInteger tailStart = _previousTextLength;
NSLayoutManager *layoutManager = _textView.layoutManager;
if ([layoutManager isKindOfClass:[TextViewLayoutManager class]]) {
[layoutManager setValue:_config forKey:@"config"];
}
objc_setAssociatedObject(_textView.textContainer, kTextViewKey, _textView, OBJC_ASSOCIATION_ASSIGN);
// Ensure the text container has unlimited height before setting content.
// updateLayoutMetrics may have shrunk the frame (and thus the text container)
// from a previous layout pass, which would clip the new attributed text.
CGFloat containerWidth = _textView.textContainer.size.width;
if (containerWidth <= 0) {
containerWidth = self.bounds.size.width;
}
_textView.textContainer.size = CGSizeMake(containerWidth, CGFLOAT_MAX);
_accessibilityElements = nil;
_accessibilityNeedsRebuild = YES;
_textView.attributedText = attributedText;
_renderedMarkdown = [_cachedMarkdown copy];
[_textView.layoutManager invalidateLayoutForCharacterRange:NSMakeRange(0, attributedText.length)
actualCharacterRange:NULL];
// When bounds width is zero (recycled view not yet laid out), skip layout
// and measurement — layoutSubviews will handle it once the view has real
// bounds. Measuring with width=0 produces a bogus single-line measurement
// that corrupts the height sent to Yoga.
[_spoilerManager setNeedsUpdate];
if (self.bounds.size.width > 0) {
[_textView.layoutManager ensureLayoutForTextContainer:_textView.textContainer];
ENRMSetNeedsDisplay(_textView);
#if !TARGET_OS_OSX
[self setNeedsLayout];
#endif
[_spoilerManager updateIfNeeded];
CGSize measured = [self measureSize:self.bounds.size.width];
if (needsHeightUpdate(measured, self.bounds)) {
[self requestHeightUpdate];
}
}
if (_textView.hidden) {
dispatch_async(dispatch_get_main_queue(), ^{ self->_textView.hidden = NO; });
}
if (_streamingAnimation) {
if (!_fadeAnimator) {
_fadeAnimator = [[ENRMTailFadeInAnimator alloc] initWithTextView:_textView];
}
[_fadeAnimator animateFrom:tailStart to:attributedText.length];
_previousTextLength = attributedText.length;
}
}
#if !TARGET_OS_OSX
- (void)layoutSubviews
{
[super layoutSubviews];
[_spoilerManager updateIfNeeded];
}
#endif
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &oldViewProps = *std::static_pointer_cast<EnrichedMarkdownTextProps const>(_props);
const auto &newViewProps = *std::static_pointer_cast<EnrichedMarkdownTextProps const>(props);
BOOL stylePropChanged = NO;
if (_config == nil) {
_config = [[StyleConfig alloc] init];
[_config setFontScaleMultiplier:_fontScaleObserver.effectiveFontScale];
_spoilerManager = [[ENRMSpoilerOverlayManager alloc] initWithTextView:_textView config:_config];
}
stylePropChanged = applyMarkdownStyleToConfig(_config, newViewProps.markdownStyle, oldViewProps.markdownStyle);
if (stylePropChanged) {
[ENRMImageAttachment clearAttachmentRegistry];
}
NSLayoutManager *layoutManager = _textView.layoutManager;
if ([layoutManager isKindOfClass:[TextViewLayoutManager class]]) {
StyleConfig *currentConfig = [layoutManager valueForKey:@"config"];
if (currentConfig != _config) {
[layoutManager setValue:_config forKey:@"config"];
}
}
if (_textView.selectable != newViewProps.selectable) {
_textView.selectable = newViewProps.selectable;
}
if (newViewProps.allowFontScaling != oldViewProps.allowFontScaling) {
_fontScaleObserver.allowFontScaling = newViewProps.allowFontScaling;
if (_config != nil) {
[_config setFontScaleMultiplier:_fontScaleObserver.effectiveFontScale];
}
stylePropChanged = YES;
}
if (newViewProps.maxFontSizeMultiplier != oldViewProps.maxFontSizeMultiplier) {
_maxFontSizeMultiplier = newViewProps.maxFontSizeMultiplier;
if (_config != nil) {
[_config setMaxFontSizeMultiplier:_maxFontSizeMultiplier];
}
stylePropChanged = YES;
}
if (newViewProps.allowTrailingMargin != oldViewProps.allowTrailingMargin) {
_allowTrailingMargin = newViewProps.allowTrailingMargin;
}
BOOL md4cFlagsChanged = NO;
if (newViewProps.md4cFlags.underline != oldViewProps.md4cFlags.underline) {
_md4cFlags.underline = newViewProps.md4cFlags.underline;
md4cFlagsChanged = YES;
}
if (newViewProps.md4cFlags.latexMath != oldViewProps.md4cFlags.latexMath) {
_md4cFlags.latexMath = newViewProps.md4cFlags.latexMath;
md4cFlagsChanged = YES;
}
BOOL markdownChanged = oldViewProps.markdown != newViewProps.markdown;
BOOL allowTrailingMarginChanged = newViewProps.allowTrailingMargin != oldViewProps.allowTrailingMargin;
_enableLinkPreview = newViewProps.enableLinkPreview;
if (ENRMContextMenuItemsChanged(oldViewProps.contextMenuItems, newViewProps.contextMenuItems)) {
_contextMenuItemTexts = ENRMContextMenuTextsFromItems(newViewProps.contextMenuItems);
_contextMenuItemIcons = ENRMContextMenuIconsFromItems(newViewProps.contextMenuItems);
}
if (newViewProps.streamingAnimation != oldViewProps.streamingAnimation) {
_streamingAnimation = newViewProps.streamingAnimation;
if (_streamingAnimation) {
_previousTextLength = ENRMGetAttributedText(_textView).length;
} else {
[_fadeAnimator cancel];
_fadeAnimator = nil;
_previousTextLength = 0;
}
}
if (newViewProps.spoilerMode != oldViewProps.spoilerMode) {
NSString *modeStr = [[NSString alloc] initWithUTF8String:newViewProps.spoilerMode.c_str()];
_spoilerManager.spoilerMode = ENRMSpoilerModeFromString(modeStr);
}
if (markdownChanged || stylePropChanged || md4cFlagsChanged || allowTrailingMarginChanged) {
NSString *markdownString = [[NSString alloc] initWithUTF8String:newViewProps.markdown.c_str()];
[self renderMarkdownContent:markdownString];
}
[super updateProps:props oldProps:oldProps];
}
- (void)didMoveToWindow
{
[super didMoveToWindow];
if (self.window && _renderedMarkdown != nil) {
_textView.hidden = NO;
ENRMRefreshTextViewAfterWindowAttach(_textView, self.bounds);
[_spoilerManager setNeedsUpdate];
[_spoilerManager updateIfNeeded];
CGSize measured = [self measureSize:self.bounds.size.width];
if (needsHeightUpdate(measured, self.bounds)) {
[self requestHeightUpdate];
}
}
}
Class<RCTComponentViewProtocol> EnrichedMarkdownTextCls(void)
{
return EnrichedMarkdownText.class;
}
- (facebook::react::SharedTouchEventEmitter)touchEventEmitterAtPoint:(CGPoint)point
{
if (_textView) {
CGPoint textViewPoint = [self convertPoint:point toView:_textView];
if (isPointOnInteractiveElement(_textView, textViewPoint)) {
return nil;
}
}
return [super touchEventEmitterAtPoint:point];
}
- (void)textTapped:(ENRMTapRecognizer *)recognizer
{
ENRMPlatformTextView *textView = (ENRMPlatformTextView *)recognizer.view;
if (handleTaskListTapWithSharedLogic(
textView, recognizer, &self->_cachedMarkdown, self->_config,
^(NSInteger index, BOOL checked, NSString *itemText) {
auto eventEmitter = std::static_pointer_cast<EnrichedMarkdownTextEventEmitter const>(self->_eventEmitter);
if (eventEmitter) {
eventEmitter->onTaskListItemPress({
.index = (int)index,
.checked = checked,
.text = std::string([itemText UTF8String] ?: ""),
});
}
},
^(NSString *updatedMarkdown) { [self renderMarkdownContent:updatedMarkdown]; })) {
return;
}
if (handleSpoilerTap(textView, recognizer, _spoilerManager)) {
return;
}
NSString *url = linkURLAtTapLocation(textView, recognizer);
if (url) {
auto eventEmitter = std::static_pointer_cast<EnrichedMarkdownTextEventEmitter const>(_eventEmitter);
if (eventEmitter) {
eventEmitter->onLinkPress({.url = std::string([url UTF8String])});
}
return;
}
ENRMClearSelection(textView);
}
#pragma mark - UITextViewDelegate (Link Interaction)
#if !TARGET_OS_OSX
- (BOOL)textView:(ENRMPlatformTextView *)textView
shouldInteractWithURL:(NSURL *)URL
inRange:(NSRange)characterRange
interaction:(UITextItemInteraction)interaction
{
if (interaction != UITextItemInteractionPresentActions) {
return YES;
}
NSString *urlString = linkURLAtRange(textView, characterRange);
if (!urlString || _enableLinkPreview) {
return YES;
}
auto eventEmitter = std::static_pointer_cast<EnrichedMarkdownTextEventEmitter const>(_eventEmitter);
if (eventEmitter) {
eventEmitter->onLinkLongPress({.url = std::string([urlString UTF8String])});
}
return NO;
}
#pragma mark - UITextViewDelegate (Edit Menu)
// TODO: Remove API_AVAILABLE(ios(16.0)) guard when the minimum iOS deployment target in RN is bumped to 16.
- (UIMenu *)textView:(ENRMPlatformTextView *)textView
editMenuForTextInRange:(NSRange)range
suggestedActions:(NSArray<UIMenuElement *> *)suggestedActions API_AVAILABLE(ios(16.0))
{
__weak EnrichedMarkdownText *weakSelf = self;
ENRMContextMenuPressHandler handler =
^(NSString *itemText, NSString *selectedText, NSUInteger selectionStart, NSUInteger selectionEnd) {
EnrichedMarkdownText *strongSelf = weakSelf;
if (!strongSelf)
return;
auto eventEmitter = std::static_pointer_cast<EnrichedMarkdownTextEventEmitter const>(strongSelf->_eventEmitter);
if (eventEmitter) {
eventEmitter->onContextMenuItemPress({
.itemText = std::string(itemText.UTF8String),
.selectedText = std::string(selectedText.UTF8String),
.selectionStart = (int)selectionStart,
.selectionEnd = (int)selectionEnd,
});
}
};
NSMutableArray<UIAction *> *customActions =
ENRMBuildContextMenuActions(_contextMenuItemTexts, _contextMenuItemIcons, textView, range, handler);
return buildEditMenuForSelection(textView.attributedText, range, _cachedMarkdown, _config, suggestedActions,
customActions);
}
#endif
#pragma mark - Accessibility (VoiceOver Navigation)
- (void)rebuildAccessibilityElementsIfNeeded
{
if (!_accessibilityNeedsRebuild) {
return;
}
_accessibilityNeedsRebuild = NO;
#if !TARGET_OS_OSX
_accessibilityElements = [MarkdownAccessibilityElementBuilder buildElementsForTextView:_textView
info:_accessibilityInfo
container:self];
#else
_accessibilityElements = [NSMutableArray array];
#endif
}
- (BOOL)isAccessibilityElement
{
return NO;
}
- (NSInteger)accessibilityElementCount
{
[self rebuildAccessibilityElementsIfNeeded];
return _accessibilityElements.count;
}
- (id)accessibilityElementAtIndex:(NSInteger)index
{
[self rebuildAccessibilityElementsIfNeeded];
if (index < 0 || index >= (NSInteger)_accessibilityElements.count) {
return nil;
}
return _accessibilityElements[index];
}
- (NSInteger)indexOfAccessibilityElement:(id)element
{
[self rebuildAccessibilityElementsIfNeeded];
return [_accessibilityElements indexOfObject:element];
}
- (NSArray *)accessibilityElements
{
[self rebuildAccessibilityElementsIfNeeded];
return _accessibilityElements;
}
#if !TARGET_OS_OSX
- (NSArray<UIAccessibilityCustomRotor *> *)accessibilityCustomRotors
{
[self rebuildAccessibilityElementsIfNeeded];
return [MarkdownAccessibilityElementBuilder buildRotorsFromElements:_accessibilityElements];
}
#endif
@end