Skip to content

fix(editor): restore Mermaid reading position after rendering - #35031

Merged
OrcaWin merged 3 commits into
mainfrom
OrcaWin/editor-mermaid-delayed-restore
Oct 29, 2026
Merged

OrcaWin merged 3 commits into
mainfrom
OrcaWin/editor-mermaid-delayed-restore

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​124 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​124
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​61 $\color{#cf222e}{\Huge{\mathbf{−}}}$​25 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​36

Problem

MermaidViewer restores the cached diagram scroll position by retrying for up to 30 animation frames (about 500 ms), then gives up. Mermaid renders run through one serialized global queue that is shared with Markdown preview and chat diagrams, and the first render also waits for the lazy mermaid import. A large or queued diagram can finish after that window, so the viewer reopens at the top. When the workspace is hidden (zero-height viewport) the retries always fail, so the position is also lost on return.

Fix

  • MermaidBlock takes an optional onRenderComplete callback, fired from a layout effect once the current source has rendered (success or error).
  • MermaidViewer keeps the cached target pending and applies it once, clamped, when the viewport is measurable and either the render is complete or the content is already tall enough. A temporary ResizeObserver covers a render that finished while the viewport was hidden.
  • The restore keys on the same trimmed source passed to MermaidBlock, so whitespace-only edits cannot leave a restore pending without a render to complete it.
  • User scroll intent (wheel, pointerdown, touchstart, keydown) cancels a pending restore, so a late render or resize never overrides the user's own scrolling.

The renderer-local, in-memory cache key is unchanged. Other MermaidBlock callers don't pass the new prop, so they are unaffected.

Tests

MermaidViewer.test.tsx has two regression tests. Both fail on main and assert only the viewer's scrollTop:

  • A render that resolves after the old retry window restores the cached position.
  • A render that completes while the viewport is hidden restores once the viewport becomes measurable.

pnpm check:changed --maxWorkers=2 passes.

@OrcaWin
OrcaWin force-pushed the OrcaWin/editor-mermaid-delayed-restore branch from d24a08a to 7070888 Compare October 28, 2026 23:48
@OrcaWin
OrcaWin marked this pull request as ready for review October 29, 2026 00:08
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 10 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0a0d3b49-1475-462a-b359-9b259af902b3
Commits

Reviewing files that changed from the base of the PR and between 28b9049 and d0682d3.

Files selected for processing: 3
  • src/renderer/src/components/editor/MermaidBlock.tsx
  • src/renderer/src/components/editor/MermaidViewer.test.tsx
  • src/renderer/src/components/editor/MermaidViewer.tsx
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; the remaining feedback is a non-blocking request for a cancellation test.

Findings

  1. P2 User cancellation lacks a test ▶

Summary

Restores Mermaid reading positions after slow renders and after hidden workspaces become visible.

  • Mermaid file views restore their saved reading position when the diagram is ready.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Load saved position] --> B[Keep restore pending]
  B --> C{Viewport measurable?}
  C -->|No| D[Wait for resize]
  D --> C
  C -->|Yes| E{Render complete or content tall enough?}
  E -->|No| F[Wait for render completion]
  F --> C
  E -->|Yes| G[Restore clamped position]
  G --> H[Clear pending restore]
  B --> I[User input]
  I --> H
Loading

Reviews (2) · Last reviewed commit: "fix(editor): key Mermaid restore on rend..." · Reviewed by Greptile

Comment on lines +119 to +122
visible = true
act(() => resizes.forEach((callback) => callback()))

expect(viewer.scrollTop).toBe(600)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 User cancellation lacks a test

Both tests cover successful restoration, but neither checks that user input cancels it. Add a test that starts with a pending restore, dispatches a user scroll event, then completes rendering and fires the resize callback. Assert that neither callback restores the cached position. Otherwise, a change that breaks cancellation could pass both tests and let a late render override the reader's choice.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@OrcaWin
OrcaWin force-pushed the OrcaWin/editor-mermaid-delayed-restore branch from 7070888 to d0682d3 Compare October 29, 2026 00:38
@OrcaWin
OrcaWin merged commit 769d382 into main Oct 29, 2026
35 checks passed
@Pr1p Pr1p mentioned this pull request Oct 30, 2026
7 of 9 tasks
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.

1 participant