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.

arrow.ts 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import CodeShape from './index'
  2. import { uniqueId } from 'utils'
  3. import { ArrowShape, Decoration, ShapeStyles, ShapeType } from 'types'
  4. import { defaultStyle } from 'state/shape-styles'
  5. import { getShapeUtils } from 'state/shape-utils'
  6. import Vec from 'utils/vec'
  7. /**
  8. * ## Draw
  9. */
  10. export default class Arrow extends CodeShape<ArrowShape> {
  11. constructor(
  12. props = {} as Partial<ArrowShape> &
  13. Partial<ShapeStyles> & { start?: number[]; end?: number[] }
  14. ) {
  15. const { start = [0, 0], end = [0, 0] } = props
  16. const {
  17. point = [0, 0],
  18. handles = {
  19. start: {
  20. id: 'start',
  21. index: 0,
  22. point: start,
  23. },
  24. end: {
  25. id: 'end',
  26. index: 1,
  27. point: end,
  28. },
  29. bend: {
  30. id: 'bend',
  31. index: 2,
  32. point: Vec.med(start, end),
  33. },
  34. },
  35. } = props
  36. super({
  37. id: uniqueId(),
  38. seed: Math.random(),
  39. type: ShapeType.Arrow,
  40. isGenerated: false,
  41. name: 'Arrow',
  42. parentId: 'page1',
  43. childIndex: 0,
  44. point,
  45. rotation: 0,
  46. isAspectRatioLocked: false,
  47. isLocked: false,
  48. isHidden: false,
  49. bend: 0,
  50. handles,
  51. decorations: {
  52. start: null,
  53. middle: null,
  54. end: Decoration.Arrow,
  55. },
  56. ...props,
  57. style: {
  58. ...defaultStyle,
  59. ...props.style,
  60. isFilled: false,
  61. },
  62. })
  63. }
  64. get start(): number[] {
  65. return this.shape.handles.start.point
  66. }
  67. set start(point: number[]) {
  68. getShapeUtils(this.shape).onHandleChange(this.shape, {
  69. start: { ...this.shape.handles.start, point },
  70. })
  71. }
  72. get middle(): number[] {
  73. return this.shape.handles.bend.point
  74. }
  75. set middle(point: number[]) {
  76. getShapeUtils(this.shape).onHandleChange(this.shape, {
  77. bend: { ...this.shape.handles.bend, point },
  78. })
  79. }
  80. get end(): number[] {
  81. return this.shape.handles.end.point
  82. }
  83. set end(point: number[]) {
  84. getShapeUtils(this.shape).onHandleChange(this.shape, {
  85. end: { ...this.shape.handles.end, point },
  86. })
  87. }
  88. get bend(): number {
  89. return this.shape.bend
  90. }
  91. }