Fix cursor jumping to position 0 after Escape closes link popover#118
Merged
Fix cursor jumping to position 0 after Escape closes link popover#118
Conversation
Gutenberg's rich-text onFocus handler calls applyRecord with domOnly:true when the contenteditable regains focus after the link popover closes, skipping DOM selection restoration. The browser defaults the cursor to position 0 if the selection wasn't preserved during the focus transfer. Because Press This uses a controlled BlockEditorProvider that doesn't sync rich-text cursor positions to the block-editor store, the fix works entirely at the DOM level: it tracks the character offset of the caret via the selectionchange event and restores it after a rAF when Escape is pressed inside the link control popover. Fixes #116
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a DOM-level workaround in the Press This editor to prevent the caret from jumping to position 0 after pressing Escape to close the Gutenberg link popover (Ctrl/Cmd+K flow), addressing issue #116.
Changes:
- Adds a new
LinkEscapeFixcomponent that tracks caret character offsets viaselectionchangeand restores the caret after Escape closes the link control. - Renders
LinkEscapeFixwithin the Press This editor content container so it’s scoped to the editor instance.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/components/PressThisEditor.js | Imports and mounts the new workaround component in the editor content tree. |
| src/components/LinkEscapeFix.js | Implements caret offset tracking + restoration around Escape closing the link popover. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
LinkEscapeFixcomponent that works around a Gutenberg bug where pressing Escape to close the link popover (Cmd+K / Ctrl+K) causes the cursor to jump to position 0selectionchangeevents and restores it afterrequestAnimationFramewhen Escape is pressed inside.block-editor-link-controlBlockEditorProviderdoesn't sync rich-text cursor positions to the block-editor store.press-this-editor__contentto avoid interfering with other contenteditables (e.g. title field)Test plan
Fixes #116