Repository navigation
feat: add getFormattedSelection to keep the caret after formatting #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nikzanda
wants to merge
3
commits into
react-component:master
Choose a base branch
from
nikzanda:feat/formatted-selection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # getFormattedSelection | ||
|
|
||
| Adjust an input selection after its text is formatted, for example when a formatter adds a prefix or separators, or changes letter case. | ||
|
|
||
| ```tsx | pure | ||
| import { getFormattedSelection } from '@rc-component/util'; | ||
|
|
||
| // When the input changes, before formatting | ||
| const originStr = input.value; | ||
| const selection = { start: input.selectionStart, end: input.selectionEnd }; | ||
| const targetStr = format(originStr); | ||
|
|
||
| // After rendering `targetStr` | ||
| const { start, end } = getFormattedSelection(originStr, selection, targetStr); | ||
| input.setSelectionRange(start, end); | ||
| ``` | ||
|
|
||
| ```ts | ||
| interface SelectionRange { | ||
| start: number; | ||
| end: number; | ||
| } | ||
|
|
||
| function getFormattedSelection( | ||
| originStr: string, | ||
| selection: SelectionRange, | ||
| targetStr: string, | ||
| ): SelectionRange; | ||
| ``` | ||
|
|
||
| Each edge of the selection is placed by the first rule that applies: | ||
|
|
||
| | Rule | `originStr` | Caret | `targetStr` | Result | | ||
| | --------------------------------------------------------------------------- | ----------- | ----- | ----------- | ------ | | ||
| | Keep it after the same text before it | `5` | 1 | `5%` | 1 | | ||
| | Keep it before the same text after it | `AbC` | 2 | `ABC` | 2 | | ||
| | Place it after the character before it, searched from its previous position | `9$ ` | 1 | `$ 9` | 3 | | ||
| | Move it to the end when that character is gone | `abc` | 1 | `xyz` | 3 | | ||
|
|
||
| Offsets outside `originStr` are clamped, and the returned end is never before the start. The selection is not modified. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| export interface SelectionRange { | ||
| start: number; | ||
| end: number; | ||
| } | ||
|
|
||
| function getFormattedOffset( | ||
| originStr: string, | ||
| offset: number, | ||
| targetStr: string, | ||
| ) { | ||
| const position = Math.min(Math.max(offset, 0), originStr.length); | ||
| const beforeStr = originStr.slice(0, position); | ||
| const afterStr = originStr.slice(position); | ||
|
|
||
| if (targetStr.startsWith(beforeStr)) { | ||
| return position; | ||
| } | ||
| if (targetStr.endsWith(afterStr)) { | ||
| return targetStr.length - afterStr.length; | ||
| } | ||
|
|
||
| // Both sides changed, e.g. `9$ ` formatted as `$ 9`: follow the character | ||
| // before the caret, searching from its previous position. | ||
| const index = targetStr.indexOf(originStr[position - 1], position - 1); | ||
| return index === -1 ? targetStr.length : index + 1; | ||
| } | ||
|
|
||
| /** | ||
| * Adjust a selection of `originStr` after it is formatted as `targetStr`. | ||
| * Each edge stays after the same text before it, or before the same text after | ||
| * it. Otherwise it follows the character before it, or moves to the end. | ||
| */ | ||
| export default function getFormattedSelection( | ||
| originStr: string, | ||
| selection: SelectionRange, | ||
| targetStr: string, | ||
| ): SelectionRange { | ||
| const start = getFormattedOffset(originStr, selection.start, targetStr); | ||
| const end = getFormattedOffset(originStr, selection.end, targetStr); | ||
| return { start, end: Math.max(start, end) }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { getFormattedSelection } from '../src'; | ||
| import type { SelectionRange } from '../src'; | ||
|
|
||
| const caret = (position: number): SelectionRange => ({ | ||
| start: position, | ||
| end: position, | ||
| }); | ||
|
|
||
| describe('getFormattedSelection', () => { | ||
| it.each<[string, string, number, string, number]>([ | ||
| ['unchanged text', '12', 1, '12', 1], | ||
| ['an appended suffix', '5', 1, '5%', 1], | ||
| ['an uppercase character typed in the middle', 'AbC', 2, 'ABC', 2], | ||
| ['an uppercase character typed at the end', 'Ab', 2, 'AB', 2], | ||
| ['a prepended prefix', '1', 1, '$ 1', 3], | ||
| ['a prefix and thousands separators', '123456', 3, '$ 123,456', 6], | ||
| ['a removed separator', '12,34', 3, '1,234', 3], | ||
| ['a caret at the start', '1', 0, '$ 1', 0], | ||
| ['an empty origin', '', 0, '$ ', 0], | ||
| ])('keeps the caret with %s', (_, originStr, position, targetStr, result) => { | ||
| expect( | ||
| getFormattedSelection(originStr, caret(position), targetStr), | ||
| ).toEqual(caret(result)); | ||
| }); | ||
|
|
||
| it('follows the character before the caret when both sides change', () => { | ||
| // `$ ` typed `9` before the prefix, then formatted as `$ 9`. | ||
| expect(getFormattedSelection('9$ ', caret(1), '$ 9')).toEqual(caret(3)); | ||
| }); | ||
|
|
||
| it('searches for the character before the caret from its previous position', () => { | ||
| // `$` is at index 0, before the search starts at index 1, so it is not found. | ||
| expect(getFormattedSelection('9$ ', caret(2), '$ 9')).toEqual(caret(3)); | ||
| // `b` is still at index 1, where the search starts, so it is found. | ||
| expect(getFormattedSelection('abX', caret(2), 'ZbY')).toEqual(caret(2)); | ||
| }); | ||
|
|
||
| it('moves the caret to the end when the character before it is gone', () => { | ||
| expect(getFormattedSelection('abc', caret(1), 'xyz')).toEqual(caret(3)); | ||
| }); | ||
|
|
||
| it('adjusts each edge of a selection', () => { | ||
| expect( | ||
| getFormattedSelection('1234', { start: 1, end: 3 }, '1,234'), | ||
| ).toEqual({ start: 1, end: 4 }); | ||
| }); | ||
|
|
||
| it('does not return a selection that ends before it starts', () => { | ||
| expect(getFormattedSelection('a-b', { start: 1, end: 2 }, 'b-a')).toEqual( | ||
| caret(3), | ||
| ); | ||
| }); | ||
|
|
||
| it('clamps offsets outside the original text', () => { | ||
| expect(getFormattedSelection('ab', { start: -1, end: 10 }, 'AB')).toEqual({ | ||
| start: 0, | ||
| end: 2, | ||
| }); | ||
| }); | ||
|
|
||
| it('does not modify the selection', () => { | ||
| const selection = Object.freeze(caret(1)); | ||
| getFormattedSelection('9$ ', selection, '$ 9'); | ||
| expect(selection).toEqual(caret(1)); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacing
useCursorwithgetFormattedSelectioncan change where the caret lands when text is selected. In this example,[bc]marks the selected text and|marks the caret.Could we clarify in the PR description that the two behave the same when no text is selected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the description: with no text selected the two give the same caret. With a selection they can differ, as in your example, because
useCursoralways restores a caret whilegetFormattedSelectionplaces each edge on its own ([1234]formatted as1,234stays fully selected). In rc-input-number the selection is read after the edit (onChangeandonCompositionEnd), so it is a caret after typing, pasting or deleting.