Skip to content

Commit ef698fa

Browse files
committed
Make markdown refresh more stable
Fixes microsoft#80680 - Always sync the current preview line number with the editor, even when `scrollEditorWithPreview` is false - If the md file is focused and refresh is called, do not try resetting the current line to match the editor file. This mainly effects the case where `scrollEditorWithPreview` is false
1 parent d4e4956 commit ef698fa

3 files changed

Lines changed: 39 additions & 35 deletions

File tree

extensions/markdown-language-features/media/index.js

Lines changed: 13 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,18 @@ document.addEventListener('click', event => {
157157
}
158158
}, true);
159159

160-
if (settings.scrollEditorWithPreview) {
161-
window.addEventListener('scroll', throttle(() => {
162-
if (scrollDisabled) {
163-
scrollDisabled = false;
164-
} else {
165-
const line = getEditorLineNumberForPageOffset(window.scrollY);
166-
if (typeof line === 'number' && !isNaN(line)) {
167-
messaging.postMessage('revealLine', { line });
168-
state.line = line;
169-
vscode.setState(state);
170-
}
160+
window.addEventListener('scroll', throttle(() => {
161+
if (scrollDisabled) {
162+
scrollDisabled = false;
163+
} else {
164+
const line = getEditorLineNumberForPageOffset(window.scrollY);
165+
if (typeof line === 'number' && !isNaN(line)) {
166+
messaging.postMessage('revealLine', { line });
167+
state.line = line;
168+
vscode.setState(state);
171169
}
172-
}, 50));
173-
}
170+
}
171+
}, 50));
174172

175173
function escapeRegExp(text: string) {
176174
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,17 @@ export class MarkdownPreview extends Disposable {
284284
super.dispose();
285285
}
286286

287-
public update(resource: vscode.Uri) {
288-
const editor = vscode.window.activeTextEditor;
287+
public update(resource: vscode.Uri, isRefresh = true) {
289288
// Reposition scroll preview, position scroll to the top if active text editor
290289
// doesn't corresponds with preview
290+
const editor = vscode.window.activeTextEditor;
291291
if (editor) {
292-
if (editor.document.uri.fsPath === resource.fsPath) {
293-
this.line = getVisibleLine(editor);
294-
} else {
295-
this.line = 0;
292+
if (!isRefresh || this._previewConfigurations.loadAndCacheConfiguration(this._resource).scrollEditorWithPreview) {
293+
if (editor.document.uri.fsPath === resource.fsPath) {
294+
this.line = getVisibleLine(editor);
295+
} else {
296+
this.line = 0;
297+
}
296298
}
297299
}
298300

@@ -320,7 +322,7 @@ export class MarkdownPreview extends Disposable {
320322

321323
public refresh() {
322324
this.forceUpdate = true;
323-
this.update(this._resource);
325+
this.update(this._resource, true);
324326
}
325327

326328
public updateConfiguration() {
@@ -484,6 +486,12 @@ export class MarkdownPreview extends Disposable {
484486

485487
private onDidScrollPreview(line: number) {
486488
this.line = line;
489+
490+
const config = this._previewConfigurations.loadAndCacheConfiguration(this._resource);
491+
if (!config.scrollEditorWithPreview) {
492+
return;
493+
}
494+
487495
for (const editor of vscode.window.visibleTextEditors) {
488496
if (!this.isPreviewOf(editor.document.uri)) {
489497
continue;

0 commit comments

Comments
 (0)