Automattic / Automattic/harper
UX Improvement: Maintain cursor position after shortcut-based correction
- Dominant language
- Rust
- Stars
- 15.4k
- Forks
- 627
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 106
Description
First, thank you for the incredible work on Harper—it’s the fastest, most reliable local grammar checker I’ve used.
Currently, there is a friction point in the user experience: When using the key combination to correct the last misspelled word, the correction is successful, but the text cursor (caret) automatically snaps to the end of the corrected word.
If a user has already continued typing a sentence, this "jump" breaks their flow and requires them to manually click or arrow back to where they were.
Expected Behavior
The cursor should remain at its current typing position regardless of where the background correction took place.
Implementation Logic for the Fix
To resolve this, the extension needs to calculate the length difference between the original and corrected word and apply that offset to the selection range.
For Standard Inputs (, ):
const currentPos = element.selectionStart;
const lengthDiff = newWord.length - oldWord.length;
// Existing replacement logic here...
// Only offset if the cursor was ahead of the correction
const newPos = currentPos > wordStartIndex ? currentPos + lengthDiff : currentPos;
element.setSelectionRange(newPos, newPos);
For Rich Text/Browser DOM (contenteditable):
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const originalOffset = range.startOffset;
// Existing replacement logic here...
const newRange = document.createRange();
const lengthDiff = newWord.length - oldWord.length;
// Restore the relative position
newRange.setStart(range.startContainer, originalOffset + lengthDiff);
newRange.collapse(true);
selection.removeAllRanges();
selection.addRange(newRange);
Impact
This fix will make the "Correct Last Word" shortcut feel seamless and "invisible," allowing users to fix errors without ever stopping their typing momentum.
Contributor guide
Research direction
Start at the shortcut-based correction and replacement path for standard inputs and contenteditable elements, then inspect how selection positions and ranges are handled. Reproduce a correction while typing ahead of it and verify that the caret remains at its prior typing position for both standard and rich-text cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend, web-dev
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100