Skip to content

Commit fdc310f

Browse files
committed
feat(ios): avoid double space when inserting a mention
1 parent 5a6ccad commit fdc310f

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

ios/styles/MentionStyle.mm

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,25 @@ - (void)addMention:(NSString *)indicator
158158
params.indicator = indicator;
159159
params.attributes = attributes;
160160

161-
// add a single space after the mention
162-
NSString *newText = [NSString stringWithFormat:@"%@ ", text];
163161
NSRange rangeToBeReplaced = [_activeMentionRange rangeValue];
162+
163+
// add a single space after the mention if there isn't one already
164+
BOOL hasSpaceAfter = NO;
165+
NSUInteger nextCharIndex =
166+
rangeToBeReplaced.location + rangeToBeReplaced.length;
167+
168+
if (nextCharIndex < self.host.textView.textStorage.string.length) {
169+
unichar nextChar =
170+
[self.host.textView.textStorage.string characterAtIndex:nextCharIndex];
171+
if ([[NSCharacterSet whitespaceAndNewlineCharacterSet]
172+
characterIsMember:nextChar]) {
173+
hasSpaceAfter = YES;
174+
}
175+
}
176+
177+
NSString *newText =
178+
hasSpaceAfter ? text : [NSString stringWithFormat:@"%@ ", text];
179+
164180
[TextInsertionUtils replaceText:newText
165181
at:rangeToBeReplaced
166182
additionalAttributes:nullptr

0 commit comments

Comments
 (0)