Tldraw/state/inputs.tsx

166 wiersze
3.6 KiB
TypeScript
Czysty Zwykły widok Historia

2021-06-01 21:49:32 +00:00
import React from 'react'
import { PointerInfo } from 'types'
import { isDarwin, getPoint } from 'utils/utils'
2021-05-13 06:44:52 +00:00
2021-06-04 16:08:43 +00:00
const DOUBLE_CLICK_DURATION = 300
2021-05-13 06:44:52 +00:00
class Inputs {
activePointerId?: number
2021-06-04 16:08:43 +00:00
lastPointerDownTime = 0
2021-05-13 06:44:52 +00:00
points: Record<string, PointerInfo> = {}
2021-06-01 21:49:32 +00:00
touchStart(e: TouchEvent | React.TouchEvent, target: string) {
const { shiftKey, ctrlKey, metaKey, altKey } = e
const touch = e.changedTouches[0]
const info = {
target,
pointerId: touch.identifier,
origin: getPoint(touch),
point: getPoint(touch),
2021-06-06 07:33:30 +00:00
pressure: 0.5,
2021-06-01 21:49:32 +00:00
shiftKey,
ctrlKey,
metaKey: isDarwin() ? metaKey : ctrlKey,
altKey,
}
this.points[touch.identifier] = info
this.activePointerId = touch.identifier
return info
}
touchMove(e: TouchEvent | React.TouchEvent) {
const { shiftKey, ctrlKey, metaKey, altKey } = e
const touch = e.changedTouches[0]
const prev = this.points[touch.identifier]
const info = {
...prev,
pointerId: touch.identifier,
point: getPoint(touch),
2021-06-06 07:33:30 +00:00
pressure: 0.5,
2021-06-01 21:49:32 +00:00
shiftKey,
ctrlKey,
metaKey: isDarwin() ? metaKey : ctrlKey,
altKey,
}
if (this.points[touch.identifier]) {
this.points[touch.identifier] = info
}
return info
}
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: getPoint(e),
point: getPoint(e),
2021-06-06 07:33:30 +00:00
pressure: e.pressure || 0.5,
2021-05-13 06:44:52 +00:00
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
this.activePointerId = e.pointerId
2021-05-13 08:34:56 +00:00
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: getPoint(e),
point: getPoint(e),
2021-06-06 07:33:30 +00:00
pressure: e.pressure || 0.5,
2021-05-14 22:56:41 +00:00
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: getPoint(e),
2021-06-06 07:33:30 +00:00
pressure: e.pressure || 0.5,
2021-05-13 06:44:52 +00:00
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,
origin: prev?.origin || getPoint(e),
point: getPoint(e),
2021-06-06 07:33:30 +00:00
pressure: e.pressure || 0.5,
2021-05-13 08:34:56 +00:00
shiftKey,
ctrlKey,
metaKey: isDarwin() ? metaKey : ctrlKey,
altKey,
}
2021-05-13 06:44:52 +00:00
delete this.points[e.pointerId]
delete this.activePointerId
2021-06-04 16:08:43 +00:00
this.lastPointerDownTime = Date.now()
2021-05-13 06:44:52 +00:00
return info
}
2021-05-13 08:34:56 +00:00
wheel(e: WheelEvent) {
const { shiftKey, ctrlKey, metaKey, altKey } = e
return { point: getPoint(e), shiftKey, ctrlKey, metaKey, altKey }
2021-05-13 08:34:56 +00:00
}
2021-05-19 21:24:41 +00:00
canAccept(pointerId: PointerEvent['pointerId']) {
return (
this.activePointerId === undefined || this.activePointerId === pointerId
)
}
2021-06-04 16:08:43 +00:00
isDoubleClick() {
return Date.now() - this.lastPointerDownTime < DOUBLE_CLICK_DURATION
}
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()