tldraw/state/code/arrow.ts

127 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-06-23 22:32:21 +00:00
import CodeShape from './index'
2021-06-24 08:18:14 +00:00
import { uniqueId } from 'utils'
import { ArrowShape, Decoration, ShapeProps, ShapeType } from 'types'
2021-06-23 22:32:21 +00:00
import { defaultStyle } from 'state/shape-styles'
import { getShapeUtils } from 'state/shape-utils'
import Vec from 'utils/vec'
/* ----------------- Start Copy Here ---------------- */
2021-06-23 22:32:21 +00:00
export default class Arrow extends CodeShape<ArrowShape> {
constructor(
props = {} as ShapeProps<ArrowShape> & { start: number[]; end: number[] }
2021-06-23 22:32:21 +00:00
) {
const { start = [0, 0], end = [0, 0] } = props
const {
point = [0, 0],
handles = {
start: {
id: 'start',
index: 0,
point: start,
},
end: {
id: 'end',
index: 1,
point: end,
},
bend: {
id: 'bend',
index: 2,
point: Vec.med(start, end),
},
},
} = props
super({
id: uniqueId(),
2021-06-25 11:01:22 +00:00
2021-06-23 22:32:21 +00:00
type: ShapeType.Arrow,
isGenerated: false,
name: 'Arrow',
parentId: 'page1',
childIndex: 0,
point,
rotation: 0,
isAspectRatioLocked: false,
isLocked: false,
isHidden: false,
bend: 0,
handles,
decorations: {
start: null,
middle: null,
end: Decoration.Arrow,
},
...props,
style: {
...defaultStyle,
...props.style,
isFilled: false,
},
})
}
/**
* The arrow's start point.
*
* ```ts
* const startPoint = shape.start
*
* shape.start = [100, 100]
* ```
*/
2021-06-23 22:32:21 +00:00
get start(): number[] {
return this.shape.handles.start.point
}
set start(point: number[]) {
getShapeUtils(this.shape).onHandleChange(this.shape, {
start: { ...this.shape.handles.start, point },
})
}
/**
* The arrow's middle point.
*
* ```ts
* const middlePoint = shape.middle
*
* shape.middle = [100, 100]
* ```
*/
2021-06-23 22:32:21 +00:00
get middle(): number[] {
return this.shape.handles.bend.point
}
set middle(point: number[]) {
getShapeUtils(this.shape).onHandleChange(this.shape, {
bend: { ...this.shape.handles.bend, point },
})
}
/**
* The arrow's end point.
*
* ```ts
* const endPoint = shape.end
*
* shape.end = [100, 100]
* ```
*/
2021-06-23 22:32:21 +00:00
get end(): number[] {
return this.shape.handles.end.point
}
set end(point: number[]) {
getShapeUtils(this.shape).onHandleChange(this.shape, {
end: { ...this.shape.handles.end, point },
})
}
get bend(): number {
return this.shape.bend
}
}