tldraw/state/inputs.tsx

96 lines
2 KiB
TypeScript
Raw Normal View History

2021-05-13 06:44:52 +00:00
import { PointerInfo } from "types"
2021-05-13 08:34:56 +00:00
import { isDarwin } from "utils/utils"
2021-05-13 06:44:52 +00:00
class Inputs {
points: Record<string, PointerInfo> = {}
pointerDown(e: PointerEvent | React.PointerEvent, target: string) {
2021-05-13 06:44:52 +00:00
const { shiftKey, ctrlKey, metaKey, altKey } = e
2021-05-13 08:34:56 +00:00
const info = {
target,
2021-05-13 06:44:52 +00:00
pointerId: e.pointerId,
origin: [e.clientX, e.clientY],
point: [e.clientX, e.clientY],
shiftKey,
ctrlKey,
2021-05-13 08:34:56 +00:00
metaKey: isDarwin() ? metaKey : ctrlKey,
2021-05-13 06:44:52 +00:00
altKey,
}
2021-05-13 08:34:56 +00:00
this.points[e.pointerId] = info
return info
2021-05-13 06:44:52 +00:00
}
2021-05-14 22:56:41 +00:00
pointerEnter(e: PointerEvent | React.PointerEvent, target: string) {
const { shiftKey, ctrlKey, metaKey, altKey } = e
const info = {
target,
pointerId: e.pointerId,
origin: [e.clientX, e.clientY],
point: [e.clientX, e.clientY],
shiftKey,
ctrlKey,
metaKey: isDarwin() ? metaKey : ctrlKey,
altKey,
}
return info
}
2021-05-13 06:44:52 +00:00
pointerMove(e: PointerEvent | React.PointerEvent) {
const { shiftKey, ctrlKey, metaKey, altKey } = e
2021-05-13 08:34:56 +00:00
const prev = this.points[e.pointerId]
const info = {
...prev,
2021-05-13 06:44:52 +00:00
pointerId: e.pointerId,
point: [e.clientX, e.clientY],
shiftKey,
ctrlKey,
2021-05-13 08:34:56 +00:00
metaKey: isDarwin() ? metaKey : ctrlKey,
2021-05-13 06:44:52 +00:00
altKey,
}
2021-05-13 08:34:56 +00:00
if (this.points[e.pointerId]) {
this.points[e.pointerId] = info
}
return info
2021-05-13 06:44:52 +00:00
}
pointerUp(e: PointerEvent | React.PointerEvent) {
2021-05-13 08:34:56 +00:00
const { shiftKey, ctrlKey, metaKey, altKey } = e
2021-05-13 06:44:52 +00:00
2021-05-13 08:34:56 +00:00
const prev = this.points[e.pointerId]
const info = {
...prev,
2021-05-13 08:34:56 +00:00
origin: prev?.origin || [e.clientX, e.clientY],
point: [e.clientX, e.clientY],
shiftKey,
ctrlKey,
metaKey: isDarwin() ? metaKey : ctrlKey,
altKey,
}
2021-05-13 06:44:52 +00:00
delete this.points[e.pointerId]
return info
}
2021-05-13 08:34:56 +00:00
wheel(e: WheelEvent) {
const { shiftKey, ctrlKey, metaKey, altKey } = e
return { point: [e.clientX, e.clientY], shiftKey, ctrlKey, metaKey, altKey }
}
2021-05-19 21:24:41 +00:00
get pointer() {
return this.points[Object.keys(this.points)[0]]
}
2021-05-13 06:44:52 +00:00
}
export default new Inputs()