Render empty lines correctly when inserting newlines

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-01-25 13:36:32 -08:00
parent c536933c0b
commit 045c3c39bf
3 changed files with 56 additions and 7 deletions

View File

@@ -218,7 +218,7 @@ describe "Editor", ->
expect(editor).not.toMatchSelector ':focus'
expect(editor.hiddenInput).toMatchSelector ':focus'
describe "when text input events are triggered on the hidden input element", ->
fdescribe "when text input events are triggered on the hidden input element", ->
it "inserts the typed character at the cursor position, both in the buffer and the pre element", ->
editor.setPosition(row: 1, col: 6)
@@ -230,3 +230,35 @@ describe "Editor", ->
expect(editor.getPosition()).toEqual(row: 1, col: 7)
expect(editor.lines.find('pre:eq(1)')).toHaveText editor.getCurrentLine()
fdescribe "when return is pressed", ->
describe "when the cursor is at the beginning of a line", ->
it "inserts an empty line before it", ->
editor.setPosition(row: 1, col: 0)
editor.trigger keydownEvent('enter')
expect(editor.lines.find('pre:eq(1)')).toHaveHtml ' '
expect(editor.getPosition()).toEqual(row: 2, col: 0)
describe "when the cursor is in the middle of a line", ->
it "splits the current line to form a new line", ->
editor.setPosition(row: 1, col: 6)
originalLine = editor.lines.find('pre:eq(1)').text()
lineBelowOriginalLine = editor.lines.find('pre:eq(2)').text()
editor.trigger keydownEvent('enter')
expect(editor.lines.find('pre:eq(1)')).toHaveText originalLine[0...6]
expect(editor.lines.find('pre:eq(2)')).toHaveText originalLine[6..]
expect(editor.lines.find('pre:eq(3)')).toHaveText lineBelowOriginalLine
expect(editor.getPosition()).toEqual(row: 2, col: 0)
describe "when the cursor is on the end of a line", ->
it "inserts an empty line after it", ->
editor.setPosition(row: 1, col: buffer.getLine(1).length)
editor.trigger keydownEvent('enter')
expect(editor.lines.find('pre:eq(2)')).toHaveHtml ' '
expect(editor.getPosition()).toEqual(row: 2, col: 0)

View File

@@ -13,7 +13,10 @@ class Cursor extends Template
setBuffer: (@buffer) ->
@buffer.on 'insert', (e) =>
@setColumn(@getColumn() + e.string.length)
if e.string == "\n"
@setPosition {row: @getRow() + 1, col: 0}
else
@setColumn(@getColumn() + e.string.length)
setPosition: (point) ->
@point = @editor.clipPosition(point)
@@ -29,6 +32,9 @@ class Cursor extends Template
getColumn: ->
@getPosition().col
getRow: ->
@getPosition().row
moveUp: ->
{ row, col } = @getPosition()
col = @goalColumn if @goalColumn?

View File

@@ -34,11 +34,13 @@ class Editor extends Template
left: 'move-left'
down: 'move-down'
up: 'move-up'
enter: 'newline'
@on 'move-right', => @moveRight()
@on 'move-left', => @moveLeft()
@on 'move-down', => @moveDown()
@on 'move-up', => @moveUp()
@on 'newline', => @buffer.insert @getPosition(), "\n"
handleEvents: ->
@on 'focus', =>
@@ -52,18 +54,27 @@ class Editor extends Template
@calculateDimensions()
@focus()
buildLineElement: (lineText) ->
if lineText is ''
$$.pre -> @raw(' ')
else
$$.pre(lineText)
setBuffer: (@buffer) ->
@lines.empty()
for line in @buffer.getLines()
if line is ''
@lines.append $$.pre -> @raw(' ')
else
@lines.append $$.pre(line)
@lines.append @buildLineElement(line)
@setPosition(row: 0, col: 0)
@cursor.setBuffer(@buffer)
@buffer.on 'insert', (e) =>
{row} = e.range.start
@lines.find('pre').eq(row).replaceWith $$.pre(@buffer.getLine(row))
updatedLine = @buildLineElement(@buffer.getLine(row))
@lines.find('pre').eq(row).replaceWith(updatedLine)
if e.string == '\n'
updatedLine.after @buildLineElement(@buffer.getLine(row + 1))
clipPosition: ({row, col}) ->
line = @buffer.getLine(row)