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.

index.tsx 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import {
  2. Bounds,
  3. Shape,
  4. ShapeType,
  5. Corner,
  6. Edge,
  7. ShapeStyles,
  8. ShapeBinding,
  9. Mutable,
  10. ShapeByType,
  11. Data,
  12. } from 'types'
  13. import * as vec from 'utils/vec'
  14. import {
  15. getBoundsCenter,
  16. getBoundsFromPoints,
  17. getRotatedCorners,
  18. } from 'utils/utils'
  19. import {
  20. boundsCollidePolygon,
  21. boundsContainPolygon,
  22. pointInBounds,
  23. } from 'utils/bounds'
  24. import { v4 as uuid } from 'uuid'
  25. import circle from './circle'
  26. import dot from './dot'
  27. import polyline from './polyline'
  28. import rectangle from './rectangle'
  29. import ellipse from './ellipse'
  30. import line from './line'
  31. import ray from './ray'
  32. import draw from './draw'
  33. import arrow from './arrow'
  34. import group from './group'
  35. /*
  36. Shape Utiliies
  37. A shape utility is an object containing utility methods for each type of shape
  38. in the application. While shapes may be very different, each shape must support
  39. a common set of utility methods, such as hit tests or translations, that
  40. Operations throughout the app will call these utility methods
  41. when performing tests (such as hit tests) or mutations, such as translations.
  42. */
  43. export interface ShapeUtility<K extends Shape> {
  44. // A cache for the computed bounds of this kind of shape.
  45. boundsCache: WeakMap<K, Bounds>
  46. // Whether to show transform controls when this shape is selected.
  47. canTransform: boolean
  48. // Whether the shape's aspect ratio can change
  49. canChangeAspectRatio: boolean
  50. // Whether the shape's style can be filled
  51. canStyleFill: boolean
  52. // Create a new shape.
  53. create(props: Partial<K>): K
  54. // Update a shape's styles
  55. applyStyles(
  56. this: ShapeUtility<K>,
  57. shape: Mutable<K>,
  58. style: Partial<ShapeStyles>
  59. ): ShapeUtility<K>
  60. translateBy(
  61. this: ShapeUtility<K>,
  62. shape: Mutable<K>,
  63. point: number[]
  64. ): ShapeUtility<K>
  65. translateTo(
  66. this: ShapeUtility<K>,
  67. shape: Mutable<K>,
  68. point: number[]
  69. ): ShapeUtility<K>
  70. rotateBy(
  71. this: ShapeUtility<K>,
  72. shape: Mutable<K>,
  73. rotation: number
  74. ): ShapeUtility<K>
  75. rotateTo(
  76. this: ShapeUtility<K>,
  77. shape: Mutable<K>,
  78. rotation: number,
  79. delta: number
  80. ): ShapeUtility<K>
  81. // Transform to fit a new bounding box when more than one shape is selected.
  82. transform(
  83. this: ShapeUtility<K>,
  84. shape: Mutable<K>,
  85. bounds: Bounds,
  86. info: {
  87. type: Edge | Corner
  88. initialShape: K
  89. scaleX: number
  90. scaleY: number
  91. transformOrigin: number[]
  92. }
  93. ): ShapeUtility<K>
  94. // Transform a single shape to fit a new bounding box.
  95. transformSingle(
  96. this: ShapeUtility<K>,
  97. shape: Mutable<K>,
  98. bounds: Bounds,
  99. info: {
  100. type: Edge | Corner
  101. initialShape: K
  102. scaleX: number
  103. scaleY: number
  104. transformOrigin: number[]
  105. }
  106. ): ShapeUtility<K>
  107. setProperty<P extends keyof K>(
  108. this: ShapeUtility<K>,
  109. shape: Mutable<K>,
  110. prop: P,
  111. value: K[P]
  112. ): ShapeUtility<K>
  113. // Respond when any child of this shape changes.
  114. onChildrenChange(
  115. this: ShapeUtility<K>,
  116. shape: Mutable<K>,
  117. children: Shape[]
  118. ): ShapeUtility<K>
  119. // Respond when a user moves one of the shape's bound elements.
  120. onBindingChange(
  121. this: ShapeUtility<K>,
  122. shape: Mutable<K>,
  123. bindings: Record<string, ShapeBinding>
  124. ): ShapeUtility<K>
  125. // Respond when a user moves one of the shape's handles.
  126. onHandleChange(
  127. this: ShapeUtility<K>,
  128. shape: Mutable<K>,
  129. handle: Partial<K['handles']>
  130. ): ShapeUtility<K>
  131. // Clean up changes when a session ends.
  132. onSessionComplete(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  133. // Render a shape to JSX.
  134. render(this: ShapeUtility<K>, shape: K): JSX.Element
  135. // Get the bounds of the a shape.
  136. getBounds(this: ShapeUtility<K>, shape: K): Bounds
  137. // Get the routated bounds of the a shape.
  138. getRotatedBounds(this: ShapeUtility<K>, shape: K): Bounds
  139. // Get the center of the shape
  140. getCenter(this: ShapeUtility<K>, shape: K): number[]
  141. // Test whether a point lies within a shape.
  142. hitTest(this: ShapeUtility<K>, shape: K, test: number[]): boolean
  143. // Test whether bounds collide with or contain a shape.
  144. hitTestBounds(this: ShapeUtility<K>, shape: K, bounds: Bounds): boolean
  145. }
  146. // A mapping of shape types to shape utilities.
  147. const shapeUtilityMap: Record<ShapeType, ShapeUtility<Shape>> = {
  148. [ShapeType.Circle]: circle,
  149. [ShapeType.Dot]: dot,
  150. [ShapeType.Polyline]: polyline,
  151. [ShapeType.Rectangle]: rectangle,
  152. [ShapeType.Ellipse]: ellipse,
  153. [ShapeType.Line]: line,
  154. [ShapeType.Ray]: ray,
  155. [ShapeType.Draw]: draw,
  156. [ShapeType.Arrow]: arrow,
  157. [ShapeType.Text]: arrow,
  158. [ShapeType.Group]: group,
  159. }
  160. /**
  161. * A helper to retrieve a shape utility based on a shape object.
  162. * @param shape
  163. * @returns
  164. */
  165. export function getShapeUtils<T extends Shape>(shape: T): ShapeUtility<T> {
  166. return shapeUtilityMap[shape.type] as ShapeUtility<T>
  167. }
  168. function getDefaultShapeUtil<T extends Shape>(): ShapeUtility<T> {
  169. return {
  170. boundsCache: new WeakMap(),
  171. canTransform: true,
  172. canChangeAspectRatio: true,
  173. canStyleFill: true,
  174. create(props) {
  175. return {
  176. id: uuid(),
  177. isGenerated: false,
  178. point: [0, 0],
  179. name: 'Shape',
  180. parentId: 'page0',
  181. childIndex: 0,
  182. rotation: 0,
  183. isAspectRatioLocked: false,
  184. isLocked: false,
  185. isHidden: false,
  186. ...props,
  187. } as T
  188. },
  189. render(shape) {
  190. return <circle id={shape.id} />
  191. },
  192. translateBy(shape, delta) {
  193. shape.point = vec.add(shape.point, delta)
  194. return this
  195. },
  196. translateTo(shape, point) {
  197. shape.point = point
  198. return this
  199. },
  200. rotateTo(shape, rotation) {
  201. shape.rotation = rotation
  202. return this
  203. },
  204. rotateBy(shape, rotation) {
  205. shape.rotation += rotation
  206. return this
  207. },
  208. transform(shape, bounds) {
  209. shape.point = [bounds.minX, bounds.minY]
  210. return this
  211. },
  212. transformSingle(shape, bounds, info) {
  213. return this.transform(shape, bounds, info)
  214. },
  215. onChildrenChange() {
  216. return this
  217. },
  218. onBindingChange() {
  219. return this
  220. },
  221. onHandleChange() {
  222. return this
  223. },
  224. onSessionComplete() {
  225. return this
  226. },
  227. getBounds(shape) {
  228. const [x, y] = shape.point
  229. return {
  230. minX: x,
  231. minY: y,
  232. maxX: x + 1,
  233. maxY: y + 1,
  234. width: 1,
  235. height: 1,
  236. }
  237. },
  238. getRotatedBounds(shape) {
  239. return getBoundsFromPoints(
  240. getRotatedCorners(this.getBounds(shape), shape.rotation)
  241. )
  242. },
  243. getCenter(shape) {
  244. return getBoundsCenter(this.getBounds(shape))
  245. },
  246. hitTest(shape, point) {
  247. return pointInBounds(point, this.getBounds(shape))
  248. },
  249. hitTestBounds(shape, brushBounds) {
  250. const rotatedCorners = getRotatedCorners(
  251. this.getBounds(shape),
  252. shape.rotation
  253. )
  254. return (
  255. boundsContainPolygon(brushBounds, rotatedCorners) ||
  256. boundsCollidePolygon(brushBounds, rotatedCorners)
  257. )
  258. },
  259. setProperty(shape, prop, value) {
  260. shape[prop] = value
  261. return this
  262. },
  263. applyStyles(shape, style) {
  264. Object.assign(shape.style, style)
  265. return this
  266. },
  267. }
  268. }
  269. /**
  270. * A factory of shape utilities, with typing enforced.
  271. * @param shape
  272. * @returns
  273. */
  274. export function registerShapeUtils<K extends Shape>(
  275. shapeUtil: Partial<ShapeUtility<K>>
  276. ): ShapeUtility<K> {
  277. return Object.freeze({ ...getDefaultShapeUtil<K>(), ...shapeUtil })
  278. }
  279. export function createShape<T extends ShapeType>(
  280. type: T,
  281. props: Partial<ShapeByType<T>>
  282. ): ShapeByType<T> {
  283. return shapeUtilityMap[type].create(props) as ShapeByType<T>
  284. }
  285. export default shapeUtilityMap