Skip to content

markdown: preserve preview scroll position across tab switches - #342822

Open
Midhun K M (StarkGang) wants to merge 2 commits into
microsoft:mainfrom
StarkGang:fix/preserve-markdown-preview-scroll-on-tab-switch
Open

Midhun K M (StarkGang) wants to merge 2 commits into
microsoft:mainfrom
StarkGang:fix/preserve-markdown-preview-scroll-on-tab-switch

Conversation

@StarkGang

Copy link
Copy Markdown

Summary

Fixes the Markdown preview losing its scroll position when the preview tab is hidden and shown again. The preview webview now distinguishes the line the preview was opened at from the line the user has scrolled to, so that persisted scroll state is only discarded when the preview is actually navigated somewhere else.

Related issues: #317033, #339795.
Scope: this PR only covers the scroll position of the preview. The stale content after an edit plus a tab switch is tracked separately in #334681.

Root cause

extensions/markdown-language-features/preview-src/index.ts keeps two different meanings in the single state.line field:

  • the line the preview was opened at (settings.settings.line, baked into data-settings at render time), and
  • the live scroll line, which the webview writes back into state.line on every scroll event and persists with vscode.setState.

When the preview tab is hidden, VS Code discards the webview and rebuilds it from the last rendered HTML. On rebuild, the persisted state was compared against the baked starting line:

hasStartingLine && originalState.line !== settings.settings.line

originalState.line is the live scroll line, so as soon as the user scrolled away from the opening line the comparison was always true, state.scrollProgress was dropped, and the preview came back at the top or at the opening line instead of where the reader left off.

Changes

  • Track the opening line separately from the live scroll line by adding startingLine to the preview webview state.
  • Compare the persisted opening line against the current opening line when deciding whether the persisted scroll progress is still valid, instead of comparing the live scroll line.
  • Restore the persisted scroll line when the webview is re-created for the same resource, so the preview keeps its reading position.
  • Keep the existing behavior for explicit line and fragment navigation, and for switching the preview to a different document: the scroll progress is still reset there.

Review guide

The intended invariant is:

  • Hide and reveal the same preview: the persisted scroll progress survives, and the preview returns to the same position.
  • Preview opened at an explicit line (for example "Open Preview" from a search result, or markdown.preview.scrollEditorWithEditor): an explicit line navigation still wins over stale scroll progress.
  • Preview switched to a different document: the scroll progress is reset and the new document starts at the top.
  • Preview opened at a fragment: the fragment still wins over the scroll progress.
  • Webview state restored after a window reload: the preview reopens at the line the reader was on.

The change is confined to extensions/markdown-language-features/preview-src/index.ts, in the state restore block that runs before the initial content is rendered.

Validation

  • npm run build-webview --prefix .\extensions\markdown-language-features — passed
  • npx tsc --project extensions/markdown-language-features/preview-src/tsconfig.json --noEmit — passed
  • npm run eslint -- extensions/markdown-language-features/preview-src/index.ts — passed
  • npm run precommit -- extensions/markdown-language-features/preview-src/index.ts — passed
  • Manual, Code OSS from source: open a long Markdown file, scroll the preview down, switch to another tab, switch back — the preview keeps its scroll position
  • Manual: scroll the preview, then use "Open Preview" on a search result at a specific line — the preview opens at that line and not at the previous scroll position
  • Manual: scroll the preview, then follow a link to a different Markdown file — the new file starts at the top
  • Manual: open a Markdown file via a #fragment link, scroll away, switch tabs and back — the fragment still wins```

Copilot AI balanced review requested due to automatic review settings October 20, 2026 08:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A fresh render targeting the original opening line can incorrectly restore stale scroll progress.

1 open issue
What changed in this PR

Preserves Markdown preview reading position across tab switches, separate from the stale-content work in #334681.

Changes:

  • Tracks the opening line separately from the live scroll line.
  • Updates scroll-state validation and same-resource restoration.
File Description
extensions/​markdown-language-features/​preview-src/​index.ts Separates opening-line state from persisted reading position.

Review effort: Balanced

This branch has not been deployed

No deployments
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.

2 participants