-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpointer.ts
34 lines (28 loc) · 1.37 KB
/
pointer.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
import { mover } from "./mover";
addEventListener("pointerdown", (pointerDownEvent: PointerEvent) => {
const separator = pointerDownEvent.target as HTMLElement;
const container = separator.parentElement;
if (!container || !pointerDownEvent.isPrimary || pointerDownEvent.button !== 0 || separator.getAttribute("role") !== "separator") {
return;
}
const horizontal = container.hasAttribute("data-flex-splitter-horizontal");
if (!horizontal && !container.hasAttribute("data-flex-splitter-vertical")) {
return;
}
const pointerId = pointerDownEvent.pointerId;
const move = mover(separator, horizontal);
const onPointerMove: (event: PointerEvent) => void = horizontal
? (event) => event.pointerId === pointerId && move(event.clientX - pointerDownEvent.clientX)
: (event) => event.pointerId === pointerId && move(event.clientY - pointerDownEvent.clientY);
const onLostPointerCapture = (event: PointerEvent) => {
if (event.pointerId === pointerId) {
separator.removeEventListener("pointermove", onPointerMove);
separator.removeEventListener("lostpointercapture", onLostPointerCapture);
}
}
onPointerMove(pointerDownEvent);
separator.addEventListener("lostpointercapture", onLostPointerCapture)
separator.addEventListener("pointermove", onPointerMove);
separator.setPointerCapture(pointerId);
pointerDownEvent.preventDefault()
});