您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.tsx 3.7KB

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