-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathStyleUtils.mm
More file actions
299 lines (275 loc) · 11.7 KB
/
Copy pathStyleUtils.mm
File metadata and controls
299 lines (275 loc) · 11.7 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#import "StyleUtils.h"
@implementation StyleUtils
+ (NSDictionary *)conflictMap {
return @{
@([BoldStyle getType]) : @[],
@([ItalicStyle getType]) : @[],
@([UnderlineStyle getType]) : @[],
@([StrikethroughStyle getType]) : @[],
@([InlineCodeStyle getType]) :
@[ @([LinkStyle getType]), @([MentionStyle getType]) ],
@([LinkStyle getType]) :
@[ @([LinkStyle getType]), @([MentionStyle getType]) ],
@([MentionStyle getType]) : @[ @([LinkStyle getType]) ],
@([H1Style getType]) : @[
@([H2Style getType]), @([H3Style getType]), @([H4Style getType]),
@([H5Style getType]), @([H6Style getType]),
@([UnorderedListStyle getType]), @([OrderedListStyle getType]),
@([BlockQuoteStyle getType]), @([CodeBlockStyle getType]),
@([CheckboxListStyle getType])
],
@([H2Style getType]) : @[
@([H1Style getType]), @([H3Style getType]), @([H4Style getType]),
@([H5Style getType]), @([H6Style getType]),
@([UnorderedListStyle getType]), @([OrderedListStyle getType]),
@([BlockQuoteStyle getType]), @([CodeBlockStyle getType]),
@([CheckboxListStyle getType])
],
@([H3Style getType]) : @[
@([H1Style getType]), @([H2Style getType]), @([H4Style getType]),
@([H5Style getType]), @([H6Style getType]),
@([UnorderedListStyle getType]), @([OrderedListStyle getType]),
@([BlockQuoteStyle getType]), @([CodeBlockStyle getType]),
@([CheckboxListStyle getType])
],
@([H4Style getType]) : @[
@([H1Style getType]), @([H2Style getType]), @([H3Style getType]),
@([H5Style getType]), @([H6Style getType]),
@([UnorderedListStyle getType]), @([OrderedListStyle getType]),
@([BlockQuoteStyle getType]), @([CodeBlockStyle getType]),
@([CheckboxListStyle getType])
],
@([H5Style getType]) : @[
@([H1Style getType]), @([H2Style getType]), @([H3Style getType]),
@([H4Style getType]), @([H6Style getType]),
@([UnorderedListStyle getType]), @([OrderedListStyle getType]),
@([BlockQuoteStyle getType]), @([CodeBlockStyle getType]),
@([CheckboxListStyle getType])
],
@([H6Style getType]) : @[
@([H1Style getType]), @([H2Style getType]), @([H3Style getType]),
@([H4Style getType]), @([H5Style getType]),
@([UnorderedListStyle getType]), @([OrderedListStyle getType]),
@([BlockQuoteStyle getType]), @([CodeBlockStyle getType]),
@([CheckboxListStyle getType])
],
@([UnorderedListStyle getType]) : @[
@([H1Style getType]), @([H2Style getType]), @([H3Style getType]),
@([H4Style getType]), @([H5Style getType]), @([H6Style getType]),
@([OrderedListStyle getType]), @([BlockQuoteStyle getType]),
@([CodeBlockStyle getType]), @([CheckboxListStyle getType])
],
@([OrderedListStyle getType]) : @[
@([H1Style getType]), @([H2Style getType]), @([H3Style getType]),
@([H4Style getType]), @([H5Style getType]), @([H6Style getType]),
@([UnorderedListStyle getType]), @([BlockQuoteStyle getType]),
@([CodeBlockStyle getType]), @([CheckboxListStyle getType])
],
@([CheckboxListStyle getType]) : @[
@([H1Style getType]), @([H2Style getType]), @([H3Style getType]),
@([H4Style getType]), @([H5Style getType]), @([H6Style getType]),
@([UnorderedListStyle getType]), @([OrderedListStyle getType]),
@([BlockQuoteStyle getType]), @([CodeBlockStyle getType])
],
@([AlignmentStyle getType]) : @[],
@([BlockQuoteStyle getType]) : @[
@([H1Style getType]), @([H2Style getType]), @([H3Style getType]),
@([H4Style getType]), @([H5Style getType]), @([H6Style getType]),
@([UnorderedListStyle getType]), @([OrderedListStyle getType]),
@([CodeBlockStyle getType]), @([CheckboxListStyle getType])
],
@([CodeBlockStyle getType]) : @[
@([H1Style getType]), @([H2Style getType]), @([H3Style getType]),
@([H4Style getType]), @([H5Style getType]), @([H6Style getType]),
@([BoldStyle getType]), @([UnderlineStyle getType]),
@([ItalicStyle getType]), @([StrikethroughStyle getType]),
@([UnorderedListStyle getType]), @([OrderedListStyle getType]),
@([BlockQuoteStyle getType]), @([InlineCodeStyle getType]),
@([MentionStyle getType]), @([LinkStyle getType]),
@([CheckboxListStyle getType])
],
@([ImageStyle getType]) :
@[ @([LinkStyle getType]), @([MentionStyle getType]) ]
};
}
+ (NSDictionary *)blockingMap {
return @{
@([BoldStyle getType]) : @[ @([CodeBlockStyle getType]) ],
@([ItalicStyle getType]) : @[ @([CodeBlockStyle getType]) ],
@([UnderlineStyle getType]) : @[ @([CodeBlockStyle getType]) ],
@([StrikethroughStyle getType]) : @[ @([CodeBlockStyle getType]) ],
@([InlineCodeStyle getType]) :
@[ @([CodeBlockStyle getType]), @([ImageStyle getType]) ],
@([LinkStyle getType]) : @[
@([CodeBlockStyle getType]), @([ImageStyle getType]),
@([InlineCodeStyle getType])
],
@([MentionStyle getType]) : @[
@([CodeBlockStyle getType]), @([ImageStyle getType]),
@([InlineCodeStyle getType])
],
@([H1Style getType]) : @[],
@([H2Style getType]) : @[],
@([H3Style getType]) : @[],
@([H4Style getType]) : @[],
@([H5Style getType]) : @[],
@([H6Style getType]) : @[],
@([UnorderedListStyle getType]) : @[],
@([OrderedListStyle getType]) : @[],
@([CheckboxListStyle getType]) : @[],
@([AlignmentStyle getType]) : @[],
@([BlockQuoteStyle getType]) : @[],
@([CodeBlockStyle getType]) : @[],
@([ImageStyle getType]) : @[ @([InlineCodeStyle getType]) ]
};
}
+ (NSDictionary *)stylesDictForHost:(id<EnrichedViewHost>)host
isInput:(BOOL)isInput {
NSArray<Class> *baseClasses = @[
[BoldStyle class], [ItalicStyle class],
[UnderlineStyle class], [StrikethroughStyle class],
[InlineCodeStyle class], [LinkStyle class],
[MentionStyle class], [H1Style class],
[H2Style class], [H3Style class],
[H4Style class], [H5Style class],
[H6Style class], [UnorderedListStyle class],
[OrderedListStyle class], [CheckboxListStyle class],
[AlignmentStyle class], [BlockQuoteStyle class],
[CodeBlockStyle class], [ImageStyle class]
];
NSArray<Class> *viewerClasses = @[
[EnrichedTextBoldStyle class],
[EnrichedTextItalicStyle class],
[EnrichedTextUnderlineStyle class],
[EnrichedTextStrikethroughStyle class],
[EnrichedTextInlineCodeStyle class],
[EnrichedTextLinkStyle class],
[EnrichedTextMentionStyle class],
[EnrichedTextH1Style class],
[EnrichedTextH2Style class],
[EnrichedTextH3Style class],
[EnrichedTextH4Style class],
[EnrichedTextH5Style class],
[EnrichedTextH6Style class],
[EnrichedTextUnorderedListStyle class],
[EnrichedTextOrderedListStyle class],
[EnrichedTextCheckboxListStyle class],
[EnrichedTextAlignmentStyle class],
[EnrichedTextBlockQuoteStyle class],
[EnrichedTextCodeBlockStyle class],
[EnrichedTextImageStyle class]
];
NSMutableDictionary *dict = [NSMutableDictionary new];
for (NSUInteger i = 0; i < baseClasses.count; i++) {
// Choose the class based on the context
Class targetClass = isInput ? baseClasses[i] : viewerClasses[i];
// Instantiate and add to dictionary
// We use [baseClasses[i] getType] for the key to ensure the
// conflict maps (which use base types) always match.
StyleBase *instance = [[targetClass alloc] initWithHost:host];
dict[@([baseClasses[i] getType])] = instance;
}
return [dict copy];
}
// returns false when style shouldn't be applied and true when it can be
+ (BOOL)isStyleBlocked:(StyleType)type
range:(NSRange)range
forHost:(id<EnrichedViewHost>)host {
NSArray<NSNumber *> *blocking =
[self getPresentStyleTypesFrom:host.blockingStyles[@(type)]
range:range
forHost:host];
return blocking.count != 0;
}
// returns false when style shouldn't be applied and true when it can be
+ (BOOL)handleStyleBlocksAndConflicts:(StyleType)type
range:(NSRange)range
forHost:(id<EnrichedViewHost>)host {
if ([self isStyleBlocked:type range:range forHost:host]) {
return NO;
}
// handle conflicting styles: remove styles within the range
NSArray<NSNumber *> *conflicting =
[self getPresentStyleTypesFrom:host.conflictingStyles[@(type)]
range:range
forHost:host];
if (conflicting.count != 0) {
for (NSNumber *type in conflicting) {
StyleBase *style = host.stylesDict[type];
if ([style isParagraph]) {
// for paragraph styles we can just call remove since it will pick up
// proper paragraph range
[style remove:range withDirtyRange:YES];
} else {
// for inline styles we have to differentiate betweeen normal and typing
// attributes removal
range.length >= 1 ? [style remove:range withDirtyRange:YES]
: [style removeTyping];
}
}
}
return YES;
}
+ (NSArray *)getPresentStyleTypesFrom:(NSArray *)types
range:(NSRange)range
forHost:(id<EnrichedViewHost>)host {
NSMutableArray<NSNumber *> *resultArray =
[[NSMutableArray<NSNumber *> alloc] init];
// There might be a case where the given `range` doesn't have a conflicting
// style, but it is present in typing attributes. In that case, run `detect:`
// for good measure, since it considers typing attributes (unlike `any:`).
NSRange selectedRange = host.textView.selectedRange;
BOOL caretWithinRange = range.length >= 1 && selectedRange.length == 0 &&
selectedRange.location >= range.location &&
selectedRange.location <= NSMaxRange(range);
for (NSNumber *type in types) {
StyleBase *style = host.stylesDict[type];
BOOL present = range.length >= 1 ? [style any:range] : [style detect:range];
if (!present && caretWithinRange) {
present = [style detect:selectedRange];
}
if (present) {
[resultArray addObject:type];
}
}
return resultArray;
}
+ (void)addStyleBlock:(StyleType)blocking
to:(StyleType)blocked
forHost:(id<EnrichedViewHost>)host {
NSMutableArray *blocksArr = [host.blockingStyles[@(blocked)] mutableCopy];
if (![blocksArr containsObject:@(blocking)]) {
[blocksArr addObject:@(blocking)];
host.blockingStyles[@(blocked)] = blocksArr;
}
}
+ (void)removeStyleBlock:(StyleType)blocking
from:(StyleType)blocked
forHost:(id<EnrichedViewHost>)host {
NSMutableArray *blocksArr = [host.blockingStyles[@(blocked)] mutableCopy];
if ([blocksArr containsObject:@(blocking)]) {
[blocksArr removeObject:@(blocking)];
host.blockingStyles[@(blocked)] = blocksArr;
}
}
+ (void)addStyleConflict:(StyleType)conflicting
to:(StyleType)conflicted
forHost:(id<EnrichedViewHost>)host {
NSMutableArray *conflictsArr =
[host.conflictingStyles[@(conflicted)] mutableCopy];
if (![conflictsArr containsObject:@(conflicting)]) {
[conflictsArr addObject:@(conflicting)];
host.conflictingStyles[@(conflicted)] = conflictsArr;
}
}
+ (void)removeStyleConflict:(StyleType)conflicting
from:(StyleType)conflicted
forHost:(id<EnrichedViewHost>)host {
NSMutableArray *conflictsArr =
[host.conflictingStyles[@(conflicted)] mutableCopy];
if ([conflictsArr containsObject:@(conflicting)]) {
[conflictsArr removeObject:@(conflicting)];
host.conflictingStyles[@(conflicted)] = conflictsArr;
}
}
@end