Skip to content

Commit

Permalink
fix: scroll cursor line to top editor for long inline preview (ajaxor…
Browse files Browse the repository at this point in the history
…g#5514)

* fix: scroll cursor to top for long inline preview

* fix: calculation of pixels to scroll top in setGhostText

* revert: changes to pixelposition calc

* fix: whitespace
  • Loading branch information
akoreman authored Mar 20, 2024
1 parent 099011b commit 529473b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/autocomplete/inline_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,15 @@ module.exports = {
// of the container
editor.execCommand("insertstring", "\n".repeat(200));

var deltaY;
var deltaY, row;
var initialScrollBy = editor.renderer.scrollBy;
editor.renderer.scrollBy = function(varX, varY) {
var initialScrollToRow = editor.renderer.scrollToRow;
editor.renderer.scrollBy = function(_, varY) {
deltaY = varY;
};
editor.renderer.scrollToRow = function(varRow) {
row = varRow;
};

inline.show(editor, completions[6], "l");
editor.renderer.$loop._flush();
Expand All @@ -295,8 +299,9 @@ module.exports = {

setTimeout(() => {
// Should scroll as much as possbile while keeping the cursor on screen
assert.strictEqual(deltaY, 490);
assert.strictEqual(row, 202);
editor.renderer.scrollBy = initialScrollBy;
editor.renderer.scrollToRow = initialScrollToRow;
done();
}, 50);
}, 50);
Expand Down
8 changes: 4 additions & 4 deletions src/virtual_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1781,18 +1781,18 @@ class VirtualRenderer {
var el = this.container;
var height = el.getBoundingClientRect().height;
var ghostTextHeight = textLines.length * this.lineHeight;
var fitsY = ghostTextHeight < height - pixelPosition.top;
var fitsY = ghostTextHeight < (height - pixelPosition.top);

// If it fits, no action needed
if (fitsY) return;

// If it can fully fit in the screen, scroll down until it fits on the screen
// if it cannot fully fit, scroll so that the cursor is at the top of the screen
// to fit as much as possible.
// if it cannot fully fit, scroll so that the row with the cursor
// is at the top of the screen.
if (ghostTextHeight < height) {
this.scrollBy(0, (textLines.length - 1) * this.lineHeight);
} else {
this.scrollBy(0, pixelPosition.top);
this.scrollToRow(insertPosition.row);
}
}

Expand Down

0 comments on commit 529473b

Please sign in to comment.