Add Editor.bounds() and Editor.screenPositionInBounds()

This commit is contained in:
Corey Johnson
2012-04-03 14:39:09 -07:00
parent 846846abe8
commit a372a2b411
3 changed files with 44 additions and 1 deletions

View File

@@ -1882,7 +1882,6 @@ describe "Editor", ->
expect(editor.lines.find('.line:eq(14)').text()).toBe 'B'
expect(editor.lines.find('.line:eq(15)')).not.toExist()
describe "path-change event", ->
it "emits event when buffer's path is changed", ->
editor = new Editor()
@@ -1912,3 +1911,27 @@ describe "Editor", ->
eventHandler.reset()
editor.buffer.setPath("new.txt")
expect(eventHandler).toHaveBeenCalled()
describe "editorBounds()", ->
beforeEach ->
editor.attachToDom()
setEditorWidthInChars(editor, 10)
setEditorHeightInChars(editor, 10)
it "returns correct bounds based on scroll position", ->
expect(editor.bounds()).toEqual [[0,0], [10, 10]]
editor.scrollTop(editor.lineHeight * 1)
editor.horizontalScroller.scrollLeft(editor.charWidth * 1)
expect(editor.bounds()).toEqual [[1,1], [11, 11]]
describe "screenPositionInBounds(screenPosition)", ->
beforeEach ->
editor.attachToDom()
setEditorWidthInChars(editor, 20)
setEditorHeightInChars(editor, 10)
it "returns true if position is in bounds", ->
expect(editor.screenPositionInBounds([0,0])).toBeTruthy()
expect(editor.screenPositionInBounds([10,20])).toBeTruthy()
expect(editor.screenPositionInBounds([10,21])).toBeFalsy()
expect(editor.screenPositionInBounds([11,21])).toBeFalsy()

View File

@@ -110,6 +110,9 @@ window.tokensText = (tokens) ->
window.setEditorWidthInChars = (editor, widthInChars, charWidth=editor.charWidth) ->
editor.width(charWidth * widthInChars + editor.lines.position().left)
window.setEditorHeightInChars = (editor, heightInChars, charHeight=editor.lineHeight) ->
editor.height(charHeight * heightInChars + editor.lines.position().top)
$.fn.resultOfTrigger = (type) ->
event = $.Event(type)
this.trigger(event)

View File

@@ -162,6 +162,23 @@ class Editor extends View
rootView: ->
@parents('#root-view').view()
bounds: ->
rows = @height() / @lineHeight
columns = @horizontalScroller.width() / @charWidth
start = new Point(@scrollTop() / @lineHeight, @horizontalScroller.scrollLeft() / @charWidth)
end = new Point(start.row + rows, start.column + columns)
new Range(start, end)
screenPositionInBounds: (screenPosition) ->
screenPosition = Point.fromObject(screenPosition)
bounds = @bounds()
bounds.start.row <= screenPosition.row and
bounds.start.column <= screenPosition.column and
bounds.end.row >= screenPosition.row and
bounds.end.column >= screenPosition.column
selectOnMousemoveUntilMouseup: ->
moveHandler = (e) => @selectToScreenPosition(@screenPositionFromMouseEvent(e))
@on 'mousemove', moveHandler