-
Notifications
You must be signed in to change notification settings - Fork 95
fix Firefox arrow navigation bug #683
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
jmusial
wants to merge
5
commits into
Expensify:main
Choose a base branch
from
jmusial:emoji-arrow-navigation-firefox-bug
base: main
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
5 commits
Select commit
Hold shift + click to select a range
6ef5bf8
fix firefox arrow navigation bug
jmusial 2bef014
code style changes
jmusial bd2f992
add handling of left arrow navigation
jmusial 6dcb3d1
Merge branch 'main' into emoji-arrow-navigation-firefox-bug
jmusial 0b732d8
optimize implementation of firefox arrow nav
jmusial 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
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,92 @@ | ||
| import * as CursorUtils from '../cursorUtils'; | ||
| import type {MarkdownTextInputElement} from '../../../MarkdownTextInput.web'; | ||
| import {handleFirefoxArrowKeyNavigation} from '../firefoxUtils'; | ||
|
|
||
| const createMockTarget = (value: string): MarkdownTextInputElement => { | ||
| const div = document.createElement('div') as unknown as MarkdownTextInputElement; | ||
| div.value = value; | ||
| return div; | ||
| }; | ||
|
|
||
| jest.mock('../cursorUtils', () => ({ | ||
| ...jest.requireActual('../cursorUtils'), | ||
| getCurrentCursorPosition: jest.fn(), | ||
| setCursorPosition: jest.fn(), | ||
| })); | ||
|
|
||
| describe('handleFirefoxArrowKeyNavigation', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should do nothing if no cursor in target', () => { | ||
| const target = createMockTarget('test'); | ||
| (CursorUtils.getCurrentCursorPosition as jest.Mock).mockReturnValue(null); | ||
|
|
||
| handleFirefoxArrowKeyNavigation(target); | ||
| expect(CursorUtils.setCursorPosition).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should move cursor to next grapheme boundary with regular text', () => { | ||
| const target = createMockTarget('hello world'); | ||
| (CursorUtils.getCurrentCursorPosition as jest.Mock).mockReturnValue({start: 5, end: 5}); | ||
|
|
||
| handleFirefoxArrowKeyNavigation(target); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 6, 6); | ||
| }); | ||
|
|
||
| it('should move cursor correctly when inside emoji', () => { | ||
| const target = createMockTarget('😀text'); | ||
| (CursorUtils.getCurrentCursorPosition as jest.Mock).mockReturnValue({start: 1, end: 1}); | ||
|
|
||
| handleFirefoxArrowKeyNavigation(target); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 2, 2); | ||
| }); | ||
|
|
||
| it('should not move cursor beyond text length', () => { | ||
| const target = createMockTarget('test'); | ||
|
|
||
| (CursorUtils.getCurrentCursorPosition as jest.Mock).mockReturnValue({start: 4, end: 4}); | ||
| handleFirefoxArrowKeyNavigation(target); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 4, 4); | ||
| }); | ||
|
|
||
| it('should handle multiple emojis', () => { | ||
| const target = createMockTarget('😀😀text'); | ||
| (CursorUtils.getCurrentCursorPosition as jest.Mock).mockReturnValueOnce({start: 0, end: 0}).mockReturnValueOnce({start: 2, end: 2}); | ||
|
|
||
| handleFirefoxArrowKeyNavigation(target); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 2, 2); | ||
| handleFirefoxArrowKeyNavigation(target); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 4, 4); | ||
| }); | ||
|
|
||
| it('should handle multiple emojis backward navigation', () => { | ||
| const target = createMockTarget('😀😀text'); | ||
| (CursorUtils.getCurrentCursorPosition as jest.Mock).mockReturnValueOnce({start: 4, end: 4}).mockReturnValueOnce({start: 2, end: 2}); | ||
|
|
||
| handleFirefoxArrowKeyNavigation(target, false, 'left'); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 2, 2); | ||
| handleFirefoxArrowKeyNavigation(target, false, 'left'); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 2, 2); | ||
| }); | ||
|
|
||
| it('should handle emoji selection', () => { | ||
| const target = createMockTarget('😀😀text'); | ||
| (CursorUtils.getCurrentCursorPosition as jest.Mock).mockReturnValueOnce({start: 0, end: 0}).mockReturnValueOnce({start: 2, end: 2}); | ||
|
|
||
| handleFirefoxArrowKeyNavigation(target, true); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 0, 2); | ||
| handleFirefoxArrowKeyNavigation(target); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 4, 4); | ||
| }); | ||
| it('should handle emoji selection backwards', () => { | ||
| const target = createMockTarget('😀😀text'); | ||
| (CursorUtils.getCurrentCursorPosition as jest.Mock).mockReturnValueOnce({start: 4, end: 4}).mockReturnValueOnce({start: 2, end: 4}); | ||
|
|
||
| handleFirefoxArrowKeyNavigation(target, true, 'left'); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 2, 4); | ||
| handleFirefoxArrowKeyNavigation(target, true, 'left'); | ||
| expect(CursorUtils.setCursorPosition).toHaveBeenCalledWith(target, 0, 4); | ||
| }); | ||
| }); | ||
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,48 @@ | ||
| import type {MarkdownTextInputElement} from '../../MarkdownTextInput.web'; | ||
| import {getCurrentCursorPosition, setCursorPosition} from './cursorUtils'; | ||
|
|
||
| /** | ||
| * Ensures consistent arrow navigation across grapheme clusters (like emojis) | ||
| */ | ||
| function handleFirefoxArrowKeyNavigation(target: MarkdownTextInputElement, isSelectionEvent = false, direction: 'right' | 'left' = 'right'): void { | ||
| const currentSelection = getCurrentCursorPosition(target); | ||
| if (!currentSelection) { | ||
| return; | ||
| } | ||
|
|
||
| const text = target.value; | ||
|
|
||
| const segmenter = new Intl.Segmenter('en', {granularity: 'grapheme'}); | ||
| const segments = segmenter.segment(text); | ||
|
|
||
| if (direction === 'right') { | ||
| const cursorPos = currentSelection.end; | ||
| let newCursorPos = text.length; | ||
|
|
||
| // eslint-disable-next-line no-restricted-syntax | ||
| for (const {index, segment} of segments) { | ||
| const segmentEnd = index + segment.length; | ||
| if (cursorPos < segmentEnd) { | ||
| newCursorPos = segmentEnd; | ||
| break; | ||
| } | ||
| } | ||
| setCursorPosition(target, isSelectionEvent ? currentSelection.start : newCursorPos, newCursorPos); | ||
| return; | ||
| } | ||
| const cursorPos = currentSelection.start; | ||
| let newCursorPos = 0; | ||
|
|
||
| // eslint-disable-next-line no-restricted-syntax | ||
| for (const {index, segment} of segments) { | ||
| const segmentEnd = index + segment.length; | ||
| if (segmentEnd < cursorPos) { | ||
| newCursorPos = segmentEnd; | ||
| } | ||
| } | ||
|
|
||
| setCursorPosition(target, newCursorPos, isSelectionEvent ? currentSelection.end : newCursorPos); | ||
| } | ||
|
|
||
| // eslint-disable-next-line import/prefer-default-export | ||
| export {handleFirefoxArrowKeyNavigation}; |
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.
Uh oh!
There was an error while loading. Please reload this page.