-
Notifications
You must be signed in to change notification settings - Fork 244
/
pointerPress.ts
363 lines (316 loc) Β· 8.8 KB
/
pointerPress.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* eslint-disable complexity */
import {
ApiLevel,
assertPointerEvents,
focus,
isDisabled,
isElementType,
setLevelRef,
} from '../utils'
import {getUIValue, setUISelection} from '../document'
import {EventType} from '../event'
import {Config} from '../setup'
import type {
pointerKey,
pointerState,
PointerTarget,
SelectionTarget,
} from './types'
import {resolveSelectionTarget} from './resolveSelectionTarget'
import {firePointerEvent} from './firePointerEvents'
export interface PointerPressAction extends PointerTarget, SelectionTarget {
keyDef: pointerKey
releasePrevious: boolean
releaseSelf: boolean
}
export async function pointerPress(
config: Config,
action: PointerPressAction,
): Promise<void> {
const {keyDef, target, releasePrevious, releaseSelf} = action
const previous = config.pointerState.pressed.find(p => p.keyDef === keyDef)
const pointerName =
keyDef.pointerType === 'touch' ? keyDef.name : keyDef.pointerType
const targetIsDisabled = isDisabled(target)
if (previous) {
up(config, pointerName, action, previous, targetIsDisabled)
}
if (!releasePrevious) {
const press = down(config, pointerName, action, targetIsDisabled)
if (releaseSelf) {
up(config, pointerName, action, press, targetIsDisabled)
}
}
}
function getNextPointerId(state: pointerState) {
state.pointerId = state.pointerId + 1
return state.pointerId
}
function down(
config: Config,
pointerName: string,
{keyDef, node, offset, target, coords}: PointerPressAction,
targetIsDisabled: boolean,
) {
setLevelRef(config, ApiLevel.Trigger)
assertPointerEvents(config, target)
const {pointerState} = config
const {name, pointerType, button} = keyDef
const pointerId = pointerType === 'mouse' ? 1 : getNextPointerId(pointerState)
pointerState.position[pointerName] = {
...pointerState.position[pointerName],
pointerId,
pointerType,
target,
coords,
}
let isMultiTouch = false
let isPrimary = true
if (pointerType !== 'mouse') {
for (const obj of pointerState.pressed) {
// TODO: test multi device input across browsers
// istanbul ignore else
if (obj.keyDef.pointerType === pointerType) {
obj.isMultiTouch = true
isMultiTouch = true
isPrimary = false
}
}
}
if (pointerState.activeClickCount?.[0] !== name) {
delete pointerState.activeClickCount
}
const clickCount = Number(pointerState.activeClickCount?.[1] ?? 0) + 1
pointerState.activeClickCount = [name, clickCount]
const pressObj = {
keyDef,
downTarget: target,
pointerId,
unpreventedDefault: true,
isMultiTouch,
isPrimary,
clickCount,
}
pointerState.pressed.push(pressObj)
if (pointerType !== 'mouse') {
fire('pointerover')
fire('pointerenter')
}
if (
pointerType !== 'mouse' ||
!pointerState.pressed.some(
p => p.keyDef !== keyDef && p.keyDef.pointerType === pointerType,
)
) {
fire('pointerdown')
}
// TODO: touch...
if (pointerType === 'mouse') {
if (!targetIsDisabled) {
pressObj.unpreventedDefault = fire('mousedown')
}
if (pressObj.unpreventedDefault) {
mousedownDefaultBehavior({
target,
targetIsDisabled,
clickCount,
position: pointerState.position[pointerName],
node,
offset,
})
}
if (button === 'secondary') {
fire('contextmenu')
}
}
return pressObj
function fire(type: EventType) {
return firePointerEvent(config, target, type, {
button,
clickCount,
coords,
isPrimary,
pointerId,
pointerType,
})
}
}
function up(
config: Config,
pointerName: string,
{
keyDef: {pointerType, button},
target,
coords,
node,
offset,
}: PointerPressAction,
pressed: pointerState['pressed'][number],
targetIsDisabled: boolean,
) {
setLevelRef(config, ApiLevel.Trigger)
assertPointerEvents(config, target)
const {pointerState} = config
pointerState.pressed = pointerState.pressed.filter(p => p !== pressed)
const {isMultiTouch, isPrimary, pointerId, clickCount} = pressed
let {unpreventedDefault} = pressed
pointerState.position[pointerName] = {
...pointerState.position[pointerName],
target,
coords,
}
// TODO: pointerleave for touch device
if (
pointerType !== 'mouse' ||
!pointerState.pressed.filter(p => p.keyDef.pointerType === pointerType)
.length
) {
fire('pointerup')
}
if (pointerType !== 'mouse') {
fire('pointerout')
fire('pointerleave')
}
if (pointerType !== 'mouse' && !isMultiTouch) {
if (!targetIsDisabled) {
if (clickCount === 1) {
fire('mouseover')
fire('mouseenter')
}
fire('mousemove')
unpreventedDefault = fire('mousedown') && unpreventedDefault
}
if (unpreventedDefault) {
mousedownDefaultBehavior({
target,
targetIsDisabled,
clickCount,
position: pointerState.position[pointerName],
node,
offset,
})
}
}
delete pointerState.position[pointerName].selectionRange
if (!targetIsDisabled) {
if (pointerType === 'mouse' || !isMultiTouch) {
unpreventedDefault = fire('mouseup') && unpreventedDefault
const canClick = pointerType !== 'mouse' || button === 'primary'
if (canClick && target === pressed.downTarget) {
fire('click')
if (clickCount === 2) {
fire('dblclick')
}
}
}
}
function fire(type: EventType) {
return firePointerEvent(config, target, type, {
button,
clickCount,
coords,
isPrimary,
pointerId,
pointerType,
})
}
}
function mousedownDefaultBehavior({
position,
target,
targetIsDisabled,
clickCount,
node,
offset,
}: {
position: NonNullable<pointerState['position']['string']>
target: Element
targetIsDisabled: boolean
clickCount: number
node?: Node
offset?: number
}) {
// The closest focusable element is focused when a `mousedown` would have been fired.
// Even if there was no `mousedown` because the element was disabled.
// A `mousedown` that preventsDefault cancels this though.
focus(target)
// TODO: What happens if a focus event handler interfers?
// An unprevented mousedown moves the cursor to the closest character.
// We try to approximate the behavior for a no-layout environment.
if (!targetIsDisabled) {
const hasValue = isElementType(target, ['input', 'textarea'])
// On non-input elements the text selection per multiple click
// can extend beyond the target boundaries.
// The exact mechanism what is considered in the same line is unclear.
// Looks it might be every inline element.
// TODO: Check what might be considered part of the same line of text.
const text = String(hasValue ? getUIValue(target) : target.textContent)
const [start, end] = node
? // As offset is describing a DOMOffset it is non-trivial to determine
// which elements might be considered in the same line of text.
// TODO: support expanding initial range on multiple clicks if node is given
[offset, offset]
: getTextRange(text, offset, clickCount)
// TODO: implement modifying selection per shift/ctrl+mouse
if (hasValue) {
setUISelection(target, {
anchorOffset: start ?? text.length,
focusOffset: end ?? text.length,
})
position.selectionRange = {
node: target,
start: start ?? 0,
end: end ?? text.length,
}
} else {
const {node: startNode, offset: startOffset} = resolveSelectionTarget({
target,
node,
offset: start,
})
const {node: endNode, offset: endOffset} = resolveSelectionTarget({
target,
node,
offset: end,
})
const range = new Range()
range.setStart(startNode, startOffset)
range.setEnd(endNode, endOffset)
position.selectionRange = range
const selection = target.ownerDocument.getSelection() as Selection
selection.removeAllRanges()
selection.addRange(range.cloneRange())
}
}
}
function getTextRange(
text: string,
pos: number | undefined,
clickCount: number,
) {
if (clickCount % 3 === 1 || text.length === 0) {
return [pos, pos]
}
const textPos = pos ?? text.length
if (clickCount % 3 === 2) {
return [
textPos -
(text.substr(0, pos).match(/(\w+|\s+|\W)?$/) as RegExpMatchArray)[0]
.length,
pos === undefined
? pos
: pos +
(text.substr(pos).match(/^(\w+|\s+|\W)?/) as RegExpMatchArray)[0]
.length,
]
}
// triple click
return [
textPos -
(text.substr(0, pos).match(/[^\r\n]*$/) as RegExpMatchArray)[0].length,
pos === undefined
? pos
: pos +
(text.substr(pos).match(/^[^\r\n]*/) as RegExpMatchArray)[0].length,
]
}