Skip to content

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jmusial
Copy link
Contributor

@jmusial jmusial commented May 23, 2025

Fixes navigation bug on Firefox, where navigation using arrows over emojis requires 2 key presses.

Screen.Recording.2025-05-21.at.09.55.11.mov

Details

In Firefox, the arrow key navigation treats surrogate pairs (like emojis) as two separate code units, while Chromium browsers treat them as a single character. This discrepancy causes the “double-arrow press” behaviour.

Related Issues

GH_LINK

Manual Tests

  1. Open editor with some emojis typed in
  2. Try navigating with your arrows
  3. Results should be consistent across all browsers and 1 key press should move cursor over 1 emoji

Videos

Before

LM_emojis_before.mov

After

LM_emojis_after.mov

Linked PRs

Copy link

github-actions bot commented May 23, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@jmusial
Copy link
Contributor Author

jmusial commented May 23, 2025

recheck

@tomekzaw tomekzaw changed the title fix Firefix arrow navigation bug fix Firefox arrow navigation bug May 25, 2025
@@ -526,6 +528,15 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
onKeyPress(event);
}

// Handle ArrowRight for consistent navigation across grapheme clusters (like emojis) on firefox
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Handle ArrowRight for consistent navigation across grapheme clusters (like emojis) on firefox
// Handle ArrowRight for consistent navigation across grapheme clusters (like emojis) on Firefox

@@ -526,6 +528,15 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
onKeyPress(event);
}

// Handle ArrowRight for consistent navigation across grapheme clusters (like emojis) on firefox
if (e.key === 'ArrowRight' && BrowserUtils.isFirefox && !nativeEvent.altKey) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (e.key === 'ArrowRight' && BrowserUtils.isFirefox && !nativeEvent.altKey) {
if (BrowserUtils.isFirefox && e.key === 'ArrowRight' && !nativeEvent.altKey) {

describe('handleFirefoxArrowKeyNavigation', () => {
beforeEach(() => {
jest.clearAllMocks();
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add a newline here

Suggested change
});
});

Comment on lines +13 to +14
const segmenter = new Intl.Segmenter('en', {granularity: 'grapheme'});
const graphemes = Array.from(segmenter.segment(text));
Copy link
Collaborator

Choose a reason for hiding this comment

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

How long does this step take for longer values (approx 5000 characters)?

Can we somehow avoid allocating an array of graphemes and use the returned iterable instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For inputs ok ~ 0.5 M characters the whole handleFirefoxRightArrowKeyNavigation takes 40 - 60 ms. With iterating directly for (const {index, segment} of segmenter.segment(text)) that time is 15 - 30ms.

For input size of ~ 50K it's 10-20ms for Array and 8 - 15 ms

Does not seem like much, but I can change it

@jmusial jmusial marked this pull request as ready for review May 27, 2025 14:46
Copy link
Contributor Author

@jmusial jmusial left a comment

Choose a reason for hiding this comment

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

While testing noticed that the issue is also present for left arrow when navigating backwards and /or with multiple emojis. Added code and test cases to cover that.

Comment on lines +13 to +14
const segmenter = new Intl.Segmenter('en', {granularity: 'grapheme'});
const graphemes = Array.from(segmenter.segment(text));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For inputs ok ~ 0.5 M characters the whole handleFirefoxRightArrowKeyNavigation takes 40 - 60 ms. With iterating directly for (const {index, segment} of segmenter.segment(text)) that time is 15 - 30ms.

For input size of ~ 50K it's 10-20ms for Array and 8 - 15 ms

Does not seem like much, but I can change it

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.

2 participants