Handle auto-height in TextEditorPresenter

This commit is contained in:
Nathan Sobo 2015-01-29 15:05:15 -07:00
parent bbc1a264b5
commit b521e8dc97
3 changed files with 37 additions and 3 deletions

View File

@ -1655,3 +1655,20 @@ describe "TextEditorPresenter", ->
editor.undo()
expect(lineNumberStateForScreenRow(presenter, 11).foldable).toBe false
describe ".height", ->
it "tracks the computed content height if ::autoHeight is true so the editor auto-expands vertically", ->
presenter = new TextEditorPresenter(model: editor, scrollTop: 0, lineHeight: 10, autoHeight: true)
expect(presenter.state.height).toBe editor.getScreenLineCount() * 10
expectStateUpdate presenter, -> presenter.setAutoHeight(false)
expect(presenter.state.height).toBe null
expectStateUpdate presenter, -> presenter.setAutoHeight(true)
expect(presenter.state.height).toBe editor.getScreenLineCount() * 10
expectStateUpdate presenter, -> presenter.setLineHeight(20)
expect(presenter.state.height).toBe editor.getScreenLineCount() * 20
expectStateUpdate presenter, -> editor.getBuffer().append("\n\n\n")
expect(presenter.state.height).toBe editor.getScreenLineCount() * 20

View File

@ -55,7 +55,7 @@ TextEditorComponent = React.createClass
hiddenInputStyle = @getHiddenInputPosition()
hiddenInputStyle.WebkitTransform = 'translateZ(0)' if @useHardwareAcceleration
style.height = @presenter.state.content.scrollHeight if @autoHeight
style.height = @presenter.state.height if @presenter.state.height?
if useShadowDOM
className = 'editor-contents--private'
@ -675,6 +675,7 @@ TextEditorComponent = React.createClass
if position is 'absolute' or height
if @autoHeight
@autoHeight = false
@presenter.setAutoHeight(false)
@forceUpdate() if not @updatesPaused and @canUpdate()
clientHeight = scrollViewNode.clientHeight
@ -682,6 +683,7 @@ TextEditorComponent = React.createClass
@presenter.setHeight(clientHeight)
editor.setHeight(clientHeight)
else
@presenter.setAutoHeight(true)
@presenter.setHeight(null)
editor.setHeight(null)
@autoHeight = true

View File

@ -10,7 +10,7 @@ class TextEditorPresenter
mouseWheelScreenRow: null
constructor: (params) ->
{@model, @height, @contentFrameWidth, @scrollTop, @scrollLeft} = params
{@model, @autoHeight, @height, @contentFrameWidth, @scrollTop, @scrollLeft} = params
{@horizontalScrollbarHeight, @verticalScrollbarWidth} = params
{@lineHeight, @baseCharacterWidth, @lineOverdrawMargin, @backgroundColor, @gutterBackgroundColor} = params
{@cursorBlinkPeriod, @cursorBlinkResumeDelay, @stoppedScrollingDelay} = params
@ -64,6 +64,7 @@ class TextEditorPresenter
@updateState()
updateState: ->
@updateHeightState()
@updateVerticalScrollState()
@updateHorizontalScrollState()
@updateScrollbarsState()
@ -75,6 +76,14 @@ class TextEditorPresenter
@updateGutterState()
@updateLineNumbersState()
updateHeightState: ->
if @hasAutoHeight()
@state.height = @computeContentHeight()
else
@state.height = null
@emitter.emit 'did-update-state'
updateVerticalScrollState: ->
scrollHeight = @computeScrollHeight()
@state.content.scrollHeight = scrollHeight
@ -456,6 +465,11 @@ class TextEditorPresenter
getVerticalScrollbarWidth: -> @verticalScrollbarWidth
setAutoHeight: (@autoHeight) ->
@updateHeightState()
hasAutoHeight: -> @autoHeight
setHeight: (@height) ->
@updateVerticalScrollState()
@updateScrollbarsState()
@ -465,7 +479,7 @@ class TextEditorPresenter
@updateLineNumbersState()
getHeight: ->
@height ? @model.getScreenLineCount() * @getLineHeight()
@height ? @computeContentHeight()
setContentFrameWidth: (@contentFrameWidth) ->
@updateHorizontalScrollState()
@ -490,6 +504,7 @@ class TextEditorPresenter
getGutterBackgroundColor: -> @gutterBackgroundColor
setLineHeight: (@lineHeight) ->
@updateHeightState()
@updateVerticalScrollState()
@updateLinesState()
@updateCursorsState()