Skip to content

Commit 1379bf3

Browse files
authored
Fix markdown webview state updates (microsoft#178153)
Fixes microsoft#164071 Also fixes the webview not scrolling when the resources changes
1 parent eb5dc01 commit 1379bf3

File tree

1 file changed

+6
-5
lines changed
  • extensions/markdown-language-features/preview-src

1 file changed

+6
-5
lines changed

extensions/markdown-language-features/preview-src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ let documentResource = settings.settings.source;
2323
const vscode = acquireVsCodeApi();
2424

2525
const originalState = vscode.getState() ?? {} as any;
26-
2726
const state = {
28-
originalState,
27+
...originalState,
2928
...getData<any>('data-state')
3029
};
3130

32-
if (originalState?.resource !== state.resource) {
33-
state.scrollProgress = undefined;
31+
if (typeof originalState.scrollProgress !== 'undefined' && originalState?.resource !== state.resource) {
32+
state.scrollProgress = 0;
3433
}
3534

3635
// Make sure to sync VS Code state here
@@ -67,7 +66,9 @@ onceDocumentLoaded(() => {
6766
if (typeof scrollProgress === 'number' && !settings.settings.fragment) {
6867
doAfterImagesLoaded(() => {
6968
scrollDisabledCount += 1;
70-
window.scrollTo(0, scrollProgress * document.body.clientHeight);
69+
// Always set scroll of at least 1 to prevent VS Code's webview code from auto scrolling us
70+
const scrollToY = Math.max(1, scrollProgress * document.body.clientHeight);
71+
window.scrollTo(0, scrollToY);
7172
});
7273
return;
7374
}

0 commit comments

Comments
 (0)