未验证 提交 50fe9db9 编写于 作者: M Matt Bierner 提交者: GitHub

Merge pull request #79450 from txava/master

Fix for #78465: Markdown Preview scroll remains same after clicking on some othe…
......@@ -6,7 +6,7 @@
import { ActiveLineMarker } from './activeLineMarker';
import { onceDocumentLoaded } from './events';
import { createPosterForVsCode } from './messaging';
import { getEditorLineNumberForPageOffset, scrollToRevealSourceLine } from './scroll-sync';
import { getEditorLineNumberForPageOffset, scrollToRevealSourceLine, getLineElementForFragment } from './scroll-sync';
import { getSettings, getData } from './settings';
import throttle = require('lodash.throttle');
......@@ -19,7 +19,7 @@ const settings = getSettings();
const vscode = acquireVsCodeApi();
// Set VS Code state
let state = getData<{ line: number }>('data-state');
let state = getData<{ line: number, fragment: string }>('data-state');
vscode.setState(state);
const messaging = createPosterForVsCode(vscode);
......@@ -34,10 +34,19 @@ window.onload = () => {
onceDocumentLoaded(() => {
if (settings.scrollPreviewWithEditor) {
setTimeout(() => {
const initialLine = +settings.line;
if (!isNaN(initialLine)) {
scrollDisabled = true;
scrollToRevealSourceLine(initialLine);
// Try to scroll to fragment if available
if (state.fragment) {
const element = getLineElementForFragment(state.fragment);
if (element) {
scrollDisabled = true;
scrollToRevealSourceLine(element.line);
}
} else {
const initialLine = +settings.line;
if (!isNaN(initialLine)) {
scrollDisabled = true;
scrollToRevealSourceLine(initialLine);
}
}
}, 0);
}
......@@ -161,4 +170,4 @@ if (settings.scrollEditorWithPreview) {
function escapeRegExp(text: string) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
\ No newline at end of file
}
......@@ -134,3 +134,12 @@ export function getEditorLineNumberForPageOffset(offset: number) {
}
return null;
}
/**
* Try to find the html element by using a fragment id
*/
export function getLineElementForFragment(fragment: string): CodeLineElement | undefined {
return getCodeLineElements().find((element) => {
return element.element.id === fragment;
});
}
......@@ -89,6 +89,7 @@ export class MarkdownPreview extends Disposable {
private isScrolling = false;
private _disposed: boolean = false;
private imageInfo: { id: string, width: number, height: number }[] = [];
private scrollToFragment: string | undefined;
public static async revive(
webview: vscode.WebviewPanel,
......@@ -264,7 +265,8 @@ export class MarkdownPreview extends Disposable {
locked: this._locked,
line: this.line,
resourceColumn: this.resourceColumn,
imageInfo: this.imageInfo
imageInfo: this.imageInfo,
fragment: this.scrollToFragment
};
}
......@@ -284,6 +286,8 @@ export class MarkdownPreview extends Disposable {
public update(resource: vscode.Uri) {
const editor = vscode.window.activeTextEditor;
// Reposition scroll preview, position scroll to the top if active text editor
// doesn't corresponds with preview
if (editor && editor.document.uri.fsPath === resource.fsPath) {
this.line = getVisibleLine(editor);
} else {
......@@ -522,11 +526,15 @@ export class MarkdownPreview extends Disposable {
}
private async onDidClickPreviewLink(path: string, fragment: string | undefined) {
this.scrollToFragment = undefined;
const config = vscode.workspace.getConfiguration('markdown', this.resource);
const openLinks = config.get<string>('preview.openMarkdownLinks', 'inPreview');
if (openLinks === 'inPreview') {
const markdownLink = await resolveLinkToMarkdownFile(path);
if (markdownLink) {
if (fragment) {
this.scrollToFragment = fragment;
}
this.update(markdownLink);
return;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册