From a372a2b411b66237b3b006956c0647bf2adea4ba Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 3 Apr 2012 14:39:09 -0700 Subject: [PATCH] Add Editor.bounds() and Editor.screenPositionInBounds() --- spec/app/editor-spec.coffee | 25 ++++++++++++++++++++++++- spec/spec-helper.coffee | 3 +++ src/app/editor.coffee | 17 +++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 36c438ad5..9c1a3542a 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -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() \ No newline at end of file diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 146a6456c..71940d5f6 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -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) diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 8ba0df5d1..5aaf0876f 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -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