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 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { Shape, ShapeType, ShapeByType, ShapeUtility } from 'types'
  2. import dot from './dot'
  3. import polyline from './polyline'
  4. import rectangle from './rectangle'
  5. import ellipse from './ellipse'
  6. import line from './line'
  7. import ray from './ray'
  8. import draw from './draw'
  9. import arrow from './arrow'
  10. import group from './group'
  11. import text from './text'
  12. // A mapping of shape types to shape utilities.
  13. const shapeUtilityMap: Record<ShapeType, ShapeUtility<Shape>> = {
  14. [ShapeType.Rectangle]: rectangle,
  15. [ShapeType.Ellipse]: ellipse,
  16. [ShapeType.Draw]: draw,
  17. [ShapeType.Arrow]: arrow,
  18. [ShapeType.Text]: text,
  19. [ShapeType.Group]: group,
  20. [ShapeType.Dot]: dot,
  21. [ShapeType.Polyline]: polyline,
  22. [ShapeType.Line]: line,
  23. [ShapeType.Ray]: ray,
  24. }
  25. /**
  26. * A helper to retrieve a shape utility based on a shape object.
  27. * @param shape
  28. * @returns
  29. */
  30. export function getShapeUtils<T extends Shape>(shape: T): ShapeUtility<T> {
  31. return shapeUtilityMap[shape?.type] as ShapeUtility<T>
  32. }
  33. export function createShape<T extends ShapeType>(
  34. type: T,
  35. props: Partial<ShapeByType<T>>
  36. ): ShapeByType<T> {
  37. return shapeUtilityMap[type].create(props) as ShapeByType<T>
  38. }
  39. export default shapeUtilityMap