-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathParagraphAttributesUtils.mm
More file actions
296 lines (252 loc) · 10.6 KB
/
Copy pathParagraphAttributesUtils.mm
File metadata and controls
296 lines (252 loc) · 10.6 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
#import "ParagraphAttributesUtils.h"
#import "AlignmentUtils.h"
#import "EnrichedTextInputView.h"
#import "RangeUtils.h"
#import "StyleHeaders.h"
#import "StyleUtils.h"
#import "TextInsertionUtils.h"
@implementation ParagraphAttributesUtils
// if the user backspaces the last character in a line, the iOS applies typing
// attributes from the previous line in the case of some paragraph styles it
// works especially bad when a list point just appears this method handles that
// case differently with or without present paragraph styles
+ (BOOL)handleBackspaceInRange:(NSRange)range
replacementText:(NSString *)text
input:(id)input {
EnrichedTextInputView *typedInput = (EnrichedTextInputView *)input;
if (typedInput == nullptr) {
return NO;
}
// Do not manually intervene if the user is in the middle of IME
// composition. Let the iOS system handle the backspace natively to prevent
// state desync.
if (typedInput->textView.markedTextRange != nullptr) {
return NO;
}
// we make sure it was a backspace (text with 0 length) and it deleted
// something (range longer than 0)
if (text.length > 0 || range.length == 0) {
return NO;
}
// find a non-newline range of the paragraph
NSRange paragraphRange =
[typedInput->textView.textStorage.string paragraphRangeForRange:range];
NSArray *paragraphs = [RangeUtils getNonNewlineRangesIn:typedInput->textView
range:paragraphRange];
if (paragraphs.count == 0) {
return NO;
}
NSRange nonNewlineRange = [(NSValue *)paragraphs.firstObject rangeValue];
CheckboxListStyle *cbLStyle =
typedInput->stylesDict[@([CheckboxListStyle getType])];
// the backspace removes the whole content of a paragraph (possibly more but
// has to start where the paragraph starts)
if (range.location == nonNewlineRange.location &&
range.length >= nonNewlineRange.length) {
// Preserve the paragraph alignment across typing attribute resets.
NSParagraphStyle *currentParaStyle =
typedInput->textView.typingAttributes[NSParagraphStyleAttributeName];
NSTextAlignment savedAlignment =
currentParaStyle ? currentParaStyle.alignment : NSTextAlignmentNatural;
// for styles that need ZWS (lists, quotes, etc.) we do the following:
// - manually do the removing
// - reset typing attributes so that the previous line styles don't get
// applied
// - reapply the paragraph style that was present so that a zero width space
// appears here
for (NSNumber *type in typedInput->stylesDict) {
StyleBase *style = typedInput->stylesDict[type];
if ([style needsZWS] && [style detect:nonNewlineRange]) {
BOOL isCurrentlyChecked = [cbLStyle getCheckboxStateAt:range.location];
[TextInsertionUtils replaceText:text
at:range
additionalAttributes:nullptr
host:typedInput
withSelection:YES];
[self resetTypingAttributes:typedInput
preservingAlignment:savedAlignment];
if (style == cbLStyle) {
[cbLStyle addWithChecked:isCurrentlyChecked
range:NSMakeRange(range.location, 0)
withTyping:YES
withDirtyRange:YES];
} else {
[style add:NSMakeRange(range.location, 0)
withTyping:YES
withDirtyRange:YES];
}
return YES;
}
}
// otherwise (no paragraph styles present), we just do the replacement
// manually and reset typing attribtues
[TextInsertionUtils replaceText:text
at:range
additionalAttributes:nullptr
host:typedInput
withSelection:YES];
[self resetTypingAttributes:typedInput preservingAlignment:savedAlignment];
return YES;
}
return NO;
}
/**
* Handles the specific case of backspacing a newline character, which results
* in merging two paragraphs.
*
* THE PROBLEM:
* When merging a bottom paragraph (Source) into a top paragraph (Destination),
* the bottom paragraph normally brings all its attributes with it. If the top
* paragraph is a restrictive style (like a CodeBlock), and the bottom paragraph
* contains a conflicting style (like an H1 Header), a standard merge would
* create an invalid state (e.g., a CodeBlock that is also a Header).
*
* THE SOLUTION:
* 1. Identifies the dominant style of the paragraph ABOVE the deleted newline
* (`leftParagraphStyle`).
* 2. Checks the paragraph BELOW the newline (`rightRange`) for any styles that
* conflict with or are blocked by the top style.
* 3. Explicitly removes those forbidden styles from the bottom paragraph
* *before* the merge occurs.
* 4. Performs the merge (deletes the newline).
*
* @return YES if the newline backspace was handled and sanitized; NO otherwise.
*/
+ (BOOL)handleParagraphStylesMergeOnBackspace:(NSRange)range
replacementText:(NSString *)text
input:(id)input {
EnrichedTextInputView *typedInput = (EnrichedTextInputView *)input;
if (typedInput == nullptr) {
return NO;
}
// Must be a backspace.
if (text.length > 0) {
return NO;
}
// Backspace must have removed a newline character.
NSString *removedString =
[typedInput->textView.textStorage.string substringWithRange:range];
if ([removedString
rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]]
.location == NSNotFound) {
return NO;
}
NSRange leftRange = [typedInput->textView.textStorage.string
paragraphRangeForRange:NSMakeRange(range.location, 0)];
StyleBase *leftParagraphStyle = nullptr;
for (NSNumber *key in typedInput->stylesDict) {
StyleBase *style = typedInput->stylesDict[key];
if ([style isParagraph] && [style detect:leftRange]) {
leftParagraphStyle = style;
}
}
if (leftParagraphStyle == nullptr) {
return NO;
}
// index out of bounds
NSUInteger rightRangeStart = range.location + range.length;
if (rightRangeStart >= typedInput->textView.textStorage.string.length) {
return NO;
}
NSRange rightRange = [typedInput->textView.textStorage.string
paragraphRangeForRange:NSMakeRange(rightRangeStart, 1)];
StyleType type = [[leftParagraphStyle class] getType];
NSArray *conflictingStyles = [StyleUtils
getPresentStyleTypesFrom:typedInput->conflictingStyles[@(type)]
range:rightRange
forHost:typedInput];
NSArray *blockingStyles =
[StyleUtils getPresentStyleTypesFrom:typedInput->blockingStyles[@(type)]
range:rightRange
forHost:typedInput];
NSArray *allToBeRemoved =
[conflictingStyles arrayByAddingObjectsFromArray:blockingStyles];
for (NSNumber *style in allToBeRemoved) {
StyleBase *styleToRemove = typedInput->stylesDict[style];
// for ranges, we need to remove each occurence
NSArray<StylePair *> *allOccurences = [styleToRemove all:rightRange];
for (StylePair *pair in allOccurences) {
[styleToRemove remove:[pair.rangeValue rangeValue] withDirtyRange:YES];
}
}
[TextInsertionUtils replaceText:text
at:range
additionalAttributes:nullptr
host:typedInput
withSelection:YES];
return YES;
}
/**
* Resets typing attributes to defaults when the cursor lands on an empty line
* after a deletion.
*
* This override is necessary because `UITextView` automatically inherits
* attributes from the preceding newline. This prevents scenarios where a
* restrictive style (like CodeBlock) "leaks" into the next empty paragraph.
*/
+ (BOOL)handleResetTypingAttributesOnBackspace:(NSRange)range
replacementText:(NSString *)text
input:(id)input {
EnrichedTextInputView *typedInput = (EnrichedTextInputView *)input;
if (typedInput == nullptr) {
return NO;
}
NSString *storageString = typedInput->textView.textStorage.string;
if (text.length > 0 || range.location >= storageString.length) {
return NO;
}
unichar firstCharToDelete = [storageString characterAtIndex:range.location];
if (![[NSCharacterSet newlineCharacterSet]
characterIsMember:firstCharToDelete]) {
return NO;
}
NSRange leftParagraphRange =
[storageString paragraphRangeForRange:NSMakeRange(range.location, 0)];
BOOL isLeftLineEmpty = [self isParagraphEmpty:leftParagraphRange
inString:storageString];
BOOL isRightLineEmpty = YES;
NSUInteger rightRangeStart = range.location + range.length;
if (rightRangeStart < storageString.length) {
NSRange rightParagraphRange =
[storageString paragraphRangeForRange:NSMakeRange(rightRangeStart, 0)];
isRightLineEmpty = [self isParagraphEmpty:rightParagraphRange
inString:storageString];
}
if (isLeftLineEmpty && isRightLineEmpty) {
NSParagraphStyle *currentParaStyle =
typedInput->textView.typingAttributes[NSParagraphStyleAttributeName];
NSTextAlignment savedAlignment =
currentParaStyle ? currentParaStyle.alignment : NSTextAlignmentNatural;
[TextInsertionUtils replaceText:text
at:range
additionalAttributes:nullptr
host:typedInput
withSelection:YES];
[self resetTypingAttributes:typedInput preservingAlignment:savedAlignment];
return YES;
}
return NO;
}
+ (void)resetTypingAttributes:(EnrichedTextInputView *)input
preservingAlignment:(NSTextAlignment)alignment {
NSMutableDictionary *resetAttrs =
[input->defaultTypingAttributes mutableCopy];
NSMutableParagraphStyle *paraStyle =
[resetAttrs[NSParagraphStyleAttributeName] mutableCopy]
?: [[NSMutableParagraphStyle alloc] init];
paraStyle.textLists = @[ [[NSTextList alloc]
initWithMarkerFormat:[AlignmentUtils alignmentToMarker:alignment]
options:0] ];
paraStyle.alignment = alignment;
resetAttrs[NSParagraphStyleAttributeName] = paraStyle;
input->textView.typingAttributes = resetAttrs;
}
+ (BOOL)isParagraphEmpty:(NSRange)range inString:(NSString *)string {
if (range.length == 0)
return YES;
NSString *paragraphText = [string substringWithRange:range];
NSString *trimmed = [paragraphText
stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
return trimmed.length == 0;
}
@end