-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathEnrichedTextTouchHandler.mm
More file actions
168 lines (147 loc) · 4.94 KB
/
Copy pathEnrichedTextTouchHandler.mm
File metadata and controls
168 lines (147 loc) · 4.94 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
#import "EnrichedTextTouchHandler.h"
#import "ColorExtension.h"
#import "EnrichedTextView.h"
#import "ImageAttachment.h"
#import "LinkData.h"
#import "MentionParams.h"
#import "MentionStyleProps.h"
#import "StyleBase.h"
@implementation EnrichedTextTouchHandler {
NSRange _activeRange;
NSString *_activeAttrKey;
id _activeValue;
}
- (instancetype)initWithView:(EnrichedTextView *)view {
if (self = [super init]) {
_view = view;
}
return self;
}
- (void)handleTouchBeganAtPoint:(CGPoint)point {
NSUInteger charIndex = [self characterIndexAtPoint:point];
NSRange range;
NSString *key;
id value = [self findClickableAt:charIndex range:&range key:&key];
if (value) {
_activeRange = range;
_activeAttrKey = key;
_activeValue = value;
[self updateVisualsPressed:YES];
}
}
- (void)handleTouchEndedAtPoint:(CGPoint)point {
if (!_activeValue) {
return;
}
NSUInteger charIndex = [self characterIndexAtPoint:point];
if (NSLocationInRange(charIndex, _activeRange)) {
[self dispatchEvent];
}
[self updateVisualsPressed:NO];
[self reset];
}
- (void)handleTouchCancelled {
[self updateVisualsPressed:NO];
[self reset];
}
- (NSUInteger)characterIndexAtPoint:(CGPoint)point {
UITextView *tv = self.view.textView;
NSLayoutManager *lm = tv.layoutManager;
NSTextContainer *tc = tv.textContainer;
CGPoint textOffset =
CGPointMake(tv.textContainerInset.left, tv.textContainerInset.top);
CGPoint locationInContainer =
CGPointMake(point.x - textOffset.x, point.y - textOffset.y);
NSUInteger glyphIndex = [lm glyphIndexForPoint:locationInContainer
inTextContainer:tc];
CGRect glyphRect = [lm boundingRectForGlyphRange:NSMakeRange(glyphIndex, 1)
inTextContainer:tc];
if (!CGRectContainsPoint(glyphRect, locationInContainer)) {
return NSNotFound;
}
return [lm characterIndexForGlyphAtIndex:glyphIndex];
}
- (id)findClickableAt:(NSUInteger)idx
range:(NSRangePointer)range
key:(NSString **)key {
if (idx == NSNotFound || idx >= self.view.textView.textStorage.length)
return nil;
NSArray *keys = @[
@"EnrichedManualLink", @"EnrichedAutomaticLink", @"EnrichedMention",
@"EnrichedImage"
];
for (NSString *k in keys) {
id val = [self.view.textView.textStorage attribute:k
atIndex:idx
effectiveRange:range];
if (val) {
*key = k;
return val;
}
}
return nil;
}
- (void)updateVisualsPressed:(BOOL)pressed {
if ([_activeAttrKey isEqualToString:@"EnrichedImage"]) {
return;
}
if (pressed) {
UIColor *color = nil;
UIColor *bgColor = nil;
if ([_activeAttrKey isEqualToString:@"EnrichedMention"]) {
MentionParams *m = (MentionParams *)_activeValue;
MentionStyleProps *mProps =
[self.view.config mentionStylePropsForIndicator:m.indicator];
color = [mProps pressColor];
bgColor = [mProps pressBackgroundColor];
} else {
color = [self.view.config linkPressColor];
}
NSMutableDictionary *newAttrs = [[NSMutableDictionary alloc] init];
if (color) {
newAttrs[NSForegroundColorAttributeName] = color;
newAttrs[NSUnderlineColorAttributeName] = color;
newAttrs[NSStrikethroughColorAttributeName] = color;
}
if (bgColor) {
newAttrs[NSBackgroundColorAttributeName] =
[bgColor colorWithResolvedAlpha];
}
[self.view.textView.textStorage addAttributes:newAttrs range:_activeRange];
} else {
// REVERT using the Style Engine in the View
for (StyleBase *style in self.view.stylesDict.allValues) {
NSString *styleKey = [style getKey];
// If the style key matches exactly (Mentions)
// OR if both the style and the active key are Link-related
BOOL isMatch = [styleKey isEqualToString:_activeAttrKey];
BOOL isLinkMatch = ([_activeAttrKey containsString:@"Link"] &&
[styleKey containsString:@"Link"]);
if (isMatch || isLinkMatch) {
[style applyStyling:_activeRange];
}
}
}
}
- (void)dispatchEvent {
if ([_activeAttrKey containsString:@"Link"]) {
[self.view emitOnLinkPressEvent:((LinkData *)_activeValue).url];
} else if ([_activeAttrKey isEqualToString:@"EnrichedMention"]) {
[self.view emitOnMentionPressEvent:(MentionParams *)_activeValue];
} else if ([_activeAttrKey isEqualToString:@"EnrichedImage"]) {
NSRange attachmentRange = _activeRange;
ImageAttachment *attachment =
[self.view.textView.textStorage attribute:NSAttachmentAttributeName
atIndex:attachmentRange.location
effectiveRange:NULL];
if ([attachment isKindOfClass:[ImageAttachment class]]) {
[self.view emitOnImagePressEvent:attachment];
}
}
}
- (void)reset {
_activeValue = nil;
_activeAttrKey = nil;
_activeRange = NSMakeRange(0, 0);
}
@end