diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 04ea01e9b..bedcb9895 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -262,6 +262,21 @@ describe "Editor", -> expect(editor.getCursorPosition()).toEqual(lastPosition) + describe "when a click event occurs in the editor", -> + it "re-positions the cursor to the clicked row / column", -> + editor.attachToDom() + editor.css(position: 'absolute', top: 10, left: 10) + pageX = editor.offset().left + 10 * editor.charWidth + 3 + pageY = editor.offset().top + 4 * editor.lineHeight - 2 + + expect(editor.getCursorPosition()).toEqual(row: 0, column: 0) + + editor.lines.trigger clickEvent({pageX, pageY}) + + expect(editor.getCursorPosition()).toEqual(row: 3, column: 10) + + + describe "selection bindings", -> selection = null diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index b5edc1a91..0f41dc47a 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -28,6 +28,9 @@ eventPropertiesFromPattern = (pattern) -> window.keydownEvent = (pattern, properties={}) -> $.Event "keydown", _.extend(eventPropertiesFromPattern(pattern), properties) +window.clickEvent = (properties={}) -> + $.Event "click", properties + window.waitsForPromise = (fn) -> window.waitsFor (moveOn) -> fn().done(moveOn) diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 7e5a13928..cb8119581 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -67,6 +67,12 @@ class Editor extends Template @hiddenInput.focus() false + @on 'click', (e) => + { pageX, pageY } = e + left = pageX - @lines.offset().left + top = pageY - @lines.offset().top + @setCursorPosition(@pointFromPixelPosition({left, top})) + @hiddenInput.on "textInput", (e) => @insertText(e.originalEvent.data) @@ -127,6 +133,9 @@ class Editor extends Template pixelPositionFromPoint: ({row, column}) -> { top: row * @lineHeight, left: column * @charWidth } + pointFromPixelPosition: ({top, left}) -> + { row: Math.floor(top / @lineHeight), column: Math.floor(left / @charWidth) } + calculateDimensions: -> fragment = $('
') @lines.append(fragment)