Skip to content

Commit 1a29e50

Browse files
authored
fix: more accurate selection with zero width spaces on iOS (#191)
Fixed the new selection when zero width spaces are added or removed and the adding/removing of them was within the user selection.
1 parent ee9cc11 commit 1a29e50

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

ios/styles/BlockQuoteStyle.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ - (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
141141
}
142142
NSRange nonNewlineRange = [nonNewlineVal rangeValue];
143143
if(NSEqualRanges(range, nonNewlineRange)) {
144-
[self removeAttributes:range];
145144
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:_input withSelection:YES];
145+
[self removeAttributes:NSMakeRange(range.location, 0)];
146146
return YES;
147147
}
148148
}

ios/styles/OrderedListStyle.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ - (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
153153
}
154154
NSRange nonNewlineRange = [nonNewlineVal rangeValue];
155155
if(NSEqualRanges(range, nonNewlineRange)) {
156-
[self removeAttributes:range];
157156
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:_input withSelection:YES];
157+
[self removeAttributes:NSMakeRange(range.location, 0)];
158158
return YES;
159159
}
160160
}

ios/styles/UnorderedListStyle.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ - (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
153153
}
154154
NSRange nonNewlineRange = [nonNewlineVal rangeValue];
155155
if(NSEqualRanges(range, nonNewlineRange)) {
156-
[self removeAttributes:range];
157156
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:_input withSelection:YES];
157+
[self removeAttributes:NSMakeRange(range.location, 0)];
158158
return YES;
159159
}
160160
}

ios/utils/ZeroWidthSpaceUtils.mm

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,23 @@ + (void)removeSpacesIfNeededinInput:(EnrichedTextInputView *)input {
5252

5353
// do the removing
5454
NSInteger offset = 0;
55-
NSInteger postRemoveOffset = 0;
55+
NSInteger postRemoveLocationOffset = 0;
56+
NSInteger postRemoveLengthOffset = 0;
5657
for(NSNumber *index in indexesToBeRemoved) {
5758
NSRange replaceRange = NSMakeRange([index integerValue] + offset, 1);
5859
[TextInsertionUtils replaceText:@"" at:replaceRange additionalAttributes:nullptr input:input withSelection:NO];
5960
offset -= 1;
6061
if([index integerValue] < preRemoveSelection.location) {
61-
postRemoveOffset -= 1;
62+
postRemoveLocationOffset -= 1;
63+
}
64+
if([index integerValue] >= preRemoveSelection.location && [index integerValue] < NSMaxRange(preRemoveSelection)) {
65+
postRemoveLengthOffset -= 1;
6266
}
6367
}
6468

6569
// fix the selection if needed
6670
if([input->textView isFirstResponder]) {
67-
input->textView.selectedRange = NSMakeRange(preRemoveSelection.location + postRemoveOffset, preRemoveSelection.length);
71+
input->textView.selectedRange = NSMakeRange(preRemoveSelection.location + postRemoveLocationOffset, preRemoveSelection.length + postRemoveLengthOffset);
6872
}
6973
}
7074

@@ -93,13 +97,17 @@ + (void)addSpacesIfNeededinInput:(EnrichedTextInputView *)input {
9397

9498
// do the replacing
9599
NSInteger offset = 0;
96-
NSInteger postAddOffset = 0;
100+
NSInteger postAddLocationOffset = 0;
101+
NSInteger postAddLengthOffset = 0;
97102
for(NSNumber *index in indexesToBeInserted) {
98103
NSRange replaceRange = NSMakeRange([index integerValue] + offset, 1);
99104
[TextInsertionUtils replaceText:@"\u200B\n" at:replaceRange additionalAttributes:nullptr input:input withSelection:NO];
100105
offset += 1;
101106
if([index integerValue] < preAddSelection.location) {
102-
postAddOffset += 1;
107+
postAddLocationOffset += 1;
108+
}
109+
if([index integerValue] >= preAddSelection.location && [index integerValue] < NSMaxRange(preAddSelection)) {
110+
postAddLengthOffset += 1;
103111
}
104112
}
105113

@@ -112,7 +120,7 @@ + (void)addSpacesIfNeededinInput:(EnrichedTextInputView *)input {
112120

113121
// fix the selection if needed
114122
if([input->textView isFirstResponder]) {
115-
input->textView.selectedRange = NSMakeRange(preAddSelection.location + postAddOffset, preAddSelection.length);
123+
input->textView.selectedRange = NSMakeRange(preAddSelection.location + postAddLocationOffset, preAddSelection.length + postAddLengthOffset);
116124
}
117125
}
118126

0 commit comments

Comments
 (0)