Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions ios/EnrichedTextInputView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ @implementation EnrichedTextInputView {
NSArray<NSDictionary *> *_contextMenuItems;
NSString *_submitBehavior;
NSDictionary<NSAttributedStringKey, id> *_capturedAttributesBeforeChange;
NSRange _capturedReplacementRange;
NSString *_recentlyEmittedAlignment;
CustomStyleData *_recentlyEmittedCustomStyle;
}
Expand Down Expand Up @@ -2080,15 +2081,32 @@ - (void)handleKeyPressInRange:(NSString *)text range:(NSRange)range {
- (bool)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
// Capture the attributes at range.location that are being replaced
// (autocorrect / predictive) so didProcessEditing: can re-stamp them onto the
// replacement. Skip pure deletions (text.length == 0) — there is no incoming
// text to receive attributes, and capturing here would cause the deleted
// character's CustomStyleData to be re-stamped onto the widened editedRange.
if (range.length > 0 && text.length > 0) {
_capturedAttributesBeforeChange =
[textView.textStorage attributesAtIndex:range.location
effectiveRange:NULL];
// Autocorrect, spellcheck and predictive text replace a word at the caret and
// drop its styling, so capture it for didProcessEditing: to re-apply.
// Typing over a selection is handled by the typing attributes instead.
if (range.length > 0 && text.length > 0 &&
textView.selectedRange.length == 0) {
NSDictionary *attrs = [textView.textStorage attributesAtIndex:range.location
effectiveRange:NULL];
NSMutableDictionary *captured = [[NSMutableDictionary alloc] init];
for (StyleBase *style in stylesDict.allValues) {
if ([style isParagraph]) {
id value = attrs[[style getKey]];
if (value != nil) {
captured[[style getKey]] = value;
}
continue;
}
// Skips styles that never extend to new text (mentions, links, images).
AttributeEntry *entry =
[style getEntryIfPresent:NSMakeRange(range.location, 1)];
if (entry != nullptr) {
captured[entry.key] = entry.value;
}
}
_capturedAttributesBeforeChange = captured;
// Only the replacement text; didProcessEditing:'s editedRange can be wider.
_capturedReplacementRange = NSMakeRange(range.location, text.length);
}

// Check if the user pressed "Enter"
Expand Down Expand Up @@ -2174,6 +2192,14 @@ - (bool)textView:(UITextView *)textView
// native TextKit issue.
if (textView.markedTextRange == nil && text.length > 0) {
[attributesManager repeatRecentTypingAttributesManagement];

// Replacing a selection - we need to manually extend typing attributes that
// were there before, as by default the attributes manager only takes a
// preceding index into account. Must run after the repeat above, which
// resets the typing attributes.
if (range.length > 0 && textView.selectedRange.length > 0) {
[attributesManager extendTypingAttributesFromReplacedRange:range];
}
}

return YES;
Expand Down Expand Up @@ -2318,12 +2344,14 @@ - (void)textStorage:(NSTextStorage *)textStorage
if (_capturedAttributesBeforeChange != nil) {
// Skip while an IME composition is in progress; restamp on commit.
if (textView.markedTextRange == nil) {
NSRange restampRange = NSIntersectionRange(
_capturedReplacementRange, NSMakeRange(0, textStorage.length));
NSSet *customKeys = [attributesManager customAttributesKeys];
for (NSString *key in _capturedAttributesBeforeChange) {
if ([customKeys containsObject:key]) {
[textStorage addAttribute:key
value:_capturedAttributesBeforeChange[key]
range:editedRange];
range:restampRange];
}
}
}
Expand Down
1 change: 1 addition & 0 deletions ios/inputAttributesManager/InputAttributesManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- (void)clearRemovedTypingAttributes;
- (void)manageTypingAttributesWithOnlySelection:(BOOL)onlySelectionChanged;
- (void)repeatRecentTypingAttributesManagement;
- (void)extendTypingAttributesFromReplacedRange:(NSRange)range;
- (void)handleDirtyRangesStyling;
- (NSSet<NSString *> *)customAttributesKeys;
@end
12 changes: 12 additions & 0 deletions ios/inputAttributesManager/InputAttributesManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,16 @@ - (void)repeatRecentTypingAttributesManagement {
[self manageTypingAttributesWithOnlySelection:_recentOnlySelectionStatus];
}

- (void)extendTypingAttributesFromReplacedRange:(NSRange)range {
if (NSMaxRange(range) > _input->textView.textStorage.string.length) {
return;
}
// Same filtering as extending from the preceding character: styles that
// shouldn't be extended (mentions, links, images) are skipped.
NSMutableDictionary *newAttrs =
[_input->textView.typingAttributes mutableCopy];
[self restoreInlineStylesPresentInRange:range intoAttrs:newAttrs];
_input->textView.typingAttributes = newAttrs;
}

@end
Loading