Skip to content

Commit

Permalink
fix: make extractTouchEventData a free function
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Dec 29, 2020
1 parent de81494 commit 5ae0dcc
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions lib/minimap-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class MinimapElement {
this.getFrontCanvas(),
{
mousedown: (e) => { this.canvasPressed(extractMouseEventData(e)) },
touchstart: (e) => { this.canvasPressed(this.extractTouchEventData(e)) }
touchstart: (e) => { this.canvasPressed(extractTouchEventData(e)) }
},
{ passive: true }
)
Expand All @@ -533,7 +533,7 @@ class MinimapElement {
this.appendChild(this.visibleArea)
this.visibleAreaSubscription = this.subscribeTo(this.visibleArea, {
mousedown: (e) => { this.startDrag(extractMouseEventData(e)) },
touchstart: (e) => { this.startDrag(this.extractTouchEventData(e)) }
touchstart: (e) => { this.startDrag(extractTouchEventData(e)) }
}, { passive: true })

this.subscriptions.add(this.visibleAreaSubscription)
Expand Down Expand Up @@ -1139,29 +1139,6 @@ class MinimapElement {
this.minimap.setTextEditorScrollTop(ratio * this.minimap.getTextEditorMaxScrollTop())
}

/**
* A method that extracts data from a `TouchEvent` which can then be used to
* process clicks and drags of the minimap.
*
* Used together with `extractMouseEventData` to provide a unified interface
* for `MouseEvent`s and `TouchEvent`s.
*
* @param {TouchEvent} touchEvent the touch event object
* @access private
*/
extractTouchEventData (touchEvent) {
// Use the first touch on the target area. Other touches will be ignored in
// case of multi-touch.
const touch = touchEvent.changedTouches[0]

return {
x: touch.pageX,
y: touch.pageY,
isLeftMouse: true, // Touch is treated like a left mouse button click
isMiddleMouse: false
}
}

/**
* Subscribes to a media query for device pixel ratio changes and forces
* a repaint when it occurs.
Expand Down Expand Up @@ -1211,7 +1188,7 @@ class MinimapElement {
const mousemoveHandler = (e) => this.drag(extractMouseEventData(e), initial)
const mouseupHandler = (e) => this.endDrag()

const touchmoveHandler = (e) => this.drag(this.extractTouchEventData(e), initial)
const touchmoveHandler = (e) => this.drag(extractTouchEventData(e), initial)
const touchendHandler = (e) => this.endDrag()

document.body.addEventListener('mousemove', mousemoveHandler, { passive: true })
Expand Down Expand Up @@ -1296,6 +1273,29 @@ function extractMouseEventData (mouseEvent) {
}
}

/**
* A method that extracts data from a `TouchEvent` which can then be used to
* process clicks and drags of the minimap.
*
* Used together with `extractMouseEventData` to provide a unified interface
* for `MouseEvent`s and `TouchEvent`s.
*
* @param {TouchEvent} touchEvent the touch event object
* @access private
*/
function extractTouchEventData (touchEvent) {
// Use the first touch on the target area. Other touches will be ignored in
// case of multi-touch.
const touch = touchEvent.changedTouches[0]

return {
x: touch.pageX,
y: touch.pageY,
isLeftMouse: true, // Touch is treated like a left mouse button click
isMiddleMouse: false
}
}

// ###### ###### ######
// ## ## ## ## ## ##
// ## ## ##
Expand Down

0 comments on commit 5ae0dcc

Please sign in to comment.