-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1372 from didi/refactor-web-event
Refactor web event
- Loading branch information
Showing
13 changed files
with
119 additions
and
121 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
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
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
100 changes: 100 additions & 0 deletions
100
packages/webpack-plugin/lib/runtime/components/web/event.js
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,100 @@ | ||
import { extendEvent } from './getInnerListeners' | ||
import { isBrowser } from '../../env' | ||
|
||
function MpxEvent (layer) { | ||
this.targetElement = null | ||
|
||
this.touches = [] | ||
|
||
this.touchStartX = 0 | ||
|
||
this.touchStartY = 0 | ||
|
||
this.startTimer = null | ||
|
||
this.needTap = true | ||
|
||
this.isTouchDevice = document && ('ontouchstart' in document.documentElement) | ||
|
||
this.onTouchStart = (event) => { | ||
if (event.targetTouches?.length > 1) { | ||
return true | ||
} | ||
|
||
this.touches = event.targetTouches | ||
this.targetElement = event.target | ||
this.needTap = true | ||
this.startTimer = null | ||
this.touchStartX = this.touches[0].pageX | ||
this.touchStartY = this.touches[0].pageY | ||
this.startTimer = setTimeout(() => { | ||
this.needTap = false | ||
this.sendEvent(this.targetElement, 'longpress', event) | ||
this.sendEvent(this.targetElement, 'longtap', event) | ||
}, 350) | ||
} | ||
|
||
this.onTouchMove = (event) => { | ||
const touch = event.changedTouches[0] | ||
if (Math.abs(touch.pageX - this.touchStartX) > 1 || Math.abs(touch.pageY - this.touchStartY) > 1) { | ||
this.needTap = false | ||
this.startTimer && clearTimeout(this.startTimer) | ||
this.startTimer = null | ||
} | ||
} | ||
|
||
this.onTouchEnd = (event) => { | ||
if (event.targetTouches?.length > 1) { | ||
return true | ||
} | ||
this.startTimer && clearTimeout(this.startTimer) | ||
this.startTimer = null | ||
if (this.needTap) { | ||
this.sendEvent(this.targetElement, 'tap', event) | ||
} | ||
} | ||
|
||
this.onClick = (event) => { | ||
this.targetElement = event.target | ||
this.sendEvent(this.targetElement, 'tap', event) | ||
} | ||
this.sendEvent = (targetElement, type, event) => { | ||
// eslint-disable-next-line no-undef | ||
const touchEvent = new TouchEvent(type, { | ||
view: window, | ||
bubbles: true, | ||
cancelable: true | ||
}) | ||
const changedTouches = event.changedTouches || [] | ||
extendEvent(touchEvent, { | ||
timeStamp: event.timeStamp, | ||
currentTarget: event.target, | ||
changedTouches, | ||
touches: changedTouches, | ||
detail: { | ||
// pc端点击事件可能没有changedTouches,所以直接从 event中取 | ||
x: changedTouches[0]?.pageX || event.pageX || 0, | ||
y: changedTouches[0]?.pageY || event.pageY || 0 | ||
} | ||
}) | ||
targetElement && targetElement.dispatchEvent(touchEvent) | ||
} | ||
|
||
this.addListener = () => { | ||
if (this.isTouchDevice) { | ||
layer.addEventListener('touchstart', this.onTouchStart, true) | ||
layer.addEventListener('touchmove', this.onTouchMove, true) | ||
layer.addEventListener('touchend', this.onTouchEnd, true) | ||
} else { | ||
layer.addEventListener('click', this.onClick, true) | ||
} | ||
} | ||
this.addListener() | ||
} | ||
|
||
if (isBrowser) { | ||
document.addEventListener('DOMContentLoaded', function () { | ||
// eslint-disable-next-line no-new | ||
new MpxEvent(document.body) | ||
}, false) | ||
} |
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
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
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