Only autoscroll selections to center if they are offscreen

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-11-30 11:40:16 -07:00
parent f9c7c08641
commit ac0e3095ec
2 changed files with 15 additions and 7 deletions

View File

@@ -941,10 +941,14 @@ describe "Editor", ->
expect(editor.scrollTop()).toBeGreaterThan(0)
describe "when the selected buffer range is assigned with the autoscroll option set to true", ->
it "centers the selection in the viewport if possible", ->
setEditorHeightInLines(editor, 8)
it "centers the selection in the viewport if its vertical center is currently offscreen", ->
setEditorHeightInLines(editor, 4)
editor.setSelectedBufferRange([[2, 0], [4, 0]], autoscroll: true)
expect(editor.scrollTop()).toBe 0
editor.setSelectedBufferRange([[6, 0], [8, 0]], autoscroll: true)
expect(editor.scrollTop()).toBe 3 * editor.lineHeight
expect(editor.scrollTop()).toBe 5 * editor.lineHeight
describe "cursor rendering", ->
describe "when the cursor moves", ->

View File

@@ -502,9 +502,14 @@ class Editor extends View
scrollVertically: (pixelPosition, {center}={}) ->
scrollViewHeight = @scrollView.height()
scrollTop = @scrollTop()
scrollBottom = scrollTop + scrollViewHeight
if center
@scrollTop(pixelPosition.top - (scrollViewHeight / 2))
console.log scrollTop, pixelPosition.top, scrollBottom
unless scrollTop < pixelPosition.top < scrollBottom
@scrollTop(pixelPosition.top - (scrollViewHeight / 2))
else
linesInView = @scrollView.height() / @lineHeight
maxScrollMargin = Math.floor((linesInView - 1) / 2)
@@ -512,10 +517,9 @@ class Editor extends View
margin = scrollMargin * @lineHeight
desiredTop = pixelPosition.top - margin
desiredBottom = pixelPosition.top + @lineHeight + margin
if desiredBottom > @scrollTop() + scrollViewHeight
if desiredBottom > scrollBottom
@scrollTop(desiredBottom - scrollViewHeight)
else if desiredTop < @scrollTop()
else if desiredTop < scrollTop
@scrollTop(desiredTop)
scrollHorizontally: (pixelPosition) ->