Skip to content

fix: auto scroll to top in code block - #304

Merged
appflowy merged 3 commits into
mainfrom
fix_auto_scroll_300
Apr 4, 2026
Merged

fix: auto scroll to top in code block#304
appflowy merged 3 commits into
mainfrom
fix_auto_scroll_300

Conversation

@appflowy

@appflowy appflowy commented Apr 4, 2026

Copy link
Copy Markdown
Contributor

fix #300 #261

Summary by Sourcery

Prevent editor selection from causing unwanted scroll-to-top jumps in code blocks by customizing the selection scroll behavior.

Bug Fixes:

  • Avoid page jumping to the top when the caret moves inside syntax-highlighted code blocks by only scrolling when the cursor is out of view.

Tests:

  • Add Playwright end-to-end coverage to assert stable scroll behavior when editing within code blocks.

@sourcery-ai

sourcery-ai Bot commented Apr 4, 2026

Copy link
Copy Markdown

Reviewer's Guide

Replaces Slate's default scrollSelectionIntoView behavior with a custom implementation that avoids jump-to-top scrolling in code blocks and wires it into the editor, along with an end-to-end Playwright test for the regression.

Sequence diagram for custom scrollSelectionIntoView behavior

sequenceDiagram
  actor User
  participant EditorEditable
  participant SlateEditable
  participant ReactEditor
  participant scrollSelectionIntoView
  participant DOMRange
  participant ScrollContainer

  User->>EditorEditable: Type in code block
  EditorEditable->>SlateEditable: Render with scrollSelectionIntoView prop
  SlateEditable->>ReactEditor: Update selection
  SlateEditable->>DOMRange: Compute DOM range for selection
  SlateEditable->>scrollSelectionIntoView: scrollSelectionIntoView(editor, domRange)

  scrollSelectionIntoView->>DOMRange: getBoundingClientRect()
  scrollSelectionIntoView->>scrollSelectionIntoView: Validate rangeRect and selection
  scrollSelectionIntoView->>DOMRange: Read startContainer.parentElement as leafEl
  scrollSelectionIntoView->>ScrollContainer: leafEl.closest(appflowy-scroll-container)
  alt Cursor already visible
    scrollSelectionIntoView-->>SlateEditable: Return without scrolling
  else Cursor out of view
    scrollSelectionIntoView->>leafEl: Override getBoundingClientRect
    scrollSelectionIntoView->>leafEl: scrollIntoView(block=nearest)
    scrollSelectionIntoView->>leafEl: Restore getBoundingClientRect
    scrollSelectionIntoView-->>SlateEditable: Return after minimal scroll
  end
Loading

File-Level Changes

Change Details Files
Introduce custom scrollSelectionIntoView to prevent jump-to-top behavior when the cursor is in a code block.
  • Add a scrollSelectionIntoView helper that short-circuits when selection is not collapsed or when the DOM range has an invalid bounding rect.
  • Compute the cursor range rect and the nearest scroll container with the .appflowy-scroll-container selector.
  • Skip scrolling when the cursor is already within the visible vertical bounds of the scroll container.
  • Temporarily override the leaf element’s getBoundingClientRect with the range’s implementation and call scrollIntoView with block: 'nearest', then restore the original behavior.
src/components/editor/Editable.tsx
Wire the custom scrollSelectionIntoView into the Slate Editable component and add e2e coverage for the regression.
  • Pass the custom scrollSelectionIntoView function as a prop to the EditorEditable’s underlying Slate Editable component.
  • Add a Playwright E2E test that exercises code-block editing and verifies the editor no longer auto-scrolls to the top when syntax highlighting or DOM re-renders occur.
src/components/editor/Editable.tsx
playwright/e2e/editor/blocks/code_block_scroll.spec.ts

Assessment against linked issues

Issue Objective Addressed Explanation
#300 Prevent the page from auto-scrolling to the top when pressing Enter inside a code block located lower on the page, keeping the cursor movement within the code block and the current scroll position stable.
#300 Add an automated test to prevent regression of the scroll-jump behavior when editing code blocks.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The temporary monkey-patching of leafEl.getBoundingClientRect would be safer if wrapped in a try/finally (or similar guard) so the override is always cleaned up even if scrollIntoView throws, rather than relying on the final delete line executing normally.
  • The use of a hard-coded .appflowy-scroll-container selector in scrollSelectionIntoView could be brittle; consider making the scroll container lookup more generic or configurable so the behavior doesn’t silently break if the container class changes or if the editor is rendered in a different scrollable context.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The temporary monkey-patching of `leafEl.getBoundingClientRect` would be safer if wrapped in a try/finally (or similar guard) so the override is always cleaned up even if `scrollIntoView` throws, rather than relying on the final `delete` line executing normally.
- The use of a hard-coded `.appflowy-scroll-container` selector in `scrollSelectionIntoView` could be brittle; consider making the scroll container lookup more generic or configurable so the behavior doesn’t silently break if the container class changes or if the editor is rendered in a different scrollable context.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@appflowy
appflowy merged commit 026cbfb into main Apr 4, 2026
12 of 13 checks passed
@appflowy
appflowy deleted the fix_auto_scroll_300 branch April 4, 2026 11:17
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.

[Bug] Page scroll jumps to top when pressing Enter inside a lower code block

1 participant