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.

StickyTool.ts 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import Vec from '@tldraw/vec'
  2. import type { TLPointerEventHandler } from '@tldraw/core'
  3. import { Utils } from '@tldraw/core'
  4. import { Sticky } from '~state/shapes'
  5. import { SessionType, TDShapeType } from '~types'
  6. import { BaseTool, Status } from '../BaseTool'
  7. export class StickyTool extends BaseTool {
  8. type = TDShapeType.Sticky as const
  9. shapeId?: string
  10. /* ----------------- Event Handlers ----------------- */
  11. onPointerDown: TLPointerEventHandler = () => {
  12. if (this.status === Status.Creating) {
  13. this.setStatus(Status.Idle)
  14. if (!this.app.appState.isToolLocked) {
  15. this.app.selectTool('select')
  16. }
  17. return
  18. }
  19. if (this.status === Status.Idle) {
  20. const {
  21. currentPoint,
  22. currentGrid,
  23. settings: { showGrid },
  24. appState: { currentPageId, currentStyle },
  25. } = this.app
  26. const childIndex = this.getNextChildIndex()
  27. const id = Utils.uniqueId()
  28. this.shapeId = id
  29. const newShape = Sticky.create({
  30. id,
  31. parentId: currentPageId,
  32. childIndex,
  33. point: showGrid ? Vec.snap(currentPoint, currentGrid) : currentPoint,
  34. style: { ...currentStyle },
  35. })
  36. const bounds = Sticky.getBounds(newShape)
  37. newShape.point = Vec.sub(newShape.point, [bounds.width / 2, bounds.height / 2])
  38. this.app.createShapes(newShape)
  39. this.app.startSession(SessionType.Translate)
  40. this.setStatus(Status.Creating)
  41. }
  42. }
  43. onPointerUp: TLPointerEventHandler = () => {
  44. if (this.status === Status.Creating) {
  45. this.setStatus(Status.Idle)
  46. this.app.completeSession()
  47. this.app.selectTool('select')
  48. this.app.setEditingId(this.shapeId)
  49. }
  50. }
  51. }