Cursor can move around screen

Still not handling corner cases like moving off the edge of screen,
line, etc.
This commit is contained in:
Danny Greg & Nathan Sobo 2012-01-17 18:13:50 -08:00
parent 5198a88cce
commit cd127c009e
5 changed files with 71 additions and 9 deletions

1
.pairs
View File

@ -1,6 +1,7 @@
pairs:
ns: Nathan Sobo; nathan
cj: Corey Johnson; cj
dg: Danny Greg; danny
email:
domain: github.com
#global: true

View File

@ -3,28 +3,55 @@ Editor = require 'editor'
$ = require 'jquery'
fs = require 'fs'
describe "Editor", ->
fdescribe "Editor", ->
buffer = null
editor = null
beforeEach ->
buffer = new Buffer(require.resolve('fixtures/sample.js'))
editor = Editor.build()
editor.enableKeymap()
editor.setBuffer(buffer)
describe ".setBuffer", ->
beforeEach ->
it "creates a pre element for each line in the buffer", ->
editor.setBuffer(buffer)
expect(editor.lines.find('pre').length).toEqual(buffer.numLines())
it "sets the cursor to the beginning of the file", ->
expect(editor.getPosition()).toEqual(row: 0, col: 0)
describe "cursor movement", ->
it "moves the cursor when arrow keys are pressed", ->
editor.trigger keydownEvent('right')
expect(editor.getPosition()).toEqual(row: 0, col: 1)
editor.trigger keydownEvent('down')
expect(editor.getPosition()).toEqual(row: 1, col: 1)
editor.trigger keydownEvent('left')
expect(editor.getPosition()).toEqual(row: 1, col: 0)
editor.trigger keydownEvent('up')
expect(editor.getPosition()).toEqual(row: 0, col: 0)
describe ".setPosition({row, col})", ->
it "moves the cursor to cover the character at the given row and column", ->
editor.attachToDom()
editor.setBuffer(buffer)
editor.setPosition(row: 2, col: 2)
expect(editor.cursor.position().top).toBe(2 * editor.lineHeight())
expect(editor.cursor.position().left).toBe(2 * editor.charWidth())
describe "when the editor is attached to the dom", ->
it "updates the pixel position of the cursor", ->
editor.setPosition(row: 2, col: 2)
editor.attachToDom()
expect(editor.cursor.position().top).toBe(2 * editor.lineHeight())
expect(editor.cursor.position().left).toBe(2 * editor.charWidth())

View File

@ -34,6 +34,5 @@ $.fn.enableKeymap = ->
@on 'keydown', (e) => atom.globalKeymap.handleKeyEvent(e)
$.fn.attachToDom = ->
console.log this
$('#jasmine-content').append(this)

View File

@ -7,8 +7,12 @@ class Cursor extends Template
viewProperties:
setPosition: (@_position) ->
@css(@parentView.toPixelPosition(@_position))
@updateAbsolutePosition()
getPosition: ->
@_position
updateAbsolutePosition: ->
position = @parentView.toPixelPosition(@_position)
@css(position)

View File

@ -7,8 +7,8 @@ _ = require 'underscore'
module.exports =
class Editor extends Template
content: ->
@div class: 'editor', =>
@style @div outlet: 'lines'
@div class: 'editor', tabindex: -1, =>
@div outlet: 'lines'
@subview 'cursor', Cursor.build()
viewProperties:
@ -17,13 +17,43 @@ class Editor extends Template
initialize: () ->
requireStylesheet 'editor.css'
@setBuffer(new Buffer)
@one 'attach', => @calculateDimensions()
atom.bindKeys '*',
right: 'move-right'
left: 'move-left'
down: 'move-down'
up: 'move-up'
@on 'move-right', => @moveRight()
@on 'move-left', => @moveLeft()
@on 'move-down', => @moveDown()
@on 'move-up', => @moveUp()
@one 'attach', =>
@calculateDimensions()
moveRight: ->
{ row, col } = @getPosition()
@setPosition({row, col: col + 1})
moveDown: ->
{ row, col } = @getPosition()
@setPosition({row: row + 1, col})
moveLeft: ->
{ row, col } = @getPosition()
@setPosition({row, col: col - 1})
moveUp: ->
{ row, col } = @getPosition()
@setPosition({row: row - 1, col})
setBuffer: (@buffer) ->
@lines.empty()
for line in @buffer.getLines()
@lines.append "<pre>#{line}</pre>"
_.defer => @setPosition(row: 3, col: 4)
@setPosition(row: 0, col: 0)
setPosition: (position) ->
@cursor.setPosition(position)
@ -46,4 +76,5 @@ class Editor extends Template
@cachedCharWidth = fragment.width()
@cachedLineHeight = fragment.outerHeight()
fragment.remove()
@cursor.updateAbsolutePosition()