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.

register.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { Shape, ShapeUtility } from 'types'
  2. import vec from 'utils/vec'
  3. import {
  4. getBoundsCenter,
  5. getBoundsFromPoints,
  6. getRotatedCorners,
  7. } from 'utils/utils'
  8. import { boundsCollidePolygon, boundsContainPolygon } from 'utils/bounds'
  9. import { uniqueId } from 'utils/utils'
  10. import React from 'react'
  11. import { pointInBounds } from 'utils/hitTests'
  12. function getDefaultShapeUtil<T extends Shape>(): ShapeUtility<T> {
  13. return {
  14. boundsCache: new WeakMap(),
  15. canTransform: true,
  16. canChangeAspectRatio: true,
  17. canStyleFill: true,
  18. canEdit: false,
  19. isShy: false,
  20. isParent: false,
  21. isForeignObject: false,
  22. create(props) {
  23. return {
  24. id: uniqueId(),
  25. isGenerated: false,
  26. point: [0, 0],
  27. name: 'Shape',
  28. parentId: 'page1',
  29. childIndex: 0,
  30. rotation: 0,
  31. isAspectRatioLocked: false,
  32. isLocked: false,
  33. isHidden: false,
  34. ...props,
  35. } as T
  36. },
  37. render(shape) {
  38. return <circle id={shape.id} />
  39. },
  40. translateBy(shape, delta) {
  41. shape.point = vec.round(vec.add(shape.point, delta))
  42. return this
  43. },
  44. translateTo(shape, point) {
  45. shape.point = vec.round(point)
  46. return this
  47. },
  48. rotateTo(shape, rotation) {
  49. shape.rotation = rotation
  50. return this
  51. },
  52. rotateBy(shape, rotation) {
  53. shape.rotation += rotation
  54. return this
  55. },
  56. transform(shape, bounds) {
  57. shape.point = [bounds.minX, bounds.minY]
  58. return this
  59. },
  60. transformSingle(shape, bounds, info) {
  61. return this.transform(shape, bounds, info)
  62. },
  63. onChildrenChange() {
  64. return this
  65. },
  66. onBindingChange() {
  67. return this
  68. },
  69. onHandleChange() {
  70. return this
  71. },
  72. onDoublePointHandle() {
  73. return this
  74. },
  75. onDoubleFocus() {
  76. return this
  77. },
  78. onBoundsReset() {
  79. return this
  80. },
  81. onSessionComplete() {
  82. return this
  83. },
  84. getBounds(shape) {
  85. const [x, y] = shape.point
  86. return {
  87. minX: x,
  88. minY: y,
  89. maxX: x + 1,
  90. maxY: y + 1,
  91. width: 1,
  92. height: 1,
  93. }
  94. },
  95. getRotatedBounds(shape) {
  96. return getBoundsFromPoints(
  97. getRotatedCorners(this.getBounds(shape), shape.rotation)
  98. )
  99. },
  100. getCenter(shape) {
  101. return getBoundsCenter(this.getBounds(shape))
  102. },
  103. hitTest(shape, point) {
  104. return pointInBounds(point, this.getBounds(shape))
  105. },
  106. hitTestBounds(shape, brushBounds) {
  107. const rotatedCorners = getRotatedCorners(
  108. this.getBounds(shape),
  109. shape.rotation
  110. )
  111. return (
  112. boundsContainPolygon(brushBounds, rotatedCorners) ||
  113. boundsCollidePolygon(brushBounds, rotatedCorners)
  114. )
  115. },
  116. setProperty(shape, prop, value) {
  117. shape[prop] = value
  118. return this
  119. },
  120. applyStyles(shape, style) {
  121. Object.assign(shape.style, style)
  122. return this
  123. },
  124. shouldDelete() {
  125. return false
  126. },
  127. invalidate(shape) {
  128. this.boundsCache.delete(shape)
  129. return this
  130. },
  131. }
  132. }
  133. /**
  134. * A factory of shape utilities, with typing enforced.
  135. * @param shape
  136. * @returns
  137. */
  138. export function registerShapeUtils<K extends Shape>(
  139. shapeUtil: Partial<ShapeUtility<K>>
  140. ): ShapeUtility<K> {
  141. return Object.freeze({ ...getDefaultShapeUtil<K>(), ...shapeUtil })
  142. }