mirror of
https://github.com/microsoft/vscode.git
synced 2026-07-09 20:37:13 -05:00
Fix some bugs around preserving location when switching current tab to md preview
This commit is contained in:
@@ -38,8 +38,10 @@ const state: State = {
|
||||
...getData<Partial<State>>('data-state')
|
||||
};
|
||||
|
||||
if (typeof originalState.scrollProgress !== 'undefined' && originalState?.resource !== state.resource) {
|
||||
state.scrollProgress = 0;
|
||||
const hasStartingLine = typeof settings.settings.line === 'number' && !isNaN(settings.settings.line);
|
||||
if (typeof originalState.scrollProgress !== 'undefined'
|
||||
&& (originalState?.resource !== state.resource || (hasStartingLine && originalState.line !== settings.settings.line))) {
|
||||
state.scrollProgress = undefined;
|
||||
}
|
||||
|
||||
// Make sure to sync VS Code state here
|
||||
@@ -419,6 +421,8 @@ window.addEventListener('scroll', throttle(() => {
|
||||
|
||||
const line = getEditorLineNumberForPageOffset(window.scrollY, documentVersion);
|
||||
if (typeof line === 'number' && !isNaN(line)) {
|
||||
state.line = line;
|
||||
vscode.setState(state);
|
||||
messaging.postMessage('revealLine', { line });
|
||||
}
|
||||
}, 50));
|
||||
|
||||
@@ -537,7 +537,7 @@ export class StaticMarkdownPreview extends Disposable implements IManagedMarkdow
|
||||
this.#webviewPanel = webviewPanel;
|
||||
this.#previewConfigurations = previewConfigurations;
|
||||
|
||||
const topScrollLocation = scrollLine ? new StartingScrollLine(scrollLine) : undefined;
|
||||
const topScrollLocation = typeof scrollLine === 'number' ? new StartingScrollLine(scrollLine) : undefined;
|
||||
this.#preview = this._register(new MarkdownPreview(this.#webviewPanel, resource, topScrollLocation, {
|
||||
getAdditionalState: () => { return {}; },
|
||||
getLineChanges,
|
||||
|
||||
@@ -13,8 +13,8 @@ import { MdDocumentRenderer } from './documentRenderer';
|
||||
import { MarkdownPreviewLineDiffProvider } from './lineDiff';
|
||||
import { DynamicMarkdownPreview, IManagedMarkdownPreview, StaticMarkdownPreview } from './preview';
|
||||
import { MarkdownPreviewConfigurationManager } from './previewConfig';
|
||||
import { scrollEditorToLine, StartingScrollFragment } from './scrolling';
|
||||
import { TopmostLineMonitor } from './topmostLineMonitor';
|
||||
import { scrollEditorToLine, StartingScrollFragment, StartingScrollLine, StartingScrollLocation } from './scrolling';
|
||||
import { getVisibleLine, TopmostLineMonitor } from './topmostLineMonitor';
|
||||
import type { MarkdownPreviewLineChanges } from '../../types/previewMessaging';
|
||||
|
||||
|
||||
@@ -138,16 +138,17 @@ export class MarkdownPreviewManager extends Disposable implements vscode.Webview
|
||||
resource: vscode.Uri,
|
||||
settings: DynamicPreviewSettings
|
||||
): void {
|
||||
const scrollLocation = resource.fragment ? new StartingScrollFragment(resource.fragment) : this.#getActiveTextEditorScrollLocation(resource);
|
||||
let preview = this.#dynamicPreviews.get(resource, settings);
|
||||
if (preview) {
|
||||
preview.reveal(settings.previewColumn);
|
||||
} else {
|
||||
preview = this.#createNewDynamicPreview(resource, settings);
|
||||
preview = this.#createNewDynamicPreview(resource, settings, scrollLocation);
|
||||
}
|
||||
|
||||
preview.update(
|
||||
resource,
|
||||
resource.fragment ? new StartingScrollFragment(resource.fragment) : undefined
|
||||
scrollLocation
|
||||
);
|
||||
}
|
||||
|
||||
@@ -278,7 +279,7 @@ export class MarkdownPreviewManager extends Disposable implements vscode.Webview
|
||||
webview: vscode.WebviewPanel,
|
||||
getLineChanges?: () => MarkdownPreviewLineChanges | Promise<MarkdownPreviewLineChanges | undefined> | undefined,
|
||||
): StaticMarkdownPreview {
|
||||
const lineNumber = this.#topmostLineMonitor.getPreviousStaticTextEditorLineByUri(document.uri);
|
||||
const lineNumber = this.#topmostLineMonitor.getPreviousTextEditorLineByUri(document.uri);
|
||||
const preview = StaticMarkdownPreview.revive(
|
||||
document.uri,
|
||||
webview,
|
||||
@@ -383,16 +384,15 @@ export class MarkdownPreviewManager extends Disposable implements vscode.Webview
|
||||
|
||||
#createNewDynamicPreview(
|
||||
resource: vscode.Uri,
|
||||
previewSettings: DynamicPreviewSettings
|
||||
previewSettings: DynamicPreviewSettings,
|
||||
scrollLocation: StartingScrollLocation | undefined,
|
||||
): DynamicMarkdownPreview {
|
||||
const activeTextEditorURI = vscode.window.activeTextEditor?.document.uri;
|
||||
const scrollLine = (activeTextEditorURI?.toString() === resource.toString()) ? vscode.window.activeTextEditor?.visibleRanges[0].start.line : undefined;
|
||||
const preview = DynamicMarkdownPreview.create(
|
||||
{
|
||||
resource,
|
||||
resourceColumn: previewSettings.resourceColumn,
|
||||
locked: previewSettings.locked,
|
||||
line: scrollLine,
|
||||
line: scrollLocation?.type === 'line' ? scrollLocation.line : undefined,
|
||||
},
|
||||
previewSettings.previewColumn,
|
||||
this.#contentProvider,
|
||||
@@ -406,6 +406,16 @@ export class MarkdownPreviewManager extends Disposable implements vscode.Webview
|
||||
return this.#registerDynamicPreview(preview);
|
||||
}
|
||||
|
||||
#getActiveTextEditorScrollLocation(resource: vscode.Uri): StartingScrollLine | undefined {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (editor?.document.uri.toString() !== resource.toString()) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const line = getVisibleLine(editor);
|
||||
return typeof line === 'number' ? new StartingScrollLine(line) : undefined;
|
||||
}
|
||||
|
||||
#registerDynamicPreview(preview: DynamicMarkdownPreview): DynamicMarkdownPreview {
|
||||
this.#dynamicPreviews.add(preview);
|
||||
|
||||
|
||||
@@ -62,12 +62,6 @@ export class TopmostLineMonitor extends Disposable {
|
||||
this.#previousTextEditorInfo.delete(resource);
|
||||
return scrollLoc?.line;
|
||||
}
|
||||
|
||||
public getPreviousStaticTextEditorLineByUri(resource: vscode.Uri): number | undefined {
|
||||
const state = this.#previousStaticEditorInfo.get(resource);
|
||||
return state?.line;
|
||||
}
|
||||
|
||||
public updateLine(
|
||||
resource: vscode.Uri,
|
||||
line: number
|
||||
|
||||
Reference in New Issue
Block a user