Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {
  2. Bounds,
  3. Shape,
  4. ShapeType,
  5. Corner,
  6. Edge,
  7. ShapeStyles,
  8. ShapeHandle,
  9. ShapeBinding,
  10. } from 'types'
  11. import circle from './circle'
  12. import dot from './dot'
  13. import polyline from './polyline'
  14. import rectangle from './rectangle'
  15. import ellipse from './ellipse'
  16. import line from './line'
  17. import ray from './ray'
  18. import draw from './draw'
  19. import arrow from './arrow'
  20. /*
  21. Shape Utiliies
  22. A shape utility is an object containing utility methods for each type of shape
  23. in the application. While shapes may be very different, each shape must support
  24. a common set of utility methods, such as hit tests or translations, that
  25. Operations throughout the app will call these utility methods
  26. when performing tests (such as hit tests) or mutations, such as translations.
  27. */
  28. export interface ShapeUtility<K extends Shape> {
  29. // A cache for the computed bounds of this kind of shape.
  30. boundsCache: WeakMap<K, Bounds>
  31. // Whether to show transform controls when this shape is selected.
  32. canTransform: boolean
  33. // Whether the shape's aspect ratio can change
  34. canChangeAspectRatio: boolean
  35. // Whether the shape's style can be filled
  36. canStyleFill: boolean
  37. // Create a new shape.
  38. create(props: Partial<K>): K
  39. applyStyles(
  40. this: ShapeUtility<K>,
  41. shape: K,
  42. style: Partial<ShapeStyles>
  43. ): ShapeUtility<K>
  44. // Transform to fit a new bounding box when more than one shape is selected.
  45. transform(
  46. this: ShapeUtility<K>,
  47. shape: K,
  48. bounds: Bounds,
  49. info: {
  50. type: Edge | Corner
  51. initialShape: K
  52. scaleX: number
  53. scaleY: number
  54. transformOrigin: number[]
  55. }
  56. ): ShapeUtility<K>
  57. // Transform a single shape to fit a new bounding box.
  58. transformSingle(
  59. this: ShapeUtility<K>,
  60. shape: K,
  61. bounds: Bounds,
  62. info: {
  63. type: Edge | Corner
  64. initialShape: K
  65. scaleX: number
  66. scaleY: number
  67. transformOrigin: number[]
  68. }
  69. ): ShapeUtility<K>
  70. setProperty<P extends keyof K>(
  71. this: ShapeUtility<K>,
  72. shape: K,
  73. prop: P,
  74. value: K[P]
  75. ): ShapeUtility<K>
  76. // Respond when a user moves one of the shape's bound elements.
  77. onBindingMove?(
  78. this: ShapeUtility<K>,
  79. shape: K,
  80. bindings: Record<string, ShapeBinding>
  81. ): ShapeUtility<K>
  82. // Respond when a user moves one of the shape's handles.
  83. onHandleMove?(
  84. this: ShapeUtility<K>,
  85. shape: K,
  86. handle: Partial<K['handles']>
  87. ): ShapeUtility<K>
  88. // Render a shape to JSX.
  89. render(this: ShapeUtility<K>, shape: K): JSX.Element
  90. // Get the bounds of the a shape.
  91. getBounds(this: ShapeUtility<K>, shape: K): Bounds
  92. // Get the routated bounds of the a shape.
  93. getRotatedBounds(this: ShapeUtility<K>, shape: K): Bounds
  94. // Get the center of the shape
  95. getCenter(this: ShapeUtility<K>, shape: K): number[]
  96. // Test whether a point lies within a shape.
  97. hitTest(this: ShapeUtility<K>, shape: K, test: number[]): boolean
  98. // Test whether bounds collide with or contain a shape.
  99. hitTestBounds(this: ShapeUtility<K>, shape: K, bounds: Bounds): boolean
  100. }
  101. // A mapping of shape types to shape utilities.
  102. const shapeUtilityMap: Record<ShapeType, ShapeUtility<Shape>> = {
  103. [ShapeType.Circle]: circle,
  104. [ShapeType.Dot]: dot,
  105. [ShapeType.Polyline]: polyline,
  106. [ShapeType.Rectangle]: rectangle,
  107. [ShapeType.Ellipse]: ellipse,
  108. [ShapeType.Line]: line,
  109. [ShapeType.Ray]: ray,
  110. [ShapeType.Draw]: draw,
  111. [ShapeType.Arrow]: arrow,
  112. }
  113. /**
  114. * A helper to retrieve a shape utility based on a shape object.
  115. * @param shape
  116. * @returns
  117. */
  118. export function getShapeUtils<T extends Shape>(shape: T): ShapeUtility<T> {
  119. return shapeUtilityMap[shape.type] as ShapeUtility<T>
  120. }
  121. /**
  122. * A factory of shape utilities, with typing enforced.
  123. * @param shape
  124. * @returns
  125. */
  126. export function registerShapeUtils<T extends Shape>(
  127. shape: ShapeUtility<T>
  128. ): ShapeUtility<T> {
  129. return Object.freeze(shape)
  130. }
  131. export function createShape<T extends Shape>(
  132. type: T['type'],
  133. props: Partial<T>
  134. ) {
  135. return shapeUtilityMap[type].create(props) as T
  136. }
  137. export default shapeUtilityMap