Skip to content

fix(frontend): prevent markdown editor from scrolling to bottom on Enter key#3843

Draft
mmabrouk wants to merge 1 commit intomainfrom
fix/markdown-editor-scroll-on-enter
Draft

fix(frontend): prevent markdown editor from scrolling to bottom on Enter key#3843
mmabrouk wants to merge 1 commit intomainfrom
fix/markdown-editor-scroll-on-enter

Conversation

@mmabrouk
Copy link
Member

@mmabrouk mmabrouk commented Feb 26, 2026

Summary

Fixes a bug where pressing Enter in the markdown editing mode of the prompt editor causes it to scroll to the bottom of the text, forcing the user to re-scroll after every newline.

Root Cause

Two issues in the KEY_ENTER_COMMAND handler in markdownPlugin.tsx:

  1. Scroll jump from insertRawText("\n") — Lexical reconciles the DOM and sets the browser selection at the new cursor position, which triggers the browser's native scroll-into-view behavior on the caret.

  2. Command handler always returned true — The handler unconditionally claimed to handle the Enter key even when the cursor was NOT in a markdown CodeNode, preventing Lexical's default rich-text Enter behavior from running in non-markdown mode.

  3. Merge-back update listener — A registerUpdateListener that merges trailing nodes back into the CodeNode runs a second editor.update() cycle, causing an additional scroll jump after the first restore.

Fix

  • Use a read-only check (editor.getEditorState().read()) to determine if we're in a markdown CodeNode before entering the update
  • Return false early when not in markdown mode, so Lexical's default Enter handling works normally
  • Save scroll position before the insertion, flush DOM synchronously with {discrete: true}, then restore via requestAnimationFrame
  • Add the same scroll save/restore protection to the merge-back registerUpdateListener

Files Changed

  • web/packages/agenta-ui/src/Editor/plugins/markdown/markdownPlugin.tsx
  • web/oss/src/components/Editor/plugins/markdown/markdownPlugin.tsx

Open with Devin

…ter key

Two issues fixed in the KEY_ENTER_COMMAND handler in markdownPlugin:

1. insertRawText("\n") causes Lexical to reconcile the DOM and set the
   browser selection at the new cursor position, which triggers the
   browser's native scroll-into-view behavior. Fix: save scrollTop
   before the insertion and restore it via requestAnimationFrame after
   both the discrete update and the merge-back update listener.

2. The command handler unconditionally returned true, claiming to handle
   the Enter key even when the cursor was not in a markdown code node.
   This prevented Lexical's default rich-text Enter behavior from
   running. Fix: use a read-only check first and return false early
   when not in a markdown CodeNode.

Also adds scroll protection to the registerUpdateListener that merges
trailing nodes back into the markdown CodeNode, since that second
editor.update() can trigger its own scroll jump.
@vercel
Copy link

vercel bot commented Feb 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agenta-documentation Building Building Preview, Comment Feb 26, 2026 5:13pm

Request Review

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Feb 26, 2026
@dosubot dosubot bot added bug Something isn't working Frontend labels Feb 26, 2026
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 4 additional findings.

Open in Devin Review

@github-actions
Copy link
Contributor

github-actions bot commented Feb 26, 2026

Railway Preview Environment

Status Destroyed (PR converted to draft)

Updated at 2026-02-26T18:34:35.088Z

@mmabrouk mmabrouk marked this pull request as draft February 26, 2026 18:34
@mmabrouk mmabrouk marked this pull request as draft February 26, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Frontend size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant