pr feedback and combining prevEditor info for preview and non-preview

This commit is contained in:
Andrea Mah
2021-05-19 16:57:12 -06:00
parent 44d135e94f
commit 9ee1906718
3 changed files with 23 additions and 45 deletions

View File

@@ -16,15 +16,16 @@ export class TopmostLineMonitor extends Disposable {
private readonly pendingUpdates = new Map<string, number>();
private readonly throttle = 50;
private previousMDTextEditors = new Map<string, vscode.TextEditor>();
private previousStaticEditorInfo = new Map<string, LastScrollLocation>();
private isPrevEditorCustom = false;
private previousEditorInfo = new Map<string, LastScrollLocation>();
public isPrevEditorCustom = false;
constructor() {
super();
if (vscode.window.activeTextEditor) {
this.setPreviousMDTextEditorLine(vscode.window.activeTextEditor);
const line = getVisibleLine(vscode.window.activeTextEditor);
this.setPreviousEditorLine({ uri: vscode.window.activeTextEditor.document.uri, line: line ?? 0 });
}
this._register(vscode.window.onDidChangeTextEditorVisibleRanges(event => {
@@ -32,54 +33,26 @@ export class TopmostLineMonitor extends Disposable {
const line = getVisibleLine(event.textEditor);
if (typeof line === 'number') {
this.updateLine(event.textEditor.document.uri, line);
this.setPreviousEditorLine({ uri: event.textEditor.document.uri, line: line });
}
}
}));
this._register(vscode.window.onDidChangeActiveTextEditor(textEditor => {
// When at a markdown file, apply existing scroll settings from static preview if last editor was custom.
// Also save reference to text editor for line number reference later
if (textEditor && textEditor.document && isMarkdownFile(textEditor.document)) {
if (this.isPrevEditorCustom) {
const line = this.getPreviousStaticEditorLineByUri(textEditor.document.uri);
if (line) {
this._onEditorNeedsScrolling.fire({ line: line, editor: textEditor });
}
}
this.setPreviousMDTextEditorLine(textEditor);
}
this.isPrevEditorCustom = (textEditor === undefined);
}));
}
private readonly _onChanged = this._register(new vscode.EventEmitter<{ readonly resource: vscode.Uri, readonly line: number }>());
public readonly onDidChanged = this._onChanged.event;
private readonly _onEditorNeedsScrolling = this._register(new vscode.EventEmitter<{ readonly line: number, readonly editor: vscode.TextEditor }>());
public readonly onEditorNeedsScrolling = this._onEditorNeedsScrolling.event;
public setPreviousMDTextEditorLine(editor: vscode.TextEditor) {
const uri = editor.document.uri;
this.previousMDTextEditors.set(uri.toString(), editor);
public setPreviousEditorLine(scrollLocation: LastScrollLocation): void {
this.previousEditorInfo.set(scrollLocation.uri.toString(), scrollLocation);
}
public getPreviousMDTextEditorLineByUri(resource: vscode.Uri) {
const editor = this.previousMDTextEditors.get(resource.toString());
return editor?.visibleRanges[0].start.line;
}
public setPreviousStaticEditorLine(scrollLocation: LastScrollLocation) {
this.previousStaticEditorInfo.set(scrollLocation.uri.toString(), scrollLocation);
}
public getPreviousStaticEditorLineByUri(resource: vscode.Uri) {
const scrollLoc = this.previousStaticEditorInfo.get(resource.toString());
public getPreviousEditorLineByUri(resource: vscode.Uri): number | undefined {
const scrollLoc = this.previousEditorInfo.get(resource.toString());
return scrollLoc?.line;
}
private updateLine(
public updateLine(
resource: vscode.Uri,
line: number
) {