From b4d91f2bc73b7fd17dc03bd0bc9a8bc822f51db8 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Tue, 24 Jan 2012 17:19:01 -0800 Subject: [PATCH] Typing inserts a character at the cursor position --- spec/atom/buffer-spec.coffee | 19 +++++++++++++++++++ spec/atom/editor-spec.coffee | 9 +++++++++ spec/spec-helper.coffee | 4 ++++ src/atom/buffer.coffee | 20 ++++++++++++++++++++ src/atom/cursor.coffee | 7 +++++++ src/atom/editor.coffee | 9 +++++++++ 6 files changed, 68 insertions(+) diff --git a/spec/atom/buffer-spec.coffee b/spec/atom/buffer-spec.coffee index 9cf2b1294..c74c03c0a 100644 --- a/spec/atom/buffer-spec.coffee +++ b/spec/atom/buffer-spec.coffee @@ -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 diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 3a1535f4c..577d50dde 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -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() diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index f0f6f1ffe..824f123c1 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -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) diff --git a/src/atom/buffer.coffee b/src/atom/buffer.coffee index dd1b03dec..60fda8e9e 100644 --- a/src/atom/buffer.coffee +++ b/src/atom/buffer.coffee @@ -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) + diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index 021eaca89..c66a6079f 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -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? diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 79275d05a..322186e21 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -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()