Skip to content

Commit

Permalink
Implemented middle click click-and-drag behavior. Resolves #290
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticMind committed Apr 9, 2015
1 parent 175d058 commit 86c155b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
11 changes: 7 additions & 4 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ class MinimapElement extends HTMLElement
@leftMousePressedOverCanvas(e)
else if e.which is 2
@middleMousePressedOverCanvas(e)
# @requestForcedUpdate()
{top, height} = @visibleArea.getBoundingClientRect()
@startDrag({which: 2, pageY: top + height/2}) # ugly hack
else return

leftMousePressedOverCanvas: ({pageY, target}) ->
Expand Down Expand Up @@ -506,10 +509,10 @@ class MinimapElement extends HTMLElement
#
# event - The {Event} object.
startDrag: ({which, pageY}) ->
if which is 2
@mousePressedOverCanvas({which, pageY})
# if which is 2
# @middleMousePressedOverCanvas({pageY})

return if which isnt 1
return if which isnt 1 and which isnt 2
{top} = @visibleArea.getBoundingClientRect()
{top: offsetTop} = @getBoundingClientRect()

Expand Down Expand Up @@ -538,7 +541,7 @@ class MinimapElement extends HTMLElement
# offsetTop - The {MinimapElement} offset at the moment of the
# drag start.
drag: (e, initial) ->
return if e.which isnt 1
return if e.which isnt 1 and e.which isnt 2
y = e.pageY - initial.offsetTop - initial.dragOffset

ratio = y / (@minimap.getVisibleHeight() - @minimap.getTextEditorScaledHeight())
Expand Down
48 changes: 36 additions & 12 deletions spec/minimap-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,13 @@ describe 'MinimapElement', ->
expect(editorElement.component.presenter.setScrollTop).toHaveBeenCalled()

describe 'middle clicking the minimap', ->
[canvas, visibleArea, originalTop, originalLeft, maxScroll] = []
[canvas, visibleArea, originalLeft, maxScroll] = []

beforeEach ->
canvas = minimapElement.canvas
visibleArea = minimapElement.visibleArea
{top, left} = visibleArea.getBoundingClientRect()
[originalTop, originalLeft] = [top, left]
{left} = visibleArea.getBoundingClientRect()
[originalLeft] = [left]
maxScroll = minimap.getTextEditorMaxScrollTop()

it 'scrolls to the top using the middle mouse button', ->
Expand All @@ -339,17 +339,41 @@ describe 'MinimapElement', ->
visibleCenterY = top + (height / 2)
expect(visibleCenterY).toBeCloseTo(canvasMidY, 0)

it 'scrolls the editor to an arbitrary location', ->
scrollTo = 100 # pixels
scrollRatio = (scrollTo - minimap.getTextEditorScaledHeight()/2) /
(minimap.getVisibleHeight() - minimap.getTextEditorScaledHeight())
scrollRatio = Math.max(0, scrollRatio)
scrollRatio = Math.min(1, scrollRatio)
describe 'scrolling the editor to an arbitrary location', ->
[scrollTo, scrollRatio] = []

beforeEach ->
scrollTo = 100 # pixels
scrollRatio = (scrollTo - minimap.getTextEditorScaledHeight()/2) /
(minimap.getVisibleHeight() - minimap.getTextEditorScaledHeight())
scrollRatio = Math.max(0, scrollRatio)
scrollRatio = Math.min(1, scrollRatio)

mousedown(canvas, x: originalLeft + 1, y: scrollTo, btn: 1)
nextAnimationFrame()

it 'scrolls the editor to an arbitrary location', ->
expectedScroll = maxScroll * scrollRatio
expect(editor.getScrollTop()).toBeCloseTo(expectedScroll, 0)

mousedown(canvas, x: originalLeft + 1, y: scrollTo, btn: 1)
describe 'dragging the visible area with middle mouse button ' +
'after scrolling to the arbitrary location', ->
[originalTop] = []

beforeEach ->
{top} = visibleArea.getBoundingClientRect()
originalTop = top
mousemove(visibleArea, x: originalLeft + 1, y: scrollTo + 40)

nextAnimationFrame()

afterEach ->
minimapElement.endDrag()

expectedScroll = maxScroll * scrollRatio
expect(editor.getScrollTop()).toBeCloseTo(expectedScroll, 0)
it 'scrolls the editor so that the visible area was moved down ' +
'by 40 pixels from the arbitrary location', ->
{top} = visibleArea.getBoundingClientRect()
expect(top).toBeCloseTo(originalTop + 40, -1)

describe 'pressing the mouse on the minimap canvas (without scroll animation)', ->
beforeEach ->
Expand Down

0 comments on commit 86c155b

Please sign in to comment.