'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
  supportsPassive
} from 'uni-shared'
 
import {
  getTargetDataset
} from 'uni-helpers/index'
 
import {
  wrapperMPEvent
} from 'uni-helpers/patch'
 
import getWindowOffset from 'uni-platform/helpers/get-window-offset'
 
function processTarget (target, detail, checkShadowRoot = false) {
  const res = {
    id: target.id,
    offsetLeft: target.offsetLeft,
    offsetTop: target.offsetTop,
    dataset: getTargetDataset(target)
  }
  if (detail) {
    Object.assign(res, detail)
  }
  return res
}
 
function processTouches (touches) {
  if (touches) {
    const res = []
 
    const {
      top
    } = getWindowOffset()
 
    for (let i = 0; i < touches.length; i++) {
      const touch = touches[i]
      res.push({
        identifier: touch.identifier,
        pageX: touch.pageX,
        pageY: touch.pageY - top,
        clientX: touch.clientX,
        clientY: touch.clientY - top,
        force: touch.force || 0
      })
    }
    return res
  }
  return []
}
 
function isMouseEvent (name) {
  return name.startsWith('mouse') || ['contextmenu'].includes(name)
}
 
export function processEvent (name, $event = {}, detail = {}, target = {}, currentTarget = {}) {
  if ($event._processed) {
    $event.type = detail.type || name
    return $event
  }
 
  // fixed 针对小程序 click(tap)事件,补充事件详情
  if (name === 'click') {
    const {
      top
    } = getWindowOffset()
 
    detail = {
      x: $event.x,
      y: $event.y - top
    }
    $event.touches = $event.changedTouches = [{
      force: 1,
      identifier: 0,
      clientX: $event.clientX,
      clientY: $event.clientY,
      pageX: $event.pageX,
      pageY: $event.pageY
    }]
  }
 
  // fixed mp-vue
  const ret = wrapperMPEvent({
    type: detail.type || name,
    timeStamp: $event.timeStamp || 0,
    detail: detail,
    target: processTarget(target, detail),
    currentTarget: processTarget(currentTarget, false, true),
    // 只处理系统事件
    touches: ($event instanceof Event || $event instanceof CustomEvent) ? processTouches($event.touches) : $event.touches,
    changedTouches: ($event instanceof Event || $event instanceof CustomEvent) ? processTouches($event.changedTouches)
      : $event.changedTouches,
    preventDefault () {},
    stopPropagation () {}
  })
 
  if (isMouseEvent(name)) {
    const {
      top
    } = getWindowOffset()
    ret.pageX = $event.pageX
    ret.pageY = $event.pageY - top
    ret.clientX = $event.clientX
    ret.clientY = $event.clientY - top
  }
 
  if (__PLATFORM__ === 'app-plus') {
    const nid = currentTarget.getAttribute('_i')
    ret.options = {
      nid
    }
    // 保留原始 currentTarget 方便后续对比
    ret.$origCurrentTarget = currentTarget
  }
 
  return ret
}
 
const LONGPRESS_TIMEOUT = 350
const LONGPRESS_THRESHOLD = 10
 
const passiveOptions = supportsPassive ? {
  passive: true
} : false
 
let longPressTimer = false
 
function clearLongPressTimer () {
  if (longPressTimer) {
    clearTimeout(longPressTimer)
    longPressTimer = false
  }
}
 
let startPageX = 0
let startPageY = 0
 
function touchstart (evt) {
  clearLongPressTimer()
  if (evt.touches.length !== 1) {
    return
  }
  const {
    pageX,
    pageY
  } = evt.touches[0]
 
  startPageX = pageX
  startPageY = pageY
 
  longPressTimer = setTimeout(function () {
    const customEvent = new CustomEvent('longpress', {
      bubbles: true,
      cancelable: true,
      target: evt.target,
      currentTarget: evt.currentTarget
    })
    customEvent.touches = evt.touches
    customEvent.changedTouches = evt.changedTouches
    evt.target.dispatchEvent(customEvent)
  }, LONGPRESS_TIMEOUT)
}
 
function touchmove (evt) {
  if (!longPressTimer) {
    return
  }
 
  if (evt.touches.length !== 1) {
    return clearLongPressTimer()
  }
 
  const {
    pageX,
    pageY
  } = evt.touches[0]
 
  if (Math.abs(pageX - startPageX) > LONGPRESS_THRESHOLD || Math.abs(pageY - startPageY) > LONGPRESS_THRESHOLD) {
    return clearLongPressTimer()
  }
}
 
export function initEvents () {
  window.addEventListener('touchstart', touchstart, passiveOptions)
  window.addEventListener('touchmove', touchmove, passiveOptions)
  window.addEventListener('touchend', clearLongPressTimer, passiveOptions)
  window.addEventListener('touchcancel', clearLongPressTimer, passiveOptions)
}