@varlet/touch-emulator 1.24.5 → 1.24.6-alpha.1641735742059

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 +68 -63
  3. package/package.json +1 -1
package/iife.js ADDED
@@ -0,0 +1,92 @@
1
+ const inBrowser = typeof window !== 'undefined'
2
+ const supportTouch = inBrowser && 'ontouchstart' in window
3
+ let initiated = false
4
+ let eventTarget
5
+
6
+ const isMousedown = (eventType) => eventType === 'mousedown'
7
+
8
+ const isMousemove = (eventType) => eventType === 'mousemove'
9
+
10
+ const isMouseup = (eventType) => eventType === 'mouseup'
11
+
12
+ const isUpdateTarget = (eventType) =>
13
+ isMousedown(eventType) || !eventTarget || (eventTarget && !eventTarget.dispatchEvent)
14
+
15
+ function Touch(target, identifier, mouseEvent) {
16
+ const { clientX, clientY, screenX, screenY, pageX, pageY } = mouseEvent
17
+
18
+ this.identifier = identifier
19
+ this.target = target
20
+ this.clientX = clientX
21
+ this.clientY = clientY
22
+ this.screenX = screenX
23
+ this.screenY = screenY
24
+ this.pageX = pageX
25
+ this.pageY = pageY
26
+ }
27
+
28
+ function updateTouchList(mouseEvent) {
29
+ const touchList = createTouchList()
30
+
31
+ touchList.push(new Touch(eventTarget, 1, mouseEvent))
32
+ return touchList
33
+ }
34
+
35
+ function createTouchList() {
36
+ const touchList = []
37
+
38
+ touchList.item = function (index) {
39
+ return this[index] || null
40
+ }
41
+
42
+ return touchList
43
+ }
44
+
45
+ function getActiveTouches(mouseEvent) {
46
+ const { type } = mouseEvent
47
+ if (isMouseup(type)) return createTouchList()
48
+ return updateTouchList(mouseEvent)
49
+ }
50
+
51
+ function triggerTouch(touchType, mouseEvent) {
52
+ const { altKey, ctrlKey, metaKey, shiftKey } = mouseEvent
53
+ const touchEvent = document.createEvent('Event')
54
+ touchEvent.initEvent(touchType, true, true)
55
+
56
+ touchEvent.altKey = altKey
57
+ touchEvent.ctrlKey = ctrlKey
58
+ touchEvent.metaKey = metaKey
59
+ touchEvent.shiftKey = shiftKey
60
+
61
+ touchEvent.touches = getActiveTouches(mouseEvent)
62
+ touchEvent.targetTouches = getActiveTouches(mouseEvent)
63
+ touchEvent.changedTouches = createTouchList(mouseEvent)
64
+
65
+ eventTarget.dispatchEvent(touchEvent)
66
+ }
67
+
68
+ function onMouse(mouseEvent, touchType) {
69
+ const { type, target } = mouseEvent
70
+
71
+ initiated = isMousedown(type) ? true : isMouseup(type) ? false : initiated
72
+
73
+ if (isMousemove(type) && !initiated) return
74
+
75
+ if (isUpdateTarget(type)) eventTarget = target
76
+
77
+ triggerTouch(touchType, mouseEvent)
78
+
79
+ if (isMouseup(type)) eventTarget = null
80
+ }
81
+
82
+ function createTouchEmulator() {
83
+ window.addEventListener('mousedown', (event) => onMouse(event, 'touchstart'), true)
84
+ window.addEventListener('mousemove', (event) => onMouse(event, 'touchmove'), true)
85
+ window.addEventListener('mouseup', (event) => onMouse(event, 'touchend'), true)
86
+ }
87
+
88
+ if (inBrowser && !supportTouch) {
89
+ createTouchEmulator()
90
+ }
91
+
92
+ module.exports = {}
package/index.js CHANGED
@@ -1,87 +1,92 @@
1
- const supportTouch = 'ontouchstart' in window
2
- let initiated = false
3
- let eventTarget
1
+ ;(() => {
2
+ const inBrowser = typeof window !== 'undefined'
3
+ const supportTouch = inBrowser && 'ontouchstart' in window
4
+ let initiated = false
5
+ let eventTarget
4
6
 
5
- const isMousedown = (eventType) => eventType === 'mousedown'
7
+ const isMousedown = (eventType) => eventType === 'mousedown'
6
8
 
7
- const isMousemove = (eventType) => eventType === 'mousemove'
9
+ const isMousemove = (eventType) => eventType === 'mousemove'
8
10
 
9
- const isMouseup = (eventType) => eventType === 'mouseup'
11
+ const isMouseup = (eventType) => eventType === 'mouseup'
10
12
 
11
- const isUpdateTarget = (eventType) =>
12
- isMousedown(eventType) || !eventTarget || (eventTarget && !eventTarget.dispatchEvent)
13
+ const isUpdateTarget = (eventType) =>
14
+ isMousedown(eventType) || !eventTarget || (eventTarget && !eventTarget.dispatchEvent)
13
15
 
14
- function Touch(target, identifier, mouseEvent) {
15
- const { clientX, clientY, screenX, screenY, pageX, pageY } = mouseEvent
16
+ function Touch(target, identifier, mouseEvent) {
17
+ const { clientX, clientY, screenX, screenY, pageX, pageY } = mouseEvent
16
18
 
17
- this.identifier = identifier
18
- this.target = target
19
- this.clientX = clientX
20
- this.clientY = clientY
21
- this.screenX = screenX
22
- this.screenY = screenY
23
- this.pageX = pageX
24
- this.pageY = pageY
25
- }
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
+ }
26
28
 
27
- function updateTouchList(mouseEvent) {
28
- const touchList = createTouchList()
29
+ function updateTouchList(mouseEvent) {
30
+ const touchList = createTouchList()
29
31
 
30
- touchList.push(new Touch(eventTarget, 1, mouseEvent))
31
- return touchList
32
- }
32
+ touchList.push(new Touch(eventTarget, 1, mouseEvent))
33
+ return touchList
34
+ }
33
35
 
34
- function createTouchList() {
35
- const touchList = []
36
+ function createTouchList() {
37
+ const touchList = []
36
38
 
37
- touchList.item = function (index) {
38
- return this[index] || null
39
- }
39
+ touchList.item = function (index) {
40
+ return this[index] || null
41
+ }
40
42
 
41
- return touchList
42
- }
43
+ return touchList
44
+ }
43
45
 
44
- function getActiveTouches(mouseEvent) {
45
- const { type } = mouseEvent
46
- if (isMouseup(type)) return createTouchList()
47
- return updateTouchList(mouseEvent)
48
- }
46
+ function getActiveTouches(mouseEvent) {
47
+ const { type } = mouseEvent
48
+ if (isMouseup(type)) return createTouchList()
49
+ return updateTouchList(mouseEvent)
50
+ }
49
51
 
50
- function triggerTouch(touchType, mouseEvent) {
51
- const { altKey, ctrlKey, metaKey, shiftKey } = mouseEvent
52
- const touchEvent = document.createEvent('Event')
53
- touchEvent.initEvent(touchType, true, true)
52
+ function triggerTouch(touchType, mouseEvent) {
53
+ const { altKey, ctrlKey, metaKey, shiftKey } = mouseEvent
54
+ const touchEvent = document.createEvent('Event')
55
+ touchEvent.initEvent(touchType, true, true)
54
56
 
55
- touchEvent.altKey = altKey
56
- touchEvent.ctrlKey = ctrlKey
57
- touchEvent.metaKey = metaKey
58
- touchEvent.shiftKey = shiftKey
57
+ touchEvent.altKey = altKey
58
+ touchEvent.ctrlKey = ctrlKey
59
+ touchEvent.metaKey = metaKey
60
+ touchEvent.shiftKey = shiftKey
59
61
 
60
- touchEvent.touches = getActiveTouches(mouseEvent)
61
- touchEvent.targetTouches = getActiveTouches(mouseEvent)
62
- touchEvent.changedTouches = createTouchList(mouseEvent)
62
+ touchEvent.touches = getActiveTouches(mouseEvent)
63
+ touchEvent.targetTouches = getActiveTouches(mouseEvent)
64
+ touchEvent.changedTouches = createTouchList(mouseEvent)
63
65
 
64
- eventTarget.dispatchEvent(touchEvent)
65
- }
66
+ eventTarget.dispatchEvent(touchEvent)
67
+ }
66
68
 
67
- function onMouse(mouseEvent, touchType) {
68
- const { type, target } = mouseEvent
69
+ function onMouse(mouseEvent, touchType) {
70
+ const { type, target } = mouseEvent
69
71
 
70
- initiated = isMousedown(type) ? true : isMouseup(type) ? false : initiated
72
+ initiated = isMousedown(type) ? true : isMouseup(type) ? false : initiated
71
73
 
72
- if (isMousemove(type) && !initiated) return
74
+ if (isMousemove(type) && !initiated) return
73
75
 
74
- if (isUpdateTarget(type)) eventTarget = target
76
+ if (isUpdateTarget(type)) eventTarget = target
75
77
 
76
- triggerTouch(touchType, mouseEvent)
78
+ triggerTouch(touchType, mouseEvent)
77
79
 
78
- if (isMouseup(type)) eventTarget = null
79
- }
80
+ if (isMouseup(type)) eventTarget = null
81
+ }
80
82
 
81
- function createTouchEmulator() {
82
- window.addEventListener('mousedown', (event) => onMouse(event, 'touchstart'), true)
83
- window.addEventListener('mousemove', (event) => onMouse(event, 'touchmove'), true)
84
- window.addEventListener('mouseup', (event) => onMouse(event, 'touchend'), true)
85
- }
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
+ }
86
88
 
87
- if (!supportTouch) createTouchEmulator()
89
+ if (inBrowser && !supportTouch) {
90
+ createTouchEmulator()
91
+ }
92
+ })()
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.6-alpha.1641735742059",
4
4
  "description": "touch-emulator",
5
5
  "keywords": [
6
6
  "emulator",