-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathEnrichedMarkdownInternalText.m
More file actions
161 lines (132 loc) · 3.99 KB
/
EnrichedMarkdownInternalText.m
File metadata and controls
161 lines (132 loc) · 3.99 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
#import "EnrichedMarkdownInternalText.h"
#import "ENRMContextMenuTextView+macOS.h"
#import "ENRMSpoilerOverlayManager.h"
#import "ENRMTextViewSetup.h"
#import "MarkdownAccessibilityElementBuilder.h"
#import "RuntimeKeys.h"
#include <TargetConditionals.h>
@implementation EnrichedMarkdownInternalText {
ENRMPlatformTextView *_textView;
#if !TARGET_OS_OSX
NSMutableArray<UIAccessibilityElement *> *_accessibilityElements;
#else
NSMutableArray *_accessibilityElements;
#endif
BOOL _accessibilityNeedsRebuild;
ENRMSpoilerOverlayManager *_spoilerManager;
}
@synthesize textView = _textView;
@synthesize spoilerManager = _spoilerManager;
- (instancetype)initWithConfig:(StyleConfig *)config
{
if (self = [super init]) {
_config = config;
_allowTrailingMargin = NO;
_lastElementMarginBottom = 0;
[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);
[self addSubview:_textView];
[self setupLayoutManager];
_spoilerManager = [[ENRMSpoilerOverlayManager alloc] initWithTextView:_textView config:_config];
_spoilerManager.spoilerOverlay = _spoilerOverlay;
}
- (void)setupLayoutManager
{
ENRMAttachLayoutManager(_textView, _config);
}
- (void)setSpoilerOverlay:(ENRMSpoilerOverlay)spoilerOverlay
{
_spoilerOverlay = spoilerOverlay;
_spoilerManager.spoilerOverlay = spoilerOverlay;
}
- (void)applyAttributedText:(NSMutableAttributedString *)text context:(RenderContext *)context
{
NSLayoutManager *layoutManager = _textView.layoutManager;
if ([layoutManager isKindOfClass:[TextViewLayoutManager class]]) {
[layoutManager setValue:_config forKey:@"config"];
}
objc_setAssociatedObject(_textView.textContainer, kTextViewKey, _textView, OBJC_ASSOCIATION_ASSIGN);
_accessibilityElements = nil;
_accessibilityNeedsRebuild = YES;
ENRMSetAttributedText(_textView, text);
[_textView.layoutManager invalidateLayoutForCharacterRange:NSMakeRange(0, text.length) actualCharacterRange:NULL];
[_spoilerManager setNeedsUpdate];
[self setNeedsLayout];
#if !TARGET_OS_OSX
[_textView setNeedsLayout];
#endif
ENRMSetNeedsDisplay(_textView);
}
- (CGFloat)measureHeight:(CGFloat)maxWidth
{
return [self measureSize:maxWidth].height;
}
- (CGSize)measureSize:(CGFloat)maxWidth
{
return ENRMMeasureMarkdownText(_textView, maxWidth, _config, _allowTrailingMargin, _lastElementMarginBottom);
}
- (void)layoutSubviews
{
[super layoutSubviews];
_textView.frame = self.bounds;
[_spoilerManager updateIfNeeded];
}
#pragma mark - Accessibility
- (void)rebuildAccessibilityElementsIfNeeded
{
if (!_accessibilityNeedsRebuild) {
return;
}
_accessibilityNeedsRebuild = NO;
#if !TARGET_OS_OSX
_accessibilityElements = [MarkdownAccessibilityElementBuilder buildElementsForTextView:_textView
info:_accessibilityInfo
container:self];
#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
- (void)setContextMenuProvider:(ENRMContextMenuProvider)provider
{
((ENRMContextMenuTextView *)_textView).contextMenuProvider = provider;
}
#endif
@end