-
Notifications
You must be signed in to change notification settings - Fork 240
/
pointerAction.ts
56 lines (47 loc) 路 1.73 KB
/
pointerAction.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
import {Config} from '../setup'
import {wait} from '../utils'
import {pointerMove, PointerMoveAction} from './pointerMove'
import {pointerPress, PointerPressAction} from './pointerPress'
import {pointerState, PointerTarget, SelectionTarget} from './types'
export type PointerActionTarget = Partial<PointerTarget> &
Partial<SelectionTarget>
export type PointerAction = PointerActionTarget &
(
| Omit<PointerPressAction, 'target' | 'coords'>
| Omit<PointerMoveAction, 'target' | 'coords'>
)
export async function pointerAction(config: Config, actions: PointerAction[]) {
for (let i = 0; i < actions.length; i++) {
const action = actions[i]
const pointerName =
'pointerName' in action && action.pointerName
? action.pointerName
: 'keyDef' in action
? action.keyDef.pointerType === 'touch'
? action.keyDef.name
: action.keyDef.pointerType
: 'mouse'
const target =
action.target ?? getPrevTarget(pointerName, config.pointerState)
const coords =
action.coords ??
(pointerName in config.pointerState.position
? config.pointerState.position[pointerName].coords
: undefined)
await ('keyDef' in action
? pointerPress(config, {...action, target, coords})
: pointerMove(config, {...action, target, coords}))
if (i < actions.length - 1) {
await wait(config)
}
}
delete config.pointerState.activeClickCount
}
function getPrevTarget(pointerName: string, state: pointerState) {
if (!(pointerName in state.position) || !state.position[pointerName].target) {
throw new Error(
'This pointer has no previous position. Provide a target property!',
)
}
return state.position[pointerName].target as Element
}