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

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