-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathStyleBase.mm
More file actions
284 lines (247 loc) · 9.21 KB
/
Copy pathStyleBase.mm
File metadata and controls
284 lines (247 loc) · 9.21 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
#import "StyleBase.h"
#import "AttributeEntry.h"
#import "utils/OccurenceUtils.h"
#import "utils/RangeUtils.h"
#import "utils/TextListsUtils.h"
#import "utils/ZeroWidthSpaceUtils.h"
@implementation StyleBase
// This method gets overridden
+ (StyleType)getType {
return None;
}
// This method gets overridden for inline styles
- (NSString *)getKey {
if ([self isParagraph]) {
return NSParagraphStyleAttributeName;
}
return @"NoneAttribute";
}
// Basic inline styles will use this default value, paragraph styles will
// override it and parametrised ones completely don't use it
- (NSString *)getValue {
return @"AnyValue";
}
// Paragraph styles that store a family of mutually exclusive markers (e.g.
// alignment variants) should override this to return the shared prefix.
- (NSString *)getMarkerPrefix {
return nil;
}
// This method gets overridden
- (BOOL)isParagraph {
return false;
}
- (BOOL)needsZWS {
return NO;
}
- (BOOL)appliesStylingToTyping {
return NO;
}
// determines the order in which the styles are applied
- (NSInteger)stylePriority {
return [self isParagraph] ? 0 : 2;
}
- (instancetype)initWithHost:(id<EnrichedViewHost>)host {
self = [super init];
_host = host;
return self;
}
// aligns range to whole paragraph for the paragraph stlyes
- (NSRange)actualUsedRange:(NSRange)range {
if (![self isParagraph])
return range;
return [self.host.textView.textStorage.string paragraphRangeForRange:range];
}
- (void)toggle:(NSRange)range {
NSRange actualRange = [self actualUsedRange:range];
BOOL isPresent = [self detect:actualRange];
if (actualRange.length >= 1) {
isPresent ? [self remove:actualRange withDirtyRange:YES]
: [self add:actualRange withTyping:YES withDirtyRange:YES];
} else {
isPresent ? [self removeTyping] : [self addTypingWithValue:[self getValue]];
}
}
- (void)add:(NSRange)range
withTyping:(BOOL)withTyping
withDirtyRange:(BOOL)withDirtyRange {
[self add:range
withValue:[self getValue]
withTyping:withTyping
withDirtyRange:withDirtyRange];
}
- (void)add:(NSRange)range
withValue:(NSString *)value
withTyping:(BOOL)withTyping
withDirtyRange:(BOOL)withDirtyRange {
NSRange actualRange = [self actualUsedRange:range];
if (![self isParagraph]) {
[self.host.textView.textStorage addAttribute:[self getKey]
value:value
range:actualRange];
} else {
[self.host.textView.textStorage
enumerateAttribute:NSParagraphStyleAttributeName
inRange:actualRange
options:0
usingBlock:^(id _Nullable existingValue, NSRange subRange,
BOOL *_Nonnull stop) {
NSMutableParagraphStyle *pStyle =
[(NSParagraphStyle *)existingValue mutableCopy];
if (pStyle == nullptr)
return;
pStyle.textLists =
[TextListsUtils textListsByAdding:value
withExclusivePrefix:[self getMarkerPrefix]
toArray:pStyle.textLists];
[self.host.textView.textStorage
addAttribute:NSParagraphStyleAttributeName
value:pStyle
range:subRange];
}];
}
if (withTyping) {
[self addTypingWithValue:value];
}
// Notify attributes manager of styling to be re-done if needed.
if (withDirtyRange) {
[self.host.attributesManager addDirtyRange:actualRange];
}
}
- (void)remove:(NSRange)range withDirtyRange:(BOOL)withDirtyRange {
NSRange actualRange = [self actualUsedRange:range];
if (![self isParagraph]) {
[self.host.textView.textStorage removeAttribute:[self getKey]
range:actualRange];
} else {
[self.host.textView.textStorage
enumerateAttribute:NSParagraphStyleAttributeName
inRange:actualRange
options:0
usingBlock:^(id _Nullable existingValue, NSRange subRange,
BOOL *_Nonnull stop) {
NSMutableParagraphStyle *pStyle =
[(NSParagraphStyle *)existingValue mutableCopy];
if (pStyle == nullptr)
return;
pStyle.textLists =
[TextListsUtils textListsByRemoving:[self getValue]
withPrefix:[self getMarkerPrefix]
fromArray:pStyle.textLists];
[self.host.textView.textStorage
addAttribute:NSParagraphStyleAttributeName
value:pStyle
range:subRange];
}];
}
[self removeTyping];
// Notify attributes manager of styling to be re-done if needed.
if (withDirtyRange) {
[self.host.attributesManager addDirtyRange:actualRange];
}
}
- (void)addTypingWithValue:(NSString *)value {
NSMutableDictionary *newTypingAttrs =
[self.host.textView.typingAttributes mutableCopy];
if (![self isParagraph]) {
newTypingAttrs[[self getKey]] = value;
} else {
NSMutableParagraphStyle *pStyle =
[newTypingAttrs[NSParagraphStyleAttributeName] mutableCopy];
pStyle.textLists = [TextListsUtils textListsByAdding:value
withExclusivePrefix:[self getMarkerPrefix]
toArray:pStyle.textLists];
newTypingAttrs[NSParagraphStyleAttributeName] = pStyle;
}
self.host.textView.typingAttributes = newTypingAttrs;
}
- (void)removeTyping {
NSMutableDictionary *newTypingAttrs =
[self.host.textView.typingAttributes mutableCopy];
if (![self isParagraph]) {
[newTypingAttrs removeObjectForKey:[self getKey]];
// attributes manager also needs to be notified of custom attributes that
// shouldn't be extended
[self.host.attributesManager didRemoveTypingAttribute:[self getKey]];
} else {
NSMutableParagraphStyle *pStyle =
[newTypingAttrs[NSParagraphStyleAttributeName] mutableCopy];
pStyle.textLists = pStyle.textLists =
[TextListsUtils textListsByRemoving:[self getValue]
withPrefix:[self getMarkerPrefix]
fromArray:pStyle.textLists];
newTypingAttrs[NSParagraphStyleAttributeName] = pStyle;
}
self.host.textView.typingAttributes = newTypingAttrs;
}
// custom styles (e.g. ImageStyle, MentionStyle) will likely need to override
// this method
- (BOOL)styleCondition:(id)value range:(NSRange)range {
if (![self isParagraph]) {
NSString *valueString = (NSString *)value;
return valueString != nullptr &&
[valueString isEqualToString:[self getValue]];
} else {
NSParagraphStyle *pStyle = (NSParagraphStyle *)value;
return pStyle != nullptr && [TextListsUtils textLists:pStyle.textLists
containsValue:[self getValue]];
}
}
- (BOOL)detect:(NSRange)range {
if (range.length >= 1) {
return [OccurenceUtils detect:[self getKey]
withHost:self.host
inRange:range
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value range:range];
}];
} else {
return [OccurenceUtils detect:[self getKey]
withHost:self.host
atIndex:range.location
checkPrevious:[self isParagraph]
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value range:range];
}];
}
}
- (BOOL)any:(NSRange)range {
return [OccurenceUtils any:[self getKey]
withHost:self.host
inRange:range
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value range:range];
}];
}
- (NSArray<StylePair *> *)all:(NSRange)range {
return [OccurenceUtils all:[self getKey]
withHost:self.host
inRange:range
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value range:range];
}];
}
// This method gets overridden
- (void)applyStyling:(NSRange)range {
}
// This method gets overridden when the style needs to apply certain typing
// attributes
- (void)applyStylingToTypingAttrs:(NSMutableDictionary *)attributes {
}
// Called during dirty range re-application to restore a style from a saved
// StylePair
- (void)reapplyFromStylePair:(StylePair *)pair {
NSRange range = [pair.rangeValue rangeValue];
[self add:range withTyping:NO withDirtyRange:NO];
}
// Gets a custom attribtue entry for the typingAttributes.
// Only used with inline styles.
- (AttributeEntry *)getEntryIfPresent:(NSRange)range {
if (![self detect:range]) {
return nullptr;
}
AttributeEntry *entry = [[AttributeEntry alloc] init];
entry.key = [self getKey];
entry.value = [self getValue];
return entry;
}
@end