tldraw/state/code/polyline.ts

57 lines
1.3 KiB
TypeScript
Raw Normal View History

import CodeShape from './index'
import { uniqueId } from 'utils/utils'
import { PolylineShape, ShapeProps, ShapeType } from 'types'
2021-06-21 21:35:28 +00:00
import { defaultStyle } from 'state/shape-styles'
2021-05-15 13:02:13 +00:00
/* ----------------- Start Copy Here ---------------- */
2021-05-15 13:02:13 +00:00
export default class Polyline extends CodeShape<PolylineShape> {
constructor(props = {} as ShapeProps<PolylineShape>) {
2021-05-15 13:02:13 +00:00
super({
id: uniqueId(),
2021-06-08 10:32:20 +00:00
parentId: (window as any).currentPageId,
2021-05-15 13:02:13 +00:00
type: ShapeType.Polyline,
2021-05-25 09:09:51 +00:00
isGenerated: true,
name: 'Polyline',
2021-05-15 13:02:13 +00:00
childIndex: 0,
point: [0, 0],
points: [[0, 0]],
rotation: 0,
...props,
style: {
...defaultStyle,
...props.style,
},
2021-05-15 13:02:13 +00:00
})
}
2021-05-17 10:01:11 +00:00
/**
* Add a point to the polyline's points.
*
* ```ts
* shape.addPoint([100,100])
* ```
*/
addPoint(point: number[]): CodeShape<PolylineShape> {
this.utils.setProperty(this.shape, 'points', [...this.points, point])
return this
}
/**
* The polyline's points.
*
* ```ts
* const shapePoints = shape.points
*
* shape.points = [[0,0], [100,100], [100,200]]
* ```
*/
2021-06-21 21:35:28 +00:00
get points(): number[][] {
2021-05-17 10:01:11 +00:00
return this.shape.points
}
set points(points: number[][]) {
this.utils.setProperty(this.shape, 'points', points)
}
2021-05-15 13:02:13 +00:00
}