mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Typing inserts a character at the cursor position
This commit is contained in:
@@ -35,6 +35,25 @@ describe 'Buffer', ->
|
||||
expect(buffer.getLines().length).toBe fileContents.split("\n").length
|
||||
expect(buffer.getLines().join('\n')).toBe fileContents
|
||||
|
||||
describe "insert(position, string)", ->
|
||||
it "inserts the given string at the given position", ->
|
||||
expect(buffer.getLine(1).charAt(6)).not.toBe 'q'
|
||||
buffer.insert({row: 1, col: 6}, 'q')
|
||||
expect(buffer.getLine(1).charAt(6)).toBe 'q'
|
||||
|
||||
it "emits an event with the range of the change and the new text", ->
|
||||
insertHandler = jasmine.createSpy 'insertHandler'
|
||||
buffer.on 'insert', insertHandler
|
||||
|
||||
buffer.insert({row: 1, col: 6}, 'q')
|
||||
|
||||
expect(insertHandler).toHaveBeenCalled()
|
||||
[event] = insertHandler.argsForCall[0]
|
||||
|
||||
expect(event.range.start).toEqual(row: 1, col: 6)
|
||||
expect(event.range.end).toEqual(row: 1, col: 6)
|
||||
expect(event.string).toBe 'q'
|
||||
|
||||
describe ".save()", ->
|
||||
describe "when the buffer has a path", ->
|
||||
filePath = null
|
||||
|
||||
@@ -214,6 +214,15 @@ describe "Editor", ->
|
||||
expect(editor).not.toMatchSelector ':focus'
|
||||
expect(editor.hiddenInput).toMatchSelector ':focus'
|
||||
|
||||
describe "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)
|
||||
|
||||
expect(editor.getCurrentLine().charAt(6)).not.toBe 'q'
|
||||
|
||||
editor.hiddenInput.textInput 'q'
|
||||
|
||||
expect(editor.getCurrentLine().charAt(6)).toBe 'q'
|
||||
expect(editor.getPosition()).toEqual(row: 1, col: 7)
|
||||
expect(editor.lines.find('pre:eq(1)')).toHaveText editor.getCurrentLine()
|
||||
|
||||
|
||||
@@ -36,3 +36,7 @@ $.fn.enableKeymap = ->
|
||||
$.fn.attachToDom = ->
|
||||
$('#jasmine-content').append(this)
|
||||
|
||||
$.fn.textInput = (data) ->
|
||||
event = document.createEvent 'TextEvent'
|
||||
event.initTextEvent('textInput', true, true, window, data)
|
||||
this.each -> this.dispatchEvent(event)
|
||||
|
||||
@@ -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