Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Readonly<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. // Create a new shape.
  36. create(props: Partial<K>): K
  37. applyStyles(
  38. this: ShapeUtility<K>,
  39. shape: K,
  40. style: ShapeStyles
  41. ): ShapeUtility<K>
  42. // Set the shape's point.
  43. translateTo(this: ShapeUtility<K>, shape: K, delta: number[]): ShapeUtility<K>
  44. // Set the shape's rotation.
  45. rotateTo(this: ShapeUtility<K>, shape: K, rotation: number): ShapeUtility<K>
  46. // Transform to fit a new bounding box when more than one shape is selected.
  47. transform(
  48. this: ShapeUtility<K>,
  49. shape: K,
  50. bounds: Bounds,
  51. info: {
  52. type: Edge | Corner
  53. initialShape: K
  54. scaleX: number
  55. scaleY: number
  56. transformOrigin: number[]
  57. }
  58. ): ShapeUtility<K>
  59. // Transform a single shape to fit a new bounding box.
  60. transformSingle(
  61. this: ShapeUtility<K>,
  62. shape: K,
  63. bounds: Bounds,
  64. info: {
  65. type: Edge | Corner
  66. initialShape: K
  67. scaleX: number
  68. scaleY: number
  69. transformOrigin: number[]
  70. }
  71. ): ShapeUtility<K>
  72. setProperty<P extends keyof K>(
  73. this: ShapeUtility<K>,
  74. shape: K,
  75. prop: P,
  76. value: K[P]
  77. ): ShapeUtility<K>
  78. // Respond when a user moves one of the shape's bound elements.
  79. onBindingMove?(
  80. this: ShapeUtility<K>,
  81. shape: K,
  82. bindings: Record<string, ShapeBinding>
  83. ): ShapeUtility<K>
  84. // Respond when a user moves one of the shape's handles.
  85. onHandleMove?(
  86. this: ShapeUtility<K>,
  87. shape: K,
  88. handle: Partial<K['handles']>
  89. ): ShapeUtility<K>
  90. // Render a shape to JSX.
  91. render(this: ShapeUtility<K>, shape: K): JSX.Element
  92. // Get the bounds of the a shape.
  93. getBounds(this: ShapeUtility<K>, shape: K): Bounds
  94. // Get the routated bounds of the a shape.
  95. getRotatedBounds(this: ShapeUtility<K>, shape: K): Bounds
  96. // Get the center of the shape
  97. getCenter(this: ShapeUtility<K>, shape: K): number[]
  98. // Test whether a point lies within a shape.
  99. hitTest(this: ShapeUtility<K>, shape: K, test: number[]): boolean
  100. // Test whether bounds collide with or contain a shape.
  101. hitTestBounds(this: ShapeUtility<K>, shape: K, bounds: Bounds): boolean
  102. }
  103. // A mapping of shape types to shape utilities.
  104. const shapeUtilityMap: Record<ShapeType, ShapeUtility<Shape>> = {
  105. [ShapeType.Circle]: circle,
  106. [ShapeType.Dot]: dot,
  107. [ShapeType.Polyline]: polyline,
  108. [ShapeType.Rectangle]: rectangle,
  109. [ShapeType.Ellipse]: ellipse,
  110. [ShapeType.Line]: line,
  111. [ShapeType.Ray]: ray,
  112. [ShapeType.Draw]: draw,
  113. [ShapeType.Arrow]: arrow,
  114. }
  115. /**
  116. * A helper to retrieve a shape utility based on a shape object.
  117. * @param shape
  118. * @returns
  119. */
  120. export function getShapeUtils<T extends Shape>(shape: T): ShapeUtility<T> {
  121. return shapeUtilityMap[shape.type] as ShapeUtility<T>
  122. }
  123. /**
  124. * A factory of shape utilities, with typing enforced.
  125. * @param shape
  126. * @returns
  127. */
  128. export function registerShapeUtils<T extends Shape>(
  129. shape: ShapeUtility<T>
  130. ): ShapeUtility<T> {
  131. return Object.freeze(shape)
  132. }
  133. export function createShape<T extends Shape>(
  134. type: T['type'],
  135. props: Partial<T>
  136. ) {
  137. return shapeUtilityMap[type].create(props) as T
  138. }
  139. export default shapeUtilityMap