diff --git a/.maestro/enrichedInput/flows/preserve_typing_attributes_on_selection_change.yaml b/.maestro/enrichedInput/flows/preserve_typing_attributes_on_selection_change.yaml new file mode 100644 index 000000000..fd2676eea --- /dev/null +++ b/.maestro/enrichedInput/flows/preserve_typing_attributes_on_selection_change.yaml @@ -0,0 +1,34 @@ +appId: swmansion.enriched.example +--- +# Verifies that typing attributes are properly preserved on selection changes +- launchApp + +- tapOn: + id: 'toggle-screen-button' + +- tapOn: + id: "editor-input" + +- tapOn: + id: "toolbar-bold" + +- inputText: 'bold text' +- pressKey: Enter +- pressKey: Enter +- pressKey: Enter +- inputText: 'another line' + +- doubleTapOn: + id: 'editor-input' + point: '20%, 75%' + +- tapOn: + id: 'editor-input' + point: '50%, 15%' + +- inputText: 'new' + +- runFlow: + file: '../subflows/capture_or_assert_screenshot.yaml' + env: + SCREENSHOT_NAME: 'preserve_typing_attributes_on_selection_changes' diff --git a/.maestro/enrichedInput/screenshots/android/preserve_typing_attributes_on_selection_changes.png b/.maestro/enrichedInput/screenshots/android/preserve_typing_attributes_on_selection_changes.png new file mode 100644 index 000000000..1b38f3e63 Binary files /dev/null and b/.maestro/enrichedInput/screenshots/android/preserve_typing_attributes_on_selection_changes.png differ diff --git a/.maestro/enrichedInput/screenshots/ios/preserve_typing_attributes_on_selection_changes.png b/.maestro/enrichedInput/screenshots/ios/preserve_typing_attributes_on_selection_changes.png new file mode 100644 index 000000000..b4624899c Binary files /dev/null and b/.maestro/enrichedInput/screenshots/ios/preserve_typing_attributes_on_selection_changes.png differ diff --git a/ios/EnrichedTextInputView.mm b/ios/EnrichedTextInputView.mm index a899aa629..9862738ef 100644 --- a/ios/EnrichedTextInputView.mm +++ b/ios/EnrichedTextInputView.mm @@ -2000,6 +2000,14 @@ - (bool)textView:(UITextView *)textView return NO; } + // To be sure, we re-run typingAttributes management right before the + // character actually lands. Sometimes, between a selection change and the + // next keystroke, typing attributes might get removed - this seems like a + // native TextKit issue. + if (textView.markedTextRange == nil && text.length > 0) { + [attributesManager repeatRecentTypingAttributesManagement]; + } + return YES; } diff --git a/ios/inputAttributesManager/InputAttributesManager.h b/ios/inputAttributesManager/InputAttributesManager.h index c0cb19df9..b4ba67a7b 100644 --- a/ios/inputAttributesManager/InputAttributesManager.h +++ b/ios/inputAttributesManager/InputAttributesManager.h @@ -13,6 +13,7 @@ - (void)didRemoveTypingAttribute:(NSString *)key; - (void)clearRemovedTypingAttributes; - (void)manageTypingAttributesWithOnlySelection:(BOOL)onlySelectionChanged; +- (void)repeatRecentTypingAttributesManagement; - (void)handleDirtyRangesStyling; - (NSSet *)customAttributesKeys; @end diff --git a/ios/inputAttributesManager/InputAttributesManager.mm b/ios/inputAttributesManager/InputAttributesManager.mm index fc07d871f..3d844da54 100644 --- a/ios/inputAttributesManager/InputAttributesManager.mm +++ b/ios/inputAttributesManager/InputAttributesManager.mm @@ -11,6 +11,7 @@ @implementation InputAttributesManager { NSMutableArray *_dirtyRanges; NSSet *_customAttributesKeys; NSMutableSet *_removedTypingAttributes; + BOOL _recentOnlySelectionStatus; } - (instancetype)initWithInput:(EnrichedTextInputView *)input { @@ -18,6 +19,7 @@ - (instancetype)initWithInput:(EnrichedTextInputView *)input { _input = input; _dirtyRanges = [[NSMutableArray alloc] init]; _removedTypingAttributes = [[NSMutableSet alloc] init]; + _recentOnlySelectionStatus = NO; // setup customAttributes NSMutableSet *_customAttrsSet = [[NSMutableSet alloc] init]; @@ -129,6 +131,7 @@ - (void)handleDirtyRangesStyling { } - (void)manageTypingAttributesWithOnlySelection:(BOOL)onlySelectionChanged { + _recentOnlySelectionStatus = onlySelectionChanged; EnrichedInputTextView *textView = _input->textView; NSRange selectedRange = textView.selectedRange; @@ -219,4 +222,8 @@ - (void)manageTypingAttributesWithOnlySelection:(BOOL)onlySelectionChanged { textView.typingAttributes = newAttrs; } +- (void)repeatRecentTypingAttributesManagement { + [self manageTypingAttributesWithOnlySelection:_recentOnlySelectionStatus]; +} + @end