-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathUnderlineStyle.mm
More file actions
115 lines (100 loc) · 3.96 KB
/
Copy pathUnderlineStyle.mm
File metadata and controls
115 lines (100 loc) · 3.96 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
#import "StyleHeaders.h"
#import "EnrichedTextInputView.h"
#import "OccurenceUtils.h"
@implementation UnderlineStyle {
EnrichedTextInputView *_input;
}
+ (StyleType)getStyleType { return Underline; }
- (instancetype)initWithInput:(id)input {
self = [super init];
_input = (EnrichedTextInputView *)input;
return self;
}
- (void)applyStyle:(NSRange)range {
BOOL isStylePresent = [self detectStyle: range];
if(range.length >= 1) {
isStylePresent ? [self removeAttributes:range] : [self addAttributes:range];
} else {
isStylePresent ? [self removeTypingAttributes] : [self addTypingAttributes];
}
}
- (void)addAttributes:(NSRange)range {
[_input->textView.textStorage addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
}
- (void)addTypingAttributes {
NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
newTypingAttrs[NSUnderlineStyleAttributeName] = @(NSUnderlineStyleSingle);
_input->textView.typingAttributes = newTypingAttrs;
}
- (void)removeAttributes:(NSRange)range {
[_input->textView.textStorage removeAttribute:NSUnderlineStyleAttributeName range:range];
}
- (void)removeTypingAttributes {
NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
[newTypingAttrs removeObjectForKey: NSUnderlineStyleAttributeName];
_input->textView.typingAttributes = newTypingAttrs;
}
- (BOOL)underlinedLinkConflictsInRange:(NSRange)range {
BOOL conflicted = NO;
if([_input->config linkDecorationLine] == DecorationUnderline) {
LinkStyle *linkStyle = _input->stylesDict[@([LinkStyle getStyleType])];
conflicted = range.length > 0
? [linkStyle anyOccurence:range]
: [linkStyle detectStyle:range];
}
return conflicted;
}
- (BOOL)underlinedMentionConflictsInRange:(NSRange)range {
BOOL conflicted = NO;
MentionStyle *mentionStyle = _input->stylesDict[@([MentionStyle getStyleType])];
if(range.length == 0) {
if([mentionStyle detectStyle:range]) {
MentionParams *params = [mentionStyle getMentionParamsAt:range.location];
conflicted = [_input->config mentionStylePropsForIndicator:params.indicator].decorationLine == DecorationUnderline;
}
} else {
NSArray *occurences = [mentionStyle findAllOccurences:range];
for(StylePair *pair in occurences) {
MentionParams *params = [mentionStyle getMentionParamsAt:[pair.rangeValue rangeValue].location];
if([_input->config mentionStylePropsForIndicator:params.indicator].decorationLine == DecorationUnderline) {
conflicted = YES;
break;
}
}
}
return conflicted;
}
- (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
NSNumber *underlineStyle = (NSNumber *)value;
return underlineStyle != nullptr && [underlineStyle intValue] != NSUnderlineStyleNone && ![self underlinedLinkConflictsInRange:range] && ![self underlinedMentionConflictsInRange:range];
}
- (BOOL)detectStyle:(NSRange)range {
if(range.length >= 1) {
return [OccurenceUtils detect:NSUnderlineStyleAttributeName withInput:_input inRange:range
withCondition: ^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
} else {
return [OccurenceUtils detect:NSUnderlineStyleAttributeName withInput:_input atIndex:range.location checkPrevious:NO
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
}
}
- (BOOL)anyOccurence:(NSRange)range {
return [OccurenceUtils any:NSUnderlineStyleAttributeName withInput:_input inRange:range
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
}
- (NSArray<StylePair *> *_Nullable)findAllOccurences:(NSRange)range {
return [OccurenceUtils all:NSUnderlineStyleAttributeName withInput:_input inRange:range
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
}
@end