Test and fix the center option to autoscroll

This commit is contained in:
Nathan Sobo
2017-03-29 20:51:24 -06:00
committed by Antonio Scandurra
parent 8652222b22
commit eb588d4c7c
2 changed files with 14 additions and 2 deletions

View File

@@ -523,6 +523,18 @@ describe('TextEditorComponent', () => {
expect(component.getScrollBottom()).toBe((6 + 1 + scrollMarginInLines) * component.measurements.lineHeight)
})
it('autoscrolls the given range to the center of the screen if the `center` option is true', async () => {
const {component, editor} = buildComponent({height: 50})
expect(component.getLastVisibleRow()).toBe(3)
editor.scrollToScreenRange([[4, 0], [6, 0]], {center: true})
await component.getNextUpdatePromise()
const actualScrollCenter = (component.getScrollTop() + component.getScrollBottom()) / 2
const expectedScrollCenter = Math.round((4 + 7) / 2 * component.getLineHeight())
expect(actualScrollCenter).toBe(expectedScrollCenter)
})
it('automatically scrolls horizontally when the requested range is within the horizontal scroll margin of the right edge of the gutter or right edge of the scroll container', async () => {
const {component, element, editor} = buildComponent()
const {scrollContainer} = component.refs

View File

@@ -1472,8 +1472,8 @@ class TextEditorComponent {
if (options && options.center) {
const desiredScrollCenter = (screenRangeTop + screenRangeBottom) / 2
if (desiredScrollCenter < this.getScrollTop() || desiredScrollCenter > this.getScrollBottom()) {
desiredScrollTop = desiredScrollCenter - this.measurements.clientHeight / 2
desiredScrollBottom = desiredScrollCenter + this.measurements.clientHeight / 2
desiredScrollTop = desiredScrollCenter - this.getScrollContainerClientHeight() / 2
desiredScrollBottom = desiredScrollCenter + this.getScrollContainerClientHeight() / 2
}
} else {
desiredScrollTop = screenRangeTop - verticalScrollMargin