Skip to content

Fix cursor jumping to position 0 after Escape closes link popover#118

Merged
kraftbj merged 2 commits intotrunkfrom
escape-link-focus-loss
Apr 16, 2026
Merged

Fix cursor jumping to position 0 after Escape closes link popover#118
kraftbj merged 2 commits intotrunkfrom
escape-link-focus-loss

Conversation

@kraftbj
Copy link
Copy Markdown
Collaborator

@kraftbj kraftbj commented Apr 3, 2026

Summary

  • Adds LinkEscapeFix component 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 0
  • Tracks the caret's character offset via selectionchange events and restores it after requestAnimationFrame when Escape is pressed inside .block-editor-link-control
  • Works entirely at the DOM level because Press This's controlled BlockEditorProvider doesn't sync rich-text cursor positions to the block-editor store
  • Scoped to the editor instance via .press-this-editor__content to avoid interfering with other contenteditables (e.g. title field)
  • Harmless no-op once the upstream Gutenberg bug is fixed (restores cursor to position N when it's already at N)

Test plan

  • Open Press This, type some text (e.g. "hello world linkword goodbye")
  • Select a word, press Cmd+K (or Ctrl+K), enter a URL, press Enter
  • Press Escape to close the link popover
  • Verify cursor stays at the end of the linked word, not at position 0
  • Type a character to confirm it appears at the correct position
  • Repeat using the toolbar Link button instead of Cmd+K
  • Press Escape without entering a URL — cursor should not jump
  • Press Escape in other contexts (block inserter, sidebar panels) — should not interfere

Fixes #116

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
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 LinkEscapeFix component that tracks caret character offsets via selectionchange and restores the caret after Escape closes the link control.
  • Renders LinkEscapeFix within 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.

Comment thread src/components/LinkEscapeFix.js
Comment thread src/components/LinkEscapeFix.js
Comment thread src/components/LinkEscapeFix.js
Comment thread src/components/LinkEscapeFix.js
Comment thread src/components/LinkEscapeFix.js
@kraftbj kraftbj added this pull request to the merge queue Apr 16, 2026
Merged via the queue into trunk with commit 7caa33a Apr 16, 2026
6 checks passed
@kraftbj kraftbj deleted the escape-link-focus-loss branch April 16, 2026 19:16
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.

After adding link (with ctrl+k) and pressing escape, cursor goes to first character position / top left in window

2 participants