-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathInlineCodeStyle.mm
More file actions
167 lines (146 loc) · 7.26 KB
/
Copy pathInlineCodeStyle.mm
File metadata and controls
167 lines (146 loc) · 7.26 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
#import "StyleHeaders.h"
#import "EnrichedTextInputView.h"
#import "FontExtension.h"
#import "OccurenceUtils.h"
#import "ParagraphsUtils.h"
#import "ColorExtension.h"
@implementation InlineCodeStyle {
EnrichedTextInputView *_input;
}
+ (StyleType)getStyleType { return InlineCode; }
- (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 {
// we don't want to apply inline code to newline characters, it looks bad
NSArray *nonNewlineRanges = [ParagraphsUtils getNonNewlineRangesIn:_input->textView range:range];
for(NSValue *value in nonNewlineRanges) {
NSRange currentRange = [value rangeValue];
[_input->textView.textStorage beginEditing];
[_input->textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[[_input->config inlineCodeBgColor] colorWithAlphaIfNotTransparent:0.4] range:currentRange];
[_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange];
[_input->textView.textStorage addAttribute:NSUnderlineColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange];
[_input->textView.textStorage addAttribute:NSStrikethroughColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange];
[_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:currentRange options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
UIFont *font = (UIFont *)value;
if(font != nullptr) {
UIFont *newFont = [[[_input->config monospacedFont] withFontTraits:font] setSize:font.pointSize];
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
}
}
];
[_input->textView.textStorage endEditing];
}
}
- (void)addTypingAttributes {
NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
newTypingAttrs[NSBackgroundColorAttributeName] = [[_input->config inlineCodeBgColor] colorWithAlphaIfNotTransparent:0.4];
newTypingAttrs[NSForegroundColorAttributeName] = [_input->config inlineCodeFgColor];
newTypingAttrs[NSUnderlineColorAttributeName] = [_input->config inlineCodeFgColor];
newTypingAttrs[NSStrikethroughColorAttributeName] = [_input->config inlineCodeFgColor];
UIFont* currentFont = (UIFont *)newTypingAttrs[NSFontAttributeName];
if(currentFont != nullptr) {
newTypingAttrs[NSFontAttributeName] = [[[_input->config monospacedFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
}
_input->textView.typingAttributes = newTypingAttrs;
}
- (void)removeAttributes:(NSRange)range {
[_input->textView.textStorage beginEditing];
[_input->textView.textStorage removeAttribute:NSBackgroundColorAttributeName range:range];
[_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:[_input->config primaryColor] range:range];
[_input->textView.textStorage addAttribute:NSUnderlineColorAttributeName value:[_input->config primaryColor] range:range];
[_input->textView.textStorage addAttribute:NSStrikethroughColorAttributeName value:[_input->config primaryColor] range:range];
[_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:range options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
UIFont *font = (UIFont *)value;
if(font != nullptr) {
UIFont *newFont = [[[_input->config primaryFont] withFontTraits:font] setSize:font.pointSize];
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
}
}
];
[_input->textView.textStorage endEditing];
}
- (void)removeTypingAttributes {
NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
[newTypingAttrs removeObjectForKey:NSBackgroundColorAttributeName];
newTypingAttrs[NSForegroundColorAttributeName] = [_input->config primaryColor];
newTypingAttrs[NSUnderlineColorAttributeName] = [_input->config primaryColor];
newTypingAttrs[NSStrikethroughColorAttributeName] = [_input->config primaryColor];
UIFont* currentFont = (UIFont *)newTypingAttrs[NSFontAttributeName];
if(currentFont != nullptr) {
newTypingAttrs[NSFontAttributeName] = [[[_input->config primaryFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
}
_input->textView.typingAttributes = newTypingAttrs;
}
// making sure no newlines get inline code style, it looks bad
- (void)handleNewlines {
for(int i = 0; i < _input->textView.textStorage.string.length; i++) {
if([[NSCharacterSet newlineCharacterSet] characterIsMember:[_input->textView.textStorage.string characterAtIndex:i]]) {
NSRange mockRange = NSMakeRange(0, 0);
// can't use detect style because it intentionally doesn't take newlines into consideration
UIColor *bgColor = [_input->textView.textStorage attribute:NSBackgroundColorAttributeName atIndex:i effectiveRange:&mockRange];
if([self styleCondition:bgColor :NSMakeRange(i, 1)]) {
[self removeAttributes:NSMakeRange(i, 1)];
}
}
}
}
// emojis don't retain monospace font attribute so we check for the background color if there is no mention
- (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
UIColor *bgColor = (UIColor *)value;
MentionStyle *mStyle = _input->stylesDict[@([MentionStyle getStyleType])];
return bgColor != nullptr && mStyle != nullptr && ![mStyle detectStyle:range];
}
- (BOOL)detectStyle:(NSRange)range {
if(range.length >= 1) {
// detect only in non-newline characters
NSArray *nonNewlineRanges = [ParagraphsUtils getNonNewlineRangesIn:_input->textView range:range];
if(nonNewlineRanges.count == 0) {
return NO;
}
BOOL detected = YES;
for(NSValue *value in nonNewlineRanges) {
NSRange currentRange = [value rangeValue];
BOOL currentDetected = [OccurenceUtils detect:NSBackgroundColorAttributeName withInput:_input inRange:currentRange
withCondition: ^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
detected = detected && currentDetected;
}
return detected;
} else {
return [OccurenceUtils detect:NSBackgroundColorAttributeName 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:NSBackgroundColorAttributeName 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:NSBackgroundColorAttributeName withInput:_input inRange:range
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
}
@end