Re-wrap lines when window size changes.

This commit is contained in:
Nathan Sobo
2012-02-10 13:14:17 -07:00
parent 90e019a4ac
commit 12a404fe5d
2 changed files with 21 additions and 2 deletions

View File

@@ -79,10 +79,21 @@ describe "Editor", ->
expect(editor.lines.find('.line:eq(8)').text()).toBe ': right.push(current);'
expect(editor.lines.find('.line:eq(9)').text()).toBe ' }'
it "unwraps lines when softwrap is disabled", ->
it "changes the max line length when the window size changes", ->
editor.width(editor.charWidth * 40)
$(window).trigger 'resize'
expect(editor.lines.find('.line').length).toBe 19
expect(editor.lines.find('pre:eq(4)').text()).toBe "left = [], right = [];"
expect(editor.lines.find('pre:eq(5)').text()).toBe " while(items.length > 0) {"
it "unwraps lines and cancels window resize listener when softwrap is disabled", ->
editor.toggleSoftWrap()
expect(editor.lines.find('.line:eq(3)').text()).toBe ' var pivot = items.shift(), current, left = [], right = [];'
spyOn(editor, 'setMaxLineLength')
$(window).trigger 'resize'
expect(editor.setMaxLineLength).not.toHaveBeenCalled()
describe "cursor movement", ->
describe ".setCursorPosition({row, column})", ->
beforeEach ->

View File

@@ -178,7 +178,7 @@ class Editor extends View
toggleSoftWrap: ->
@setSoftWrap(not @softWrap)
setSoftWrap: (@softWrap) ->
setMaxLineLength: ->
maxLength =
if @softWrap
Math.floor(@width() / @charWidth)
@@ -187,6 +187,14 @@ class Editor extends View
@lineWrapper.setMaxLength(maxLength)
setSoftWrap: (@softWrap) ->
@setMaxLineLength()
if @softWrap
@_setMaxLineLength = => @setMaxLineLength()
$(window).on 'resize', @_setMaxLineLength
else
$(window).off 'resize', @_setMaxLineLength
clipPosition: ({row, column}) ->
if row > @buffer.lastRow()
row = @buffer.lastRow()