Correctly translate clicks to screen positions w/ var-width fonts

Closes #267
This commit is contained in:
Corey Johnson & Nathan Sobo
2013-02-14 11:50:07 -07:00
parent d5759752b5
commit 52cbf5e367
2 changed files with 34 additions and 6 deletions

View File

@@ -672,6 +672,18 @@ describe "Editor", ->
editor.renderedLines.trigger mousedownEvent(editor: editor, point: [3, 50])
expect(editor.getCursorBufferPosition()).toEqual(row: 3, column: 50)
describe "when the editor is using a variable-width font", ->
beforeEach ->
editor.setFontFamily('sans-serif')
afterEach ->
editor.clearFontFamily()
it "positions the cursor to the clicked row and column", ->
{top, left} = editor.pixelOffsetForScreenPosition([3, 30])
editor.renderedLines.trigger mousedownEvent(pageX: left, pageY: top)
expect(editor.getCursorScreenPosition()).toEqual [3, 30]
describe "double-click", ->
it "selects the word under the cursor, and expands the selection wordwise in either direction on a subsequent shift-click", ->
expect(editor.getCursorScreenPosition()).toEqual(row: 0, column: 0)

View File

@@ -1229,14 +1229,30 @@ class Editor extends View
offset = @renderedLines.offset()
{top: top + offset.top, left: left + offset.left}
screenPositionFromPixelPosition: ({top, left}) ->
screenPosition = new Point(Math.floor(top / @lineHeight), Math.floor(left / @charWidth))
screenPositionFromMouseEvent: (e) ->
{ pageX, pageY } = e
@screenPositionFromPixelPosition
top: pageY - @scrollView.offset().top + @scrollTop()
left: pageX - @scrollView.offset().left + @scrollView.scrollLeft()
editorRelativeTop = pageY - @scrollView.offset().top + @scrollTop()
row = Math.floor(editorRelativeTop / @lineHeight)
column = 0
if lineElement = @lineElementForScreenRow(row)[0]
@overlayer.hide()
@css '-webkit-user-select', 'auto'
if range = document.caretRangeFromPoint(pageX, pageY)
clickedTextNode = range.endContainer
clickedOffset = range.endOffset
range.detach()
@css '-webkit-user-select', ''
@overlayer.show()
if clickedTextNode and lineElement
iterator = document.createNodeIterator(lineElement, NodeFilter.SHOW_TEXT, acceptNode: -> NodeFilter.FILTER_ACCEPT)
while (node = iterator.nextNode()) and node isnt clickedTextNode
column += node.textContent.length
column += clickedOffset
new Point(row, column)
highlightCursorLine: ->
return if @mini