@varlet/touch-emulator 1.24.5 → 1.24.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/iife.js +92 -0
  2. package/index.js +7 -2
  3. package/package.json +1 -1
package/iife.js ADDED
@@ -0,0 +1,92 @@
1
+ ;(() => {
2
+ const inBrowser = typeof window !== 'undefined'
3
+ const supportTouch = inBrowser && 'ontouchstart' in window
4
+ let initiated = false
5
+ let eventTarget
6
+
7
+ const isMousedown = (eventType) => eventType === 'mousedown'
8
+
9
+ const isMousemove = (eventType) => eventType === 'mousemove'
10
+
11
+ const isMouseup = (eventType) => eventType === 'mouseup'
12
+
13
+ const isUpdateTarget = (eventType) =>
14
+ isMousedown(eventType) || !eventTarget || (eventTarget && !eventTarget.dispatchEvent)
15
+
16
+ function Touch(target, identifier, mouseEvent) {
17
+ const { clientX, clientY, screenX, screenY, pageX, pageY } = mouseEvent
18
+
19
+ this.identifier = identifier
20
+ this.target = target
21
+ this.clientX = clientX
22
+ this.clientY = clientY
23
+ this.screenX = screenX
24
+ this.screenY = screenY
25
+ this.pageX = pageX
26
+ this.pageY = pageY
27
+ }
28
+
29
+ function updateTouchList(mouseEvent) {
30
+ const touchList = createTouchList()
31
+
32
+ touchList.push(new Touch(eventTarget, 1, mouseEvent))
33
+ return touchList
34
+ }
35
+
36
+ function createTouchList() {
37
+ const touchList = []
38
+
39
+ touchList.item = function (index) {
40
+ return this[index] || null
41
+ }
42
+
43
+ return touchList
44
+ }
45
+
46
+ function getActiveTouches(mouseEvent) {
47
+ const { type } = mouseEvent
48
+ if (isMouseup(type)) return createTouchList()
49
+ return updateTouchList(mouseEvent)
50
+ }
51
+
52
+ function triggerTouch(touchType, mouseEvent) {
53
+ const { altKey, ctrlKey, metaKey, shiftKey } = mouseEvent
54
+ const touchEvent = document.createEvent('Event')
55
+ touchEvent.initEvent(touchType, true, true)
56
+
57
+ touchEvent.altKey = altKey
58
+ touchEvent.ctrlKey = ctrlKey
59
+ touchEvent.metaKey = metaKey
60
+ touchEvent.shiftKey = shiftKey
61
+
62
+ touchEvent.touches = getActiveTouches(mouseEvent)
63
+ touchEvent.targetTouches = getActiveTouches(mouseEvent)
64
+ touchEvent.changedTouches = createTouchList(mouseEvent)
65
+
66
+ eventTarget.dispatchEvent(touchEvent)
67
+ }
68
+
69
+ function onMouse(mouseEvent, touchType) {
70
+ const { type, target } = mouseEvent
71
+
72
+ initiated = isMousedown(type) ? true : isMouseup(type) ? false : initiated
73
+
74
+ if (isMousemove(type) && !initiated) return
75
+
76
+ if (isUpdateTarget(type)) eventTarget = target
77
+
78
+ triggerTouch(touchType, mouseEvent)
79
+
80
+ if (isMouseup(type)) eventTarget = null
81
+ }
82
+
83
+ function createTouchEmulator() {
84
+ window.addEventListener('mousedown', (event) => onMouse(event, 'touchstart'), true)
85
+ window.addEventListener('mousemove', (event) => onMouse(event, 'touchmove'), true)
86
+ window.addEventListener('mouseup', (event) => onMouse(event, 'touchend'), true)
87
+ }
88
+
89
+ if (inBrowser && !supportTouch) {
90
+ createTouchEmulator()
91
+ }
92
+ })()
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
- const supportTouch = 'ontouchstart' in window
1
+ const inBrowser = typeof window !== 'undefined'
2
+ const supportTouch = inBrowser && 'ontouchstart' in window
2
3
  let initiated = false
3
4
  let eventTarget
4
5
 
@@ -84,4 +85,8 @@ function createTouchEmulator() {
84
85
  window.addEventListener('mouseup', (event) => onMouse(event, 'touchend'), true)
85
86
  }
86
87
 
87
- if (!supportTouch) createTouchEmulator()
88
+ if (inBrowser && !supportTouch) {
89
+ createTouchEmulator()
90
+ }
91
+
92
+ module.exports = {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/touch-emulator",
3
- "version": "1.24.5",
3
+ "version": "1.24.7",
4
4
  "description": "touch-emulator",
5
5
  "keywords": [
6
6
  "emulator",