123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import { inputs, TLBoundsEdge, TLBoundsCorner } from '@tldraw/core'
- import type { TLDrawState } from '~state'
-
- interface PointerOptions {
- id?: number
- x?: number
- y?: number
- shiftKey?: boolean
- altKey?: boolean
- ctrlKey?: boolean
- }
-
- export class TLStateUtils {
- tlstate: TLDrawState
-
- constructor(tlstate: TLDrawState) {
- this.tlstate = tlstate
- }
-
- pointCanvas = (options: PointerOptions = {}) => {
- this.tlstate.onPointCanvas(
- inputs.pointerDown(this.getPoint(options), 'canvas'),
- {} as React.PointerEvent
- )
- }
-
- pointShape = (id: string, options: PointerOptions = {}) => {
- this.tlstate.onPointShape(
- inputs.pointerDown(this.getPoint(options), id),
- {} as React.PointerEvent
- )
- }
-
- doubleClickShape = (id: string, options: PointerOptions = {}) => {
- this.tlstate.onDoubleClickShape(
- inputs.pointerDown(this.getPoint(options), id),
- {} as React.PointerEvent
- )
- }
-
- pointBounds = (options: PointerOptions = {}) => {
- this.tlstate.onPointBounds(
- inputs.pointerDown(this.getPoint(options), 'bounds'),
- {} as React.PointerEvent
- )
- }
-
- pointBoundsHandle = (
- id: TLBoundsCorner | TLBoundsEdge | 'rotate',
- options: PointerOptions = {}
- ) => {
- this.tlstate.onPointBounds(
- inputs.pointerDown(this.getPoint(options), 'bounds'),
- {} as React.PointerEvent
- )
- }
-
- stopPointing = (target = 'canvas', options: PointerOptions = {}) => {
- this.tlstate.onPointerUp(
- inputs.pointerDown(this.getPoint(options), target),
- {} as React.PointerEvent
- )
- }
-
- clickCanvas = (options: PointerOptions = {}) => {
- this.pointCanvas(options)
- this.stopPointing()
- }
-
- clickShape = (id: string, options: PointerOptions = {}) => {
- this.pointShape(id, options)
- this.stopPointing(id, options)
- }
-
- clickBounds = (options: PointerOptions = {}) => {
- this.pointBounds(options)
- this.stopPointing()
- }
-
- clickBoundsHandle = (
- id: TLBoundsCorner | TLBoundsEdge | 'rotate',
- options: PointerOptions = {}
- ) => {
- this.pointBoundsHandle(id, options)
- this.stopPointing(id)
- }
-
- getPoint(options: PointerOptions = {} as PointerOptions): PointerEvent {
- const { id = 1, x = 0, y = 0, shiftKey = false, altKey = false, ctrlKey = false } = options
-
- return {
- shiftKey,
- altKey,
- ctrlKey,
- pointerId: id,
- clientX: x,
- clientY: y,
- } as PointerEvent
- }
- }
|