Skip to content

Commit

Permalink
ios cursor position
Browse files Browse the repository at this point in the history
  • Loading branch information
perunt committed Mar 24, 2023
1 parent 1c2d784 commit b9c8dfb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
8 changes: 7 additions & 1 deletion Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ - (RCTTextSelection *)selection
id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;
UITextRange *selectedTextRange = backedTextInputView.selectedTextRange;
return [[RCTTextSelection new] initWithStart:[backedTextInputView offsetFromPosition:backedTextInputView.beginningOfDocument toPosition:selectedTextRange.start]
end:[backedTextInputView offsetFromPosition:backedTextInputView.beginningOfDocument toPosition:selectedTextRange.end]];
end:[backedTextInputView offsetFromPosition:backedTextInputView.beginningOfDocument toPosition:selectedTextRange.end]
cursorPosition:[backedTextInputView caretRectForPosition:selectedTextRange.start].origin];
}

- (void)setSelection:(RCTTextSelection *)selection
Expand Down Expand Up @@ -491,10 +492,15 @@ - (void)textInputDidChangeSelection

RCTTextSelection *selection = self.selection;

UITextRange *selectedTextRange = self.backedTextInputView.selectedTextRange;
CGPoint selectionOrigin = [self.backedTextInputView caretRectForPosition:selectedTextRange.start].origin;

_onSelectionChange(@{
@"selection": @{
@"start": @(selection.start),
@"end": @(selection.end),
@"positionY": @(selectionOrigin.y),
@"positionX": @(selectionOrigin.x),
},
});
}
Expand Down
3 changes: 2 additions & 1 deletion Libraries/Text/TextInput/RCTTextSelection.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

@property (nonatomic, assign, readonly) NSInteger start;
@property (nonatomic, assign, readonly) NSInteger end;
@property (nonatomic, assign, readonly) CGPoint cursorPosition;

- (instancetype)initWithStart:(NSInteger)start end:(NSInteger)end;
- (instancetype)initWithStart:(NSInteger)start end:(NSInteger)end cursorPosition:(CGPoint)cursorPosition;

@end

Expand Down
11 changes: 9 additions & 2 deletions Libraries/Text/TextInput/RCTTextSelection.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

@implementation RCTTextSelection

- (instancetype)initWithStart:(NSInteger)start end:(NSInteger)end
- (instancetype)initWithStart:(NSInteger)start end:(NSInteger)end cursorPosition:(CGPoint)cursorPosition
{
if (self = [super init]) {
_start = start;
_end = end;
_cursorPosition = cursorPosition;
}
return self;
}
Expand All @@ -27,7 +28,13 @@ + (RCTTextSelection *)RCTTextSelection:(id)json
if ([json isKindOfClass:[NSDictionary class]]) {
NSInteger start = [self NSInteger:json[@"start"]];
NSInteger end = [self NSInteger:json[@"end"]];
return [[RCTTextSelection alloc] initWithStart:start end:end];
CGPoint cursorPosition = CGPointMake(
[self CGFloat:json[@"cursorPositionX"]],
[self CGFloat:json[@"cursorPositionY"]]
);
return [[RCTTextSelection alloc] initWithStart:start
end:end
cursorPosition:cursorPosition];
}

return nil;
Expand Down

0 comments on commit b9c8dfb

Please sign in to comment.