You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

state-utils.tsx 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { inputs, TLBoundsEdge, TLBoundsCorner } from '@tldraw/core'
  2. import type { TLDrawState } from '~state'
  3. interface PointerOptions {
  4. id?: number
  5. x?: number
  6. y?: number
  7. shiftKey?: boolean
  8. altKey?: boolean
  9. ctrlKey?: boolean
  10. }
  11. export class TLStateUtils {
  12. tlstate: TLDrawState
  13. constructor(tlstate: TLDrawState) {
  14. this.tlstate = tlstate
  15. }
  16. pointCanvas = (options: PointerOptions = {}) => {
  17. this.tlstate.onPointCanvas(
  18. inputs.pointerDown(this.getPoint(options), 'canvas'),
  19. {} as React.PointerEvent
  20. )
  21. }
  22. pointShape = (id: string, options: PointerOptions = {}) => {
  23. this.tlstate.onPointShape(
  24. inputs.pointerDown(this.getPoint(options), id),
  25. {} as React.PointerEvent
  26. )
  27. }
  28. doubleClickShape = (id: string, options: PointerOptions = {}) => {
  29. this.tlstate.onDoubleClickShape(
  30. inputs.pointerDown(this.getPoint(options), id),
  31. {} as React.PointerEvent
  32. )
  33. }
  34. pointBounds = (options: PointerOptions = {}) => {
  35. this.tlstate.onPointBounds(
  36. inputs.pointerDown(this.getPoint(options), 'bounds'),
  37. {} as React.PointerEvent
  38. )
  39. }
  40. pointBoundsHandle = (
  41. id: TLBoundsCorner | TLBoundsEdge | 'rotate',
  42. options: PointerOptions = {}
  43. ) => {
  44. this.tlstate.onPointBounds(
  45. inputs.pointerDown(this.getPoint(options), 'bounds'),
  46. {} as React.PointerEvent
  47. )
  48. }
  49. stopPointing = (target = 'canvas', options: PointerOptions = {}) => {
  50. this.tlstate.onPointerUp(
  51. inputs.pointerDown(this.getPoint(options), target),
  52. {} as React.PointerEvent
  53. )
  54. }
  55. clickCanvas = (options: PointerOptions = {}) => {
  56. this.pointCanvas(options)
  57. this.stopPointing()
  58. }
  59. clickShape = (id: string, options: PointerOptions = {}) => {
  60. this.pointShape(id, options)
  61. this.stopPointing(id, options)
  62. }
  63. clickBounds = (options: PointerOptions = {}) => {
  64. this.pointBounds(options)
  65. this.stopPointing()
  66. }
  67. clickBoundsHandle = (
  68. id: TLBoundsCorner | TLBoundsEdge | 'rotate',
  69. options: PointerOptions = {}
  70. ) => {
  71. this.pointBoundsHandle(id, options)
  72. this.stopPointing(id)
  73. }
  74. getPoint(options: PointerOptions = {} as PointerOptions): PointerEvent {
  75. const { id = 1, x = 0, y = 0, shiftKey = false, altKey = false, ctrlKey = false } = options
  76. return {
  77. shiftKey,
  78. altKey,
  79. ctrlKey,
  80. pointerId: id,
  81. clientX: x,
  82. clientY: y,
  83. } as PointerEvent
  84. }
  85. }