Skip to content

feat: add setSelection method#311

Merged
exploIF merged 10 commits intosoftware-mansion:mainfrom
IslamRustamov:feature/add-set-selection
Dec 22, 2025
Merged

feat: add setSelection method#311
exploIF merged 10 commits intosoftware-mansion:mainfrom
IslamRustamov:feature/add-set-selection

Conversation

@IslamRustamov
Copy link
Copy Markdown
Contributor

@IslamRustamov IslamRustamov commented Dec 8, 2025

Summary

Explain the motivation for making this change: here are some points to help you:

  • This PR solves Allow setSelection on Ref #204
  • This PR add a new method called setSelection that allows user to programmatically set selection on text input
  • Added new types, added function implementation on Android and iOS
  • This PR impacts area of available ref methods

Test Plan

  1. Enter text
  2. Call ref.current?.setSelection(1, 2)
  3. See that text got selected

Screenshots / Videos

Not applicable

Compatibility

OS Implemented
iOS
Android

@IslamRustamov IslamRustamov force-pushed the feature/add-set-selection branch from 2779fca to 4bd32df Compare December 8, 2025 19:19
@exploIF exploIF requested review from exploIF and szydlovsky December 9, 2025 08:08
@szydlovsky
Copy link
Copy Markdown
Collaborator

szydlovsky commented Dec 9, 2025

Hey @IslamRustamov thanks for the contribution! I'll have to address a few matters first.
We definitely want the API to work exactly the same on both platforms, so we will most likely use the Android approach as other selection topics have been handled this way.
Except for that, I want to know your exact use case for the feature. I'm asking it because iOS might have some inconsistencies - emitted text isn't always exactly what the input holds because we sometimes put some zero-width spaces in lines with empty lists, blockquotes, codeblocks (so that they can be drawn), but we don't emit them. This way selection might be different than what you expect because, for example, some zero-width space got selected too on the way. Ideally there could just be a check that extends the selection accordingly if any zero-width spaces were in the range of the setSelection method, if you're interested I can just suggest it in the review.

@IslamRustamov
Copy link
Copy Markdown
Contributor Author

Ah I didn't know that you were using unseen chars in text.

As for use case - nothing in particular, just wanted to contribute something and saw this issue #204 that required this exact feature.

If you want to develop this feature yourself or don't consider it needed for now - feel free to close this PR, or if you want me to continue working on it then let me know.

@szydlovsky
Copy link
Copy Markdown
Collaborator

szydlovsky commented Dec 10, 2025

I see. Honestly, this PR isn't far from being ready. If you just made sure both platforms treat the selection in the same (Android) way it would be great. You can do it similarly on iOS to how addLink does that:

- (void)addLinkAt:(NSInteger)start end:(NSInteger)end text:(NSString *)text url:(NSString *)url {
LinkStyle *linkStyleClass = (LinkStyle *)stylesDict[@([LinkStyle getStyleType])];
if(linkStyleClass == nullptr) { return; }
// translate the output start-end notation to range
NSRange linkRange = NSMakeRange(start, end - start);
if([self handleStyleBlocksAndConflicts:[LinkStyle getStyleType] range:linkRange]) {
[linkStyleClass addLink:text url:url range:linkRange manual:YES];
[self anyTextMayHaveBeenModified];
}
}

And with the unseen chars on iOS (afaik Android won't have that problem), all that's needed is to check the text in the range to be selected. Then extend the selection by 1 length for each \u200B (zero-width-space character). Make sure the selection won't go outside of the input range ;)

We really appreciate meaningful contributions and I don't want to take it from you by basing a PR on your wok.

@IslamRustamov IslamRustamov force-pushed the feature/add-set-selection branch 2 times, most recently from 772e83c to 48668e5 Compare December 10, 2025 21:20
@IslamRustamov
Copy link
Copy Markdown
Contributor Author

IslamRustamov commented Dec 10, 2025

@szydlovsky I believe that does it. If I understood correctly how you want zero width spaces handled. Tested on lists and it seemed to work.

This is with zero width spaces in mind:
Screenshot 2025-12-11 at 00 23 27

And this is not:
Screenshot 2025-12-11 at 00 24 22

Selection params are start === 2 and end === 6.

Copy link
Copy Markdown
Collaborator

@szydlovsky szydlovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got quite a bit iOS only cosmetic changes. Also, please pull the latest main afterwards and run lint-clang: fix in the repo root.

@IslamRustamov IslamRustamov force-pushed the feature/add-set-selection branch from 48668e5 to a0f9fd1 Compare December 12, 2025 14:11
@IslamRustamov
Copy link
Copy Markdown
Contributor Author

@szydlovsky made the changes, check please if everything is to your liking

@szydlovsky
Copy link
Copy Markdown
Collaborator

@IslamRustamov great, will check it out on Monday, stay tuned!

Copy link
Copy Markdown
Collaborator

@exploIF exploIF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me in general. However, there is one missing thing. Similar to iOS part, we should include zero width spaces in selection range calculations on Android

@kacperzolkiewski
Copy link
Copy Markdown
Collaborator

kacperzolkiewski commented Dec 19, 2025

I added including zero width spaces on Android setSelection(2, 6) result:
image

Copy link
Copy Markdown
Collaborator

@exploIF exploIF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you 🙌

@exploIF exploIF merged commit a6060ad into software-mansion:main Dec 22, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants