From a62bd4b30455993d43c8bd0f122a400223e4a46b Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 2 Feb 2012 14:57:05 -0800 Subject: [PATCH] Double clicking selects a word. --- spec/atom/editor-spec.coffee | 33 +++++++++++++++++++++------------ spec/spec-helper.coffee | 6 +----- src/atom/editor.coffee | 13 ++++++------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 92c6391cb..0b345e826 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -244,22 +244,30 @@ describe "Editor", -> expect(editor.getCursorPosition()).toEqual(lastPosition) + describe "when a mousedown event occurs in the editor", -> - it "re-positions the cursor to the clicked row / column", -> + beforeEach -> editor.attachToDom() - expect(editor.getCursorPosition()).toEqual(row: 0, column: 0) + editor.css(position: 'absolute', top: 10, left: 10) - [pageX, pageY] = window.pixelPositionForPoint(editor, [3, 10]) - editor.lines.trigger mousedownEvent({pageX, pageY}) - expect(editor.getCursorPosition()).toEqual(row: 3, column: 10) + describe "when it is a single click", -> + it "re-positions the cursor to the clicked row / column", -> + editor.attachToDom() + editor.css(position: 'absolute', top: 10, left: 10) - describe "when doubleclick occurs in the editor", -> - it "selects the word under the cursor", -> - editor.attachToDom() - expect(editor.getCursorPosition()).toEqual(row: 0, column: 0) - [pageX, pageY] = window.pixelPositionForPoint(editor, [0, 8]) - editor.lines.trigger mouseupEvent({pageX, pageY, originalEvent: {detail: 2}}) - expect(editor.getSelectedText()).toBe "quicksort" + expect(editor.getCursorPosition()).toEqual(row: 0, column: 0) + + [pageX, pageY] = window.pixelPositionForPoint(editor, [3, 10]) + editor.lines.trigger mousedownEvent({pageX, pageY}) + expect(editor.getCursorPosition()).toEqual(row: 3, column: 10) + + describe "when it is a double click", -> + it "selects the word under the cursor", -> + expect(editor.getCursorPosition()).toEqual(row: 0, column: 0) + [pageX, pageY] = window.pixelPositionForPoint(editor, [0, 8]) + editor.lines.trigger mousedownEvent({pageX, pageY, originalEvent: {detail: 1}}) + editor.lines.trigger mousedownEvent({pageX, pageY, originalEvent: {detail: 2}}) + expect(editor.getSelectedText()).toBe "quicksort" describe "selection", -> selection = null @@ -325,6 +333,7 @@ describe "Editor", -> describe "when the mouse is dragged across the text", -> it "creates a selection from the initial click to mouse cursor's location ", -> editor.attachToDom() + editor.css(position: 'absolute', top: 10, left: 10) # start [pageX, pageY] = window.pixelPositionForPoint(editor, [4, 10]) diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 340618a5e..9aafe7dd8 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -33,11 +33,9 @@ window.clickEvent = (properties={}) -> $.Event "click", properties window.mousedownEvent = (properties={}) -> + properties.originalEvent ?= {detail: 1} $.Event "mousedown", properties -window.mouseupEvent = (properties={}) -> - $.Event "mouseup", properties - window.mousemoveEvent = (properties={}) -> $.Event "mousemove", properties @@ -68,8 +66,6 @@ window.advanceClock = (delta) -> true window.pixelPositionForPoint = (editor, point) -> - editor.css(position: 'absolute', top: 10, left: 10) - point = Point.fromObject point pageY = editor.offset().top + point.row * editor.lineHeight + 1 # ensure the pixel is inside the char pageX = editor.offset().left + point.column * editor.charWidth + 1 # ensure the pixel is inside the char diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index b759a7ede..c2f525c4b 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -76,15 +76,14 @@ class Editor extends Template false @on 'mousedown', (e) => - @setCursorPosition @pointFromMouseEvent(e) - moveHandler = (e) => @selectToPosition(@pointFromMouseEvent(e)) - @on 'mousemove', moveHandler - $(document).one 'mouseup', => @off 'mousemove', moveHandler - - @on 'mouseup', (e) => clickCount = e.originalEvent.detail - if clickCount == 2 + + if clickCount == 1 @setCursorPosition @pointFromMouseEvent(e) + moveHandler = (e) => @selectToPosition(@pointFromMouseEvent(e)) + @on 'mousemove', moveHandler + $(document).one 'mouseup', => @off 'mousemove', moveHandler + else if clickCount == 2 @selection.selectWord() @hiddenInput.on "textInput", (e) =>