/
pointerMove.ts
146 lines (127 loc) Β· 4.4 KB
/
pointerMove.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
import {setUISelection} from '../document'
import {EventType, PointerCoords} from '../event'
import {Config} from '../setup'
import {
isDescendantOrSelf,
isDisabled,
assertPointerEvents,
setLevelRef,
ApiLevel,
hasPointerEvents,
} from '../utils'
import {firePointerEvent} from './firePointerEvents'
import {resolveSelectionTarget} from './resolveSelectionTarget'
import {PointerTarget, SelectionTarget} from './types'
export interface PointerMoveAction extends PointerTarget, SelectionTarget {
pointerName?: string
}
export async function pointerMove(
config: Config,
{pointerName = 'mouse', target, coords, node, offset}: PointerMoveAction,
): Promise<void> {
const {pointerState} = config
if (!(pointerName in pointerState.position)) {
throw new Error(
`Trying to move pointer "${pointerName}" which does not exist.`,
)
}
const {
pointerId,
pointerType,
target: prevTarget,
coords: prevCoords,
selectionRange,
} = pointerState.position[pointerName]
if (prevTarget && prevTarget !== target) {
setLevelRef(config, ApiLevel.Trigger)
if (hasPointerEvents(config, prevTarget)) {
// Here we could probably calculate a few coords to a fake boundary(?)
fireMove(prevTarget, prevCoords)
if (!isDescendantOrSelf(target, prevTarget)) {
fireLeave(prevTarget, prevCoords)
}
}
}
setLevelRef(config, ApiLevel.Trigger)
assertPointerEvents(config, target)
pointerState.position[pointerName] = {
...pointerState.position[pointerName],
target,
coords,
}
if (prevTarget !== target) {
if (!prevTarget || !isDescendantOrSelf(prevTarget, target)) {
fireEnter(target, coords)
}
}
// TODO: drag if the target didn't change?
// Here we could probably calculate a few coords leading up to the final position
fireMove(target, coords)
if (selectionRange) {
// TODO: support extending range (shift)
const selectionFocus = resolveSelectionTarget({target, node, offset})
if ('node' in selectionRange) {
// When the mouse is dragged outside of an input/textarea,
// the selection is extended to the beginning or end of the input
// depending on pointer position.
// TODO: extend selection according to pointer position
/* istanbul ignore else */
if (selectionFocus.node === selectionRange.node) {
const anchorOffset =
selectionFocus.offset < selectionRange.start
? selectionRange.end
: selectionRange.start
const focusOffset =
selectionFocus.offset > selectionRange.end ||
selectionFocus.offset < selectionRange.start
? selectionFocus.offset
: selectionRange.end
setUISelection(selectionRange.node, {anchorOffset, focusOffset})
}
} else {
const range = selectionRange.cloneRange()
const cmp = range.comparePoint(selectionFocus.node, selectionFocus.offset)
if (cmp < 0) {
range.setStart(selectionFocus.node, selectionFocus.offset)
} else if (cmp > 0) {
range.setEnd(selectionFocus.node, selectionFocus.offset)
}
const selection = target.ownerDocument.getSelection() as Selection
selection.removeAllRanges()
selection.addRange(range.cloneRange())
}
}
function fireMove(eventTarget: Element, eventCoords?: PointerCoords) {
fire(eventTarget, 'pointermove', eventCoords)
if (pointerType === 'mouse' && !isDisabled(eventTarget)) {
fire(eventTarget, 'mousemove', eventCoords)
}
}
function fireLeave(eventTarget: Element, eventCoords?: PointerCoords) {
fire(eventTarget, 'pointerout', eventCoords)
fire(eventTarget, 'pointerleave', eventCoords)
if (pointerType === 'mouse' && !isDisabled(eventTarget)) {
fire(eventTarget, 'mouseout', eventCoords)
fire(eventTarget, 'mouseleave', eventCoords)
}
}
function fireEnter(eventTarget: Element, eventCoords?: PointerCoords) {
fire(eventTarget, 'pointerover', eventCoords)
fire(eventTarget, 'pointerenter', eventCoords)
if (pointerType === 'mouse' && !isDisabled(eventTarget)) {
fire(eventTarget, 'mouseover', eventCoords)
fire(eventTarget, 'mouseenter', eventCoords)
}
}
function fire(
eventTarget: Element,
type: EventType,
eventCoords?: PointerCoords,
) {
return firePointerEvent(config, eventTarget, type, {
coords: eventCoords,
pointerId,
pointerType,
})
}
}