Repository navigation
markdown: preserve preview scroll position across tab switches - #342822
Open
Midhun K M (StarkGang) wants to merge 2 commits into
Open
Midhun K M (StarkGang) wants to merge 2 commits into
Midhun K M (StarkGang) wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟡 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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.tskeeps two different meanings in the singlestate.linefield:settings.settings.line, baked intodata-settingsat render time), andstate.lineon every scroll event and persists withvscode.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:
originalState.lineis the live scroll line, so as soon as the user scrolled away from the opening line the comparison was always true,state.scrollProgresswas dropped, and the preview came back at the top or at the opening line instead of where the reader left off.Changes
startingLineto the preview webview state.Review guide
The intended invariant is:
markdown.preview.scrollEditorWithEditor): an explicit line navigation still wins over stale scroll progress.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— passednpx tsc --project extensions/markdown-language-features/preview-src/tsconfig.json --noEmit— passednpm run eslint -- extensions/markdown-language-features/preview-src/index.ts— passednpm run precommit -- extensions/markdown-language-features/preview-src/index.ts— passed#fragmentlink, scroll away, switch tabs and back — the fragment still wins```