-
-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(touch-emulator): add iife script
- Loading branch information
Showing
2 changed files
with
160 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const inBrowser = typeof window !== 'undefined' | ||
const supportTouch = inBrowser && 'ontouchstart' in window | ||
let initiated = false | ||
let eventTarget | ||
|
||
const isMousedown = (eventType) => eventType === 'mousedown' | ||
|
||
const isMousemove = (eventType) => eventType === 'mousemove' | ||
|
||
const isMouseup = (eventType) => eventType === 'mouseup' | ||
|
||
const isUpdateTarget = (eventType) => | ||
isMousedown(eventType) || !eventTarget || (eventTarget && !eventTarget.dispatchEvent) | ||
|
||
function Touch(target, identifier, mouseEvent) { | ||
const { clientX, clientY, screenX, screenY, pageX, pageY } = mouseEvent | ||
|
||
this.identifier = identifier | ||
this.target = target | ||
this.clientX = clientX | ||
this.clientY = clientY | ||
this.screenX = screenX | ||
this.screenY = screenY | ||
this.pageX = pageX | ||
this.pageY = pageY | ||
} | ||
|
||
function updateTouchList(mouseEvent) { | ||
const touchList = createTouchList() | ||
|
||
touchList.push(new Touch(eventTarget, 1, mouseEvent)) | ||
return touchList | ||
} | ||
|
||
function createTouchList() { | ||
const touchList = [] | ||
|
||
touchList.item = function (index) { | ||
return this[index] || null | ||
} | ||
|
||
return touchList | ||
} | ||
|
||
function getActiveTouches(mouseEvent) { | ||
const { type } = mouseEvent | ||
if (isMouseup(type)) return createTouchList() | ||
return updateTouchList(mouseEvent) | ||
} | ||
|
||
function triggerTouch(touchType, mouseEvent) { | ||
const { altKey, ctrlKey, metaKey, shiftKey } = mouseEvent | ||
const touchEvent = document.createEvent('Event') | ||
touchEvent.initEvent(touchType, true, true) | ||
|
||
touchEvent.altKey = altKey | ||
touchEvent.ctrlKey = ctrlKey | ||
touchEvent.metaKey = metaKey | ||
touchEvent.shiftKey = shiftKey | ||
|
||
touchEvent.touches = getActiveTouches(mouseEvent) | ||
touchEvent.targetTouches = getActiveTouches(mouseEvent) | ||
touchEvent.changedTouches = createTouchList(mouseEvent) | ||
|
||
eventTarget.dispatchEvent(touchEvent) | ||
} | ||
|
||
function onMouse(mouseEvent, touchType) { | ||
const { type, target } = mouseEvent | ||
|
||
initiated = isMousedown(type) ? true : isMouseup(type) ? false : initiated | ||
|
||
if (isMousemove(type) && !initiated) return | ||
|
||
if (isUpdateTarget(type)) eventTarget = target | ||
|
||
triggerTouch(touchType, mouseEvent) | ||
|
||
if (isMouseup(type)) eventTarget = null | ||
} | ||
|
||
function createTouchEmulator() { | ||
window.addEventListener('mousedown', (event) => onMouse(event, 'touchstart'), true) | ||
window.addEventListener('mousemove', (event) => onMouse(event, 'touchmove'), true) | ||
window.addEventListener('mouseup', (event) => onMouse(event, 'touchend'), true) | ||
} | ||
|
||
if (inBrowser && !supportTouch) { | ||
createTouchEmulator() | ||
} | ||
|
||
module.exports = {} |
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 |
---|---|---|
@@ -1,92 +1,92 @@ | ||
const inBrowser = typeof window !== 'undefined' | ||
const supportTouch = inBrowser && 'ontouchstart' in window | ||
let initiated = false | ||
let eventTarget | ||
;(() => { | ||
const inBrowser = typeof window !== 'undefined' | ||
const supportTouch = inBrowser && 'ontouchstart' in window | ||
let initiated = false | ||
let eventTarget | ||
|
||
const isMousedown = (eventType) => eventType === 'mousedown' | ||
const isMousedown = (eventType) => eventType === 'mousedown' | ||
|
||
const isMousemove = (eventType) => eventType === 'mousemove' | ||
const isMousemove = (eventType) => eventType === 'mousemove' | ||
|
||
const isMouseup = (eventType) => eventType === 'mouseup' | ||
const isMouseup = (eventType) => eventType === 'mouseup' | ||
|
||
const isUpdateTarget = (eventType) => | ||
isMousedown(eventType) || !eventTarget || (eventTarget && !eventTarget.dispatchEvent) | ||
const isUpdateTarget = (eventType) => | ||
isMousedown(eventType) || !eventTarget || (eventTarget && !eventTarget.dispatchEvent) | ||
|
||
function Touch(target, identifier, mouseEvent) { | ||
const { clientX, clientY, screenX, screenY, pageX, pageY } = mouseEvent | ||
function Touch(target, identifier, mouseEvent) { | ||
const { clientX, clientY, screenX, screenY, pageX, pageY } = mouseEvent | ||
|
||
this.identifier = identifier | ||
this.target = target | ||
this.clientX = clientX | ||
this.clientY = clientY | ||
this.screenX = screenX | ||
this.screenY = screenY | ||
this.pageX = pageX | ||
this.pageY = pageY | ||
} | ||
|
||
function updateTouchList(mouseEvent) { | ||
const touchList = createTouchList() | ||
|
||
touchList.push(new Touch(eventTarget, 1, mouseEvent)) | ||
return touchList | ||
} | ||
this.identifier = identifier | ||
this.target = target | ||
this.clientX = clientX | ||
this.clientY = clientY | ||
this.screenX = screenX | ||
this.screenY = screenY | ||
this.pageX = pageX | ||
this.pageY = pageY | ||
} | ||
|
||
function createTouchList() { | ||
const touchList = [] | ||
function updateTouchList(mouseEvent) { | ||
const touchList = createTouchList() | ||
|
||
touchList.item = function (index) { | ||
return this[index] || null | ||
touchList.push(new Touch(eventTarget, 1, mouseEvent)) | ||
return touchList | ||
} | ||
|
||
return touchList | ||
} | ||
function createTouchList() { | ||
const touchList = [] | ||
|
||
function getActiveTouches(mouseEvent) { | ||
const { type } = mouseEvent | ||
if (isMouseup(type)) return createTouchList() | ||
return updateTouchList(mouseEvent) | ||
} | ||
touchList.item = function (index) { | ||
return this[index] || null | ||
} | ||
|
||
return touchList | ||
} | ||
|
||
function triggerTouch(touchType, mouseEvent) { | ||
const { altKey, ctrlKey, metaKey, shiftKey } = mouseEvent | ||
const touchEvent = document.createEvent('Event') | ||
touchEvent.initEvent(touchType, true, true) | ||
function getActiveTouches(mouseEvent) { | ||
const { type } = mouseEvent | ||
if (isMouseup(type)) return createTouchList() | ||
return updateTouchList(mouseEvent) | ||
} | ||
|
||
touchEvent.altKey = altKey | ||
touchEvent.ctrlKey = ctrlKey | ||
touchEvent.metaKey = metaKey | ||
touchEvent.shiftKey = shiftKey | ||
function triggerTouch(touchType, mouseEvent) { | ||
const { altKey, ctrlKey, metaKey, shiftKey } = mouseEvent | ||
const touchEvent = document.createEvent('Event') | ||
touchEvent.initEvent(touchType, true, true) | ||
|
||
touchEvent.touches = getActiveTouches(mouseEvent) | ||
touchEvent.targetTouches = getActiveTouches(mouseEvent) | ||
touchEvent.changedTouches = createTouchList(mouseEvent) | ||
touchEvent.altKey = altKey | ||
touchEvent.ctrlKey = ctrlKey | ||
touchEvent.metaKey = metaKey | ||
touchEvent.shiftKey = shiftKey | ||
|
||
eventTarget.dispatchEvent(touchEvent) | ||
} | ||
touchEvent.touches = getActiveTouches(mouseEvent) | ||
touchEvent.targetTouches = getActiveTouches(mouseEvent) | ||
touchEvent.changedTouches = createTouchList(mouseEvent) | ||
|
||
function onMouse(mouseEvent, touchType) { | ||
const { type, target } = mouseEvent | ||
eventTarget.dispatchEvent(touchEvent) | ||
} | ||
|
||
initiated = isMousedown(type) ? true : isMouseup(type) ? false : initiated | ||
function onMouse(mouseEvent, touchType) { | ||
const { type, target } = mouseEvent | ||
|
||
if (isMousemove(type) && !initiated) return | ||
initiated = isMousedown(type) ? true : isMouseup(type) ? false : initiated | ||
|
||
if (isUpdateTarget(type)) eventTarget = target | ||
if (isMousemove(type) && !initiated) return | ||
|
||
triggerTouch(touchType, mouseEvent) | ||
if (isUpdateTarget(type)) eventTarget = target | ||
|
||
if (isMouseup(type)) eventTarget = null | ||
} | ||
triggerTouch(touchType, mouseEvent) | ||
|
||
function createTouchEmulator() { | ||
window.addEventListener('mousedown', (event) => onMouse(event, 'touchstart'), true) | ||
window.addEventListener('mousemove', (event) => onMouse(event, 'touchmove'), true) | ||
window.addEventListener('mouseup', (event) => onMouse(event, 'touchend'), true) | ||
} | ||
if (isMouseup(type)) eventTarget = null | ||
} | ||
|
||
if (inBrowser && !supportTouch) { | ||
createTouchEmulator() | ||
} | ||
function createTouchEmulator() { | ||
window.addEventListener('mousedown', (event) => onMouse(event, 'touchstart'), true) | ||
window.addEventListener('mousemove', (event) => onMouse(event, 'touchmove'), true) | ||
window.addEventListener('mouseup', (event) => onMouse(event, 'touchend'), true) | ||
} | ||
|
||
module.exports = {} | ||
if (inBrowser && !supportTouch) { | ||
createTouchEmulator() | ||
} | ||
})() |