mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Typing inserts a character at the cursor position
This commit is contained in:
@@ -24,6 +24,18 @@ class Buffer
|
||||
getLine: (n) ->
|
||||
@lines[n]
|
||||
|
||||
insert: ({row, col}, string) ->
|
||||
line = @getLine(row)
|
||||
before = line.substring(0, col)
|
||||
after = line.substring(col)
|
||||
@lines[row] = before + string + after
|
||||
|
||||
@trigger 'insert'
|
||||
string: string
|
||||
range:
|
||||
start: {row, col}
|
||||
end: {row, col}
|
||||
|
||||
numLines: ->
|
||||
@getLines().length
|
||||
|
||||
@@ -31,3 +43,11 @@ class Buffer
|
||||
if not @path then throw new Error("Tried to save buffer with no url")
|
||||
fs.write @path, @getText()
|
||||
|
||||
on: (eventName, handler) ->
|
||||
@handlers ?= {}
|
||||
@handlers[eventName] ?= []
|
||||
@handlers[eventName].push(handler)
|
||||
|
||||
trigger: (eventName, data) ->
|
||||
@handlers?[eventName]?.forEach (handler) -> handler(data)
|
||||
|
||||
|
||||
@@ -6,6 +6,10 @@ class Cursor extends Template
|
||||
@pre class: 'cursor', style: 'position: absolute;', => @raw ' '
|
||||
|
||||
viewProperties:
|
||||
setBuffer: (@buffer) ->
|
||||
@buffer.on 'insert', (e) =>
|
||||
@setColumn(@getColumn() + e.string.length)
|
||||
|
||||
setPosition: (point) ->
|
||||
@point = @parentView.clipPosition(point)
|
||||
@goalColumn = null
|
||||
@@ -17,6 +21,9 @@ class Cursor extends Template
|
||||
{ row } = @getPosition()
|
||||
@setPosition {row, col}
|
||||
|
||||
getColumn: ->
|
||||
@getPosition().col
|
||||
|
||||
moveUp: ->
|
||||
{ row, col } = @getPosition()
|
||||
col = @goalColumn if @goalColumn?
|
||||
|
||||
@@ -36,6 +36,9 @@ class Editor extends Template
|
||||
@hiddenInput.focus()
|
||||
false
|
||||
|
||||
@hiddenInput.on "textInput", (e) =>
|
||||
@buffer.insert(@getPosition(), e.originalEvent.data)
|
||||
|
||||
@one 'attach', =>
|
||||
@calculateDimensions()
|
||||
|
||||
@@ -47,6 +50,10 @@ class Editor extends Template
|
||||
else
|
||||
@lines.append $$.pre(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))
|
||||
|
||||
clipPosition: ({row, col}) ->
|
||||
line = @buffer.getLine(row)
|
||||
@@ -69,6 +76,8 @@ class Editor extends Template
|
||||
else
|
||||
@scrollTop() + @height()
|
||||
|
||||
getCurrentLine: -> @buffer.getLine(@getPosition().row)
|
||||
|
||||
moveUp: -> @cursor.moveUp()
|
||||
moveDown: -> @cursor.moveDown()
|
||||
moveRight: -> @cursor.moveRight()
|
||||
|
||||
Reference in New Issue
Block a user