Prevent out of range line acecss on markdown preview scrolling when user scrolls to end of document

This commit is contained in:
Matt Bierner 2018-02-26 17:17:22 -08:00
parent 6b85d3afdc
commit 68c21cde76
2 changed files with 19 additions and 2 deletions

View file

@ -29,6 +29,22 @@
};
}
/**
* @param {number} min
* @param {number} max
* @param {number} value
*/
function clamp(min, max, value) {
return Math.min(max, Math.max(min, value));
}
/**
* @param {number} line
*/
function clampLine(line) {
return clamp(0, settings.lineCount - 1, line);
}
/**
* @param {string} command
* @param {any[]} args
@ -157,9 +173,9 @@
if (next) {
const betweenProgress = (offset - window.scrollY - previous.element.getBoundingClientRect().top) / (next.element.getBoundingClientRect().top - previous.element.getBoundingClientRect().top);
const line = previous.line + betweenProgress * (next.line - previous.line);
return Math.max(line, 0);
return clampLine(line);
} else {
return Math.max(previous.line, 0);
return clampLine(previous.line);
}
}
return null;

View file

@ -227,6 +227,7 @@ export class MarkdownContentProvider {
const initialData = {
source: sourceUri.toString(),
line: initialLine,
lineCount: markdownDocument.lineCount,
scrollPreviewWithEditor: config.scrollPreviewWithEditor,
scrollEditorWithPreview: config.scrollEditorWithPreview,
doubleClickToSwitchToEditor: config.doubleClickToSwitchToEditor,