Skip to content

Commit afb34aa

Browse files
kacperzolkiewskiIvanIhnatsiuk
authored andcommitted
fix: cursor position when inserting image (software-mansion#337)
Fixes: software-mansion#329 1. Go to Example app 2. Add `<html><p><img src="https://picsum.photos/id/1/200/200" width="200" height="200"/></p></html>` through `Set input's value` button 3. See that cursor is applied after image Before: https://github.com/user-attachments/assets/258132ae-1bf4-43e5-b08f-b8ade46ade67 After: https://github.com/user-attachments/assets/6dede546-471c-489c-81e5-7e36f80377d4 | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | ❌ |
1 parent da34766 commit afb34aa

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

ios/EnrichedTextInputView.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,9 @@ - (void)updateProps:(Props::Shared const &)props
851851
newViewProps.defaultValue != oldViewProps.defaultValue;
852852

853853
if (stylePropChanged) {
854+
// we want to preserve the selection between props changes
855+
NSRange prevSelectedRange = textView.selectedRange;
856+
854857
// now set the new config
855858
config = newConfig;
856859

@@ -887,6 +890,7 @@ - (void)updateProps:(Props::Shared const &)props
887890
defaultTypingAttributes[NSParagraphStyleAttributeName] =
888891
[[NSParagraphStyle alloc] init];
889892
textView.typingAttributes = defaultTypingAttributes;
893+
textView.selectedRange = prevSelectedRange;
890894

891895
// update the placeholder as well
892896
[self refreshPlaceholderLabelStyles];
@@ -911,6 +915,7 @@ - (void)updateProps:(Props::Shared const &)props
911915
[parser replaceWholeFromHtml:newDefaultValue
912916
notifyAnyTextMayHaveBeenModified:!isFirstMount];
913917
}
918+
textView.selectedRange = NSRange(textView.textStorage.string.length, 0);
914919
}
915920

916921
// placeholderTextColor
@@ -1332,6 +1337,7 @@ - (void)setValue:(NSString *)value {
13321337

13331338
// set recentlyChangedRange and check for changes
13341339
recentlyChangedRange = NSMakeRange(0, textView.textStorage.string.length);
1340+
textView.selectedRange = NSRange(textView.textStorage.string.length, 0);
13351341
[self anyTextMayHaveBeenModified];
13361342
}
13371343

ios/attachments/ImageAttachment.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ - (instancetype)initWithImageData:(ImageData *)data {
1616

1717
- (void)loadAsync {
1818
NSURL *url = [NSURL URLWithString:self.uri];
19-
if (!url)
19+
if (!url) {
20+
self.image = [UIImage systemImageNamed:@"file"];
2021
return;
22+
}
2123

2224
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
2325
NSData *bytes = [NSData dataWithContentsOfURL:url];

0 commit comments

Comments
 (0)