-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import isUndef from 'licia/isUndef' | ||
import toBool from 'licia/toBool' | ||
import Protocol from 'devtools-protocol' | ||
import Input = Protocol.Input | ||
|
||
let isClick = false | ||
|
||
export function emulateTouchFromMouseEvent( | ||
params: Input.EmulateTouchFromMouseEventRequest | ||
) { | ||
const { type, x, y, deltaX, deltaY } = params | ||
|
||
const el = document.elementFromPoint(x, y) || document.documentElement | ||
|
||
switch (type) { | ||
case 'mousePressed': | ||
isClick = true | ||
triggerTouchEvent('touchstart', el, x, y) | ||
break | ||
case 'mouseMoved': | ||
isClick = false | ||
triggerTouchEvent('touchmove', el, x, y) | ||
break | ||
case 'mouseReleased': | ||
triggerTouchEvent('touchend', el, x, y) | ||
if (isClick) { | ||
el.dispatchEvent( | ||
new MouseEvent('click', { | ||
bubbles: true, | ||
cancelable: true, | ||
view: window, | ||
}) | ||
) | ||
} | ||
isClick = false | ||
break | ||
case 'mouseWheel': | ||
if (!isUndef(deltaX) && !isUndef(deltaY)) { | ||
triggerScroll(el, deltaX, deltaY) | ||
} | ||
break | ||
} | ||
} | ||
|
||
function triggerTouchEvent(type: string, el: Element, x: number, y: number) { | ||
const touch = new Touch({ | ||
identifier: 0, | ||
target: el, | ||
clientX: x, | ||
clientY: y, | ||
force: 1, | ||
}) | ||
|
||
el.dispatchEvent( | ||
new TouchEvent(type, { | ||
bubbles: true, | ||
touches: [touch], | ||
changedTouches: [touch], | ||
targetTouches: [touch], | ||
}) | ||
) | ||
} | ||
|
||
function triggerScroll(el: Element, deltaX: number, deltaY: number) { | ||
el = findScrollableEl(el, deltaX, deltaY) | ||
el.scrollLeft -= deltaX | ||
el.scrollTop -= deltaY | ||
} | ||
|
||
function findScrollableEl(el: Element | null, deltaX: number, deltaY: number) { | ||
while (el) { | ||
if (toBool(deltaX) && isScrollable(el, 'x')) { | ||
return el | ||
} | ||
if (toBool(deltaY) && isScrollable(el, 'y')) { | ||
return el | ||
} | ||
|
||
el = el.parentElement | ||
} | ||
|
||
return el || document.documentElement | ||
} | ||
|
||
function isScrollable(el: Element, direction: 'x' | 'y') { | ||
const computedStyle = getComputedStyle(el) | ||
|
||
if (direction === 'x') { | ||
return ( | ||
el.scrollWidth > el.clientWidth && computedStyle.overflowX !== 'hidden' | ||
) | ||
} | ||
|
||
return ( | ||
el.scrollHeight > el.clientHeight && computedStyle.overflowY !== 'hidden' | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters